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

{{/* Generate basic labels */}}
{{- define "hubot.labels" }}
app.kubernetes.io/name: {{ include "hubot.name" . }}
app.kubernetes.io/instance: {{ .Release.Name }}
app.kubernetes.io/managed-by: {{ .Release.Service }}
helm.sh/chart: {{ include "hubot.chart" . }}
{{- end }}

{{- define "hubot.redis.fullname" -}}
{{- $name := default "redis" .Values.redis.nameOverride -}}
{{- printf "%s-%s" .Release.Name $name | trunc 63 | trimSuffix "-" -}}
{{- end -}}
 # config-cm.yaml
{{- if .Values.config }}
apiVersion: v1
kind: ConfigMap
metadata:
  name: {{ template "hubot.fullname" . }}-config
  labels:
{{ include "hubot.labels" . | indent 4 }}
{{- if .Values.extraLabels }}
{{ toYaml .Values.extraLabels | indent 4 }}
{{- end }}
data:
{{ toYaml .Values.config | indent 2 }}
{{- end }}
 # config-secret.yaml
{{- if .Values.secretConfig }}
apiVersion: v1
kind: Secret
metadata:
  name: {{ template "hubot.fullname" . }}-config
  labels:
{{ include "hubot.labels" . | indent 4 }}
{{- if .Values.extraLabels }}
{{ toYaml .Values.extraLabels | indent 4 }}
{{- end }}
type: Opaque
data:
{{ range $key, $value := .Values.secretConfig }}
  {{ $key }}: {{ $value | b64enc }}
{{- end }}
{{- end }}
 # deployment.yaml
---

