{{- if or (.Values.infraToken) (.Values.logsToken) (.Values.containerToken) }}
{{- if .Capabilities.APIVersions.Has "apps/v1" }}
apiVersion: apps/v1
{{- else }}
apiVersion: extensions/v1beta1
{{- end }}
kind: DaemonSet
metadata:
  name: {{ template "sematext-agent.fullname" . }}
  labels:
    app: {{ template "sematext-agent.name" . }}
    chart: {{ template "sematext-agent.chart" . }}
    release: {{ .Release.Name }}
    heritage: {{ .Release.Service }}
spec:
  selector:
    matchLabels:
      app: {{ template "sematext-agent.name" . }}
      release: {{ .Release.Name }}
  template:
    metadata:
      labels:
        app: {{ template "sematext-agent.name" . }}
        release: {{ .Release.Name }}
      annotations:
        checksum/config-agent: {{ include (print $.Template.BasePath "/configmap-agent.yaml") . | sha256sum }}
        checksum/config-agent-autodisco: {{ include (print $.Template.BasePath "/configmap-agent-autodisco.yaml") . | sha256sum }}
        checksum/config-logagent: {{ include (print $.Template.BasePath "/configmap-logagent.yaml") . | sha256sum }}
        checksum/secret: {{ include (print $.Template.BasePath "/secret.yaml") . | sha256sum }}
    spec:
      serviceAccountName: {{ template "sematext-agent.serviceAccountName" . }}
      containers:
        - name: agent
          image: "{{ .Values.agent.image.repository }}:{{ .Values.agent.image.tag }}"
          imagePullPolicy: {{ .Values.agent.image.pullPolicy }}
          env:
          - name: INFRA_TOKEN
            valueFrom:
              secretKeyRef:
                name: {{ template "sematext-agent.fullname" . }}
                key: infra-token
          - name: CONTAINER_TOKEN
            valueFrom:
              secretKeyRef:
                name: {{ template "sematext-agent.fullname" . }}
                key: container-token
          - name: LOGS_TOKEN
            valueFrom:
              secretKeyRef:
                name: {{ template "sematext-agent.fullname" . }}
                key: logs-token
          - name: NODE_NAME
            valueFrom:
              fieldRef:
                fieldPath: spec.nodeName
          envFrom:
          - configMapRef:
              name: {{ template "sematext-agent.fullname" . }}-agent
          livenessProbe:
            httpGet:
              path: /health
              port: {{ .Values.agent.service.port }}
          readinessProbe:
            httpGet:
              path: /health
              port: {{ .Values.agent.service.port }}
          volumeMounts:
            - name: procfs
              mountPath: /host/proc
              readOnly: true
            - name: sysfs
              mountPath: /host/sys
              readOnly: true
            - name: etc
              mountPath: /host/etc
              readOnly: true
            - name: debugfs
              mountPath: /sys/kernel/debug
            - name: docker-sock
              mountPath: /var/run/docker.sock
            - name: journal
              mountPath: /opt/spm/st-agent
            - name: autodisco-template
              mountPath: /etc/agent
          securityContext:
            privileged: true
          ports:
            - name: http
              containerPort: {{ .Values.agent.service.port }}
              protocol: TCP
          resources:
{{ toYaml .Values.agent.resources | indent 12 }}
        - name: logagent
          image: "{{ .Values.logagent.image.repository }}:{{ .Values.logagent.image.tag }}"
          imagePullPolicy: {{ .Values.logagent.image.pullPolicy }}
          env:
          - name: LOGS_TOKEN
            valueFrom:
              secretKeyRef:
                name: {{ template "sematext-agent.fullname" . }}
                key: logs-token
          envFrom:
          - configMapRef:
              name: {{ template "sematext-agent.fullname" . }}-logagent
          volumeMounts:
            - name: docker-sock
              mountPath: /var/run/docker.sock
          resources:
{{ toYaml .Values.logagent.resources | indent 12 }}
      volumes:
        - name: procfs
          hostPath:
            path: /proc
        - name: sysfs
          hostPath:
            path: /sys
        - name: etc
          hostPath:
            path: /etc
        - name: debugfs
          hostPath:
            path: /sys/kernel/debug
        - name: docker-sock
          hostPath:
            path: /var/run/docker.sock
        - name: journal
          hostPath:
            path: /opt/spm/st-agent
        - name: autodisco-template
          configMap:
            name: {{ template "sematext-agent.fullname" . }}-agent-autodisco
      {{ if .Values.imagePullSecrets }}
      imagePullSecrets:
        - name: {{ .Values.imagePullSecrets }}
      {{- end }}
      nodeSelector:
{{ toYaml .Values.nodeSelector | indent 8 }}
      tolerations:
{{ toYaml .Values.tolerations | indent 8 }}
{{- end }}
