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

{{/*
Create the name of the service account
*/}}
{{- define "goldpinger.serviceAccountName" -}}
{{- if .Values.serviceAccount.create -}}
    {{ default (include "goldpinger.fullname" .) .Values.serviceAccount.name }}
{{- else -}}
    {{ default "default" .Values.serviceAccount.name }}
{{- end -}}
{{- end -}} # clusterrole.yaml
{{- if .Values.rbac.create }}
kind: ClusterRole
apiVersion: rbac.authorization.k8s.io/v1
metadata:
  name: {{ template "goldpinger.fullname" . }}-clusterrole
  labels:
    app.kubernetes.io/name: {{ include "goldpinger.name" . }}
    helm.sh/chart: {{ include "goldpinger.chart" . }}
    app.kubernetes.io/instance: {{ .Release.Name }}
    app.kubernetes.io/managed-by: {{ .Release.Service }}
rules:
- apiGroups: [""]
  resources: ["pods"]
  verbs: ["list"]
{{- end }} # clusterrolebinding.yaml
{{- if .Values.rbac.create }}
kind: ClusterRoleBinding
apiVersion: rbac.authorization.k8s.io/v1
metadata:
  name: {{ include "goldpinger.fullname" . }}-clusterrolebinding
  labels:
    app.kubernetes.io/name: {{ include "goldpinger.name" . }}
    helm.sh/chart: {{ include "goldpinger.chart" . }}
    app.kubernetes.io/instance: {{ .Release.Name }}
    app.kubernetes.io/managed-by: {{ .Release.Service }}
subjects:
  - kind: ServiceAccount
    name: {{ template "goldpinger.serviceAccountName" . }}
    namespace: {{ .Release.Namespace }}
roleRef:
  kind: ClusterRole
  name: {{ template "goldpinger.fullname" . }}-clusterrole
  apiGroup: rbac.authorization.k8s.io
{{- end }} # daemonset.yaml
apiVersion: apps/v1
kind: DaemonSet
metadata:
  name: {{ include "goldpinger.fullname" . }}
  labels:
    app.kubernetes.io/name: {{ include "goldpinger.name" . }}
    helm.sh/chart: {{ include "goldpinger.chart" . }}
    app.kubernetes.io/instance: {{ .Release.Name }}
    app.kubernetes.io/managed-by: {{ .Release.Service }}
