# _helpers.tpl
{{/* vim: set filetype=mustache: */}}
{{/*
Expand the name of the chart.
*/}}
{{- define "name" -}}
{{- default .Chart.Name .Values.nameOverride | trunc 24 -}}
{{- end -}}

{{/*
Create a default fully qualified app name.
We truncate at 24 chars because some Kubernetes name fields are limited to this (by the DNS naming spec).
*/}}
{{- define "fullname" -}}
{{- $name := default .Chart.Name .Values.nameOverride -}}
{{- printf "%s-%s" .Release.Name $name | trunc 24 -}}
{{- end -}}
 # config.yaml
apiVersion: v1
kind: ConfigMap
metadata:
metadata:
  name: "{{ template "fullname" . }}-config-{{ .Release.Time.Seconds }}"
  labels:
    app: {{ template "fullname" . }}
    chart: "{{ .Chart.Name }}-{{ .Chart.Version }}"
    release: "{{ .Release.Name }}"
    heritage: "{{ .Release.Service }}"
  annotations:
    "helm.sh/hook": pre-install,pre-upgrade
data:
  sumo-sources.json: |-
    {
      "api.version": "v1",
      "sources": [
        {
          "sourceType" : "LocalFile",
          "name": "kubernetes-localfile",
          "pathExpression": {{ default "/var/log/containers/*.log" .Values.sumologic.pathExpression | quote }},
          "multilineProcessingEnabled": {{ default false .Values.sumologic.multilineProcessingEnabled }},
          "automaticDateParsing": {{ default true .Values.sumologic.automaticDateParsing }},
          "category": {{ default "kubernetes" .Values.sumologic.categoryName | quote }},
          "forceTimeZone": {{ default false .Values.sumologic.forceTimeZone }}
        }
      ]
    }
 # daemonset.yaml
{{- if and .Values.sumologic.accessId .Values.sumologic.accessKey -}}
apiVersion: extensions/v1beta1
kind: DaemonSet
metadata:
  name: {{ template "fullname" . }}
  labels:
    chart: "{{ .Chart.Name }}-{{ .Chart.Version }}"
spec:
  template:
    metadata:
      name: {{ template "fullname" . }}
      labels:
        app: {{ template "fullname" . }}
        chart: "{{ .Chart.Name }}-{{ .Chart.Version }}"
        release: "{{ .Release.Name }}"
        heritage: "{{ .Release.Service }}"
    spec:
      serviceAccount: {{ template "fullname" . }}
      containers:
      - name: {{ template "fullname" . }}
        image: "{{.Values.image.name}}:{{.Values.image.tag}}"
        imagePullPolicy: {{.Values.image.pullPolicy}}
        resources:
{{ toYaml .Values.resources | indent 12 }}
        securityContext:
          privileged: true
        volumeMounts:
        - name: varlog
          mountPath: /var/log
        - name: varlibdockercontainers
          mountPath: /var/lib/docker/containers
          readOnly: true
        - name: sumo-sources
          mountPath: /etc/sumologic
          readOnly: true
        env:
        - name: SUMO_ACCESS_ID
          valueFrom:
            secretKeyRef:
              name: "{{ template "fullname" . }}-secrets-{{.Release.Time.Seconds }}"
              key: access-id
        - name: SUMO_ACCESS_KEY
          valueFrom:
            secretKeyRef:
              name: "{{ template "fullname" . }}-secrets-{{.Release.Time.Seconds }}"
              key: access-key
        - name: SUMO_COLLECTOR_NAME
          valueFrom:
            secretKeyRef:
              name: "{{ template "fullname" . }}-secrets-{{.Release.Time.Seconds }}"
              key: collector-name
        - name: SUMO_SOURCES_JSON
          value: "/etc/sumologic/sumo-sources.json"
      volumes:
      - name: varlog
        hostPath:
          path: /var/log
      - name: varlibdockercontainers
        hostPath:
          path: /var/lib/docker/containers
      - name: sumo-sources
        configMap:
          name: "{{ template "fullname" . }}-config-{{.Release.Time.Seconds }}"
      tolerations:
{{ toYaml .Values.daemonset.tolerations | indent 8 }}
  updateStrategy:
    type: {{ default "OnDelete" .Values.daemonset.updateStrategy | quote }}
{{ end }}
 # secrets.yaml
apiVersion: v1
kind: Secret
metadata:
  name: "{{ template "fullname" . }}-secrets-{{ .Release.Time.Seconds }}"
  labels:
    app: {{ template "fullname" . }}
    chart: "{{ .Chart.Name }}-{{ .Chart.Version }}"
    release: "{{ .Release.Name }}"
    heritage: "{{ .Release.Service }}"
  annotations:
    "helm.sh/hook": pre-install,pre-upgrade
type: Opaque
data:
  access-id: {{ default "MISSING" .Values.sumologic.accessId | b64enc | quote }}
  access-key: {{ default "MISSING" .Values.sumologic.accessKey | b64enc | quote }}
  collector-name: {{ default "kubernetes" .Values.sumologic.collectorName | b64enc | quote }}
 # serviceaccount.yaml
apiVersion: v1
kind: ServiceAccount
metadata:
  name: {{ template "fullname" . }}
  labels:
    app: {{ template "fullname" . }}
    chart: "{{ .Chart.Name }}-{{ .Chart.Version }}"
    release: "{{ .Release.Name }}"
    heritage: "{{ .Release.Service }}"

# Default values for sumokube.
image:
  name: sumologic/collector
  tag: latest
  pullPolicy: IfNotPresent

sumologic:
  ## You'll need to set this to your Sumologic API access key and access ID
  ## before the agent will run.
  ## ref: https://help.sumologic.com/Manage/Security/Access_Keys
  ##
  # accessId:
  # accessKey:

  collectorName: "kubernetes-collector"

  ## A full list of configurable JSON source options can be found at:
  ## https://help.sumologic.com/Send_Data/Sources/03Use_JSON_to_Configure_Sources
    # categoryName: "kubernetes"
    # multilineProcessingEnabled:
    # automaticDateParsing:
    # forceTimeZone:
    # pathExpression:

resources:
  requests:
    cpu: 100m
    memory: 128Mi
  limits:
    cpu: 256m
    memory: 256Mi

daemonset:
  tolerations: []
