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

{{- define "labels.standard" -}}
app.kubernetes.io/name: {{ include "chaoskube.name" . }}
app.kubernetes.io/managed-by: {{ .Release.Service | quote }}
app.kubernetes.io/instance: {{ .Release.Name | quote }}
helm.sh/chart: {{ include "chaoskube.chart" . }}
{{- end -}}
 # clusterrole.yaml
{{- if .Values.rbac.create -}}
apiVersion: rbac.authorization.k8s.io/v1
kind: ClusterRole
metadata:
  labels:
{{ include "labels.standard" . | indent 4 }}
  name: {{ printf "%s-%s" .Release.Name .Values.name }}
rules:
  - apiGroups: [""]
    resources: ["pods"]
    verbs: ["list", "delete"]
  - apiGroups: [""]
    resources: ["events"]
    verbs: ["create"]
{{- end -}}
 # clusterrolebinding.yaml
{{- if .Values.rbac.create -}}
apiVersion: rbac.authorization.k8s.io/v1
kind: ClusterRoleBinding
metadata:
  labels:
{{ include "labels.standard" . | indent 4 }}
  name: {{ printf "%s-%s" .Release.Name .Values.name }}
roleRef:
  apiGroup: rbac.authorization.k8s.io
  kind: ClusterRole
  name: {{ printf "%s-%s" .Release.Name .Values.name }}
subjects:
  - kind: ServiceAccount
    name: {{ printf "%s-%s" .Release.Name .Values.name }}
    namespace: {{ .Release.Namespace }}
{{- end -}}
 # deployment.yaml
apiVersion: apps/v1
kind: Deployment
metadata:
  name: {{ include "chaoskube.fullname" . }}
  labels:
{{ include "labels.standard" . | indent 4 }}
spec:
  replicas: {{ .Values.replicas }}
  selector:
    matchLabels:
      app.kubernetes.io/name: {{ include "chaoskube.name" . }}
      app.kubernetes.io/instance: {{ .Release.Name }}
  template:
    metadata:
      labels:
{{ include "labels.standard" . | indent 8 }}
    {{- if .Values.podLabels }}
{{ toYaml .Values.podLabels | indent 8 }}
    {{- end }}
    {{- if .Values.podAnnotations }}
      annotations:
{{ toYaml .Values.podAnnotations | indent 8 }}
    {{- end }}
    spec:
      containers:
        - name: {{ .Values.name }}
          image: {{ .Values.image }}:{{ .Values.imageTag }}
          args:
            - --interval={{ .Values.interval }}
            - --labels={{ .Values.labels }}
            {{- if .Values.kinds }}
            - --kinds={{ .Values.kinds }}
            {{- end }}
            - --annotations={{ .Values.annotations }}
            - --namespaces={{ .Values.namespaces }}
            {{- if not .Values.dryRun }}
            - --no-dry-run
            {{- end }}
            {{- if .Values.includedPodNames }}
            - --included-pod-names={{ .Values.includedPodNames }}
            {{- end }}
            {{- if .Values.excludedPodNames }}
            - --excluded-pod-names={{ .Values.excludedPodNames }}
            {{- end }}
            - --excluded-weekdays={{ .Values.excludedWeekdays }}
            - --excluded-times-of-day={{ .Values.excludedTimesOfDay }}
            - --excluded-days-of-year={{ .Values.excludedDaysOfYear }}
            - --timezone={{ .Values.timezone }}
            {{- if .Values.logFormat }}
            - --log-format={{ .Values.logFormat }}
            {{- end }}
            {{- if .Values.debug }}
            - --debug
            {{- end }}
            - --minimum-age={{ .Values.minimumAge }}
            - --grace-period={{ .Values.gracePeriod }}
          {{- if .Values.metrics.enabled }}
            - --metrics-address=:{{ .Values.metrics.port }}
          {{- else }}
            - --metrics-address=
          {{- end }}
          {{- if .Values.metrics.enabled }}
          ports:
            - name: metrics
              containerPort: {{ .Values.metrics.port }}
          {{- end }}
          resources:
{{ toYaml .Values.resources | indent 12 }}
          securityContext:
            runAsNonRoot: true
            runAsUser: 65534
            readOnlyRootFilesystem: true
            capabilities:
              drop: ["ALL"]
{{- if .Values.priorityClassName }}
      priorityClassName: "{{ .Values.priorityClassName }}"
{{- end }}
      serviceAccountName: {{ if .Values.rbac.create }}{{ printf "%s-%s" .Release.Name .Values.name }}{{ else }}"{{ .Values.rbac.serviceAccountName }}"{{ end }}
{{- if .Values.imagePullSecrets }}
      imagePullSecrets:
{{ toYaml .Values.imagePullSecrets | indent 8 }}
{{- 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 }}
 # role.yaml
{{- if .Values.rbac.create -}}
apiVersion: rbac.authorization.k8s.io/v1
kind: Role
metadata:
  labels:
{{ include "labels.standard" . | indent 4 }}
  name: {{ printf "%s-%s" .Release.Name .Values.name }}
