{{ $nsqdNodeCount := .Values.nsqd.replicas | int }}
{{ $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.docker_tag }}
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:
      serviceAccount: drycc-monitor-telegraf
      containers:
        - name: drycc-monitor-telegraf
          image: {{.Values.telegraf.docker_registry}}{{.Values.telegraf.org}}/telegraf:{{.Values.telegraf.docker_tag}}
          imagePullPolicy: {{.Values.telegraf.pull_policy}}
{{- if or (.Values.telegraf.limits_cpu) (.Values.telegraf.limits_memory)}}
          resources:
            limits:
{{- if (.Values.telegraf.limits_cpu) }}
              cpu: {{.Values.telegraf.limits_cpu}}
{{- end}}
{{- if (.Values.telegraf.limits_memory) }}
              memory: {{.Values.telegraf.limits_memory}}
{{- end}}
{{- end}}
          env:
          - name: POD_NAMESPACE
            valueFrom:
              fieldRef:
                fieldPath: metadata.namespace
          {{- if eq .Values.global.influxdb_location "off-cluster" }}
          - name: "INFLUXDB_URLS"
            valueFrom:
              secretKeyRef:
                name: influxdb-creds
                key: url
          - name: "INFLUXDB_DATABASE"
            valueFrom:
              secretKeyRef:
                name: influxdb-creds
                key: database
          - name: "INFLUXDB_USERNAME"
            valueFrom:
              secretKeyRef:
                name: influxdb-creds
                key: user
          - name: "INFLUXDB_PASSWORD"
            valueFrom:
              secretKeyRef:
                name: influxdb-creds
                key: password
          {{- else }}
          - name: "INFLUXDB_URLS"
            value: "\"http://$(DRYCC_MONITOR_INFLUXDB_SERVICE_HOST):$(DRYCC_MONITOR_INFLUXDB_SERVICE_PORT_TRANSPORT)\""
          - name: "INFLUXDB_INPUT_URLS"
            value: "\"http://$(DRYCC_MONITOR_INFLUXDB_SERVICE_HOST):$(DRYCC_MONITOR_INFLUXDB_SERVICE_PORT_TRANSPORT)/debug/vars\""
          - name: "ENABLE_INFLUXDB_INPUT"
            value: "true"
          {{- end }}
          - name: "AGENT_QUIET"
            value: "true"
          - name: "AGENT_BUFFER_LIMIT"
            value: "100000"
          - name: "NSQ_CONSUMER_SERVERS"
            value: "[{{range $i := until $nsqdNodeCount}}\"drycc-nsqd-{{$i}}.drycc-nsqd.{{$.Release.Namespace}}.svc.{{$.Values.global.cluster_domain}}:{{$.Values.nsqd.tcp_port}}\"{{if lt (add 1 $i) $nsqdNodeCount}}, {{end}}{{end}}]"
          - name: "NSQ_CONSUMER_TOPIC"
            value: "metrics"
          - name: "NSQ_ENDPOINTS"
            value: "[{{range $i := until $nsqdNodeCount}}\"http://drycc-nsqd-{{$i}}.drycc-nsqd.{{$.Release.Namespace}}.svc.{{$.Values.global.cluster_domain}}:{{$.Values.nsqd.http_port}}\"{{if lt (add 1 $i) $nsqdNodeCount}}, {{end}}{{end}}]"
{{- if ne .Values.global.logger_redis_location "off-cluster" }}
          - name: DRYCC_LOGGER_REDIS_PASSWORD
            valueFrom:
              secretKeyRef:
                name: logger-redis-creds
                key: password
          - name: "REDIS_SERVERS"
            value: "[{{range $i := until $redisNodeCount}}\":$(DRYCC_LOGGER_REDIS_PASSWORD)@drycc-logger-redis-{{$i}}.drycc-logger-redis.{{$.Release.Namespace}}.svc.{{$.Values.global.cluster_domain}}:{{$.Values.redis.port}}\"{{if lt (add 1 $i) $redisNodeCount}}, {{end}}{{end}}]"
          volumeMounts:
          - name: logger-redis-creds
            mountPath: /var/run/secrets/drycc/redis/creds
      volumes:
      - name: logger-redis-creds
        secret:
          secretName: logger-redis-creds
{{- end }}
