{{- if and .Values.licenseKey .Values.cluster }}
apiVersion: apps/v1beta2
kind: DaemonSet
metadata:
  labels: {{ include "newrelic.labels" . | indent 4 }}
  name: {{ template "newrelic.fullname" . }}
spec:
  updateStrategy:
    type: {{ .Values.updateStrategy }}
  selector:
    matchLabels:
      app: {{ template "newrelic.name" . }}
      release: {{.Release.Name }}
  template:
    metadata:
    {{- if .Values.podAnnotations }}
      annotations:
{{ toYaml .Values.podAnnotations | indent 8}}
    {{- end }}
      labels:
        app: {{ template "newrelic.name" . }}
        release: {{.Release.Name }}
        {{- if .Values.podLabels}}
        {{ toYaml .Values.podLabels }}
        {{- end }}
    spec:
      serviceAccountName: {{ template "newrelic.serviceAccountName" . }}
      containers:
        - name: {{ template "newrelic.name" . }}
          image: "{{ .Values.image.repository }}:{{ .Values.image.tag }}"
          imagePullPolicy: "{{ .Values.image.pullPolicy }}"
          securityContext:
            privileged: true
          env:
            - name: NRIA_LICENSE_KEY
              valueFrom:
                secretKeyRef:
                  name: {{ template "newrelic.fullname" . }}-config
                  key: license
            - name: "CLUSTER_NAME"
              value: "{{ .Values.cluster }}"
           {{- if .Values.kubeStateMetricsUrl }}
            - name: "KUBE_STATE_METRICS_URL"
              value: "{{ .Values.kubeStateMetricsUrl }}"
           {{- end }}
            - name: "NRIA_DISPLAY_NAME"
              valueFrom:
                fieldRef:
                  apiVersion: "v1"
                  fieldPath: "spec.nodeName"
            - name: "NRK8S_NODE_NAME"
              valueFrom:
                fieldRef:
                  apiVersion: "v1"
                  fieldPath: "spec.nodeName"
            - name: "NRIA_PASSTHROUGH_ENVIRONMENT"
              value: "KUBERNETES_SERVICE_HOST,KUBERNETES_SERVICE_PORT,CLUSTER_NAME,CADVISOR_PORT,NRK8S_NODE_NAME,KUBE_STATE_METRICS_URL"
            {{- if .Values.verboseLog }}
            - name: NRIA_VERBOSE
              value: "1"
            {{- end }}
            {{- if .Values.logFile }}
            - name: NRIA_LOG_FILE
              value: {{ .Values.logFile }}
            {{- end }}
          volumeMounts:
            {{- if .Values.config }}
            - name: config
              mountPath: /etc/newrelic-infra.yml
              subPath: newrelic-infra.yml
            {{- end }}
            - name: dev
              mountPath: /dev
            - name: host-docker-socket
              mountPath: /var/run/docker.sock
            - name: log
              mountPath: /var/log
            - name: host-volume
              mountPath: /host
              readOnly: true
          {{- if .Values.resources }}
          resources:
{{ toYaml .Values.resources | indent 12 }}
          {{- end }}
      volumes:
        - name: dev
          hostPath:
            path: /dev
        - name: host-docker-socket
          hostPath:
              path: /var/run/docker.sock
        - name: log
          hostPath:
              path: /var/log
        - name: host-volume
          hostPath:
              path: /
        {{- if .Values.config }}
        - name: config
          configMap:
            name: {{ template "newrelic.fullname" . }}
            items:
            - key: newrelic-infra.yml
              path: newrelic-infra.yml
        {{- end }}
      {{- if $.Values.nodeSelector }}
      nodeSelector:
{{ toYaml $.Values.nodeSelector | indent 8 }}
      {{- end }}
      {{- if .Values.tolerations }}
      tolerations:
{{ toYaml .Values.tolerations | indent 8 }}
      {{- end }}
{{- end }}
