apiVersion: apps/v1
kind: Deployment
metadata:
  name: {{ template "prometheus-snmp-exporter.fullname" . }}
  namespace: {{ .Release.Namespace }}
  labels:
    app.kubernetes.io/name: {{ include "prometheus-snmp-exporter.name" . }}
    app.kubernetes.io/instance: {{ .Release.Name }}
    app.kubernetes.io/managed-by: {{ .Release.Service }}
    helm.sh/chart: {{ include "prometheus-snmp-exporter.chart" . }}
spec:
  replicas: {{ .Values.replicas }}
  selector:
    matchLabels:
      app.kubernetes.io/name: {{ include "prometheus-snmp-exporter.name" . }}
      app.kubernetes.io/instance: {{ .Release.Name }}
  strategy:
    rollingUpdate:
      maxSurge: 1
      maxUnavailable: 0
    type: RollingUpdate
  template:
    metadata:
      labels:
        app.kubernetes.io/name: {{ include "prometheus-snmp-exporter.name" . }}
        app.kubernetes.io/instance: {{ .Release.Name }}
        app.kubernetes.io/managed-by: {{ .Release.Service }}
        helm.sh/chart: {{ include "prometheus-snmp-exporter.chart" . }}
      annotations:
{{ toYaml .Values.podAnnotations | indent 8 }}
    spec:
    {{- if .Values.nodeSelector }}
      nodeSelector:
{{ toYaml .Values.nodeSelector | indent 8 }}
    {{- end }}
    {{- if .Values.affinity }}
      affinity:
{{ toYaml .Values.affinity | indent 8 }}
    {{- end }}
    {{- if .Values.tolerations }}
      tolerations:
{{ toYaml .Values.tolerations | indent 6 }}
    {{- end }}

      restartPolicy: {{ .Values.restartPolicy }}
      serviceAccountName: {{ template "prometheus-snmp-exporter.serviceAccountName" . }}
      containers:
        - name: snmp-exporter
          image: "{{ .Values.image.repository }}:{{ .Values.image.tag }}"
          imagePullPolicy: {{ .Values.image.pullPolicy }}
          securityContext:
            runAsNonRoot: true
            runAsUser: 1000
          args:
{{- if .Values.config }}
            - "--config.file=/config/snmp.yaml"
{{- end }}
        {{- if .Values.extraArgs }}
{{ toYaml .Values.extraArgs | indent 12 }}
        {{- end }}
          securityContext:
            readOnlyRootFilesystem: true
          resources:
{{ toYaml .Values.resources | indent 12 }}
          ports:
            - containerPort: {{ .Values.service.port }}
              name: http
          livenessProbe:
            httpGet:
              path: /health
              port: http
          readinessProbe:
            httpGet:
              path: /health
              port: http
        {{- if .Values.config }}
          volumeMounts:
            - mountPath: /config
              name: config
        - name: configmap-reload
          image: "{{ .Values.configmapReload.image.repository }}:{{ .Values.configmapReload.image.tag }}"
          imagePullPolicy: "{{ .Values.configmapReload.image.pullPolicy }}"
          args:
            - --volume-dir=/etc/config
            - --webhook-url=http://localhost:{{ .Values.service.port }}/-/reload
          resources:
{{ toYaml .Values.configmapReload.resources | indent 12 }}
          volumeMounts:
            - mountPath: /etc/config
              name: config
              readOnly: true
      volumes:
        - name: config
          configMap:
            name: {{ template "prometheus-snmp-exporter.fullname" . }}
        {{- end }}
