apiVersion: apps/v1
kind: Deployment
metadata:
  name: {{ include "telegraf.fullname" . }}
  labels:
    app.kubernetes.io/name: {{ include "telegraf.name" . }}
    helm.sh/chart: {{ include "telegraf.chart" . }}
    app.kubernetes.io/instance: {{ .Release.Name }}
spec:
  replicas: {{ .Values.replicaCount }}
  selector:
    matchLabels:
      app.kubernetes.io/name: {{ include "telegraf.name" . }}
      app.kubernetes.io/instance: {{ .Release.Name }}
  template:
    metadata:
      labels:
        app.kubernetes.io/name: {{ include "telegraf.name" . }}
        app.kubernetes.io/instance: {{ .Release.Name }}
      annotations:
        checksum/config: {{ include (print $.Template.BasePath "/configmap.yaml") . | sha256sum }}
        {{- if .Values.podAnnotations }}
{{ toYaml .Values.podAnnotations | indent 8 }}
        {{- end }}
    spec:
      serviceAccountName: {{ template "telegraf.serviceAccountName" . }}
      containers:
      - name: {{ .Chart.Name }}
        image: "{{ .Values.image.repo }}:{{ .Values.image.tag }}"
        imagePullPolicy: {{ default "" .Values.image.pullPolicy | quote }}
        resources:
{{ toYaml .Values.resources | indent 10 }}
        env:
{{ toYaml .Values.env | indent 8 }}
        volumeMounts:
        - name: config
          mountPath: /etc/telegraf
        {{- range .Values.volumeMounts}}
        - name: {{ .name }}
          mountPath: {{ .mountPath }}
        {{- end }}
        {{- range $objectKey, $objectValue := .Values.config.outputs }}
        {{- range $key, $value := . -}}
        {{- $tp := typeOf $value -}}
        {{- if eq $key "health" }}
        livenessProbe:
          httpGet:
            path: /
            port: {{ trimPrefix "http://:" $value.service_address | int64 }}
        readinessProbe:
          httpGet:
            path: /
            port: {{ trimPrefix "http://:" $value.service_address | int64 }}
        {{- end }}
        {{- end }}
        {{- end }}
      {{- if .Values.imagePullSecrets }}
      imagePullSecrets:
{{ toYaml .Values.imagePullSecrets | indent 8 }}
      {{- end }}
      {{- with .Values.nodeSelector }}
      nodeSelector:
        {{- toYaml . | nindent 8 }}
      {{- end }}
      volumes:
      - name: config
        configMap:
          name: {{ include "telegraf.fullname" . }}
      {{- if .Values.volumeMounts }}
{{ toYaml .Values.volumeMounts | indent 6 }}
      {{- end}}
