# _helpers.tpl
{{/* vim: set filetype=mustache: */}}
{{/*
Expand the name of the chart.
*/}}
{{- define "kafka-manager.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 "kafka-manager.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 a default fully qualified bootstrap job name.
We truncate at 63 chars because some Kubernetes name fields are limited to this (by the DNS naming spec).
*/}}
{{- define "kafka-manager.bootstrap.fullname" -}}
{{- $name := default .Chart.Name .Values.nameOverride -}}
{{- printf "%s-%s-bootstrap" .Release.Name $name | trunc 63 | trimSuffix "-" -}}
{{- end -}}

{{/*
Create a default fully qualified zookeeper name.
We truncate at 63 chars because some Kubernetes name fields are limited to this (by the DNS naming spec).
*/}}
{{- define "kafka-manager.zookeeper.fullname" -}}
{{- $name := default "zookeeper" .Values.zookeeper.nameOverride -}}
{{- printf "%s-%s" .Release.Name $name | trunc 63 | trimSuffix "-" -}}
{{- end -}}

{{/*
Create chart name and version as used by the chart label.
*/}}
{{- define "kafka-manager.chart" -}}
{{- printf "%s-%s" .Chart.Name .Chart.Version | replace "+" "_" | trunc 63 | trimSuffix "-" -}}
{{- end -}}

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

{{/*
Create the kafka-manager zookeeper hosts url.
*/}}
{{- define "kafka-manager.zkHosts" -}}
{{- if .Values.zookeeper.enabled -}}
    {{- printf "%s:2181" (include "kafka-manager.zookeeper.fullname" .) }}
{{- else -}}
    {{- default "localhost:2181" (tpl .Values.zkHosts .) -}}
{{- end -}}
{{- end -}}
 # configmap.yaml
{{- if .Values.clusters -}}
apiVersion: v1
kind: ConfigMap
metadata:
  name: {{ template "kafka-manager.bootstrap.fullname" . }}
  labels:
    app.kubernetes.io/instance: {{ .Release.Name }}
    app.kubernetes.io/managed-by: {{ .Release.Service }}
    app.kubernetes.io/name: {{ include "kafka-manager.name" . }}
    app.kubernetes.io/version: {{ .Chart.AppVersion | replace "+" "_" | trunc 63 }}
    helm.sh/chart: {{ include "kafka-manager.chart" . }}
