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

{{/*
Common labels
*/}}
{{- define "centrifugo.labels" -}}
helm.sh/chart: {{ include "centrifugo.chart" . }}
{{ include "centrifugo.selectorLabels" . }}
{{- if .Chart.AppVersion }}
app.kubernetes.io/version: {{ .Chart.AppVersion | quote }}
{{- end }}
app.kubernetes.io/managed-by: {{ .Release.Service }}
{{- end -}}

{{/*
Selector labels
*/}}
{{- define "centrifugo.selectorLabels" -}}
app.kubernetes.io/name: {{ include "centrifugo.name" . }}
app.kubernetes.io/instance: {{ .Release.Name }}
{{- end -}}
 # centrifugo-config-test.yaml
apiVersion: v1
kind: ConfigMap
metadata:
  name: {{ include "centrifugo.fullname" . }}-tests
  namespace: {{ .Release.Namespace }}
  labels:
    {{- include "centrifugo.labels" . | nindent 4 }}
data:
  run.sh: |-
    @test "Testing if centrifugo ui is accessible" {
      curl --retry 48 --retry-delay 10 {{ include "centrifugo.fullname" . }}.{{ .Release.Namespace }}.svc.cluster.local:{{ .Values.service.port }}
    }
 # centrifugo-test.yaml
apiVersion: v1
kind: Pod
metadata:
  name: {{ include "centrifugo.fullname" . }}-test-{{ randAlphaNum 5 | lower }}
  namespace: {{ .Release.Namespace }}
  labels:
    {{- include "centrifugo.labels" . | nindent 4 }}
  annotations:
    "helm.sh/hook": test-success
spec:
  initContainers:
  - name: "test-framework"
    image: "dduportal/bats:0.4.0"
    command:
    - "bash"
    - "-c"
    - |
      set -ex
      # copy bats to tools dir
      cp -R /usr/local/libexec/ /tools/bats/
    volumeMounts:
    - mountPath: /tools
      name: tools
  containers:
  - name: {{ .Release.Name }}-test
    image: "dduportal/bats:0.4.0"
    command: ["/tools/bats/bats", "-t", "/tests/run.sh"]
    volumeMounts:
    - mountPath: /tests
      name: tests
      readOnly: true
    - mountPath: /tools
      name: tools
  volumes:
  - name: tests
    configMap:
      name: {{ include "centrifugo.fullname" . }}-tests
  - name: tools
    emptyDir: {}
  restartPolicy: Never
 # configmap.yaml
apiVersion: v1
kind: ConfigMap
metadata:
  name: {{ include "centrifugo.fullname" . }}-config
  namespace: {{ .Release.Namespace }}
  labels:
    {{- include "centrifugo.labels" . | nindent 4 }}
data:
  config.json: |-
{{ toJson .Values.config| indent 4 }}
 # deployment.yaml
apiVersion: apps/v1
kind: Deployment
metadata:
  name: {{ include "centrifugo.fullname" . }}
  namespace: {{ .Release.Namespace }}
  labels:
    {{- include "centrifugo.labels" . | nindent 4 }}
