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

{{/*
Create the name of the service account to use
*/}}
{{- define "kube-slack.serviceAccountName" -}}
{{- if .Values.serviceAccount.create -}}
    {{ default (include "kube-slack.fullname" .) .Values.serviceAccount.name }}
{{- else -}}
    {{ default "default" .Values.serviceAccount.name }}
{{- end -}}
{{- end -}}

{{/*
Return the appropriate apiVersion for RBAC APIs.
*/}}
{{- define "kube-slack.rbac.apiVersion" -}}
{{- if semverCompare ">= 1.8-0" .Capabilities.KubeVersion.GitVersion -}}
"rbac.authorization.k8s.io/v1"
{{- else -}}
"rbac.authorization.k8s.io/v1beta1"
{{- end -}}
{{- end -}}

{{/*
Return the appropriate apiVersion for deployment.
*/}}
{{- define "kube-slack.deployment.apiVersion" -}}
{{- if semverCompare "<1.9-0" .Capabilities.KubeVersion.GitVersion -}}
{{- print "extensions/v1beta1" -}}
{{- else if semverCompare "^1.9-0" .Capabilities.KubeVersion.GitVersion -}}
{{- print "apps/v1" -}}
{{- end -}}
{{- end -}}
 # clusterrole.yaml
{{- if .Values.rbac.create -}}
apiVersion: {{ template "kube-slack.rbac.apiVersion" . }}
kind: ClusterRole
metadata:
  name: {{ template "kube-slack.fullname" . }}
  labels:
    app: {{ template "kube-slack.name" . }}
    chart: {{ template "kube-slack.chart" . }}
    release: {{ .Release.Name }}
    heritage: {{ .Release.Service }}
rules:
  - apiGroups:
      - ""
    resources:
      - pods
    verbs:
      - get
      - list
      - watch
{{- end -}}
 # clusterrolebinding.yaml
{{- if .Values.rbac.create -}}
apiVersion: {{ template "kube-slack.rbac.apiVersion" . }}
kind: ClusterRoleBinding
metadata:
  name: {{ template "kube-slack.fullname" . }}
  labels:
    app: {{ template "kube-slack.name" . }}
    chart: {{ template "kube-slack.chart" . }}
    release: {{ .Release.Name }}
    heritage: {{ .Release.Service }}
roleRef:
  apiGroup: rbac.authorization.k8s.io
  kind: ClusterRole
  name: {{ template "kube-slack.fullname" . }}
subjects:
  - kind: ServiceAccount
    name: {{ template "kube-slack.serviceAccountName" . }}
    namespace: {{ .Release.Namespace }}
{{- end -}}
 # configmap.yaml
apiVersion: v1
kind: ConfigMap
metadata:
  name: {{ template "kube-slack.fullname" . }}
  labels:
    app: {{ template "kube-slack.name" . }}
    chart: {{ template "kube-slack.chart" . }}
    release: {{ .Release.Name }}
    heritage: {{ .Release.Service }}
data:
{{ toYaml .Values.envVars | indent 2 }}
 # deployment.yaml
apiVersion: {{ template "kube-slack.deployment.apiVersion" . }}
kind: Deployment
metadata:
  name: {{ template "kube-slack.fullname" . }}
  labels:
    app: {{ template "kube-slack.name" . }}
    chart: {{ template "kube-slack.chart" . }}
    release: {{ .Release.Name }}
    heritage: {{ .Release.Service }}
spec:
  replicas: {{ .Values.replicaCount }}
  selector:
    matchLabels:
      app: {{ template "kube-slack.name" . }}
      release: {{ .Release.Name }}
  template:
    metadata:
      annotations:
        checksum/config: {{ include (print $.Template.BasePath "/configmap.yaml") . | sha256sum }}
      labels:
        app: {{ template "kube-slack.name" . }}
        release: {{ .Release.Name }}
      {{- if .Values.annotations }}
      annotations:
{{ toYaml .Values.annotations | indent 4 }}
      {{- end }}
    spec:
      containers:
        - name: {{ .Chart.Name }}
          image: "{{ .Values.image.repository }}:{{ .Values.image.tag }}"
          imagePullPolicy: {{ .Values.image.pullPolicy }}
          env:
          {{- range $key, $value := .Values.envVarsFromSecret }}
          - name: {{ $key }}
            valueFrom:
              secretKeyRef:
                name: {{ $value.secretKeyRef | quote }}
                key: {{ $value.key | quote }}
          {{- end }}
          envFrom:
            - configMapRef:
                name: {{ template "kube-slack.fullname" . }}
          resources:
      {{ if .Values.image.pullSecret }}
      imagePullSecrets:
        - name: {{ .Values.image.pullSecret }}
      {{- end }}
{{ toYaml .Values.resources | indent 12 }}
    {{- if .Values.rbac.create }}
      serviceAccountName: {{ template "kube-slack.fullname" . }}
    {{- else }}
      serviceAccountName: "{{ .Values.rbac.serviceAccountName }}"
    {{- 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 }}
 # serviceaccount.yaml
{{- if .Values.serviceAccount.create -}}
apiVersion: v1
kind: ServiceAccount
metadata:
  name: {{ template "kube-slack.serviceAccountName" . }}
  labels:
    app: {{ template "kube-slack.name" . }}
    chart: {{ template "kube-slack.chart" . }}
    release: {{ .Release.Name }}
    heritage: {{ .Release.Service }}
{{- end -}}

## Environment values for the deployment
envVars:
  ## URL of your Incoming Webhook in Slack
  ## e.g. https://hooks.slack.com/services/Txxxxxxxx/Bxxxxxxxx/XXXXXXXXXXXXXXXXXXXXXXXX
  SLACK_URL: ""
  ## Override channel to send
  SLACK_USERNAME: ""
  ## Override username to send
  SLACK_CHANNEL: ""
  ## URL of HTTP proxy used to connect to Slack
  SLACK_PROXY: ""
  ## Time to wait after pod becomes not ready before notifying
  NOT_READY_MIN_TIME: "60000"  # in ms
  ## Set to false to disable alert on pod recovery
  RECOVERY_ALERT: "true"
  ## How often to update in milliseconds
  TICK_RATE: "15000"
  ## Repeat notification after this many milliseconds has passed after status returned to normal
  FLOOD_EXPIRE: "60000"
  ## Enable/disable metric alerting on cpu
  METRICS_CPU: "true"
  ## Enable/disable metric alerting on memory
  METRICS_MEMORY: "true"
  ## Set percentage threshold on metric alerts
  METRICS_PERCENT: "80"
  ##  If no metrics limit defined, alert if the pod utilization is more than the resource request amount
  METRICS_REQUESTS: "false"

## Alternative to specify environment values for the deployment via secrets.
## See envVars above for some examples
# envVarsFromSecret:
#  SLACK_URL:
#    secretKeyRef: kube-slack
#    key: SLACK_URL

## Configuration for the deployment:
image:
  repository: willwill/kube-slack
  tag: v4.2.0
  pullPolicy: IfNotPresent
  # If specified, use these secrets to access the image
  # pullSecret: registry-secret

resources: {}
  # limits:
  #  cpu: 100m
  #  memory: 128Mi
  # requests:
  #  cpu: 100m
  #  memory: 128Mi

rbac:
  # Specifies whether RBAC resources should be created
  create: true

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:

nodeSelector: {}

tolerations: []

affinity: {}

annotations: {}
  # Specify which annotations must be added to kube-slack pods
  # fluentbit.io/exclude: true
