---
- name: Enterprise Gateway Deployment
  hosts: master
  become: true
  vars:
    namespace: gateway
    version: 3.2.2
  tasks:
    - name: Enterprise Gateway Configuration
      copy:
        dest: /tmp/gateway.yaml
        mode: 0640
        content: |
          authToken: "{{ lookup('community.hashi_vault.hashi_vault', vault_mount_point + '/data/gateway_authtoken:value', token_validate=true) }}"
          global:
            rbac: true
          deployment:
            replicas: 1
            # serviceAccountName: 'enterprise-gateway-sa'
            terminationGracePeriodSeconds: 60
          ingress:
            enabled: true
            annotations:
              kubernetes.io/ingress.class: "nginx"
              kubernetes.io/tls-acme: "true"
            hostName: "{{ gateway_hostname }}"
            tls:
              - hosts:
                - "{{ gateway_hostname }}"
                secretName: acme-tls-gateway
          kip:
            enabled: true
            # serviceAccountName: 'kernel-image-puller-sa'
            # podSecurityPolicy:
            #   create: true
          service:
            type: "ClusterIP"
            ports:
              # Enterprise Gateway reqursts
              - name: http
                port: 8888
                targetPort: 8888
              # Kernel connection info responses
              - name: http-reponse
                port: 8887
                targetPort: 8887
    - name: Enterprise Gateway Download
      get_url:
        url: "https://github.com/jupyter-server/enterprise_gateway/releases/download/v{{ version }}/jupyter_enterprise_gateway_helm-{{ version }}.tar.gz"
        dest: "/tmp/jupyter_enterprise_gateway_helm-{{ version }}.tar.gz"
        mode: 0644
    - name: Enterprise Gateway Helm
      vars:
        config: >-
          --namespace {{ namespace }}
          --kube-context kubernetes-admin@kubernetes
          -f /tmp/gateway.yaml
          enterprise-gateway
          /tmp/jupyter_enterprise_gateway_helm-{{ version }}.tar.gz
      shell: |-
        helm status --namespace {{ namespace }} enterprise-gateway
        if [ $? -ne 0 ]; then
          kubectl create namespace {{ namespace }} || :
          helm install {{ config }}
        else
          helm upgrade {{ config }}
        fi
      environment:
        KUBECONFIG: /etc/kubernetes/admin.conf
        PATH: /sbin:/bin:/usr/sbin:/usr/bin:/usr/local/bin
      when: true
