{{- if or .Values.autoDiscovery.clusterName .Values.autoscalingGroups }}
{{/* one of the above is required */}}
apiVersion: extensions/v1beta1
kind: Deployment
metadata:
  labels:
    app: {{ template "cluster-autoscaler.name" . }}
    chart: {{ .Chart.Name }}-{{ .Chart.Version }}
    heritage: {{ .Release.Service }}
    release: {{ .Release.Name }}
  name: {{ template "cluster-autoscaler.fullname" . }}
spec:
  replicas: {{ .Values.replicaCount }}
  template:
    metadata:
    {{- if .Values.podAnnotations }}
      annotations:
{{ toYaml .Values.podAnnotations | indent 8 }}
    {{- end }}
      labels:
        app: {{ template "cluster-autoscaler.name" . }}
        release: {{ .Release.Name }}
      {{- if .Values.podLabels }}
{{ toYaml .Values.podLabels | indent 8 }}
      {{- end }}
    spec:
      {{- if .Values.priorityClassName }}
      priorityClassName: "{{ .Values.priorityClassName }}"
      {{- 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=k8s.io/cluster-autoscaler/enabled,kubernetes.io/cluster/
              {{- .Values.autoDiscovery.clusterName }}
            {{- 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 }}{{ if $value }}={{ $value }}{{ end }}
          {{- end }}

          env:
          {{- if eq .Values.cloudProvider "aws" }}
            - name: AWS_REGION
              value: "{{ .Values.awsRegion }}"
          {{- else if eq .Values.cloudProvider "spotinst" }}
            - name: SPOTINST_TOKEN
              value: "{{ .Values.spotinst.token }}"
            - name: SPOTINST_ACCOUNT
              value: "{{ .Values.spotinst.account }}"
          {{- end }}
          {{- range $key, $value := .Values.extraEnv }}
            - name: {{ $key }}
              value: "{{ $value }}"
          {{- end }}
          livenessProbe:
            httpGet:
              path: /health-check
              port: 8085
          ports:
            - containerPort: 8085
          resources:
{{ toYaml .Values.resources | indent 12 }}
          volumeMounts:
            - name: ssl-certs
              mountPath: {{ .Values.sslCertPath }}
              readOnly: true
            {{- if eq .Values.cloudProvider "gce" }}
            - 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: {{ if .Values.rbac.create }}{{ template "cluster-autoscaler.fullname" . }}{{ else }}"{{ .Values.rbac.serviceAccountName }}"{{ end }}
      tolerations:
{{ toYaml .Values.tolerations | indent 8 }}
      volumes:
        - name: ssl-certs
          hostPath:
            path: {{ .Values.sslCertPath }}
        {{- if eq .Values.cloudProvider "gce" }}
        - name: cloudconfig
          hostPath:
            path: {{ .Values.cloudConfigPath }}
        {{- end }}
{{- end}}
