{{ $redisNodeCount := .Values.redis.replicas | int }}
apiVersion: apps/v1
kind: Deployment
metadata:
  name: drycc-monitor-telegraf
  labels:
    heritage: drycc
  annotations:
    component.drycc.cc/version: {{ .Values.telegraf.imageTag }}
spec:
  replicas: 1
  selector:
    matchLabels:
      app: drycc-monitor-telegraf
  {{- if ge .Capabilities.KubeVersion.Minor "6" }}
  updateStrategy:
    type: RollingUpdate
  {{- end }}
  template:
    metadata:
      labels:
        app: drycc-monitor-telegraf
    spec:
      {{- include "common.affinities.pods.soft" (dict "key" "app" "values" (list "drycc-monitor-telegraf")) | indent 6 }}
      securityContext:
        fsGroup: 1001
        runAsGroup: 1001
        runAsUser: 1001
      serviceAccount: drycc-monitor-telegraf
      initContainers:
      - name: drycc-monitor-telegraf-init
        image: docker.io/drycc/python-dev:latest
        imagePullPolicy: {{.Values.imagePullPolicy}}
        args:
          - netcat
          - -v
          - -u
          - $(INFLUXDB_URLS_V2)
        env:
{{- if eq .Values.global.influxdbLocation "off-cluster" }}
        - name: "INFLUXDB_URLS_V2"
          valueFrom:
            secretKeyRef:
              name: influxdb-creds
              key: url
{{- else }}
        - name: "INFLUXDB_URLS_V2"
          value: "\"http://$(DRYCC_INFLUXDB_SERVICE_HOST):$(DRYCC_INFLUXDB_SERVICE_PORT_TRANSPORT)\""
        - name: "INFLUXDB_V2_INPUT_URLS"
          value: "\"http://$(DRYCC_INFLUXDB_SERVICE_HOST):$(DRYCC_INFLUXDB_SERVICE_PORT_TRANSPORT)/metrics\""
        - name: "ENABLE_INFLUXDB_V2_INPUT"
          value: "true"
{{- end }}
      containers:
      - name: drycc-monitor-telegraf
        image: {{.Values.telegraf.imageRegistry}}/{{.Values.telegraf.imageOrg}}/telegraf:{{.Values.telegraf.imageTag}}
        imagePullPolicy: {{.Values.telegraf.imagePullPolicy}}
{{- if or (.Values.telegraf.limitsCpu) (.Values.telegraf.limitsMemory)}}
        resources:
          limits:
{{- if (.Values.telegraf.limitsCpu) }}
            cpu: {{.Values.telegraf.limitsCpu}}
{{- end}}
{{- if (.Values.telegraf.limitsMemory) }}
            memory: {{.Values.telegraf.limitsMemory}}
{{- end}}
{{- end}}
        env:
        - name: POD_NAMESPACE
          valueFrom:
            fieldRef:
              fieldPath: metadata.namespace
        {{- if eq .Values.global.influxdbLocation "off-cluster" }}
        - name: "INFLUXDB_URLS_V2"
          valueFrom:
            secretKeyRef:
              name: influxdb-creds
              key: url
        {{- else }}
        - name: "INFLUXDB_URLS_V2"
          value: "\"http://$(DRYCC_INFLUXDB_SERVICE_HOST):$(DRYCC_INFLUXDB_SERVICE_PORT_TRANSPORT)\""
        - name: "INFLUXDB_V2_INPUT_URLS"
          value: "\"http://$(DRYCC_INFLUXDB_SERVICE_HOST):$(DRYCC_INFLUXDB_SERVICE_PORT_TRANSPORT)/metrics\""
        - name: "ENABLE_INFLUXDB_V2_INPUT"
          value: "true"
        {{- end }}
        - name: "INFLUXDB_BUCKET"
          valueFrom:
            secretKeyRef:
              name: influxdb-creds
              key: bucket
        - name: "INFLUXDB_ORG"
          valueFrom:
            secretKeyRef:
              name: influxdb-creds
              key: org
        - name: "INFLUXDB_TOKEN"
          valueFrom:
            secretKeyRef:
              name: influxdb-creds
              key: token
        - name: "AGENT_QUIET"
          value: "true"
        - name: "AGENT_BUFFER_LIMIT"
          value: "100000"
{{- if ne .Values.global.redisLocation "off-cluster" }}
        - name: DRYCC_REDIS_PASSWORD
          valueFrom:
            secretKeyRef:
              name: redis-creds
              key: password
        - name: "REDIS_SERVERS"
          value: "[{{range $i := until $redisNodeCount}}\":$(DRYCC_REDIS_PASSWORD)@drycc-redis-{{$i}}.drycc-redis.{{$.Release.Namespace}}.svc.{{$.Values.global.clusterDomain}}:6379\"{{if lt (add 1 $i) $redisNodeCount}}, {{end}}{{end}}]"
        volumeMounts:
        - name: redis-creds
          mountPath: /var/run/secrets/drycc/redis/creds
      volumes:
      - name: redis-creds
        secret:
          secretName: redis-creds
{{- end }}