spec:
  replicas: {{ .Values.replicaCount }}
  selector:
    matchLabels:
      {{- include "centrifugo.selectorLabels" . | nindent 6 }}
  strategy:
    type: RollingUpdate
    rollingUpdate:
      maxUnavailable: 0
  template:
    metadata:
      annotations:
        checksum/config: {{ include (print $.Template.BasePath "/configmap.yaml") . | sha256sum }}
        checksum/secret: {{ include (print $.Template.BasePath "/secret.yaml") . | sha256sum }}
        {{- if .Values.podAnnotations }}
{{ toYaml .Values.podAnnotations | indent 8 }}
        {{- end }}
      labels:
        {{- include "centrifugo.selectorLabels" . | nindent 8 }}
    spec:
      containers:
      - name: {{ .Chart.Name }}
        image: "{{ .Values.image.repository }}:{{ .Values.image.tag }}"
        imagePullPolicy: {{ .Values.image.pullPolicy }}
        command:
        - centrifugo
        args:
        - --port={{ .Values.service.port }}
        {{- if .Values.grpc.enabled }}
        - --grpc_api
        - --grpc_api_port={{ .Values.service.grpcPort }}
        {{- end }}
        {{- range $arg := .Values.args }}
        - {{ $arg }}
        {{- end }}
        env:
        - name: CENTRIFUGO_ADMIN_PASSWORD
          valueFrom:
            secretKeyRef:
              name: {{ include "centrifugo.fullname" . }}
              key: admin-password
        - name: CENTRIFUGO_ADMIN_SECRET
          valueFrom:
            secretKeyRef:
              name: {{ include "centrifugo.fullname" . }}
              key: admin-secret
        - name: CENTRIFUGO_SECRET
          valueFrom:
            secretKeyRef:
              name: {{ include "centrifugo.fullname" . }}
              key: secret
        - name: CENTRIFUGO_API_KEY
          valueFrom:
            secretKeyRef:
              name: {{ include "centrifugo.fullname" . }}
              key: api_key
        - name: CENTRIFUGO_TOKEN_HMAC_SECRET_KEY
          valueFrom:
            secretKeyRef:
              name: {{ include "centrifugo.fullname" . }}
              key: token_hmac_secret_key
        volumeMounts:
        - name: "{{ include "centrifugo.fullname" . }}-config"
          mountPath: "/centrifugo"
          readOnly: true
        ports:
        - name: http
          containerPort: {{ .Values.service.port }}
        {{- if .Values.grpc.enabled }}
        - name: grpc
          containerPort: {{ .Values.service.grpcPort }}
        {{- end }}
        livenessProbe:
          httpGet:
            path: /health
            port: {{ .Values.service.port }}
          initialDelaySeconds: 30
          periodSeconds: 10
        readinessProbe:
          httpGet:
            path: /health
            port: {{ .Values.service.port }}
          initialDelaySeconds: 15
          periodSeconds: 10
        resources:
{{ toYaml .Values.resources | indent 10 }}
      volumes:
      - name: {{ include "centrifugo.fullname" . }}-config
        configMap:
          name: {{ include "centrifugo.fullname" . }}-config
 # ingress.yaml
{{- if .Values.ingress.enabled -}}
{{- $serviceName := include "centrifugo.fullname" . -}}
{{- $servicePort := .Values.service.port -}}
apiVersion: extensions/v1beta1
kind: Ingress
metadata:
  name: {{ include "centrifugo.fullname" . }}
  namespace: {{ .Release.Namespace }}
  labels:
    {{- include "centrifugo.labels" . | nindent 4 }}
  {{- with .Values.ingress.annotations }}
  annotations:
    {{- toYaml . | nindent 4 }}
  {{- end }}
spec:
  rules:
    {{- range $host := .Values.ingress.hosts }}
    - host: {{ $host }}
      http:
        paths:
          - path: /
            backend:
              serviceName: {{ $serviceName }}
              servicePort: {{ $servicePort }}
    {{- end -}}
  {{- if .Values.ingress.tls }}
  tls:
{{ toYaml .Values.ingress.tls | indent 4 }}
  {{- end -}}
{{- end -}}
 # secret.yaml
apiVersion: v1
kind: Secret
metadata:
  name: {{ include "centrifugo.fullname" . }}
  namespace: {{ .Release.Namespace }}
  labels:
    {{- include "centrifugo.labels" . | nindent 4 }}
type: Opaque
data:
  {{- if .Values.admin.password }}
  admin-password: {{ .Values.admin.password | b64enc | quote }}
  {{- else }}
  admin-password: {{ randAlphaNum 10 | b64enc | quote }}
  {{- end }}
  {{- if .Values.admin.secret }}
  admin-secret: {{ .Values.admin.secret | b64enc | quote }}
  {{- else }}
  admin-secret: {{ randAlphaNum 10 | b64enc | quote }}
  {{- end }}
  {{- if .Values.secret }}
  secret: {{ .Values.secret | b64enc | quote }}
  {{- else }}
  secret: {{ randAlphaNum 10 | b64enc | quote }}
  {{- end }}
  {{- if .Values.token_hmac_secret_key }}
  token_hmac_secret_key: {{ .Values.token_hmac_secret_key | b64enc | quote }}
  {{- else }}
  token_hmac_secret_key: {{ randAlphaNum 10 | b64enc | quote }}
  {{- end }}
  {{- if .Values.api_key }}
  api_key: {{ .Values.api_key | b64enc | quote }}
  {{- else }}
  api_key: {{ randAlphaNum 10 | b64enc | quote }}
  {{- end }}
 # service.yaml
