# _helpers.tpl
{{/* vim: set filetype=mustache: */}}
{{/*
Expand the name of the chart.
*/}}
{{- define "sematext.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).
*/}}
{{- define "sematext.fullname" -}}
{{- $name := default .Chart.Name .Values.nameOverride -}}
{{- printf "%s-%s" .Release.Name $name | trunc 63 | trimSuffix "-" -}}
{{- end -}}
 # configmap.yaml
apiVersion: v1
kind: ConfigMap
metadata:
  name: {{ template "sematext.fullname" . }}
  labels:
    app: {{ template "sematext.name" . }}
    chart: {{ .Chart.Name }}-{{ .Chart.Version | replace "+" "_" }}
    release: {{ .Release.Name }}
data:
{{- if .Values.sematext.config }}
{{ toYaml .Values.sematext.config | indent 2 }}
{{- end }}
{{- if .Values.sematext.url }}
  SPM_RECEIVER_URL: {{ default "" .Values.sematext.url.spmReceiver | quote }}
  LOGSENE_RECEIVER_URL: {{ default "" .Values.sematext.url.logseneReceiver | quote }}
  EVENTS_RECEIVER_URL: {{ default "" .Values.sematext.url.eventsReceiver | quote }}
{{- else if eq .Values.sematext.region "EU" }}
  SPM_RECEIVER_URL: "https://spm-receiver.eu.sematext.com/receiver/v1"
  LOGSENE_RECEIVER_URL: "https://logsene-receiver.eu.sematext.com"
  EVENTS_RECEIVER_URL: "https://event-receiver.eu.sematext.com"
{{- else if eq .Values.sematext.region "US" }}
  SPM_RECEIVER_URL: "https://spm-receiver.sematext.com/receiver/v1"
  LOGSENE_RECEIVER_URL: "https://logsene-receiver.sematext.com"
  EVENTS_RECEIVER_URL: "https://event-receiver.sematext.com"
{{- end }}
 # daemonset.yaml
{{- if or (.Values.sematext.spmToken) (.Values.sematext.logseneToken) }}
{{- if .Capabilities.APIVersions.Has "apps/v1beta2" }}
apiVersion: apps/v1beta2
{{- else }}
apiVersion: extensions/v1beta1
{{- end }}
kind: DaemonSet
metadata:
  name: {{ template "sematext.fullname" . }}
  labels:
    app: {{ template "sematext.name" . }}
    chart: {{ .Chart.Name }}-{{ .Chart.Version | replace "+" "_" }}
    release: {{ .Release.Name }}
spec:
  selector:
    matchLabels:
      app: {{ template "sematext.name" . }}
      release: {{ .Release.Name }}
  template:
    metadata:
      labels:
        app: {{ template "sematext.name" . }}
        release: {{ .Release.Name }}
      annotations:
        checksum/config: {{ include (print $.Template.BasePath "/configmap.yaml") . | sha256sum }}
    spec:
      {{- if .Values.sematext.useHostNetwork }}
      hostNetwork: {{ .Values.sematext.useHostNetwork }}
      {{- end }}
      dnsPolicy: "ClusterFirst"
      restartPolicy: "Always"
      containers:
      - name: {{ .Chart.Name }}
        image: "{{ .Values.image.repository }}:{{ .Values.image.tag }}"
        imagePullPolicy: "{{ .Values.image.pullPolicy }}"
        resources:
{{ toYaml .Values.sematext.resources | indent 10 }}
        env:
        - name: SPM_TOKEN
          valueFrom:
            secretKeyRef:
              name: {{ template "sematext.fullname" . }}
              key: spm-token
        - name: LOGSENE_TOKEN
          valueFrom:
            secretKeyRef:
              name: {{ template "sematext.fullname" . }}
              key: logsene-token
        envFrom:
          - configMapRef:
              name: {{ template "sematext.fullname" . }}
        volumeMounts:
          - mountPath: /var/run/docker.sock
            name: docker-sock
          - mountPath: /etc/localtime
            name: localtime
            readOnly: true
        securityContext:
          privileged: true
      volumes:
        - name: docker-sock
          hostPath:
            path: /var/run/docker.sock
        - name: localtime
          hostPath:
            path: /etc/localtime
      nodeSelector:
{{ toYaml .Values.sematext.nodeSelector | indent 8 }}
      tolerations:
{{ toYaml .Values.sematext.tolerations | indent 8 }}
{{- end }}
 # secrets.yaml
apiVersion: v1
kind: Secret
metadata:
  name: {{ template "sematext.fullname" . }}
  labels:
    app: {{ template "sematext.name" . }}
    chart: {{ .Chart.Name }}-{{ .Chart.Version | replace "+" "_" }}
    release: {{ .Release.Name }}
type: Opaque
data:
  spm-token: {{ default "" .Values.sematext.spmToken | b64enc | quote }}
  logsene-token: {{ default "" .Values.sematext.logseneToken | b64enc | quote }}

image:
  repository: sematext/sematext-agent-docker
  # For all available tags check here https://hub.docker.com/r/sematext/sematext-agent-docker/tags/
  tag: 1.31.53
  pullPolicy: IfNotPresent

sematext:
  # You will need to Create and Set at least one token before starting this agent!

  # spmToken:
  # logseneToken:

  # Depending on where you created your apps, choose US or EU region.
  region: US

  useHostNetwork: false
  name: sematext-docker-agent

  # Support for custom URLs
  # url:
  #   spmReceiver: http://spm-receiver:8080
  #   logseneReceiver: http://logsene-receiver:8080
  #   eventsReceiver: http://events-receiver:8080

  # The full list of available options is here https://sematext.com/docs/sematext-docker-agent/configuration/
  config:
    # MATCH_BY_NAME: .*_default_
    # SPM_MATCH_BY_NAME: .*_default_

  resources: {}
  #  requests:
  #    cpu: 500m
  #    memory: 128Mi
  #  limits:
  #    cpu: 1000m
  #    memory: 1024Mi

  tolerations: []

  # Node labels for pod assignment
  # Ref: https://kubernetes.io/docs/user-guide/node-selection/
  nodeSelector: {}