apiVersion: apps/v1
kind: Deployment
metadata:
  name: {{ template "hubot.fullname" . }}
  labels:
{{ include "hubot.labels" . | indent 4 }}
{{- if .Values.extraLabels }}
{{ toYaml .Values.extraLabels | indent 4 }}
{{- end }}
spec:
  replicas: {{ .Values.replicaCount }}
  strategy:
    type: {{ .Values.strategyType }}
  selector:
    matchLabels:
      app.kubernetes.io/name: {{ include "hubot.name" . }}
      app.kubernetes.io/instance: {{ .Release.Name }}
  template:
    metadata:
      annotations:
        checksum/config: {{ include (print $.Template.BasePath "/config-cm.yaml") . | sha256sum }}
        checksum/secret-config: {{ include (print $.Template.BasePath "/config-secret.yaml") . | sha256sum }}
        checksum/external-scripts.json: {{ include (print $.Template.BasePath "/external-scripts-cm.yaml") . | sha256sum }}
        {{- if .Values.scripts }}
        checksum/scripts: {{ include (print $.Template.BasePath "/scripts-cm.yaml") . | sha256sum }}
        {{- end }}
      labels:
        app.kubernetes.io/name: {{ include "hubot.name" . }}
        app.kubernetes.io/instance: {{ .Release.Name }}
    spec:
      initContainers:
        {{- if .Values.redis.enabled }}
        - name: wait-for-redis
          image: busybox
          env:
          - name: HUBOT_REDIS_HOST
            value: {{ template "hubot.redis.fullname" . }}-master
          - name: HUBOT_REDIS_PORT
            value: "{{ .Values.redis.master.service.port }}"
          command: [ "/bin/sh", "-c", "until nc -zv $HUBOT_REDIS_HOST $HUBOT_REDIS_PORT -w1; do echo 'waiting for redis'; sleep 1; done" ]
        {{- end }}
        {{- if .Values.scriptsRepo.enabled }}
        - name: checkout-scripts-repo
          image: {{ .Values.scriptsRepo.image }}
          imagePullPolicy: Always
          volumeMounts:
            - name: scripts
              mountPath: /tmp/git
          env:
            - name: GIT_SYNC_DEST
              value: "scripts"
            - name: GIT_SYNC_REPO
              value: {{ .Values.scriptsRepo.repository | quote }}
            - name: GIT_SYNC_ONE_TIME
              value: "true"
            - name: GIT_SYNC_DEPTH
              value: "1"
          {{- if .Values.scriptsRepo.branch }}
            - name: GIT_SYNC_BRANCH
              value: {{ .Values.scriptsRepo.branch }}
          {{- end }}
          {{- if .Values.scriptsRepo.username }}
          {{- if .Values.scriptsRepo.existingSecretName }}
            - name: GIT_SYNC_USERNAME
              valueFrom:
                secretKeyRef:
                  name: {{ .Values.scriptsRepo.existingSecretName }}
                  key: username
            - name: GIT_SYNC_PASSWORD
              valueFrom:
                secretKeyRef:
                  name: {{ .Values.scriptsRepo.existingSecretName }}
                  key: password
          {{- else }}
            - name: GIT_SYNC_USERNAME
              valueFrom:
                secretKeyRef:
                  name: {{ template "hubot.fullname" . }}-scripts-repo
                  key: username
            - name: GIT_SYNC_PASSWORD
              valueFrom:
                secretKeyRef:
                  name: {{ template "hubot.fullname" . }}-scripts-repo
                  key: password
          {{- end }}
          {{- end }}
          {{- end }}
      containers:
        - name: hubot
          image: "{{ .Values.image.repository }}:{{ .Values.image.tag }}"
          imagePullPolicy: {{ .Values.image.pullPolicy }}
          args:
{{ toYaml .Values.args | indent 12 }}
        {{- if .Values.extraArgs }}
{{ toYaml .Values.extraArgs | indent 12 }}
        {{- end }}
        {{- if .Values.tty }}
          tty: true
        {{- end }}
          ports:
            - name: http
              containerPort: 8080
              protocol: TCP
          livenessProbe:
            httpGet:
              path: /health
              port: http
          readinessProbe:
            httpGet:
              path: /health
              port: http
          volumeMounts:
            - name: {{ template "hubot.fullname" . }}-external-scripts
              mountPath: /home/hubot/external-scripts.json
              subPath: external-scripts.json
              readOnly: true
            {{- range .Values.extraConfigmapMounts }}
            - name: {{ .name }}
              mountPath: {{ .mountPath }}
              readOnly: {{ .readOnly }}
            {{- end }}
            {{- if .Values.scriptsRepo.enabled }}
            - name: scripts
              mountPath: /home/hubot/scripts
              subPath: scripts
            {{- end }}
            {{- if .Values.scripts }}
              {{- range keys .Values.scripts }}
            - name: "{{ template "hubot.fullname" $ }}-scripts"
              mountPath: "/home/hubot/scripts/{{ . }}"
              subPath: "{{ . }}"
              {{- end }}
            {{- end }}

          env:
        {{- if .Values.redis.enabled }}
            - name: REDIS_URL
              value: redis://{{ template "hubot.redis.fullname" . }}-master:{{ .Values.redis.port }}/hubot
        {{- end }}
            - name: EXTRA_PACKAGES
              value: {{ (join "," .Values.extraPackages) | quote }}
          envFrom:
          {{- if .Values.config }}
            - configMapRef:
                name: {{ template "hubot.fullname" . }}-config
          {{- end }}
          {{- if .Values.secretConfig }}
            - secretRef:
                name: {{ template "hubot.fullname" . }}-config
          {{- end }}
          {{- if .Values.existingSecretConfigName }}
            - secretRef:
                name: {{ .Values.existingSecretConfigName }}
          {{- end }}
          resources:
{{ toYaml .Values.resources | indent 12 }}
      volumes:
    {{- if .Values.scriptsRepo.enabled }}
        - name: scripts
          emptyDir: {}
    {{- end }}
        - name: {{ template "hubot.fullname" . }}-external-scripts
          configMap:
            name: {{ template "hubot.fullname" . }}-external-scripts
      {{- range .Values.extraConfigmapMounts }}
        - name: {{ .name }}
          configMap:
            name: {{ .configMap }}
      {{- end }}
      {{- if .Values.scripts }}
        - name: {{ template "hubot.fullname" . }}-scripts
          configMap:
            name: {{ template "hubot.fullname" . }}-scripts
      {{- 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 }}
    {{- if .Values.imagePullSecrets }}
      imagePullSecrets:
        - name: {{ .Values.imagePullSecrets }}
    {{- end }}
 # external-scripts-cm.yaml
