apiVersion: apps/v1
kind: Deployment
metadata:
  labels:
    app: {{ template "k8s-prometheus-adapter.name" . }}
    chart: {{ template "k8s-prometheus-adapter.chart" . }}
    release: {{ .Release.Name }}
    heritage: {{ .Release.Service }}
  name: {{ template "k8s-prometheus-adapter.fullname" . }}
spec:
  replicas: {{ .Values.replicas }}
  selector:
    matchLabels:
      app: {{ template "k8s-prometheus-adapter.name" . }}
      release: {{ .Release.Name }}
  template:
    metadata:
      labels:
        app: {{ template "k8s-prometheus-adapter.name" . }}
        chart: {{ template "k8s-prometheus-adapter.chart" . }}
        release: {{ .Release.Name }}
        heritage: {{ .Release.Service }}
      name: {{ template "k8s-prometheus-adapter.name" . }}
      annotations:
        checksum/config: {{ include (print $.Template.BasePath "/custom-metrics-configmap.yaml") . | sha256sum }}
    spec:
      securityContext:
        allowPrivilegeEscalation: false
        capabilities:
          drop: ["all"]
        runAsNonRoot: true
        runAsUser: 10001
      serviceAccountName: {{ template "k8s-prometheus-adapter.serviceAccountName" . }}
      containers:
      - name: {{ .Chart.Name }}
        image: "{{ .Values.image.repository }}:{{ .Values.image.tag }}"
        imagePullPolicy: {{ .Values.image.pullPolicy }}
        args:
        - /adapter
        - --secure-port=6443
{{- if .Values.tls.enable }}
        - --tls-cert-file=/var/run/serving-cert/tls.crt
        - --tls-private-key-file=/var/run/serving-cert/tls.key
{{- end }}
        - --cert-dir=/tmp/cert
        - --logtostderr=true
        - --prometheus-url={{ .Values.prometheus.url }}:{{ .Values.prometheus.port }}
        - --metrics-relist-interval={{ .Values.metricsRelistInterval }}
        - --v={{ .Values.logLevel }}
        - --config=/etc/adapter/config.yaml
        ports:
        - containerPort: 6443
          name: https
        livenessProbe:
          httpGet:
            path: /healthz
            port: https
            scheme: HTTPS
          initialDelaySeconds: 30
        readinessProbe:
          httpGet:
            path: /healthz
            port: https
            scheme: HTTPS
          initialDelaySeconds: 30
{{- if .Values.resources }}
        resources:
{{ toYaml .Values.resources | indent 10 }}
{{- end }}
        securityContext:
          readOnlyRootFilesystem: true
        volumeMounts:
        - mountPath: /etc/adapter/
          name: config
          readOnly: true
        - mountPath: /tmp
          name: tmp
{{- if .Values.tls.enable }}
        - mountPath: /var/run/serving-cert
          name: volume-serving-cert
          readOnly: true
{{- end }}
    {{- with .Values.nodeSelector }}
      nodeSelector:
{{ toYaml . | indent 8 }}
    {{- end }}
    {{- with .Values.affinity }}
      affinity:
{{ toYaml . | indent 8 }}
    {{- end }}
    {{- with .Values.tolerations }}
      tolerations:
{{ toYaml . | indent 8 }}
    {{- end }}
{{- if .Values.image.pullSecrets }}
      imagePullSecrets:
{{- range .Values.image.pullSecrets }}
        - name: {{ . }}
{{- end }}
{{- end }}
      volumes:
      - name: config
        configMap:
          {{- if .Values.rules.existing }}
          name: {{ .Values.rules.existing }}
          {{- else }}
          name: {{ template "k8s-prometheus-adapter.fullname" . }}
          {{- end }}
      - name: tmp
        emptyDir: {}
{{- if .Values.tls.enable }}
      - name: volume-serving-cert
        secret:
          secretName: {{ template "k8s-prometheus-adapter.fullname" . }}
{{- end }}