data:
  addClusters.sh: |
    #!/bin/bash
    set -e
    {{- range $cluster := .Values.clusters }}
    {{ printf "curl http://%s:%v/clusters -X POST -f " (include "kafka-manager.fullname" $) $.Values.service.port -}}
    {{- printf "-d name=%v " (default "default" $cluster.name) -}}
    {{- printf "-d zkHosts=%v " (default (include "kafka-manager.zkHosts" $) $cluster.zkHosts) -}}
    {{- printf "-d kafkaVersion=%v " (default "2.0.0" $cluster.kafkaVersion) -}}
    {{- printf "-d jmxEnabled=%v " (default "false" $cluster.jmxEnabled) -}}
    {{- printf "-d jmxUser=%v " (default "" $cluster.jmxUser) -}}
    {{- printf "-d jmxPass=%v " (default "" $cluster.jmxPass) -}}
    {{- printf "-d jmxSsl=%v " (default "false" $cluster.jmxSsl) -}}
    {{- printf "-d logkafkaEnabled=%v " (default "false" $cluster.logkafkaEnabled) -}}
    {{- printf "-d pollConsumers=%v " (default "false" $cluster.pollConsumers) -}}
    {{- printf "-d filterConsumers=%v " (default "false" $cluster.filterConsumers) -}}
    {{- printf "-d activeOffsetCacheEnabled=%v " (default "false" $cluster.activeOffsetCacheEnabled) -}}
    {{- printf "-d displaySizeEnabled=%v " (default "false" $cluster.displaySizeEnabled) -}}
    {{- printf "-d tuning.brokerViewUpdatePeriodSeconds=%v " (default "30" $cluster.tuning.brokerViewUpdatePeriodSeconds) -}}
    {{- printf "-d tuning.clusterManagerThreadPoolSize=%v " (default "2" $cluster.tuning.clusterManagerThreadPoolSize) -}}
    {{- printf "-d tuning.clusterManagerThreadPoolQueueSize=%v " (default "100" $cluster.tuning.clusterManagerThreadPoolQueueSize) -}}
    {{- printf "-d tuning.kafkaCommandThreadPoolSize=%v " (default "2" $cluster.tuning.kafkaCommandThreadPoolSize) -}}
    {{- printf "-d tuning.kafkaCommandThreadPoolQueueSize=%v " (default "100" $cluster.tuning.kafkaCommandThreadPoolQueueSize) -}}
    {{- printf "-d tuning.logkafkaCommandThreadPoolSize=%v " (default "2" $cluster.tuning.logkafkaCommandThreadPoolSize) -}}
    {{- printf "-d tuning.logkafkaCommandThreadPoolQueueSize=%v " (default "100" $cluster.tuning.logkafkaCommandThreadPoolQueueSize) -}}
    {{- printf "-d tuning.logkafkaUpdatePeriodSeconds=%v " (default "30" $cluster.tuning.logkafkaUpdatePeriodSeconds) -}}
    {{- printf "-d tuning.partitionOffsetCacheTimeoutSecs=%v " (default "5" $cluster.tuning.partitionOffsetCacheTimeoutSecs) -}}
    {{- printf "-d tuning.brokerViewThreadPoolSize=%v " (default "4" $cluster.tuning.brokerViewThreadPoolSize) -}}
    {{- printf "-d tuning.brokerViewThreadPoolQueueSize=%v " (default "1000" $cluster.tuning.brokerViewThreadPoolQueueSize) -}}
    {{- printf "-d tuning.offsetCacheThreadPoolSize=%v " (default "4" $cluster.tuning.offsetCacheThreadPoolSize) -}}
    {{- printf "-d tuning.offsetCacheThreadPoolQueueSize=%v " (default "1000" $cluster.tuning.offsetCacheThreadPoolQueueSize) -}}
    {{- printf "-d tuning.kafkaAdminClientThreadPoolSize=%v " (default "4" $cluster.tuning.kafkaAdminClientThreadPoolSize) -}}
    {{- printf "-d tuning.kafkaAdminClientThreadPoolQueueSize=%v " (default "1000" $cluster.tuning.kafkaAdminClientThreadPoolQueueSize) -}}
    {{- printf "-d tuning.kafkaManagedOffsetMetadataCheckMillis=%v " (default "30000" $cluster.tuning.kafkaManagedOffsetMetadataCheckMillis) -}}
    {{- printf "-d tuning.kafkaManagedOffsetGroupCacheSize=%v " (default "1000000" $cluster.tuning.kafkaManagedOffsetGroupCacheSize) -}}
    {{- printf "-d tuning.kafkaManagedOffsetGroupExpireDays=%v " (default "7" $cluster.tuning.kafkaManagedOffsetGroupExpireDays) -}}
    {{- printf "-d securityProtocol=%v " (default "PLAINTEXT" $cluster.securityProtocol) -}}
    {{- printf "$( if $KAFKA_MANAGER_AUTH_ENABLED; then echo -u $KAFKA_MANAGER_USERNAME:$KAFKA_MANAGER_PASSWORD ; fi ) " -}}
    {{- end -}}
{{- end -}}
 # deployment.yaml
apiVersion: apps/v1
kind: Deployment
metadata:
  name: {{ template "kafka-manager.fullname" . }}
  labels:
    app.kubernetes.io/instance: {{ .Release.Name }}
    app.kubernetes.io/managed-by: {{ .Release.Service }}
    app.kubernetes.io/name: {{ include "kafka-manager.name" . }}
    app.kubernetes.io/version: {{ .Chart.AppVersion | replace "+" "_" | trunc 63 }}
    helm.sh/chart: {{ include "kafka-manager.chart" . }}
