{{- if and .Values.collector.enabled (not .Values.collector.useDaemonset) }}
apiVersion: apps/v1
kind: Deployment
metadata:
  labels: {{- include "common.labels.standard" . | nindent 4 }}
    app.kubernetes.io/component: collector
    {{- if .Values.commonLabels }}
    {{- include "common.tplvalues.render" ( dict "value" .Values.commonLabels "context" $ ) | nindent 4 }}
    {{- end }}
  name: {{ template "common.names.fullname" . }}-collector
  {{- if .Values.commonAnnotations }}
  annotations: {{- include "common.tplvalues.render" ( dict "value" .Values.commonAnnotations "context" $ ) | nindent 4 }}
  {{- end }}
spec:
  {{- if .Values.collector.updateStrategy }}
  strategy: {{- toYaml .Values.collector.updateStrategy | nindent 4 }}
  {{- end }}
  selector:
    matchLabels: {{- include "common.labels.matchLabels" . | nindent 6 }}
      app.kubernetes.io/component: collector
  template:
    metadata:
      {{- if .Values.collector.podAnnotations }}
      annotations: {{- include "common.tplvalues.render" (dict "value" .Values.collector.podAnnotations "context" $) | nindent 8 }}
      {{- end }}
      labels: {{- include "common.labels.standard" . | nindent 8 }}
        app.kubernetes.io/component: collector
        {{- if .Values.collector.podLabels }}
        {{- include "common.tplvalues.render" (dict "value" .Values.collector.podLabels "context" $) | nindent 8 }}
        {{- end }}
    spec:
      serviceAccountName: {{ template "wavefront.collector.serviceAccountName" . }}
      {{- include "wavefront.imagePullSecrets" . | nindent 6 }}
      {{- if .Values.collector.affinity }}
      affinity: {{- include "common.tplvalues.render" ( dict "value" .Values.collector.affinity "context" $) | nindent 8 }}
      {{- end }}
      {{- if .Values.collector.nodeSelector }}
      nodeSelector: {{- include "common.tplvalues.render" ( dict "value" .Values.collector.nodeSelector "context" $) | nindent 8 }}
      {{- end }}
      tolerations:
        - effect: NoSchedule
          key: node.alpha.kubernetes.io/role
          operator: Exists
        - effect: NoSchedule
          key: node-role.kubernetes.io/master
          operator: Exists
        {{- if .Values.collector.tolerations }}
        {{- include "common.tplvalues.render" (dict "value" .Values.collector.tolerations "context" .) | nindent 8 }}
        {{- end }}
      {{- if .Values.collector.priorityClassName }}
      priorityClassName: {{ .Values.collector.priorityClassName | quote }}
      {{- end }}
      {{- if .Values.collector.podSecurityContext.enabled }}
      securityContext:
        fsGroup: {{ .Values.collector.podSecurityContext.fsGroup }}
      {{- end }}
      {{- if .Values.collector.initContainers }}
      initContainers: {{- include "common.tplvalues.render" (dict "value" .Values.collector.initContainers "context" $) | nindent 8 }}
      {{- end }}
      containers:
        - name: wavefront-collector
          image: {{ template "wavefront.collector.image" . }}
          imagePullPolicy: {{ .Values.collector.image.pullPolicy }}
          {{- if .Values.collector.lifecycleHooks }}
          lifecycle: {{- include "common.tplvalues.render" (dict "value" .Values.collector.lifecycleHooks "context" $) | nindent 12 }}
          {{- end }}
          {{- if .Values.collector.containerSecurityContext.enabled }}
          securityContext:
            runAsUser: {{ .Values.collector.containerSecurityContext.runAsUser }}
            runAsNonRoot: {{ .Values.collector.containerSecurityContext.runAsNonRoot }}
          {{- end }}
          command:
          {{- if .Values.collector.command }}
          {{- include "common.tplvalues.render" (dict "value" .Values.collector.command "context" $) | nindent 12 }}
          {{- else }}
            - wavefront-collector
            - --daemon=false
            - --config-file=/etc/collector/config.yaml
            {{- if .Values.collector.maxProcs }}
            - --max-procs={{ .Values.collector.maxProcs }}
            {{- end }}
            {{- if .Values.collector.logLevel }}
            - --log-level={{ .Values.collector.logLevel }}
            {{- end }}
          {{- end }}
          {{- if .Values.collector.args }}
          args: {{- include "common.tplvalues.render" (dict "value" .Values.collector.args "context" $) | nindent 12 }}
          {{- end }}
          envFrom:
            {{- if .Values.collector.extraEnvVarsCM }}
            - configMapRef:
                name: {{ include "common.tplvalues.render" (dict "value" .Values.collector.extraEnvVarsCM "context" $) }}
            {{- end }}
            {{- if .Values.collector.extraEnvVarsSecret }}
            - secretRef:
                name: {{ include "common.tplvalues.render" (dict "value" .Values.collector.extraEnvVarsSecret "context" $) }}
            {{- end }}
          {{- if .Values.collector.resources }}
          resources: {{- toYaml .Values.collector.resources | nindent 12 }}
          {{- end }}
          {{- if .Values.collector.customLivenessProbe }}
          livenessProbe: {{- include "common.tplvalues.render" (dict "value" .Values.collector.customLivenessProbe "context" $) | nindent 12 }}
          {{- end }}
          {{- if .Values.collector.customReadinessProbe }}
          readinessProbe: {{- include "common.tplvalues.render" (dict "value" .Values.collector.customReadinessProbe "context" $) | nindent 12 }}
          {{- end }}
          volumeMounts:
            - name: config
              mountPath: /etc/collector/
              readOnly: true
            - name: ssl-certs
              mountPath: /etc/ssl/certs
              readOnly: true
          {{- if .Values.collector.extraVolumeMounts }}
          {{- include "common.tplvalues.render" (dict "value" .Values.collector.extraVolumeMounts "context" $) | nindent 12 }}
          {{- end }}
        {{- if .Values.collector.sidecars }}
        {{- include "common.tplvalues.render" ( dict "value" .Values.collector.sidecars "context" $) | nindent 8 }}
        {{- end }}
      volumes:
        - name: config
          configMap:
            name: {{ include "wavefront.collector.configmapName" . }}
        - name: ssl-certs
          hostPath:
            path: /etc/ssl/certs
        {{- if .Values.collector.extraVolumes }}
        {{- include "common.tplvalues.render" (dict "value" .Values.collector.extraVolumes "context" $) | nindent 8 }}
        {{- end }}
{{- end }}
