apiVersion: apps/v1
kind: DaemonSet
metadata:
  name: drycc-monitor-telegraf
  labels:
    heritage: drycc
  annotations:
    component.drycc.cc/version: {{ .Values.telegraf.image_tag }}
spec:
  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
      initContainers:
      - name: drycc-monitor-telegraf-init
        image: docker.io/drycc/python-dev:latest
        imagePullPolicy: {{.Values.image_pull_policy}}
        command:
          - netcat
          - -v
          - -u
          - $(INFLUXDB_URLS_V2)
        env:
{{- if eq .Values.global.influxdb_location "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.image_registry}}/{{.Values.telegraf.image_org}}/telegraf:{{.Values.telegraf.image_tag}}
        imagePullPolicy: {{.Values.telegraf.image_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_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: "HOST_PROC"
          value: "/rootfs/proc"
        - name: "HOST_SYS"
          value: "/rootfs/sys"
        - name: "AGENT_QUIET"
          value: "true"
        - name: "AGENT_BUFFER_LIMIT"
          value: "100000"
        - name: "ENABLE_KUBERNETES"
          value: "true"
        - name: "ENABLE_BARE_METAL_SERVER"
          value: "true"
        volumeMounts:
        - mountPath: /var/run/docker.sock
          name: docker-socket
        - name: sysro
          mountPath: /rootfs/sys
          readOnly: true
        - name: procro
          mountPath: /rootfs/proc
          readOnly: true
        - name: varrunutmpro
          mountPath: /var/run/utmp
          readOnly: true
      volumes:
      - name: docker-socket
        hostPath:
          path: /var/run/docker.sock
      - name: sysro
        hostPath:
          path: /sys
      - name: procro
        hostPath:
          path: /proc
      - name: varrunutmpro
        hostPath:
          path: /var/run/utmp
