{{- if or .Values.autoDiscovery.clusterName .Values.autoscalingGroups }}
{{/* one of the above is required */}}
apiVersion: {{ template "deployment.apiVersion" . }}
kind: Deployment
metadata:
  labels:
{{ include "cluster-autoscaler.labels" . | indent 4 }}
  name: {{ template "cluster-autoscaler.fullname" . }}
spec:
  replicas: {{ .Values.replicaCount }}
  selector:
    matchLabels:
{{ include "cluster-autoscaler.instance-name" . | indent 6 }}
    {{- if .Values.podLabels }}
{{ toYaml .Values.podLabels | indent 6 }}
    {{- end }}
{{- if .Values.updateStrategy }}
  strategy:
    {{ toYaml .Values.updateStrategy | nindent 4 | trim }}
{{- end }}
  template:
    metadata:
    {{- if .Values.podAnnotations }}
      annotations:
{{ toYaml .Values.podAnnotations | indent 8 }}
    {{- end }}
      labels:
{{ include "cluster-autoscaler.instance-name" . | indent 8 }}
      {{- if .Values.podLabels }}
{{ toYaml .Values.podLabels | indent 8 }}
      {{- end }}
    spec:
      {{- if .Values.priorityClassName }}
      priorityClassName: "{{ .Values.priorityClassName }}"
      {{- end }}
      {{- if .Values.dnsPolicy }}
      dnsPolicy: "{{ .Values.dnsPolicy }}"
      {{- end }}
      containers:
        - name: {{ template "cluster-autoscaler.name" . }}
        {{- if eq .Values.cloudProvider "spotinst" }}
          image: "{{ .Values.spotinst.image.repository }}:{{ .Values.spotinst.image.tag }}"
          imagePullPolicy: "{{ .Values.spotinst.image.pullPolicy }}"
        {{- else }}
          image: "{{ .Values.image.repository }}:{{ .Values.image.tag }}"
          imagePullPolicy: "{{ .Values.image.pullPolicy }}"
        {{- end }}
          command:
            - ./cluster-autoscaler
            - --cloud-provider={{ .Values.cloudProvider }}
            - --namespace={{ .Release.Namespace }}
          {{- if .Values.autoscalingGroups }}
            {{- range .Values.autoscalingGroups }}
            - --nodes={{ .minSize }}:{{ .maxSize }}:{{ .name }}
            {{- end }}
          {{- end }}
          {{- if eq .Values.cloudProvider "aws" }}
            {{- if .Values.autoDiscovery.clusterName }}
            - --node-group-auto-discovery=asg:tag={{ tpl (join "," .Values.autoDiscovery.tags) . }}
            {{- end }}
          {{- else if eq .Values.cloudProvider "gce" }}
          {{- if .Values.autoscalingGroupsnamePrefix }}
            {{- range .Values.autoscalingGroupsnamePrefix }}
            - --node-group-auto-discovery=mig:namePrefix={{ .name }},min={{ .minSize }},max={{ .maxSize }}
            {{- end }}
          {{- end }}
          {{- end }}
          {{- if eq .Values.cloudProvider "gce" }}
            - --cloud-config={{ .Values.cloudConfigPath }}
            {{- end }}
          {{- range $key, $value := .Values.extraArgs }}
            - --{{ $key }}={{ $value }}
          {{- end }}

          env:
          {{- if and (eq .Values.cloudProvider "aws") (ne .Values.awsRegion "") }}
            - name: AWS_REGION
              value: "{{ .Values.awsRegion }}"
            {{- if .Values.awsAccessKeyID }}
            - name: AWS_ACCESS_KEY_ID
              valueFrom:
                secretKeyRef:
                  key: AwsAccessKeyId
                  name: {{ template "cluster-autoscaler.fullname" . }}
            {{- end }}
            {{- if .Values.awsSecretAccessKey }}
            - name: AWS_SECRET_ACCESS_KEY
              valueFrom:
                secretKeyRef:
                  key: AwsSecretAccessKey
                  name: {{ template "cluster-autoscaler.fullname" . }}
            {{- end }}
          {{- else if eq .Values.cloudProvider "spotinst" }}
            - name: SPOTINST_TOKEN
              value: "{{ .Values.spotinst.token }}"
            - name: SPOTINST_ACCOUNT
              value: "{{ .Values.spotinst.account }}"
          {{- else if eq .Values.cloudProvider "azure" }}
            - name: ARM_SUBSCRIPTION_ID
              valueFrom:
                secretKeyRef:
                  key: SubscriptionID
                  name: {{ template "cluster-autoscaler.fullname" . }}
            - name: ARM_RESOURCE_GROUP
              valueFrom:
                secretKeyRef:
                  key: ResourceGroup
                  name: {{ template "cluster-autoscaler.fullname" . }}
            - name: ARM_VM_TYPE
              valueFrom:
                secretKeyRef:
                  key: VMType
                  name: {{ template "cluster-autoscaler.fullname" . }}
            {{- if .Values.azureUseManagedIdentityExtension }}
            - name: ARM_USE_MANAGED_IDENTITY_EXTENSION
              value: "true"
            {{- else }}
            - name: ARM_TENANT_ID
              valueFrom:
                secretKeyRef:
                  key: TenantID
                  name: {{ template "cluster-autoscaler.fullname" . }}
            - name: ARM_CLIENT_ID
              valueFrom:
                secretKeyRef:
                  key: ClientID
                  name: {{ template "cluster-autoscaler.fullname" . }}
            - name: ARM_CLIENT_SECRET
              valueFrom:
                secretKeyRef:
                  key: ClientSecret
                  name: {{ template "cluster-autoscaler.fullname" . }}
            - name: AZURE_CLUSTER_NAME
              valueFrom:
                secretKeyRef:
                  key: ClusterName
                  name: {{ template "cluster-autoscaler.fullname" . }}
            - name: AZURE_NODE_RESOURCE_GROUP
              valueFrom:
                secretKeyRef:
                  key: NodeResourceGroup
                  name: {{ template "cluster-autoscaler.fullname" . }}
            {{- end }}
          {{- end }}
          {{- range $key, $value := .Values.extraEnv }}
            - name: {{ $key }}
              value: "{{ $value }}"
          {{- end }}
          {{- range $key, $value := .Values.envFromConfigMap }}
            - name: {{ $key }}
              valueFrom:
                configMapKeyRef:
                  name: {{ default (include "cluster-autoscaler.fullname" $) $value.name }}
                  key: {{ required "Must specify key!" $value.key }}
          {{- end }}
          {{- range $key, $value := .Values.extraEnvSecrets }}
            - name: {{ $key }}
              valueFrom:
                secretKeyRef:
                  name: {{ default (include "cluster-autoscaler.fullname" $) $value.name }}
                  key: {{ required "Must specify key!" $value.key }}
          {{- end }}
          {{- if .Values.envFromSecret }}
          envFrom:
            - secretRef:
                name: {{ .Values.envFromSecret }}
          {{- end }}
          livenessProbe:
            httpGet:
              path: /health-check
              port: 8085
          ports:
            - containerPort: 8085
          resources:
{{ toYaml .Values.resources | indent 12 }}
          {{- if .Values.containerSecurityContext }}
          securityContext:
            {{ toYaml .Values.containerSecurityContext | nindent 12 | trim }}
          {{- end }}
          {{- if eq .Values.cloudProvider "gce" }}
          volumeMounts:
            - name: cloudconfig
              mountPath: {{ .Values.cloudConfigPath }}
              readOnly: true
          {{- end }}
    {{- if .Values.affinity }}
      affinity:
{{ toYaml .Values.affinity | indent 8 }}
    {{- end }}
    {{- if .Values.nodeSelector }}
      nodeSelector:
{{ toYaml .Values.nodeSelector | indent 8 }}
    {{- end }}
      serviceAccountName: {{ template "cluster-autoscaler.serviceAccountName" . }}
      tolerations:
{{ toYaml .Values.tolerations | indent 8 }}
      {{- if .Values.securityContext }}
      securityContext:
        {{ toYaml .Values.securityContext | nindent 8 | trim }}
      {{- end }}
      {{- if eq .Values.cloudProvider "gce" }}
      volumes:
        - name: cloudconfig
          hostPath:
            path: {{ .Values.cloudConfigPath }}
      {{- end }}
      {{- if .Values.image.pullSecrets }}
      imagePullSecrets:
      {{- range .Values.image.pullSecrets }}
        - name: {{ . }}
      {{- end }}
    {{- end }}
{{- end }}