rules:
  - apiGroups: [""]
    resources: ["pods"]
    verbs: ["list", "delete"]
  - apiGroups: [""]
    resources: ["events"]
    verbs: ["create"]
{{- end -}}
 # rolebinding.yaml
{{- if .Values.rbac.create -}}
apiVersion: rbac.authorization.k8s.io/v1
kind: RoleBinding
metadata:
  labels:
{{ include "labels.standard" . | indent 4 }}
  name: {{ printf "%s-%s" .Release.Name .Values.name }}
roleRef:
  apiGroup: rbac.authorization.k8s.io
  kind: Role
  name: {{ printf "%s-%s" .Release.Name .Values.name }}
subjects:
  - kind: ServiceAccount
    name: {{ printf "%s-%s" .Release.Name .Values.name }}
    namespace: {{ .Release.Namespace }}
{{- end -}}
 # service.yaml
{{- if .Values.metrics.enabled }}
apiVersion: v1
kind: Service
metadata:
  name: {{ include "chaoskube.fullname" . }}
  labels:
{{ include "labels.standard" . | indent 4 }}
spec:
  type: {{ .Values.metrics.service.type }}
  ports:
    - port: {{ .Values.metrics.service.port }}
      targetPort: metrics
      protocol: TCP
      name: metrics
  selector:
    app.kubernetes.io/name: {{ include "chaoskube.name" . }}
    app.kubernetes.io/instance: {{ .Release.Name }}
{{- end }}
 # serviceaccount.yaml
{{- if .Values.rbac.create -}}
apiVersion: v1
kind: ServiceAccount
metadata:
  labels:
{{ include "labels.standard" . | indent 4 }}
  name: {{ printf "%s-%s" .Release.Name .Values.name }}
{{- end -}}
 # servicemonitor.yaml
{{- if and .Values.metrics.enabled .Values.metrics.serviceMonitor.enabled }}
apiVersion: monitoring.coreos.com/v1
kind: ServiceMonitor
metadata:
  name: {{ include "chaoskube.fullname" . }}
  labels:
{{ include "labels.standard" . | indent 4 }}
    {{- if .Values.metrics.serviceMonitor.additionalLabels }}
{{ toYaml .Values.metrics.serviceMonitor.additionalLabels | indent 4 }}
    {{- end }}
spec:
  endpoints:
    - port: metrics
      interval: 30s
  namespaceSelector:
    matchNames:
      - {{ .Release.Namespace }}
  selector:
    matchLabels:
      app.kubernetes.io/name: {{ include "chaoskube.name" . }}
      app.kubernetes.io/instance: {{ .Release.Name }}
{{- end }}

# container name
name: chaoskube

# docker image
image: quay.io/linki/chaoskube

# docker image tag
imageTag: v0.21.0

## secrets for pulling images from a private docker registry.
# imagePullSecrets:
#   - name: secret_name

# number of replicas to run
replicas: 1

# interval between pod terminations
interval: 10m

# label selector to filter pods by, e.g. app=foo,stage!=prod
labels:

# kinds selector to filter pods by, e.g. '!DaemonSet' (use quotes)
kinds:

# annotation selector to filter pods by, e.g. chaos.alpha.kubernetes.io/enabled=true
annotations:

# namespace selector to filter pods by, e.g. '!kube-system,!production' (use quotes)
namespaces:

# don't kill pods, only log what would have been done
dryRun: true

# Enable debug logging
debug: false

# regular expression pattern for pod names to include (all included by default)
includedPodNames:

# regular expression pattern for pod names to exclude (none excluded by default)
excludedPodNames:

# Set values for exempting specific week days from Chaoskube Actions
excludedWeekdays:

# Set values for exempting specific times of day from Chaoskube Actions
excludedTimesOfDay:

# Set values for exempting specific days of the year from Chaoskube Actions (Dec24,Jan1)
excludedDaysOfYear:

# Set specific Timezone for Actions to take place
timezone: UTC

# If nothing set, defaults to "text". Switch to "json" to enable structured logging
logFormat:

# minimum lifetime of a pod before it's considered for termination (0: immediately)
minimumAge: 0s

# grace period to give pods when terminating them (negative: pod decides)
gracePeriod: -1s

metrics:
  # Enable metrics handler
  enabled: false

  # Listening port for metrics handler
  port: 8080

  service:
    # Metrics service type
    type: ClusterIP

    # Metrics service port
    port: 8080

  serviceMonitor:
    # Set this to `true` to create ServiceMonitor for Prometheus operator
    enabled: false

    # Additional labels that can be used so ServiceMonitor will be discovered by Prometheus
    additionalLabels: {}

priorityClassName: ""

# create service account with permission to list and kill pods
rbac:
  create: false

  # only used when create is false
  serviceAccountName: default

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: {}
  ## Suggested antiAffinity, as each CA should be on a separate Node for resilience
  # podAntiAffinity:
  #   requiredDuringSchedulingIgnoredDuringExecution:
  #     - topologyKey: "kubernetes.io/hostname"
  #       labelSelector:
  #         matchLabels:
  #           app.kubernetes.io/name: chaoskube

podAnnotations: {}
  ## Annotations for the chaoskube pod.
  #  podAnnotations:
  #    prometheus.io/scrape: "true"
  #    prometheus.io/port: "8080"

podLabels: {}
  ## Labels for the chaoskube pod.
  #  podLabels:
  #    my.first.label: "value"
  #    my.other.label: "other-value"
