apiVersion: apps/v1beta1
kind: StatefulSet
metadata:
  name: {{ template "keycloak.fullname" . }}
  annotations:
    checksum/config: {{ include (print $.Template.BasePath "/configmap.yaml") . | sha256sum }}
  labels:
    app: {{ template "keycloak.name" . }}
    chart: {{ template "keycloak.chart" . }}
    release: "{{ .Release.Name }}"
    heritage: "{{ .Release.Service }}"
spec:
  replicas: {{ .Values.keycloak.replicas }}
  serviceName: {{ template "keycloak.fullname" . }}-headless
  podManagementPolicy: Parallel
  updateStrategy:
    type: RollingUpdate
  template:
    metadata:
      labels:
        app: {{ template "keycloak.name" . }}
        release: "{{ .Release.Name }}"
    spec:
      securityContext:
{{ toYaml .Values.keycloak.securityContext | indent 8 }}
    {{- with .Values.keycloak.image.pullSecrets }}
      imagePullSecrets:
{{ toYaml . | indent 8 }}
    {{- end }}
    {{- if or .Values.keycloak.persistence.deployPostgres .Values.keycloak.extraInitContainers }}
      initContainers:
      {{- if .Values.keycloak.persistence.deployPostgres }}
        - name: wait-for-postgresql
          image: "{{ .Values.init.image.repository }}:{{ .Values.init.image.tag }}"
          imagePullPolicy: {{ .Values.init.image.pullPolicy }}
          command:
            - sh
            - -c
            - |
              until printf "." && nc -z -w 2 {{ template "keycloak.postgresql.fullname" . }} {{ .Values.postgresql.service.port }}; do
                  sleep 2;
              done;

              echo 'PostgreSQL OK ✓'
      {{- end }}
      {{- if .Values.keycloak.extraInitContainers }}
{{ tpl .Values.keycloak.extraInitContainers . | indent 8 }}
      {{- end }}
    {{- end }}
      containers:
        - name: {{ .Chart.Name }}
          image: "{{ .Values.keycloak.image.repository }}:{{ .Values.keycloak.image.tag }}"
          imagePullPolicy: {{ .Values.keycloak.image.pullPolicy }}
          command:
            - /scripts/keycloak.sh
          env:
          {{- if .Release.IsInstall }}
            - name: KEYCLOAK_USER
              value: {{ .Values.keycloak.username }}
            - name: KEYCLOAK_PASSWORD
              valueFrom:
                secretKeyRef:
                  name: {{ template "keycloak.fullname" . }}-http
                  key: password
          {{- end }}
{{ include "keycloak.dbEnvVars" . | indent 12 }}
{{- with .Values.keycloak.extraEnv }}
{{ tpl . $ | indent 12 }}
{{- end }}
          volumeMounts:
            - name: scripts
              mountPath: /scripts
{{- with .Values.keycloak.extraVolumeMounts }}
{{ tpl . $ | indent 12 }}
{{- end }}
          ports:
            - name: http
              containerPort: 8080
              protocol: TCP
          livenessProbe:
            httpGet:
              path: /auth/
              port: http
            initialDelaySeconds: {{ .Values.keycloak.livenessProbe.initialDelaySeconds }}
            timeoutSeconds: {{ .Values.keycloak.livenessProbe.timeoutSeconds }}
          readinessProbe:
            httpGet:
              path: /auth/
              port: http
            initialDelaySeconds: {{ .Values.keycloak.readinessProbe.initialDelaySeconds }}
            timeoutSeconds: {{ .Values.keycloak.readinessProbe.timeoutSeconds }}
          resources:
{{ toYaml .Values.keycloak.resources | indent 12 }}
{{- with .Values.keycloak.extraContainers }}
{{ tpl . $ | indent 8 }}
{{- end }}
    {{- with .Values.keycloak.affinity }}
      affinity:
{{ tpl . $ | indent 8 }}
    {{- end }}
    {{- with .Values.keycloak.nodeSelector }}
      nodeSelector:
{{ toYaml . | indent 8 }}
    {{- end }}
    {{- with .Values.keycloak.tolerations }}
      tolerations:
{{ toYaml . | indent 8 }}
    {{- end }}
      terminationGracePeriodSeconds: 60
      volumes:
        - name: scripts
          configMap:
            name: {{ template "keycloak.fullname" . }}
            defaultMode: 0555
{{- with .Values.keycloak.extraVolumes }}
{{ tpl . $ | indent 8 }}
{{- end }}