spec:
  replicas: 1
  selector:
    matchLabels:
      app: {{ template "kafka-manager.name" . }}
      release: {{ .Release.Name }}
  template:
    metadata:
      labels:
        app: {{ template "kafka-manager.name" . }}
        release: {{ .Release.Name }}
    spec:
      serviceAccountName: {{ template "kafka-manager.serviceAccountName" . }}
      containers:
        - name: {{ .Chart.Name }}
          image: "{{ .Values.image.repository }}:{{ .Values.image.tag }}"
          imagePullPolicy: {{ .Values.image.pullPolicy }}
          ports:
            - name: kafka-manager
              containerPort: 9000
              protocol: TCP
          env:
            - name: ZK_HOSTS
              value: {{ include "kafka-manager.zkHosts" . | quote }}
            - name: JAVA_OPTS
              value: {{ .Values.javaOptions }}
            - name: APPLICATION_SECRET
              valueFrom:
                secretKeyRef:
                  name: {{ template "kafka-manager.fullname" . }}
                  key: applicationSecret
            - name: KAFKA_MANAGER_AUTH_ENABLED
              value: {{ .Values.basicAuth.enabled | quote }}
            - name: KAFKA_MANAGER_USERNAME
              valueFrom:
                secretKeyRef:
                  name: {{ template "kafka-manager.fullname" . }}
                  key: basicAuthUsername
            - name: KAFKA_MANAGER_PASSWORD
              valueFrom:
                secretKeyRef:
                  name: {{ template "kafka-manager.fullname" . }}
                  key: basicAuthPassword
            - name: KAFKA_MANAGER_LDAP_ENABLED
              value: {{ .Values.basicAuth.ldap.enabled | quote }}
            - name: KAFKA_MANAGER_LDAP_SERVER
              value: {{ .Values.basicAuth.ldap.server | quote }}
            - name: KAFKA_MANAGER_LDAP_PORT
              value: {{ .Values.basicAuth.ldap.port | quote }}
            - name: KAFKA_MANAGER_LDAP_USERNAME
              valueFrom:
                secretKeyRef:
                  name: {{ template "kafka-manager.fullname" . }}
                  key: basicAuthLDAPUsername
            - name: KAFKA_MANAGER_LDAP_PASSWORD
              valueFrom:
                secretKeyRef:
                  name: {{ template "kafka-manager.fullname" . }}
                  key: basicAuthLDAPPassword
            - name: KAFKA_MANAGER_LDAP_SEARCH_BASE_DN
              value: {{ .Values.basicAuth.ldap.searchBaseDn | quote }}
            - name: KAFKA_MANAGER_LDAP_SEARCH_FILTER
              value: {{ .Values.basicAuth.ldap.searchFilter | quote }}
            - name: KAFKA_MANAGER_LDAP_CONNECTION_POOL_SIZE
              value: {{ .Values.basicAuth.ldap.connectionPoolSize | quote }}
            - name: KAFKA_MANAGER_LDAP_SSL
              value: {{ .Values.basicAuth.ldap.ssl | quote }}
          livenessProbe:
{{ toYaml .Values.livenessProbe | indent 12 }}
          readinessProbe:
{{ toYaml .Values.readinessProbe | indent 12 }}
          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 "kafka-manager.fullname" . -}}
{{- $servicePort := .Values.service.port -}}
{{- $ingressPath := .Values.ingress.path -}}
apiVersion: extensions/v1beta1
kind: Ingress
metadata:
  name: {{ $fullName }}
  labels:
    app.kubernetes.io/instance: {{ .Release.Name }}
    app.kubernetes.io/managed-by: {{ .Release.Service }}
    app.kubernetes.io/name: {{ include "kafka-manager.name" . }}
    app.kubernetes.io/version: {{ .Chart.AppVersion | replace "+" "_" | trunc 63 }}
    helm.sh/chart: {{ include "kafka-manager.chart" . }}
{{- 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: kafka-manager
  {{- end }}
{{- end }}
 # job.yaml
{{- if .Values.clusters -}}
{{- $scriptHash := include (print $.Template.BasePath "/configmap.yaml") . | sha256sum | trunc 8 -}}
apiVersion: batch/v1
kind: Job
metadata:
  name: "{{ template "kafka-manager.bootstrap.fullname" . }}-{{ $scriptHash }}"
  labels:
    app.kubernetes.io/instance: {{ .Release.Name }}
    app.kubernetes.io/managed-by: {{ .Release.Service }}
    app.kubernetes.io/name: {{ include "kafka-manager.name" . }}
    app.kubernetes.io/version: {{ .Chart.AppVersion | replace "+" "_" | trunc 63 }}
    helm.sh/chart: {{ include "kafka-manager.chart" . }}
spec:
  template:
    metadata:
      labels:
        app: {{ template "kafka-manager.name" . }}
        release: {{ .Release.Name }}
    spec:
      serviceAccountName: {{ template "kafka-manager.serviceAccountName" . }}
      restartPolicy: OnFailure
      volumes:
        - name: script-volume
          configMap:
            name: {{ template "kafka-manager.bootstrap.fullname" . }}
            defaultMode: 0744
      nodeSelector:
        kubernetes.io/arch: amd64
      containers:
        - name: {{ .Chart.Name }}-bootstrap
          image: "{{ .Values.image.repository }}:{{ .Values.image.tag }}"
          imagePullPolicy: {{ .Values.image.pullPolicy }}
          command: ["/usr/local/script/addClusters.sh"]
          env:
            - name: KAFKA_MANAGER_AUTH_ENABLED
              value: {{ .Values.basicAuth.enabled | quote }}
            - name: KAFKA_MANAGER_USERNAME
              valueFrom:
                secretKeyRef:
                  name: {{ template "kafka-manager.fullname" . }}
                  key: basicAuthUsername
            - name: KAFKA_MANAGER_PASSWORD
              valueFrom:
                secretKeyRef:
                  name: {{ template "kafka-manager.fullname" . }}
                  key: basicAuthPassword
          volumeMounts:
            - name: script-volume
              mountPath: "/usr/local/script"
  backoffLimit: 20
{{- end -}}
 # secrets.yaml
apiVersion: v1
kind: Secret
metadata:
  name: {{ template "kafka-manager.fullname" . }}
  labels:
    app.kubernetes.io/instance: {{ .Release.Name }}
    app.kubernetes.io/managed-by: {{ .Release.Service }}
    app.kubernetes.io/name: {{ include "kafka-manager.name" . }}
    app.kubernetes.io/version: {{ .Chart.AppVersion | replace "+" "_" | trunc 63 }}
    helm.sh/chart: {{ include "kafka-manager.chart" . }}
type: Opaque
data:
  {{ if .Values.applicationSecret }}
  applicationSecret:  {{ .Values.applicationSecret | b64enc | quote }}
  {{ else }}
  applicationSecret: {{ randAlphaNum 10 | b64enc | quote }}
  {{ end }}
  basicAuthUsername:  {{ .Values.basicAuth.username | b64enc | quote }}
  {{ if .Values.basicAuth.password }}
  basicAuthPassword:  {{ .Values.basicAuth.password | b64enc | quote }}
  {{ else }}
  basicAuthPassword: {{ randAlphaNum 10 | b64enc | quote }}
  {{ end }}
  basicAuthLDAPUsername: {{ .Values.basicAuth.ldap.username | b64enc | quote }}
  {{ if .Values.basicAuth.ldap.password }}
  basicAuthLDAPPassword: {{ .Values.basicAuth.ldap.password | b64enc | quote }}
  {{ else }}
  basicAuthLDAPPassword: {{ randAlphaNum 10 | b64enc | quote }}
  {{ end }} # service.yaml
apiVersion: v1
kind: Service
metadata:
  name: {{ template "kafka-manager.fullname" . }}
  labels:
    app.kubernetes.io/instance: {{ .Release.Name }}
    app.kubernetes.io/managed-by: {{ .Release.Service }}
    app.kubernetes.io/name: {{ include "kafka-manager.name" . }}
    app.kubernetes.io/version: {{ .Chart.AppVersion | replace "+" "_" | trunc 63 }}
    helm.sh/chart: {{ include "kafka-manager.chart" . }}
{{- with .Values.service.annotations }}
  annotations:
{{ toYaml . | indent 4 }}
{{- end }}
spec:
  type: {{ .Values.service.type }}
  ports:
    - name: kafka-manager
      port: {{ .Values.service.port }}
      targetPort: kafka-manager
      protocol: TCP
  selector:
    app: {{ template "kafka-manager.name" . }}
    release: {{ .Release.Name }}
 # serviceaccount.yaml
{{- if .Values.serviceAccount.create }}
apiVersion: v1
kind: ServiceAccount
metadata:
  labels:
    app.kubernetes.io/instance: {{ .Release.Name }}
    app.kubernetes.io/managed-by: {{ .Release.Service }}
    app.kubernetes.io/name: {{ include "kafka-manager.name" . }}
    app.kubernetes.io/version: {{ .Chart.AppVersion | replace "+" "_" | trunc 63 }}
    helm.sh/chart: {{ include "kafka-manager.chart" . }}
  name: {{ template "kafka-manager.serviceAccountName" . }}
{{- end }}

# ------------------------------------------------------------------------------
# Kafka Manager:
# ------------------------------------------------------------------------------

## Service account configuration
## Ref: https://kubernetes.io/docs/tasks/configure-pod-container/configure-service-account/
##
serviceAccount:
  create: true
  ## Define serviceAccount name. Defaults to fully qualified name or "default"
  ## when create is false
  ##
  name: ""

livenessProbe:
  httpGet:
    path: /api/health
    port: kafka-manager
  initialDelaySeconds: 60
  timeoutSeconds: 30
  failureThreshold: 10

readinessProbe:
  httpGet:
    path: /api/health
    port: kafka-manager

## Specs for the Kafka-manager image
##
image:
  repository: zenko/kafka-manager
  tag: 1.3.3.22
  pullPolicy: IfNotPresent

## Kafka-manager zookeeper hosts. Default to localhost:2181 or
## the bundled zookeeper chart service url if enabled (see below).
## This value can be a template
##
zkHosts: ""

## Clusters to be added through the kafka-manager api
##
clusters:
  ## Name of your cluster
  # - name: "default"

    ## Cluster zookeeper hosts. It will default to the
    ## Kafka-manager zookeeper hosts if not specified
    ##
    # zkHosts: ""

    ## The following parameters can be configured for your cluster.
    ## See '_helpers.tpl' for the default values. Supported values for kafkaVersion: 0.8.1.1, 0.8.2.0, 0.8.2.1, 0.8.2.2, 0.9.0.0, 0.9.0.1, 0.10.0.0, 0.10.0.1, 0.10.1.0, 0.10.1.1, 0.10.2.0, 0.10.2.1, 0.11.0.0, 0.11.0.2, 1.0.0, 1.0.1, 1.1.0, 1.1.1, 2.0.0. Default: 2.0.0
    ##
    # kafkaVersion: ""
    # jmxEnabled: ""
    # jmxUser: ""
    # jmxPass: ""
    # jmxSsl: ""
    # logkafkaEnabled: ""
    # pollConsumers: ""
    # filterConsumers: ""
    # activeOffsetCacheEnabled: ""
    # displaySizeEnabled: ""
    # securityProtocol: ""

    ## Additional cluster tunning. It is mandatory that this value exists,
    ## even if it's empty '{}'.
    ##
    # tuning: {}
      # brokerViewUpdatePeriodSeconds:
      # clusterManagerThreadPoolSize:
      # clusterManagerThreadPoolQueueSize:
      # kafkaCommandThreadPoolSize:
      # kafkaCommandThreadPoolQueueSize:
      # logkafkaCommandThreadPoolSize:
      # logkafkaCommandThreadPoolQueueSize:
      # logkafkaUpdatePeriodSeconds:
      # partitionOffsetCacheTimeoutSecs:
      # brokerViewThreadPoolSize:
      # brokerViewThreadPoolQueueSize:
      # offsetCacheThreadPoolSize:
      # offsetCacheThreadPoolQueueSize:
      # kafkaAdminClientThreadPoolSize:
      # kafkaAdminClientThreadPoolQueueSize:
      # kafkaManagedOffsetMetadataCheckMillis:
      # kafkaManagedOffsetGroupCacheSize:
      # kafkaManagedOffsetGroupExpireDays:

## Application secret. Defaults to a random 10-character alphanumeric string
##
applicationSecret: ""

## Basic Auth configuration
##
basicAuth:
  enabled: false
  username: "admin"
  ## Defaults to a random 10-character alphanumeric string if not set
  ##
  password: ""
  ## LDAP Authentication
  ## Ref : https://github.com/yahoo/CMAK#authenticating-a-user-with-ldap
  ##
  ldap:
    enabled: false
    server: ""
    port: 389
    username: ""
    password: ""
    searchBaseDn: ""
    searchFilter: "(uid=$capturedLogin$)"
    connectionPoolSize: 10
    ssl: false

## Java runtime options. Passed through the JAVA_OPTS environmental variable
##
javaOptions: ""

## Service configuration
## Ref: http://kubernetes.io/docs/user-guide/services/
##
service:
  type: ClusterIP
  port: 9000
  annotations: {}

## Ingress configuration
## Ref: https://kubernetes.io/docs/concepts/services-networking/ingress/
##
ingress:
  enabled: false
  annotations: {}
    # kubernetes.io/ingress.class: nginx
    # kubernetes.io/tls-acme: "true"
  path: /
  hosts:
    - kafka-manager.local
  tls: []
    # - secretName: kafka-manager-tls
    #   hosts:
    #     - kafka-manager.local

## Pod resource requests and limits
## Ref: http://kubernetes.io/docs/user-guide/compute-resources/
##
resources: {}
#  limits:
#    cpu: 100m
#    memory: 128Mi
#  requests:
#    cpu: 100m
#    memory: 128Mi

## 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: {}

# ------------------------------------------------------------------------------
# Zookeeper:
# ------------------------------------------------------------------------------

zookeeper:
  enabled: false

  ## Environmental variables to set in Zookeeper
  ##
  env:
    ## The JVM heap size to allocate to Zookeeper
    ZK_HEAP_SIZE: "1G"

  ## Configure Zookeeper persistence
  persistence:
    enabled: false
