apiVersion: apps/v1
kind: Deployment
metadata:
  name: {{ include "guacamole.fullname" . }}-guacamole
  labels:
{{ include "guacamole.labels" . | indent 4 }}
spec:
  replicas: {{ .Values.replicaCount }}
  selector:
    matchLabels:
      app.kubernetes.io/name: {{ include "guacamole.name" . }}-guacamole
      app.kubernetes.io/instance: {{ .Release.Name }}
  template:
    metadata:
      labels:
        app.kubernetes.io/name: {{ include "guacamole.name" . }}-guacamole
        app.kubernetes.io/instance: {{ .Release.Name }}
    spec:
    {{- with .Values.guacamole.imagePullSecrets }}
      imagePullSecrets:
        {{- toYaml . | nindent 8 }}
    {{- end }}
      dnsConfig:
        options:
          - name: ndots
            value: "1"
      serviceAccountName: {{ template "guacamole.serviceAccountName" . }}
      securityContext:
        {{- toYaml .Values.podSecurityContext | nindent 8 }}
      initContainers:
        - name: create-init-db
          securityContext:
            {{- toYaml .Values.securityContext | nindent 12 }}
          image: "{{ .Values.guacamole.image.repository }}:{{ tpl .Values.guacamole.image.tag . }}"
          imagePullPolicy: {{ .Values.guacamole.image.pullPolicy }}
          envFrom:
            - secretRef:
                name: {{ include "guacamole.fullname" . }}
          volumeMounts:
            - name: initdb
              mountPath: /data
          command: ["/bin/sh"]
          args:
            - -c
            - |
              /opt/guacamole/bin/initdb.sh --postgres > /data/initdb.sql
          resources:
            {{- toYaml .Values.resources | nindent 12 }}
        - name: loaddb
          securityContext:
            {{- toYaml .Values.securityContext | nindent 12 }}
          image: "{{ .Values.dbcreation.image.repository }}:{{ .Values.dbcreation.image.tag }}"
          imagePullPolicy: {{ .Values.dbcreation.image.pullPolicy }}
          envFrom:
            - secretRef:
                name: {{ include "guacamole.fullname" . }}
          volumeMounts:
            - name: initdb
              mountPath: /data
          command: ["/bin/sh"]
          args:
            - -c
            - |
              export PGPASSWORD=$POSTGRES_PASSWORD
              # most likely already created, so don't fail, just log and move on
              psql -h $POSTGRES_HOSTNAME -d $POSTGRES_DATABASE -U $POSTGRES_USER -p $POSTGRES_PORT -a -w -f /data/initdb.sql || true
          resources:
            {{- toYaml .Values.resources | nindent 12 }}
      containers:
        - name: {{ .Chart.Name }}
          securityContext:
            {{- toYaml .Values.securityContext | nindent 12 }}
          image: "{{ .Values.guacamole.image.repository }}:{{ tpl .Values.guacamole.image.tag . }}"
          imagePullPolicy: {{ .Values.guacamole.image.pullPolicy }}
          envFrom:
            - secretRef:
                name: {{ include "guacamole.fullname" . }}
          ports:
            - name: http
              containerPort: 8080
              protocol: TCP
          {{- with .Values.guacamole.livenessProbe }}
          livenessProbe:
            {{- toYaml . | nindent 12 }}
          {{- end }}
          {{- with .Values.guacamole.readinessProbe }}
          readinessProbe:
            {{- toYaml . | nindent 12 }}
          {{- end }}
          resources:
            {{- toYaml .Values.resources | nindent 12 }}
      {{- with .Values.nodeSelector }}
      nodeSelector:
        {{- toYaml . | nindent 8 }}
      {{- end }}
    {{- with .Values.affinity }}
      affinity:
        {{- toYaml . | nindent 8 }}
    {{- end }}
    {{- with .Values.tolerations }}
      tolerations:
        {{- toYaml . | nindent 8 }}
    {{- end }}
      volumes:
        - name: initdb
          emptyDir: {}
