# _helpers.tpl
{{/* vim: set filetype=mustache: */}}
{{/*
Expand the name of the chart.
*/}}
{{- define "karma.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 "karma.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 "karma.chart" -}}
{{- printf "%s-%s" .Chart.Name .Chart.Version | replace "+" "_" | trunc 63 | trimSuffix "-" -}}
{{- end -}}

{{/*
Create the name of the service account to use
*/}}
{{- define "karma.serviceAccountName" -}}
{{- if .Values.serviceAccount.create -}}
    {{ default (include "karma.fullname" .) .Values.serviceAccount.name }}
{{- else -}}
    {{ default "default" .Values.serviceAccount.name }}
{{- end -}}
{{- end -}}
 # configmap.yaml
{{ if .Values.configMap.enabled }}
apiVersion: v1
kind: ConfigMap
metadata:
  labels:
    app: "{{ template "karma.fullname" . }}"
    chart: "{{ .Chart.Name }}-{{ .Chart.Version }}"
    heritage: "{{ .Release.Service }}"
    release: "{{ .Release.Name }}"
{{- with .Values.configMap.annotations }}
  annotations:
{{ toYaml . | indent 4 }}
{{- end }}
  name: "{{ .Release.Name }}-config"
data:
  karma.conf: |
{{ toYaml .Values.configMap.rawConfig | indent 4 }}
{{- end }}
 # deployment.yaml
