apiVersion: apps/v1
kind: Deployment
metadata:
  labels:
    {{- include "elasticsearch-metrics-apiserver.combinedLabels" . | nindent 4 }}
  annotations:
    # ensure this resource is created after and delete before the rbac to minimise errors during namespace transition
    argocd.argoproj.io/sync-wave: "2"
  name: elasticsearch-metrics-apiserver
spec:
  replicas: {{ .Values.replicaCount }}
  selector:
    matchLabels:
      app: elasticsearch-metrics-apiserver
  template:
    metadata:
      labels:
        {{- include "elasticsearch-metrics-apiserver.combinedPodLabels" . | nindent 8 }}
        {{- with (include "elasticsearch-metrics-apiserver.deploymentslice" . | trim) }}
        {{- . | nindent 8 }}
        {{- end }}
      annotations:
        {{- with .Values.podAnnotations }}
        {{- toYaml . | nindent 8 }}
        {{- end }}
      name: elasticsearch-metrics-apiserver
    spec:
      serviceAccountName: elasticsearch-metrics-apiserver
      containers:
        - name: elasticsearch-metrics-apiserver
          image: "{{ .Values.image.repository }}:{{ .Values.image.tag }}"
          workingDir: /
          securityContext:
            runAsNonRoot: true
            runAsUser: {{ .Values.runAsUser }}
          envFrom:
            {{- with .Values.envFrom }}
              {{- toYaml . | nindent 12 }}
            {{- end }}
          env:
            {{- with .Values.metadata.cluster }}
            - name: KUBERNETES_CLUSTER_NAME
              value: "{{ . }}"
            {{- end }}
            {{- with .Values.env }}
              {{- toYaml . | nindent 12 }}
            {{- end }}
          args: [
            "--secure-port", "6443",
            "--cert-dir=/var/run/serving-cert",
            "--v", "{{ .Values.logVerbosity }}",
            "--profiling-port", "{{ .Values.profilingPort}}",
          ]
          {{- with $.Values.resources }}
          resources:
          {{- toYaml . | nindent 12 }}
          {{- end }}
          ports:
            - containerPort: 6443
              name: https
            - containerPort: 8080
              name: http
            - containerPort: 9090
              name: monitoring
          readinessProbe:
            httpGet:
              port: monitoring
              path: /readyz
          volumeMounts:
            - name: config-volume
              mountPath: /config
            - name: temp-vol
              mountPath: /tmp
            - mountPath: /var/run/serving-cert
              name: volume-serving-cert
              readOnly: false
      {{- with .Values.tolerations }}
      tolerations:
        {{- toYaml . | nindent 12 }}
      {{- end }}
      {{- with .Values.affinity }}
      affinity: {{ toYaml . | nindent 8 }}
      {{- end }}
      {{- with .Values.topologySpreadConstraints }}
      topologySpreadConstraints: {{ toYaml . | nindent 8 }}
      {{- end }}
      imagePullSecrets:
        - name: elastic-docker-registry
      volumes:
        - name: config-volume
          configMap:
            name: elasticsearch-custom-metrics-config-{{ include "yamlHash" .Values.config  }}
        - name: temp-vol
          emptyDir: {}
        - name: volume-serving-cert
          emptyDir: { }
