{{ if .Values.miracl.enabled }}
apiVersion: apps/v1
kind: Deployment
metadata:
  name: miracl
spec:
  replicas: 1
  selector:
    matchLabels:
      app: miracl
  template:
    metadata:
      name: miracl
      labels:
        app: miracl
    spec:
      containers:
      - name: miracl
        image: docker.io/goalprofit/miracl:{{.Values.miracl.version}}
        imagePullPolicy: Always
        ports:
          - containerPort: 8000

        env:
          - name: COOKIE_SESSION_NAME
            value: {{.Values.cookie_session_name}}
          - name: COOKIE_SESSION_NO_EXPIRES
            value: {{.Values.cookie_session_no_expires | quote}}
          - name: COOKIE_SESSION_MAX_AGE
            value: {{.Values.cookie_session_max_age | quote}}
          - name: COOKIE_SESSION_HTTP_ONLY
            value: {{.Values.cookie_session_http_only | quote}}
          - name: COOKIE_SESSION_SECURE
            value: {{.Values.cookie_session_secure | quote}}
          - name: COOKIE_SESSION_DOMAIN
            value: {{.Values.cookie_session_domain}}
          - name: COOKIE_SESSION_KEY
            valueFrom:
              secretKeyRef:
                name: cookie-session
                key: key
          - name: CLIENT_ID
            valueFrom:
              secretKeyRef:
                name: miracl-client
                key: id
                optional: true
          - name: CLIENT_SECRET
            valueFrom:
              secretKeyRef:
                name: miracl-client
                key: secret
                optional: true
          - name: PIM_HOST
            valueFrom:
              secretKeyRef:
                name: pim-auth
                key: pim-host
                optional: true
          - name: PIM_PUBLIC_KEY
            valueFrom:
              secretKeyRef:
                name: pim-auth
                key: pim-public-key
                optional: true
          - name: OUR_HOST
            valueFrom:
              secretKeyRef:
                name: pim-auth
                key: our-host
                optional: true                
          - name: REDIRECT_URL
            value: {{.Values.protocol}}{{.Values.domain}}/miracl
          - name: CA_CERT
            value: /src/ca.pem
          - name: SERVICE_LOGIN
            valueFrom:
              secretKeyRef:
                name: secret-service
                key: username
                optional: false
          - name: SERVICE_PASSWORD
            valueFrom:
              secretKeyRef:
                name: secret-service
                key: password
                optional: false
          - name: PIN_AUTH_METHOD_NAME
            value: pin
          - name: ADFS_AUTH_METHOD_NAME
            value: adfs
          - name: PIN_AUTH_METHOD_PATH
            value: {{.Values.miracl.pin_auth_method_path}}
          - name: PIN_AUTH_METHOD_TITLE
            value: {{.Values.miracl.pin_auth_method_title}}
          - name: ADFS_AUTH_METHOD_PATH
            value: {{.Values.miracl.adfs_auth_method_path}}
          - name: ADFS_AUTH_METHOD_TITLE
            value: {{.Values.miracl.adfs_auth_method_title}}

          {{ if ne (default "" .Values.miracl.saml_entry_point) "" }}
          - name: SAML_ENTRY_POINT
            value: {{.Values.miracl.saml_entry_point}}
          - name: SAML_ISSUER
            value: {{.Values.miracl.saml_issuer}}
          - name: SAML_CALLBACK_URL
            value: {{.Values.miracl.saml_callback_url}}
          - name: SAML_KEY
            valueFrom:
              secretKeyRef:
                name: saml
                key: key
                optional: false
          - name: SAML_CERT
            valueFrom:
              secretKeyRef:
                name: saml
                key: cert
                optional: false
          {{ end }}

        livenessProbe:
          httpGet:
            path: /health
            port: 8000
      imagePullSecrets:
      - name: regcred
      {{ include "app.affinity" . | nindent 6}}
---
apiVersion: v1
kind: Service
metadata:
  name: miracl
spec:
  type: ClusterIP
  selector:
    app: miracl
  ports:
    - name: http
      protocol: TCP
      port: 8000
      targetPort: 8000
{{ end }}