apiVersion: apps/v1
kind: Deployment
metadata:
  name: {{ include "karma.fullname" . }}
  labels:
    app.kubernetes.io/name: {{ include "karma.name" . }}
    helm.sh/chart: {{ include "karma.chart" . }}
    app.kubernetes.io/instance: {{ .Release.Name }}
    app.kubernetes.io/managed-by: {{ .Release.Service }}
{{- with .Values.deployment.annotations }}
  annotations:
{{ toYaml . | indent 4 }}
{{- end }}
spec:
  replicas: {{ .Values.replicaCount }}
  selector:
    matchLabels:
      app.kubernetes.io/name: {{ include "karma.name" . }}
      app.kubernetes.io/instance: {{ .Release.Name }}
  template:
    metadata:
      labels:
        app.kubernetes.io/name: {{ include "karma.name" . }}
        app.kubernetes.io/instance: {{ .Release.Name }}
        {{- if .Values.podLabels -}}
        {{ toYaml .Values.podLabels | nindent 8 }}
        {{- end }}
      {{- if or .Values.configMap.enabled .Values.podAnnotations }}
      annotations:
        {{- if .Values.configMap.enabled }}
        checksum/config: {{ include (print $.Template.BasePath "/configmap.yaml") . | sha256sum }}
        {{- end }}
        {{- if .Values.podAnnotations -}}
        {{ toYaml .Values.podAnnotations | nindent 8 }}
        {{- end }}
      {{- end }}
    spec:
      serviceAccountName: {{ template "karma.serviceAccountName" . }}
      {{- if .Values.securityContext }}
      securityContext:
{{ toYaml .Values.securityContext | indent 8 }}
      {{- end }}
      containers:
        - name: {{ .Chart.Name }}
          image: "{{ .Values.image.repository }}:{{ .Values.image.tag }}"
          imagePullPolicy: {{ .Values.image.pullPolicy }}
          ports:
            - name: http
              containerPort: 8080
              protocol: TCP
          env:
          {{- if .Values.configMap.enabled }}
          - name: CONFIG_FILE
            value: /etc/karma/karma.conf
          {{- end }}
          {{- with .Values.env }}
{{ toYaml . | indent 10 }}
          {{- end }}
          livenessProbe:
            httpGet:
              path: {{ .Values.livenessProbe.path }}
              port: http
            initialDelaySeconds: {{ .Values.livenessProbe.delay }}
            periodSeconds: {{ .Values.livenessProbe.period }}
          readinessProbe:
            httpGet:
              path: {{ .Values.livenessProbe.path }}
              port: http
            initialDelaySeconds: {{ .Values.livenessProbe.delay }}
            periodSeconds: {{ .Values.livenessProbe.period }}
          resources:
{{ toYaml .Values.resources | indent 12 }}
          volumeMounts:
          {{- if .Values.configMap.enabled }}
            - name: karma-config
              mountPath: /etc/karma
          {{- end }}
          {{- range .Values.certSecretNames }}
            - name: {{ . }}
              mountPath: /etc/certs/{{ . }}
              readOnly: true
          {{- end }}
    {{- with .Values.nodeSelector }}
      nodeSelector:
{{ toYaml . | indent 8 }}
    {{- end }}
    {{- with .Values.affinity }}
      affinity:
{{ toYaml . | indent 8 }}
    {{- end }}
    {{- with .Values.tolerations }}
      tolerations:
{{ toYaml . | indent 8 }}
    {{- end }}
      volumes:
      {{- if .Values.configMap.enabled }}
      - name: karma-config
        configMap:
          name: {{ .Release.Name }}-config
      {{- end }}
      {{- range .Values.certSecretNames }}
      - name: {{ . }}
        secret:
          defaultMode: 420
          secretName: {{ . }}
      {{- end }}
 # ingress.yaml
{{- if .Values.ingress.enabled -}}
{{- $fullName := include "karma.fullname" . -}}
{{- $ingressPath := .Values.ingress.path -}}
apiVersion: extensions/v1beta1
kind: Ingress
metadata:
  name: {{ $fullName }}
  labels:
    app.kubernetes.io/name: {{ include "karma.name" . }}
    helm.sh/chart: {{ include "karma.chart" . }}
    app.kubernetes.io/instance: {{ .Release.Name }}
    app.kubernetes.io/managed-by: {{ .Release.Service }}
{{- with .Values.ingress.annotations }}
  annotations:
{{ toYaml . | indent 4 }}
{{- end }}
spec:
{{- if .Values.ingress.tls }}
  tls:
  {{- range .Values.ingress.tls }}
    - hosts:
      {{- range .hosts }}
        - {{ . | quote }}
      {{- end }}
      secretName: {{ .secretName }}
  {{- end }}
{{- end }}
  rules:
  {{- range .Values.ingress.hosts }}
    - host: {{ . | quote }}
      http:
        paths:
          - path: {{ $ingressPath }}
            backend:
              serviceName: {{ $fullName }}
              servicePort: http
  {{- end }}
{{- end }}
 # service.yaml
apiVersion: v1
kind: Service
metadata:
  name: {{ include "karma.fullname" . }}
  labels:
    app.kubernetes.io/name: {{ include "karma.name" . }}
    helm.sh/chart: {{ include "karma.chart" . }}
    app.kubernetes.io/instance: {{ .Release.Name }}
    app.kubernetes.io/managed-by: {{ .Release.Service }}
{{- if .Values.service.labels }}
{{ toYaml .Values.service.labels | indent 4 }}
{{- end }}
  annotations:
{{ toYaml .Values.service.annotations | indent 4 }}
spec:
  type: {{ .Values.service.type }}
  ports:
    - port: {{ .Values.service.port }}
      targetPort: http
      protocol: TCP
      name: http
  selector:
    app.kubernetes.io/name: {{ include "karma.name" . }}
    app.kubernetes.io/instance: {{ .Release.Name }}
 # serviceaccount.yaml
{{- if .Values.serviceAccount.create }}
apiVersion: v1
kind: ServiceAccount
metadata:
  labels:
    app.kubernetes.io/name: {{ include "karma.name" . }}
    helm.sh/chart: {{ include "karma.chart" . }}
    app.kubernetes.io/instance: {{ .Release.Name }}
    app.kubernetes.io/managed-by: {{ .Release.Service }}
  name: {{ template "karma.serviceAccountName" . }}
{{- end }}

