# _helpers.tpl
{{/*
Create the name of the service account to use
*/}}
{{- define "logdna.serviceAccountName" -}}
{{- if .Values.serviceAccount.create -}}
  {{ .Values.serviceAccount.name | default .Values.logdna.name }}
{{- else -}}
  {{ .Values.serviceAccount.name | default "default" }}
{{- end -}}
{{- end -}}

{{/* vim: set filetype=mustache: */}}
{{/*
Expand the name of the chart.
*/}}
{{- define "logdna.name" -}}
  {{- default .Chart.Name .Values.nameOverride | trunc 63 | trimSuffix "-" -}}
{{- end -}}

{{/*
Create a default fully qualified app name.
We truncate at 63 chars because some Kubernetes name fields are limited to this (by the DNS naming spec).
If release name contains chart name it will be used as a full name.
*/}}
{{- define "logdna.fullname" -}}
  {{- if .Values.fullnameOverride -}}
    {{- .Values.fullnameOverride | trunc 63 | trimSuffix "-" -}}
  {{- else -}}
    {{- $name := default .Chart.Name .Values.nameOverride -}}
    {{- if contains $name .Release.Name -}}
      {{- .Release.Name | trunc 63 | trimSuffix "-" -}}
    {{- else -}}
      {{- printf "%s-%s" .Release.Name $name | trunc 63 | trimSuffix "-" -}}
    {{- end -}}
  {{- end -}}
{{- end -}}

{{/*
Create chart name and version as used by the chart label.
*/}}
{{- define "logdna.chart" -}}
  {{ .Chart.Name }}-{{ .Chart.Version | replace "+" "_" }}
{{- end -}}
 # cluster_role.yaml
apiVersion: rbac.authorization.k8s.io/v1beta1
kind: ClusterRole
metadata:
  name: {{ .Values.logdna.name }}
  labels:
    app.kubernetes.io/name: {{ template "logdna.name" . }}
    app.kubernetes.io/managed-by: {{ .Release.Service }}
    app.kubernetes.io/instance: {{ .Release.Name }}
    app.kubernetes.io/version: {{ .Chart.AppVersion }}
    helm.sh/chart: {{ template "logdna.chart" . }}
rules:
  - apiGroups: [""]
    resources: ["events"]
    verbs: ["get","list","create","watch"]
  - apiGroups: [""]
    resources: ["pods"]
    verbs: ["get","list","watch"]
 # cluster_role_binding.yaml
apiVersion: rbac.authorization.k8s.io/v1beta1
kind: ClusterRoleBinding
metadata:
  name: {{ .Values.logdna.name }}
  labels:
    app.kubernetes.io/name: {{ template "logdna.name" . }}
    app.kubernetes.io/managed-by: {{ .Release.Service }}
    app.kubernetes.io/instance: {{ .Release.Name }}
    app.kubernetes.io/version: {{ .Chart.AppVersion }}
    helm.sh/chart: {{ template "logdna.chart" . }}
roleRef:
  apiGroup: rbac.authorization.k8s.io
  kind: ClusterRole
  name: {{ .Values.logdna.name }}
subjects:
  - kind: ServiceAccount
    name: {{ template "logdna.serviceAccountName" . }}
    namespace: {{ .Release.Namespace }}
 # daemon_set.yaml
{{- if .Values.logdna.key -}}
apiVersion: apps/v1
kind: DaemonSet
metadata:
  name: {{ .Values.logdna.name }}
  namespace: {{ .Release.Namespace }}
  labels:
    app.kubernetes.io/name: {{ template "logdna.name" . }}
    app.kubernetes.io/managed-by: {{ .Release.Service }}
    app.kubernetes.io/instance: {{ .Release.Name }}
    app.kubernetes.io/version: {{ .Chart.AppVersion }}
    helm.sh/chart: {{ template "logdna.chart" . }}