spec:
  selector:
    matchLabels:
      app.kubernetes.io/name: {{ include "goldpinger.name" . }}
      app.kubernetes.io/instance: {{ .Release.Name }}
  template:
    metadata:
      labels:
        app.kubernetes.io/name: {{ include "goldpinger.name" . }}
        app.kubernetes.io/instance: {{ .Release.Name }}
    {{- with .Values.podAnnotations }}
      annotations:
    {{ toYaml . | nindent 8 }}
    {{- end }}
    spec:
      priorityClassName: {{ .Values.priorityClassName }}
      serviceAccountName: {{ template "goldpinger.serviceAccountName" . }}
      {{- if .Values.image.pullSecrets }}
      imagePullSecrets:
        {{- range .Values.image.pullSecrets }}
        - name: {{ . }}
        {{- end }}
      {{- end }}
      containers:
      - name: goldpinger-daemon
        image: "{{ .Values.image.repository }}:{{ .Values.image.tag }}"
        imagePullPolicy: {{ .Values.image.pullPolicy }}
        env:
          - name: HOSTNAME
            valueFrom:
              fieldRef:
                fieldPath: spec.nodeName
          - name: HOST
            value: "0.0.0.0"
          - name: PORT
            value: "{{ .Values.goldpinger.port }}"
          - name: LABEL_SELECTOR
            value: "app.kubernetes.io/name={{ include "goldpinger.name" . }}"
{{- if .Values.extraEnv }}
{{ toYaml .Values.extraEnv | indent 10 }}
{{- end }}
        ports:
        - name: http
          containerPort: {{ .Values.goldpinger.port }}
          protocol: TCP
        livenessProbe:
          httpGet:
            path: /
            port: http
        readinessProbe:
          httpGet:
            path: /
            port: http
        resources:
{{ toYaml .Values.resources | indent 12 }}
    {{- with .Values.nodeSelector }}
      nodeSelector:
{{ toYaml . | indent 8 }}
    {{- end }}
    {{- with .Values.affinity }}
      affinity:
{{ toYaml . | indent 8 }}
    {{- end }}
    {{- with .Values.tolerations }}
      tolerations:
{{ toYaml . | indent 8 }}
    {{- end }}
 # ingress.yaml
{{- if .Values.ingress.enabled -}}
{{- $fullName := include "goldpinger.fullname" . -}}
{{- $servicePort := .Values.service.port -}}
{{- $ingressPath := .Values.ingress.path -}}
apiVersion: extensions/v1beta1
kind: Ingress
metadata:
  name: {{ $fullName }}
  labels:
    app.kubernetes.io/name: {{ include "goldpinger.name" . }}
    helm.sh/chart: {{ include "goldpinger.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 }}
        - {{ . }}
      {{- end }}
      secretName: {{ .secretName }}
  {{- end }}
{{- end }}
  rules:
  {{- range .Values.ingress.hosts }}
    - host: {{ . }}
      http:
        paths:
          - path: {{ $ingressPath }}
            backend:
              serviceName: {{ $fullName }}
              servicePort: http
  {{- end }}
{{- end }} # podsecuritypolicy.yaml
{{- if .Values.podSecurityPolicy.enabled }}
---
kind: Role
apiVersion: rbac.authorization.k8s.io/v1
metadata:
  name: {{ template "goldpinger.fullname" . }}-pod-security-policy
  labels:
    app.kubernetes.io/name: {{ include "goldpinger.name" . }}
    helm.sh/chart: {{ include "goldpinger.chart" . }}
    app.kubernetes.io/instance: {{ .Release.Name }}
    app.kubernetes.io/managed-by: {{ .Release.Service }}
rules:
- apiGroups: ["extensions"]
  resources: ["podsecuritypolicies"]
  resourceNames: [{{ .Values.podSecurityPolicy.policyName | quote }}]
  verbs: ["use"]
---
kind: RoleBinding
apiVersion: rbac.authorization.k8s.io/v1
metadata:
  name: {{ template "goldpinger.fullname" . }}-pod-security-polic
  labels:
    app.kubernetes.io/name: {{ include "goldpinger.name" . }}
    helm.sh/chart: {{ include "goldpinger.chart" . }}
    app.kubernetes.io/instance: {{ .Release.Name }}
    app.kubernetes.io/managed-by: {{ .Release.Service }}
roleRef:
  kind: Role
  name: {{ template "goldpinger.fullname" . }}-pod-security-policy
  apiGroup: rbac.authorization.k8s.io
subjects:
- kind: ServiceAccount
  name: {{ template "goldpinger.serviceAccountName" . }}
  namespace: {{ .Release.Namespace }}
{{- end }}
 # service.yaml
apiVersion: v1
kind: Service
metadata:
  name: {{ include "goldpinger.fullname" . }}
  labels:
    app.kubernetes.io/name: {{ include "goldpinger.name" . }}
    helm.sh/chart: {{ include "goldpinger.chart" . }}
    app.kubernetes.io/instance: {{ .Release.Name }}
    app.kubernetes.io/managed-by: {{ .Release.Service }}
{{- with .Values.service.annotations }}
  annotations:
{{ toYaml . | indent 4 }}
{{- end }}
spec:
  type: {{ .Values.service.type }}
  ports:
    - port: {{ .Values.service.port }}
      targetPort: {{ .Values.goldpinger.port }}
      protocol: TCP
      name: http
  selector:
    app.kubernetes.io/name: {{ include "goldpinger.name" . }}
    app.kubernetes.io/instance: {{ .Release.Name }} # serviceaccount.yaml
{{- if .Values.serviceAccount.create }}
apiVersion: v1
kind: ServiceAccount
metadata:
  labels:
    app.kubernetes.io/name: {{ include "goldpinger.name" . }}
    helm.sh/chart: {{ include "goldpinger.chart" . }}
    app.kubernetes.io/instance: {{ .Release.Name }}
    app.kubernetes.io/managed-by: {{ .Release.Service }}
  name: {{ template "goldpinger.serviceAccountName" . }}
{{- end }} # servicemonitor.yaml
{{- if .Values.serviceMonitor.enabled }}
apiVersion: monitoring.coreos.com/v1
kind: ServiceMonitor
metadata:
  name: {{ template "goldpinger.fullname" . }}
  {{- if .Values.serviceMonitor.namespace }}
  namespace: {{ .Values.serviceMonitor.namespace }}
  {{- end }}
  labels:
    app.kubernetes.io/name: {{ include "goldpinger.name" . }}
    helm.sh/chart: {{ include "goldpinger.chart" . }}
    app.kubernetes.io/instance: {{ .Release.Name }}
    app.kubernetes.io/managed-by: {{ .Release.Service }}
    {{- range $key, $value := .Values.serviceMonitor.selector }}
    {{ $key }}: {{ $value | quote }}
    {{- end }}
spec:
  endpoints:
    - port: http
      interval: {{ .Values.serviceMonitor.interval }}
      {{- if .Values.serviceMonitor.honorLabels }}
      honorLabels: true
      {{- end }}
  jobLabel: name
  namespaceSelector:
    matchNames:
      - {{ .Release.Namespace }}
  selector:
    matchLabels:
      app.kubernetes.io/name: {{ template "goldpinger.name" . }}
      app.kubernetes.io/instance: {{ .Release.Name }}
{{- end -}}
# Default values for goldpinger.
# This is a YAML-formatted file.
# Declare variables to be passed into your templates.
image:
  repository: bloomberg/goldpinger
  tag: 2.0.0
  pullPolicy: IfNotPresent
  ## Optionally specify an array of imagePullSecrets.
  ## Secrets must be manually created in the namespace.
  ## ref: https://kubernetes.io/docs/tasks/configure-pod-container/pull-image-private-registry/
  ##
  # pullSecrets:
  #   - myRegistrKeySecretName

rbac:
  create: true
serviceAccount:
  create: true
  name:

goldpinger:
  port: 80

extraEnv: []

service:
  type: LoadBalancer
  port: 80
  annotations: {}

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

## Set a priorityClassName for the pod. If left blank a default priority will be set.
priorityClassName:

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

podAnnotations: {}

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

## Tolerations for pod assignment
## Ref: https://kubernetes.io/docs/concepts/configuration/taint-and-toleration/
##
tolerations: []

## Affinity for pod assignment
## Ref: https://kubernetes.io/docs/concepts/configuration/assign-pod-node/#affinity-and-anti-affinity
##
affinity: {}

## Enable this if pod security policy enabled in your cluster
## It will bind ServiceAccount with unrestricted podSecurityPolicy
## Ref: https://kubernetes.io/docs/concepts/policy/pod-security-policy/
podSecurityPolicy:
  enabled: false
  policyName: unrestricted-psp

serviceMonitor:
  enabled: false
  selector:
    prometheus: "kube-prometheus"
  namespace: "monitoring"
  interval: 30s
  # honorLabels: true