# Default values for karma.
# This is a YAML-formatted file.
# Declare variables to be passed into your templates.

replicaCount: 1

image:
  repository: lmierzwa/karma
  tag: v0.72
  pullPolicy: IfNotPresent

nameOverride: ""
fullnameOverride: ""

# env:
# - name: ALERTMANAGER_URI
#   value: http://monitoring-prometheus-alertmanager

service:
  type: ClusterIP
  port: 80
  annotations: {}
    # prometheus.io/scrape: "true"
  labels: {}

serviceAccount:
  # Specifies whether a ServiceAccount should be created
  create: true
  # The name of the ServiceAccount to use.
  # If not set and create is true, a name is generated using the fullname template
  name:

ingress:
  enabled: false
  annotations: {}
    # kubernetes.io/ingress.class: nginx
    # kubernetes.io/tls-acme: "true"
  path: /
  hosts:
    - chart-example.local
  tls: []
  #  - secretName: chart-example-tls
  #    hosts:
  #      - chart-example.local

deployment:
  annotations: {}

resources: {}
  # We usually recommend not to specify default resources and to leave this as a conscious
  # choice for the user. This also increases chances charts run on environments with little
  # resources, such as Minikube. If you do want to specify resources, uncomment the following
  # lines, adjust them as necessary, and remove the curly braces after 'resources:'.
  # limits:
  #  cpu: 100m
  #  memory: 128Mi
  # requests:
  #  cpu: 100m
  #  memory: 128Mi

nodeSelector: {}

tolerations: []

affinity: {}

# Labels to be added to pod
podLabels: {}

# Annotations to be added to pod
podAnnotations: {}

securityContext: {}

# configuration for liveness probe
livenessProbe:
  delay: 5
  period: 5
  path: /health

# configMap dictates if a configmap based configuration for Karma should be used
# to provide advanced configuration. NOTE, you must use port 8080!
configMap:
  enabled: false
  annotations: {}
  # rawConfig:
  #   alertmanager:
  #   interval: 30s
  #   servers:
  #     - name: local
  #       uri: http://localhost:9093
  #       timeout: 10s
  #       proxy: true
  #       headers:
  #         x-auth-token: some-token
  #         any-header: string-value
  #     - name: client-auth
  #       uri: https://localhost:9093
  #       timeout: 10s
  #       tls:
  #         ca: /etc/ssl/certs/ca-bundle.crt
  #         cert: /etc/karma/client.pem
  #         key: /etc/karma/client.key
  #   annotations:
  #     default:
  #       hidden: false
  #     hidden:
  #       - help
  #     visible: []
  #   filters:
  #     default:
  #       - "@receiver=by-cluster-service"
  #   labels:
  #     color:
  #       static:
  #         - job
  #       unique:
  #         - cluster
  #         - instance
  #         - "@receiver"
  #     keep: []
  #     strip: []
  #   listen:
  #     address: "0.0.0.0"
  #     port: 8080
  #     prefix: /
  #   log:
  #     config: false
  #     level: info
  #   jira:
  #     - regex: DEVOPS-[0-9]+
  #       uri: https://jira.example.com
  #   receivers:
  #     keep: []
  #     strip: []
  #   sentry:
  #     private: secret
  #   public: 123456789

# Names of secrets that will be mounted to `/etc/certs/<secret_name>` in the karma container.
# These secrets can be used to configure TLS for Karma's Alertmanagers by passing their paths to
# the appropriate Karma configuration parameters. For example, secret `foo` containing a `ca.crt`
# will yield a file at `/etc/certs/foo/ca.crt`. That path can then be provided as the value to
# `configMap.rawConfig.alertmanager.servers[].tls.ca`.
# See <https://github.com/prymitive/karma/blob/master/docs/CONFIGURATION.md#alertmanagers>.
certSecretNames: []
