apiVersion: apps/v1
kind: Deployment
metadata:
  name: {{ include "guacamole.fullname" . }}-app
  labels:
    {{- include "guacamole.labels" . | nindent 4 }}
spec:
{{- if not .Values.guacamole.autoscaling.enabled }}
  replicas: {{ .Values.guacamole.replicaCount }}
{{- end }}
  selector:
    matchLabels:
      {{- include "guacamole.selectorLabels" . | nindent 6 }}
  template:
    metadata:
    {{- with .Values.guacamole.podAnnotations }}
      annotations:
        {{- toYaml . | nindent 8 }}
    {{- end }}
      labels:
        {{- include "guacamole.selectorLabels" . | nindent 8 }}
    spec:
      {{- with .Values.guacamole.imagePullSecrets }}
      imagePullSecrets:
        {{- toYaml . | nindent 8 }}
      {{- end }}
      serviceAccountName: {{ include "guacamole.serviceAccountName" . }}
      securityContext:
        {{- toYaml .Values.guacamole.podSecurityContext | nindent 8 }}
      containers:
        - name: {{ .Chart.Name }}
          securityContext:
            {{- toYaml .Values.guacamole.securityContext | nindent 12 }}
          image: "{{ .Values.guacamole.image.repository }}:{{ .Values.guacamole.image.tag | default .Chart.AppVersion }}"
          imagePullPolicy: {{ .Values.guacamole.image.pullPolicy }}
          env:
          - name: GUACD_HOSTNAME
            valueFrom:
              configMapKeyRef:
                name: guacamole-config
                key: guacd_host
          - name: GUACD_PORT
            valueFrom:
              configMapKeyRef:
                name: guacamole-config
                key: "guacd_port"
          - name: MYSQL_PORT
            valueFrom:
              configMapKeyRef:
                name: guacamole-config
                key: "mysql_port"
          - name: MYSQL_HOSTNAME
            valueFrom:
              configMapKeyRef:
                name: guacamole-config
                key: "mysql_host"
          - name: MYSQL_DATABASE
            valueFrom:
              secretKeyRef:
                name: guacamole-mysql
                key: database
          - name: MYSQL_USER
            valueFrom:
              secretKeyRef:
                name: guacamole-mysql
                key: username
          - name: MYSQL_PASSWORD
            valueFrom:
              secretKeyRef:
                name: guacamole-mysql
                key: user_password
          - name: OPENID_AUTHORIZATION_ENDPOINT
            valueFrom:
              secretKeyRef:
                name: guacamole-mysql
                key: openid-authorization-endpoint
          - name: OPENID_JWKS_ENDPOINT
            valueFrom:
              secretKeyRef:
                name: guacamole-mysql
                key: openid-jwks-endpoint
          - name: OPENID_ISSUER
            valueFrom:
              secretKeyRef:
                name: guacamole-mysql
                key: openid-issuer
          - name: OPENID_CLIENT_ID
            valueFrom:
              secretKeyRef:
                name: guacamole-mysql
                key: openid-client-id
          - name: OPENID_REDIRECT_URI
            valueFrom:
              secretKeyRef:
                name: guacamole-mysql
                key: openid-redirect-uri
          ports:
            - name: http
              containerPort: 8080
              protocol: TCP
          livenessProbe:
            tcpSocket:
              port: 8080
          readinessProbe:
            tcpSocket:
              port: 8080
          resources:
            {{- toYaml .Values.guacamole.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 }}
