apiVersion: apps/v1
kind: Deployment
metadata:
  name: {{ include "telegraf.fullname" . }}
  labels:
    {{- include "telegraf.labels" . | nindent 4 }}
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 }}
        {{- if .Values.mountPoints }}
{{ toYaml .Values.mountPoints | indent 8 }}
        {{- end }}
        {{- if .Values.hooks }}
        {{- if or (.Values.hooks.postStart) (.Values.hooks.preStop) }}
        lifecycle:
        {{- if .Values.hooks.postStart }}
          postStart:
            exec:
              command:
        {{- range .Values.hooks.postStart }}
                - {{ . | quote }}
        {{- end }}
        {{ end }}
        {{- if .Values.hooks.preStop }}
          preStop:
            exec:
              command:
        {{- range .Values.hooks.preStop }}
                - {{ . | quote }}
        {{- end }}
        {{ end }}
        {{ end }}
        {{ 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.volumes }}
{{ toYaml .Values.volumes | indent 6 }}
      {{- end }}