apiVersion: v1
kind: ConfigMap
metadata:
  name: {{ template "hubot.fullname" . }}-external-scripts
  labels:
{{ include "hubot.labels" . | indent 4 }}
{{- if .Values.extraLabels }}
{{ toYaml .Values.extraLabels | indent 4 }}
{{- end }}
data:
  external-scripts.json: |-
    [
    {{- range .Values.externalScripts }}
      {{ . | quote }},
    {{- end }}
      "hubot-diagnostics",
      "hubot-help",
      "hubot-redis-brain",
      "hubot-rules",
      "hubot-health"
    ]
 # ingress.yaml
{{- if .Values.ingress.enabled -}}
{{- $fullName := include "hubot.fullname" . -}}
{{- $servicePort := .Values.service.port -}}
{{- $ingressPath := .Values.ingress.path -}}
apiVersion: extensions/v1beta1
kind: Ingress
metadata:
  name: {{ $fullName }}
  labels:
{{ include "hubot.labels" . | indent 4 }}
{{- if .Values.extraLabels }}
{{ toYaml .Values.extraLabels | indent 4 }}
{{- end }}
{{- 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 }}
 # scripts-cm.yaml
{{- if .Values.scripts }}
apiVersion: v1
kind: ConfigMap
metadata:
  name: {{ template "hubot.fullname" . }}-scripts
  labels:
{{ include "hubot.labels" . | indent 4 }}
{{- if .Values.extraLabels }}
{{ toYaml .Values.extraLabels | indent 4 }}
{{- end }}
data:
{{ toYaml .Values.scripts | indent 2 }}
{{- end }}
 # scripts-repo-secret.yaml
{{- if and .Values.scriptsRepo.enabled }}
{{- if and .Values.scriptsRepo.username .Values.scriptsRepo.password (not .Values.scriptsRepo.existingSecretName) }}
apiVersion: v1
kind: Secret
metadata:
  name: {{ template "hubot.fullname" . }}-scripts-repo
  labels:
{{ include "hubot.labels" . | indent 4 }}
{{- if .Values.extraLabels }}
{{ toYaml .Values.extraLabels | indent 4 }}
{{- end }}
type: Opaque
data:
  username: {{ .Values.scriptsRepo.username | b64enc | quote }}
  password: {{ .Values.scriptsRepo.password | b64enc | quote }}
{{- end }}
{{- end }}
 # service.yaml
---

apiVersion: v1
kind: Service
metadata:
  name: {{ template "hubot.fullname" . }}
  labels:
{{ include "hubot.labels" . | indent 4 }}
{{- if .Values.extraLabels }}
{{ toYaml .Values.extraLabels | indent 4 }}
{{- end }}
spec:
  type: {{ .Values.service.type }}
  ports:
    - name: http
      port: {{ .Values.service.port }}
      targetPort: http
      protocol: TCP
  selector:
    app.kubernetes.io/name: {{ include "hubot.name" . }}
    app.kubernetes.io/instance: {{ .Release.Name }}

---

fullnameOverride: ""

replicaCount: 1

strategyType: RollingUpdate

image:
  repository: minddocdev/hubot
  tag: 3.3.2
  pullPolicy: IfNotPresent

service:
  type: NodePort
  port: 80

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

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

extraLabels: {}

extraConfigmapMounts: []
# - name: certs-configmap
#   mountPath: /hubot/ssl/
#   configMap: certs-configmap
#   readOnly: true

args:
  - --name
  - "${HUBOT_NAME}"
  - --adapter
  - slack

extraArgs: []
# - --alias
# - '!'

scripts: {}
# hithere.coffee: |
#   module.exports = (robot) ->
#     robot.respond /hi my bot/i, (msg) ->
#       msg.send 'Hi there my human'

scriptsRepo:
  enabled: false
  image: k8s.gcr.io/git-sync:v3.1.2
  repository: ""  # https:///git.mycompany.com/hubot-scripts.git
  branch: master
# username: git_username
# password: mysecretpassword
# existingSecretName: git-readonly-secret


# npm packages, required for custom scripts
extraPackages: []
#  - aws-sdk
#  - cron
#  - underscore

# external scripts (npm packages) to be enabled
externalScripts: []
# - hubot-pugme
# - hubot-plusplus

# Environment variables passed to Hubot.
config: {}
# HUBOT_URL: "http://hubot.mycompany.internal/"
# HUBOT_LOG_LEVEL: 'debug'
# HUBOT_CACHE_IMAGE: 'true'

# Sensitive environment variables passed to Hubot as Secret.
secretConfig: {}
# HUBOT_SLACK_TOKEN: 'xoxb-somenumbers-someletters'

# Reference to an existing Secret with sensitive env vars.
existingSecretConfigName: ""

redis:
  enabled: true
  usePassword: false
  cluster:
    enabled: false

# This should be changed only during CI tests
tty: false