spec:
  {{- if .Values.daemonset.updateStrategy }}
  updateStrategy:
{{ toYaml .Values.daemonset.updateStrategy | indent 4 }}
  {{- end }}
  selector:
    matchLabels:
      app.kubernetes.io/name: {{ template "logdna.name" . }}
  template:
    metadata:
      labels:
        app.kubernetes.io/name: {{ template "logdna.name" . }}
        app.kubernetes.io/managed-by: {{ .Release.Service }}
        app.kubernetes.io/instance: {{ .Release.Name }}
        app.kubernetes.io/version: {{ .Chart.AppVersion }}
        helm.sh/chart: {{ template "logdna.chart" . }}
      {{- if .Values.updateOnSecretChange }}
      annotations:
        checksum/secrets: {{ include (print $.Template.BasePath "/secrets.yaml") . | sha256sum | quote }}
      {{- end }}
    spec:
      serviceAccountName: {{ template "logdna.serviceAccountName" . }}
      {{- if .Values.priorityClassName }}
      priorityClassName: {{ .Values.priorityClassName | quote }}
      {{- end }}
      containers:
      - name: {{ .Values.logdna.name }}
        image: "{{ .Values.image.name }}:{{ .Chart.AppVersion }}"
        imagePullPolicy: {{ .Values.image.pullPolicy }}
        env:
          - name: LOGDNA_AGENT_KEY
            valueFrom:
              secretKeyRef:
                name: {{ template "logdna.name" . }}
                key: logdna-agent-key
          - name: RUST_LOG
            value: info
          - name: NODE_NAME
            valueFrom:
              fieldRef:
                fieldPath: spec.nodeName
          - name: NAMESPACE
            valueFrom:
              fieldRef:
                fieldPath: metadata.namespace
{{- if .Values.logdna.tags }}
          - name: LOGDNA_TAGS
            value: {{ .Values.logdna.tags }}
{{- end }}
{{- if .Values.extraEnv }}
{{ tpl (toYaml .Values.extraEnv) $ | indent 10 }}
{{- end }}
        resources:
{{ toYaml .Values.resources | indent 12 }}
        volumeMounts:
          - name: varlog
            mountPath: /var/log
          - name: vardata
            mountPath: /var/data
          - name: varlibdockercontainers
            mountPath: /var/lib/docker/containers
            readOnly: true
          - name: mnt
            mountPath: /mnt
            readOnly: true
          - name: docker
            mountPath: /var/run/docker.sock
          - name: osrelease
            mountPath: /etc/os-release
          - name: logdnahostname
            mountPath: /etc/logdna-hostname
{{- if .Values.extraVolumeMounts }}
{{ toYaml .Values.extraVolumeMounts | nindent 10 }}
{{- end }}
      volumes:
        - name: varlog
          hostPath:
            path: /var/log
        - name: vardata
          hostPath:
            path: /var/data
        - name: varlibdockercontainers
          hostPath:
            path: /var/lib/docker/containers
        - name: mnt
          hostPath:
            path: /mnt
        - name: docker
          hostPath:
            path: /var/run/docker.sock
        - name: osrelease
          hostPath:
            path: /etc/os-release
        - name: logdnahostname
          hostPath:
            path: /etc/hostname
{{- if .Values.extraVolumes }}
{{ toYaml .Values.extraVolumes | indent 8}}
{{- end }}
      tolerations:
{{ toYaml .Values.daemonset.tolerations | indent 8 }}
{{- end -}}
 # role.yaml
apiVersion: rbac.authorization.k8s.io/v1
kind: Role
metadata:
  name: {{ .Values.logdna.name }}
  namespace: {{ .Release.Namespace }}
  labels:
    app.kubernetes.io/name: {{ template "logdna.name" . }}
    app.kubernetes.io/managed-by: {{ .Release.Service }}
    app.kubernetes.io/instance: {{ .Release.Name }}
    app.kubernetes.io/version: {{ .Chart.AppVersion }}
    helm.sh/chart: {{ template "logdna.chart" . }}
rules:
  - apiGroups: [""]
    resources: ["configmaps"]
    verbs: ["get","list","create","watch"]
 # role_binding.yaml
apiVersion: rbac.authorization.k8s.io/v1
kind: RoleBinding
metadata:
  name: {{ .Values.logdna.name }}
  namespace: {{ .Release.Namespace }}
  labels:
    app.kubernetes.io/name: {{ template "logdna.name" . }}
    app.kubernetes.io/managed-by: {{ .Release.Service }}
    app.kubernetes.io/instance: {{ .Release.Name }}
    app.kubernetes.io/version: {{ .Chart.AppVersion }}
    helm.sh/chart: {{ template "logdna.chart" . }}
roleRef:
  apiGroup: rbac.authorization.k8s.io
  kind: Role
  name: {{ .Values.logdna.name }}
subjects:
  - kind: ServiceAccount
    name: {{ template "logdna.serviceAccountName" . }}
    namespace: {{ .Release.Namespace }}
 # secrets.yaml
apiVersion: v1
kind: Secret
metadata:
  name: {{ template "logdna.name" . }}
  namespace: {{ .Release.Namespace }}
  labels:
    app.kubernetes.io/name: {{ template "logdna.name" . }}
    app.kubernetes.io/managed-by: {{ .Release.Service }}
    app.kubernetes.io/instance: {{ .Release.Name }}
    app.kubernetes.io/version: {{ .Chart.AppVersion }}
    helm.sh/chart: {{ template "logdna.chart" . }}
type: Opaque
data:
  logdna-agent-key: {{ default "" .Values.logdna.key | b64enc | quote }}
 # service_account.yaml
{{- if .Values.serviceAccount.create -}}
apiVersion: v1
kind: ServiceAccount
metadata:
  name: {{ template "logdna.serviceAccountName" . }}
  namespace: {{ .Release.Namespace }}
  labels:
    app.kubernetes.io/name: {{ template "logdna.name" . }}
    app.kubernetes.io/managed-by: {{ .Release.Service }}
    app.kubernetes.io/instance: {{ .Release.Name }}
    app.kubernetes.io/version: {{ .Chart.AppVersion }}
    helm.sh/chart: {{ template "logdna.chart" . }}
{{ end }}

image:
  name: logdna/logdna-agent
  pullPolicy: IfNotPresent

logdna:
  ## Please follow directions from https://app.logdna.com/pages/add-source

  ## An ingestion key is required for this chart to run
  # key:

  # tags:

  name: logdna-agent

resources:
  requests:
    cpu: 20m
  limits:
    memory: 75Mi

daemonset:
  # updateStrategy:
  #   type: "RollingUpdate"
  tolerations: []

priorityClassName: ""

# updateOnSecretChange: true
extraEnv: {}
extraVolumeMounts: []
extraVolumes: []

serviceAccount:
  create: true
  name:
