- hosts: localhost
  vars:
    - oc_cluster_url: ""
    - oc_username: ""
    - oc_password: ""
  tasks:
    - name: Print Variables
      debug:
        msg: "OpenShift url: {{ oc_cluster_url }}, OpenShift username: {{ oc_username }}, OpenShift password: {{ oc_password }}"

    - name: Try to login to OCP cluster
      k8s_auth:
        host: "{{ oc_cluster_url }}"
        username: "{{ oc_username }}"
        password: "{{ oc_password }}"
        validate_certs: no
      register: k8s_auth_result

    - name: Create a Namespace
      k8s:
        state: present
        host: "{{ oc_cluster_url }}"
        api_key: "{{ k8s_auth_result.k8s_auth.api_key }}"
        validate_certs: no
        definition:
          apiVersion: v1
          kind: Namespace
          metadata:
            name: kerberos
      register: k8s_namespace_result

    - name: Create a Persistent volume claim
      k8s:
        state: present
        host: "{{ oc_cluster_url }}"
        api_key: "{{ k8s_auth_result.k8s_auth.api_key }}"
        validate_certs: no
        namespace: kerberos
        definition:
          apiVersion: v1
          kind: PersistentVolumeClaim
          metadata:
            name: kerberos-data
          spec:
            accessModes:
              - ReadWriteOnce
            resources:
              requests:
                storage: 2Gi
            storageClassName: managed-premium
      register: pvc_result

    - name: Deploy Kerberos Agent
      k8s:
        state: present
        apply: yes
        namespace: kerberos
        host: "{{ oc_cluster_url }}"
        api_key: "{{ k8s_auth_result.k8s_auth.api_key }}"
        validate_certs: no
        definition:
          apiVersion: apps/v1
          kind: Deployment
          metadata:
            name: agent
            labels:
              name: agent
          spec:
            selector:
              matchLabels:
                app: agent
            replicas: 1
            template:
              metadata:
                labels:
                  app: agent
              spec:
                volumes:
                  - name: kerberos-data
                    persistentVolumeClaim:
                      claimName: kerberos-data

                initContainers:
                  - name: download-config
                    image: kerberos/agent:latest
                    volumeMounts:
                      - name: kerberos-data
                        mountPath: /home/agent/data/config
                        subPath: config
                    command:
                      [
                        "cp",
                        "/home/agent/data/config.template.json",
                        "/home/agent/data/config/config.json",
                      ]

                containers:
                  - name: agent
                    image: kerberos/agent:latest
                    volumeMounts:
                      - name: kerberos-data
                        mountPath: /home/agent/data/config
                        subPath: config
                      - name: kerberos-data
                        mountPath: /home/agent/data/recordings
                        subPath: recordings
                      - name: kerberos-data
                        mountPath: /home/agent/data/snapshots
                        subPath: snapshots
                      - name: kerberos-data
                        mountPath: /home/agent/data/cloud
                        subPath: cloud
                    ports:
                      - containerPort: 80
                        protocol: TCP

    - name: Expose Kerberos Agent
      k8s:
        state: present
        apply: yes
        namespace: kerberos
        host: "{{ oc_cluster_url }}"
        api_key: "{{ k8s_auth_result.k8s_auth.api_key }}"
        validate_certs: no
        definition:
          kind: Service
          apiVersion: v1
          metadata:
            name: agent-svc
            labels:
              name: agent-svc
          spec:
            selector:
              app: agent
            type: LoadBalancer
            ports:
              - name: http
                port: 80
                targetPort: 80
                protocol: TCP
