{{- if .Values.agent.key -}}
{{- if or .Values.zone.name .Values.cluster.name -}}
apiVersion: apps/v1
kind: DaemonSet
metadata:
  name: {{ template "instana-agent.fullname" . }}
  namespace: {{ .Release.Namespace }}
  labels:
    {{- include "instana-agent.commonLabels" . | nindent 4 }}
spec:
  selector:
    matchLabels:
      {{- include "instana-agent.selectorLabels" . | nindent 6 }}
  template:
    metadata:
      labels:
        {{- include "instana-agent.commonLabels" . | nindent 8 }}
      {{- if .Values.agent.pod.annotations }}
      annotations:
        {{- toYaml .Values.agent.pod.annotations | nindent 8 }}
      {{- end }}
    spec:
      {{- if .Values.agent.pod.nodeSelector }}
      nodeSelector:
      {{- range $key, $value := .Values.agent.pod.nodeSelector }}
        {{ $key }}: {{ $value | quote }}
      {{- end }}
      {{- end }}
      serviceAccountName: {{ template "instana-agent.serviceAccountName" . }}
      hostIPC: true
      hostNetwork: true
      hostPID: true
      {{- if .Values.agent.pod.priorityClassName }}
      priorityClassName: {{ .Values.agent.pod.priorityClassName | quote }}
      {{- end }}
      dnsPolicy: ClusterFirstWithHostNet
      containers:
        - name: instana-agent
          image: "{{ .Values.agent.image.name }}:{{ .Values.agent.image.tag }}"
          imagePullPolicy: {{ .Values.agent.image.pullPolicy }}
          env:
            - name: INSTANA_AGENT_LEADER_ELECTOR_PORT
              value: {{ .Values.leaderElector.port | quote }}
            - name: INSTANA_ZONE
              value: {{ .Values.zone.name | quote }}
              {{- if .Values.cluster.name }}
            - name: INSTANA_KUBERNETES_CLUSTER_NAME
              value: {{ .Values.cluster.name | quote }}
              {{- end }}
            - name: INSTANA_AGENT_ENDPOINT
              value: {{ .Values.agent.endpointHost | quote }}
            - name: INSTANA_AGENT_ENDPOINT_PORT
              value: {{ .Values.agent.endpointPort | quote }}
            - name: INSTANA_AGENT_KEY
              valueFrom:
                secretKeyRef:
                  name: {{ template "instana-agent.fullname" . }}
                  key: key
            {{- if .Values.agent.mode }}
            - name: INSTANA_AGENT_MODE
              value: {{ .Values.agent.mode | quote }}
            {{- end }}
            {{- if .Values.agent.downloadKey }}
            - name: INSTANA_DOWNLOAD_KEY
              valueFrom:
                secretKeyRef:
                  name: {{ template "instana-agent.fullname" . }}
                  key: downloadKey
            {{- end }}
            {{- if .Values.agent.proxyHost }}
            - name: INSTANA_AGENT_PROXY_HOST
              value: {{ .Values.agent.proxyHost | quote }}
            {{- end }}
            {{- if .Values.agent.proxyPort }}
            - name: INSTANA_AGENT_PROXY_PORT
              value: {{ .Values.agent.proxyPort | quote }}
            {{- end }}
            {{- if .Values.agent.proxyProtocol }}
            - name: INSTANA_AGENT_PROXY_PROTOCOL
              value: {{ .Values.agent.proxyProtocol | quote }}
            {{- end }}
            {{- if .Values.agent.proxyUser }}
            - name: INSTANA_AGENT_PROXY_USER
              value: {{ .Values.agent.proxyUser | quote }}
            {{- end }}
            {{- if .Values.agent.proxyPassword }}
            - name: INSTANA_AGENT_PROXY_PASSWORD
              value: {{ .Values.agent.proxyPassword | quote }}
            {{- end }}
            {{- if .Values.agent.proxyUseDNS }}
            - name: INSTANA_AGENT_PROXY_USE_DNS
              value: {{ .Values.agent.proxyUseDNS | quote }}
            {{- end }}
            {{- if .Values.agent.listenAddress }}
            - name: INSTANA_AGENT_HTTP_LISTEN
              value: {{ .Values.agent.listenAddress | quote }}
            {{- end }}
            {{- if .Values.agent.redactKubernetesSecrets }}
            - name: INSTANA_KUBERNETES_REDACT_SECRETS
              value: {{ .Values.agent.redactKubernetesSecrets | quote }}
            {{- end }}
            - name: JAVA_OPTS
              # Approximately 1/3 of container memory requests to allow for direct-buffer memory usage and JVM overhead
              value: "-Xmx{{ div (default 512 .Values.agent.pod.requests.memory) 3 }}M -XX:+ExitOnOutOfMemoryError"
            - name: INSTANA_AGENT_POD_NAME
              valueFrom:
                fieldRef:
                  fieldPath: metadata.name
            - name: POD_IP
              valueFrom:
                fieldRef:
                  fieldPath: status.podIP
            {{- range $key, $value := .Values.agent.env }}
            - name: {{ $key }}
              value: {{ $value | quote }}
            {{- end }}
          securityContext:
            privileged: true
          volumeMounts:
            - name: dev
              mountPath: /dev
            - name: run
              mountPath: /run
            - name: var-run
              mountPath: /var/run
            - name: sys
              mountPath: /sys
            - name: var-log
              mountPath: /var/log
            - name: var-lib
              mountPath: /var/lib/containers/storage
            - name: machine-id
              mountPath: /etc/machine-id
            - name: configuration
              subPath: configuration.yaml
              mountPath: /root/configuration.yaml
            {{- if .Values.agent.host.repository }}
            - name: repo
              mountPath: /opt/instana/agent/data/repo
            {{- end }}
            {{- if .Values.agent.additionalBackends }}
            {{- range $index,$backend := .Values.agent.additionalBackends }}
            {{ $backendIndex :=add $index 2 -}}
            - name: additional-backend-{{$backendIndex}}
              subPath: additional-backend-{{$backendIndex}}
              mountPath: /opt/instana/agent/etc/instana/com.instana.agent.main.sender.Backend-{{$backendIndex}}.cfg
            {{- end }}
            {{- end }}
          livenessProbe:
            httpGet:
              path: /status
              port: 42699
            initialDelaySeconds: 300
            timeoutSeconds: 3
          resources:
            requests:
              memory: "{{ default 512 .Values.agent.pod.requests.memory }}Mi"
              cpu: {{ default 0.5 .Values.agent.pod.requests.cpu }}
            limits:
              memory: "{{ default 512 .Values.agent.pod.limits.memory }}Mi"
              cpu: {{ default 1.5 .Values.agent.pod.limits.cpu }}
          ports:
            - containerPort: 42699
        - name: instana-agent-leader-elector
          image: "{{ .Values.leaderElector.image.name }}:{{ .Values.leaderElector.image.tag }}"
          env:
            - name: INSTANA_AGENT_POD_NAME
              valueFrom:
                fieldRef:
                  fieldPath: metadata.name
          command:
            - "/busybox/sh"
            - "-c"
            - "sleep 12 && /app/server --election=instana --http=localhost:{{ .Values.leaderElector.port }} --id=$(INSTANA_AGENT_POD_NAME)"
          resources:
            requests:
              cpu: 0.1
              memory: 64Mi
          livenessProbe:
            httpGet: # Leader elector liveness is tied to agent, published on localhost:42699
              path: /com.instana.agent.coordination.sidecar/health
              port: 42699
            initialDelaySeconds: 300
            timeoutSeconds: 3
          ports:
            - containerPort: {{ .Values.leaderElector.port }}
      {{- if .Values.agent.pod.tolerations }}
      tolerations:
        {{- toYaml .Values.agent.pod.tolerations | nindent 8 }}
      {{- end }}
      volumes:
        - name: dev
          hostPath:
            path: /dev
        - name: run
          hostPath:
            path: /run
        - name: var-run
          hostPath:
            path: /var/run
        - name: sys
          hostPath:
            path: /sys
        - name: var-log
          hostPath:
            path: /var/log
        - name: var-lib
          hostPath:
            path: /var/lib/containers/storage
        - name: machine-id
          hostPath:
            path: /etc/machine-id
        - name: configuration
          configMap:
            name: {{ template "instana-agent.fullname" . }}
        {{- if .Values.agent.host.repository }}
        - name: repo
          hostPath:
            path: {{ .Values.agent.host.repository }}
        {{- end }}
        {{- if .Values.agent.additionalBackends }}
        {{- $fullname := include "instana-agent.fullname" . -}}
        {{- range $index,$backend := .Values.agent.additionalBackends }}
        {{ $backendIndex :=add $index 2 -}}
        - name: additional-backend-{{$backendIndex}}
          configMap:
            name: {{ $fullname }}
        {{- end }}
        {{- end }}
{{- end -}}
{{- end -}}
