{{- if (or (.Values.sumologic.collectorUrlExistingSecret) (.Values.sumologic.collectorUrl)) -}}
# Sumologic collector URL is required
apiVersion: extensions/v1beta1
kind: DaemonSet
metadata:
  name: {{ template "sumologic-fluentd.fullname" . }}
  labels:
    app: {{ template "sumologic-fluentd.name" . }}
    chart: {{ .Chart.Name }}-{{ .Chart.Version }}
    release: {{ .Release.Name }}
    heritage: {{ .Release.Service }}
spec:
  template:
    metadata:
      name: {{ template "sumologic-fluentd.fullname" . }}
    {{- if .Values.podAnnotations }}
      annotations:
{{ toYaml .Values.podAnnotations | indent 8 }}
    {{- end }}
      labels:
        app: {{ template "sumologic-fluentd.name" . }}
        release: {{ .Release.Name }}
    spec:
      {{- if .Values.daemonset.priorityClassName }}
      priorityClassName: {{ .Values.daemonset.priorityClassName }}
      {{- end }}
      containers:
        - name: {{ template "sumologic-fluentd.fullname" . }}
          image: "{{ .Values.image.name }}:{{ .Values.image.tag }}"
          imagePullPolicy: {{.Values.image.pullPolicy}}
          resources:
{{ toYaml .Values.resources | indent 12 }}
          volumeMounts:
            - name: host-logs
              mountPath: /mnt/log/
              readOnly: true
            - name: host-logs
              mountPath: /var/log/
              readOnly: true
            - name: docker-logs
              mountPath: /var/lib/docker/
              readOnly: true
            - name: pos-files
              mountPath: /mnt/pos/
            {{- if eq .Values.sumologic.fluentdSource "systemd" }}
            - name: rkt-logs
              mountPath: /var/lib/rkt/
              readOnly: true
            {{- end }}
            {{- if .Values.sumologic.fluentdUserConfig }}
              {{- if .Values.sumologic.fluentdUserConfigDir }}
            - name: fluentd-user-conf
              mountPath: {{ quote .Values.sumologic.fluentdUserConfigDir }}
              readOnly: true
              {{- else }}
            - name: fluentd-user-conf
              mountPath: /fluentd/conf.d/user
              readOnly: true
              {{- end }}
            {{- end }}
          env:
            - name: COLLECTOR_URL
              valueFrom:
                secretKeyRef:
            {{- if .Values.sumologic.collectorUrlExistingSecret }}
                  name: "{{ .Values.sumologic.collectorUrlExistingSecret }}"
            {{- else }}
                  name: "{{ template "sumologic-fluentd.fullname" . }}"
            {{- end }}
                  key: collector-url
            - name: K8S_NODE_NAME
              valueFrom:
                fieldRef:
                  fieldPath: spec.nodeName
            - name: FLUENTD_SOURCE
              value: {{ quote .Values.sumologic.fluentdSource }}
            {{- if .Values.sumologic.fluentdUserConfigDir }}
            - name: FLUENTD_USER_CONFIG_DIR
              value: {{ quote .Values.sumologic.fluentdUserConfigDir }}
            {{- end }}
            {{- if .Values.sumologic.flushInterval }}
            - name: FLUSH_INTERVAL
              value: {{ quote .Values.sumologic.flushInterval }}
            {{- end }}
            {{- if .Values.sumologic.numThreads }}
            - name: NUM_THREADS
              value: {{ quote .Values.sumologic.numThreads }}
            {{- end }}
            {{- if .Values.sumologic.sourceName }}
            - name: SOURCE_NAME
              value: {{ quote .Values.sumologic.sourceName }}
            {{- end }}
            {{- if .Values.sumologic.sourceHost }}
            - name: SOURCE_HOST
              value: {{ quote .Values.sumologic.sourceHost }}
            {{- end }}
            {{- if .Values.sumologic.sourceCategory }}
            - name: SOURCE_CATEGORY
              value: {{ quote .Values.sumologic.sourceCategory }}
            {{- end }}
            {{- if .Values.sumologic.sourceCategoryPrefix }}
            - name: SOURCE_CATEGORY_PREFIX
              value: {{ quote .Values.sumologic.sourceCategoryPrefix }}
            {{- end }}
            {{- if .Values.sumologic.sourceCategoryReplaceDash }}
            - name: SOURCE_CATEGORY_REPLACE_DASH
              value: {{ quote .Values.sumologic.sourceCategoryReplaceDash }}
            {{- end }}
            {{- if .Values.sumologic.logFormat }}
            - name: LOG_FORMAT
              value: {{ quote .Values.sumologic.logFormat }}
            {{- end }}
            {{- if quote .Values.sumologic.kubernetesMeta }}
            - name: KUBERNETES_META
              value: {{ quote .Values.sumologic.kubernetesMeta }}
            {{- end }}
            {{- if .Values.sumologic.excludeContainerRegex }}
            - name: EXCLUDE_CONTAINER_REGEX
              value: {{ quote .Values.sumologic.excludeContainerRegex }}
            {{- end }}
            {{- if .Values.sumologic.excludeFacilityRegex }}
            - name: EXCLUDE_FACILITY_REGEX
              value: {{ quote .Values.sumologic.excludeFacilityRegex }}
            {{- end }}
            {{- if .Values.sumologic.excludeHostRegex }}
            - name: EXCLUDE_HOST_REGEX
              value: {{ quote .Values.sumologic.excludeHostRegex }}
            {{- end }}
            {{- if .Values.sumologic.excludeNamespaceRegex }}
            - name: EXCLUDE_NAMESPACE_REGEX
              value: {{ quote .Values.sumologic.excludeNamespaceRegex }}
            {{- end }}
            {{- if .Values.sumologic.excludePath }}
            - name: EXCLUDE_PATH
              value: {{ quote .Values.sumologic.excludePath }}
            {{- end }}
            {{- if .Values.sumologic.excludePodRegex }}
            - name: EXCLUDE_POD_REGEX
              value: {{ quote .Values.sumologic.excludePodRegex }}
            {{- end }}
            {{- if .Values.sumologic.excludePriorityRegex }}
            - name: EXCLUDE_PRIORITY_REGEX
              value: {{ quote .Values.sumologic.excludePriorityRegex }}
            {{- end }}
            {{- if .Values.sumologic.excludeUnitRegex }}
            - name: EXCLUDE_UNIT_REGEX
              value: {{ quote .Values.sumologic.excludeUnitRegex }}
            {{- end }}
            {{- if .Values.sumologic.fluentdOpt }}
            - name: FLUENTD_OPT
              value: {{ quote .Values.sumologic.fluentdOpt }}
            {{- end }}
            {{- if quote .Values.sumologic.timeKey }}
            - name: TIME_KEY
              value: {{ quote .Values.sumologic.timeKey }}
            {{- end }}
            {{- if quote .Values.sumologic.addTimeStamp }}
            - name: ADD_TIMESTAMP
              value: {{ quote .Values.sumologic.addTimeStamp }}
            {{- end }}
            {{- if quote .Values.sumologic.addTime }}
            - name: ADD_TIME
              value: {{ quote .Values.sumologic.addTime }}
            {{- end }}
            {{- if quote .Values.sumologic.addStream }}
            - name: ADD_STREAM
              value: {{ quote .Values.sumologic.addStream }}
            {{- end }}
            {{- if quote .Values.sumologic.verifySsl }}
            - name: VERIFY_SSL
              value: {{ quote .Values.sumologic.verifySsl }}
            {{- end }}
            {{- if .Values.sumologic.multilineStartRegexp }}
            - name: MULTILINE_START_REGEXP
              value: {{ quote .Values.sumologic.multilineStartRegexp }}
            {{- end }}
            {{- if quote .Values.sumologic.readFromHead }}
            - name: READ_FROM_HEAD
              value: {{ quote .Values.sumologic.readFromHead }}
            {{- end }}
            {{- if .Values.sumologic.concatSeparator }}
            - name: CONCAT_SEPARATOR
              value: {{ quote .Values.sumologic.concatSeparator }}
            {{- end }}
            {{- if .Values.sumologic.auditLogPath }}
            - name: AUDIT_LOG_PATH
              value: {{ quote .Values.sumologic.auditLogPath }}
            {{- end }}
            {{- if .Values.sumologic.containerLogsPath }}
            - name: CONTAINER_LOGS_PATH
              value: {{ quote .Values.sumologic.containerLogsPath }}
            {{- end }}
            {{- if .Values.sumologic.proxyUri }}
            - name: PROXY_URI
              value: {{ quote .Values.sumologic.proxyUri }}
            {{- end }}
            {{- if .Values.sumologic.enableStatWatcher }}
            - name: ENABLE_STAT_WATCHER
              value: {{ quote .Values.sumologic.enableStatWatcher }}
            {{- end }}
{{- if .Values.extraEnv }}
{{ toYaml .Values.extraEnv | indent 12 }}
{{- end }}
      serviceAccountName: {{ if .Values.rbac.create }}{{ template "sumologic-fluentd.fullname" . }}{{ else }}"{{ .Values.rbac.serviceAccountName }}"{{ end }}
      volumes:
        - name: pos-files
          {{- if .Values.persistence.enabled }}
          hostPath:
            path: {{ .Values.persistence.hostPath }}
            type: "{{ if .Values.persistence.createPath }}DirectoryOrCreate{{ end }}"
          {{- else }}
          emptyDir: {}
          {{- end }}
        - name: host-logs
          hostPath:
            path: /var/log/
        {{- if eq .Values.sumologic.fluentdSource "systemd" }}
        - name: rkt-logs
          hostPath:
            path: /var/lib/rkt
        {{- end }}
        - name: docker-logs
          hostPath:
            path: /var/lib/docker
        {{- if .Values.sumologic.fluentdUserConfig }}
        - name: fluentd-user-conf
          configMap:
            name: {{ template "sumologic-fluentd.fluentdUserConfig.fullname" . }}
        {{- end }}
    {{- if .Values.tolerations }}
      tolerations:
{{ toYaml .Values.tolerations | indent 8 }}
    {{- end }}
  updateStrategy:
    type: "{{ .Values.updateStrategy }}"
{{- end }}