apiVersion: v1
kind: Service
metadata:
{{- if .Values.service.annotations }}
  annotations:
{{ toYaml .Values.service.annotations | indent 4 }}
{{- end }}
  labels:
    {{- include "centrifugo.labels" . | nindent 4 }}
  name: {{ include "centrifugo.fullname" . }}
  namespace: {{ .Release.Namespace }}
spec:
  type: "{{ .Values.service.type }}"
{{- if .Values.service.clusterIP }}
  clusterIP: "{{ .Values.service.clusterIP }}"
{{- end }}
{{- if .Values.service.externalIPs }}
  externalIPs:
{{ toYaml .Values.service.externalIPs | indent 4 }}
{{- end }}
{{- if .Values.service.loadBalancerIP }}
  loadBalancerIP: "{{ .Values.service.loadBalancerIP }}"
{{- end }}
{{- if .Values.service.loadBalancerSourceRanges }}
  loadBalancerSourceRanges:
{{ toYaml .Values.service.loadBalancerSourceRanges | indent 4 }}
{{- end }}
  ports:
    - name: http
      port: {{ .Values.service.port }}
      protocol: TCP
      targetPort: http
      {{- if (and (eq .Values.service.type "NodePort") (not (empty .Values.service.nodePort))) }}
      nodePort: {{ .Values.service.nodePort }}
      {{- end }}
      {{- if .Values.grpc.enabled }}
    - name: grpc
      port: {{ .Values.service.grpcPort }}
      protocol: TCP
      targetPort: grpc
      {{- if (and (eq .Values.service.type "NodePort") (not (empty .Values.service.grpcNodePort))) }}
      nodePort: {{ .Values.service.grpcNodePort }}
      {{- end }}
      {{- end }}
  selector:
    {{- include "centrifugo.selectorLabels" . | nindent 4 }}

replicaCount: 1

fullnameOverride:

nameOverride:

image:
  repository: centrifugo/centrifugo
  tag: v2.4.0
  pullPolicy: IfNotPresent

service:
  annotations: {}
  clusterIP: ""
  externalIPs: []
  loadBalancerIP: ""
  loadBalancerSourceRanges: []
  type: ClusterIP
  port: 8000
  grpcPort: 10000
  nodePort: ""
  grpcNodePort: ""

ingress:
  enabled: false
  hosts: []
  annotations: {}
    # kubernetes.io/ingress.class: nginx
    # ingress.kubernetes.io/upstream-fail-timeout: 60
    # ingress.kubernetes.io/upstream-max-fails: 2
    # ingress.kubernetes.io/proxy-read-timeout: 3600
    # ingress.kubernetes.io/proxy-send-timeout: 3600

config:
  namespaces:
  - name: chat
    anonymous: false
    publish: true
    join_leave: true
    presence: true
    presence_stats: true
    history_size: 10
    history_lifetime: 60
    history_recover: true
  - name: public
    anonymous: true
    publish: true
    join_leave: true
    presence: true
    presence_stats: true
    history_size: 10
    history_lifetime: 60
    history_recover: true

## Centrifugo secret for application backend
## Defaults to a random 10-character alphanumeric string if not set
secret: ""

admin:
  ## Centrifugo admin secret to sign authorization token used to call admin API endpoints
  ## Defaults to a random 10-character alphanumeric string if not set
  secret: ""
  ## Centrifugo admin password to log into admin web interface
  ## Defaults to a random 10-character alphanumeric string if not set
  password: ""

## Centrifugo token_hmac_secret_key to check JWT signature
## Defaults to a random 10-character alphanumeric string if not set
token_hmac_secret_key: ""

## Centrifugo api_key for Centrifugo API endpoint authorization
## Defaults to a random 10-character alphanumeric string if not set
api_key: ""

args:
- --config=/centrifugo/config.json
- --admin
- --health
## For redis sentinel
# - --engine=redis
# - --redis_master_name=master
# - --redis_sentinels=redis:26379

resources: {}

# for GRPC support
grpc:
  enabled: false
