---

- name: Verify
  hosts: localhost
  connection: local
  collections:
    - community.kubernetes

  vars:
    ansible_python_interpreter: '{{ ansible_playbook_python }}'
    deploy_dir: "{{ lookup('env', 'MOLECULE_PROJECT_DIRECTORY') }}/deploy"
    custom_resource: "{{ lookup('file', '/'.join([deploy_dir, 'crds/ansible.example.com_v1alpha1_memcached_cr.yaml'])) | from_yaml }}"

  tasks:
    - block:
      - name: debug memcached lookup
        debug:
          var: deploy
        vars:
          deploy: '{{ lookup("k8s",
            kind="Deployment",
            api_version="apps/v1",
            namespace=namespace,
            label_selector="app=memcached"
          )}}'

      - name: Wait 2 minutes for memcached deployment
        debug:
          var: deploy
        until:
        - deploy is defined
        - deploy.status is defined
        - deploy.status.replicas is defined
        - deploy.status.replicas == deploy.status.get("availableReplicas", 0)
        retries: 12
        delay: 10
        vars:
          deploy: '{{ lookup("k8s",
            kind="Deployment",
            api_version="apps/v1",
            namespace=namespace,
            label_selector="app=memcached"
          )}}'

      - name: Create ConfigMap that the Operator should delete
        k8s:
          definition:
            apiVersion: v1
            kind: ConfigMap
            metadata:
              name: deleteme
              namespace: '{{ namespace }}'
            data:
              delete: me

      - name: Verify custom status exists
        assert:
          that: debug_cr.status.get("test") == "hello world"
        vars:
          debug_cr: '{{ lookup("k8s",
            kind=custom_resource.kind,
            api_version=custom_resource.apiVersion,
            namespace=namespace,
            resource_name=custom_resource.metadata.name
          )}}'

      # This will verify that the `secret` role was executed
      - name: Verify that test-service was created
        assert:
          that: lookup('k8s', kind='Service', api_version='v1', namespace=namespace, resource_name='test-service')

      - name: Verify that project testing-foo was created
        assert:
          that: lookup('k8s', kind='Namespace', api_version='v1', resource_name='testing-foo')
        when: "'project.openshift.io' in lookup('k8s', cluster_info='api_groups')"

      - when: molecule_yml.scenario.name == "test-local"
        block:
        - name: Restart the operator by killing the pod
          k8s:
            state: absent
            definition:
              api_version: v1
              kind: Pod
              metadata:
                namespace: '{{ namespace }}'
                name: '{{ pod.metadata.name }}'
          vars:
            pod: '{{ q("k8s", api_version="v1", kind="Pod", namespace=namespace, label_selector="name=memcached-operator").0 }}'

        - name: Wait 2 minutes for operator deployment
          debug:
            var: deploy
          until:
          - deploy is defined
          - deploy.status is defined
          - deploy.status.replicas is defined
          - deploy.status.replicas == deploy.status.get("availableReplicas", 0)
          retries: 12
          delay: 10
          vars:
            deploy: '{{ lookup("k8s",
              kind="Deployment",
              api_version="apps/v1",
              namespace=namespace,
              resource_name="memcached-operator"
            )}}'

        - name: Wait for reconciliation to have a chance at finishing
          pause:
            seconds:  15

        - name: Delete the service that is created.
          k8s:
            kind: Service
            api_version: v1
            namespace: '{{ namespace }}'
            name: test-service
            state: absent

        - name: Verify that test-service was re-created
          debug:
            var: service
          until: service
          retries: 12
          delay: 10
          vars:
            service: '{{ lookup("k8s",
              kind="Service",
              api_version="v1",
              namespace=namespace,
              resource_name="test-service",
            )}}'

      - name: Delete the custom resource
        k8s:
          state: absent
          namespace: '{{ namespace }}'
          definition: '{{ custom_resource }}'

      - name: Wait for the custom resource to be deleted
        k8s_info:
          api_version: '{{ custom_resource.apiVersion }}'
          kind: '{{ custom_resource.kind }}'
          namespace: '{{ namespace }}'
          name: '{{ custom_resource.metadata.name }}'
        register: cr
        retries: 10
        delay: 6
        until: not cr.resources
        failed_when: cr.resources

      - name: Verify the Deployment was deleted (wait 30s)
        assert:
          that: not lookup('k8s', kind='Deployment', api_version='apps/v1', namespace=namespace, label_selector='app=memcached')
        retries: 10
        delay: 3

      - name: Add operator pod to inventory
        add_host:
          name: '{{ pod.metadata.name }}'
          groups: operator
          ansible_connection: kubectl
          ansible_remote_tmp: /tmp/ansible
          kubectl_namespace: '{{ namespace }}'
        when: molecule_yml.scenario.name == "test-local"
        vars:
          pod: '{{ q("k8s", api_version="v1", kind="Pod", namespace=namespace, label_selector="name=memcached-operator").0 }}'
      rescue:
      - name: debug cr
        ignore_errors: yes
        failed_when: false
        debug:
          var: debug_cr
        vars:
          debug_cr: '{{ lookup("k8s",
            kind=custom_resource.kind,
            api_version=custom_resource.apiVersion,
            namespace=namespace,
            resource_name=custom_resource.metadata.name
          )}}'

      - name: debug memcached lookup
        ignore_errors: yes
        failed_when: false
        debug:
          var: deploy
        vars:
          deploy: '{{ lookup("k8s",
            kind="Deployment",
            api_version="apps/v1",
            namespace=namespace,
            label_selector="app=memcached"
          )}}'

      - name: get operator logs
        ignore_errors: yes
        failed_when: false
        command: kubectl logs deployment/{{ definition.metadata.name }} -n {{ namespace }}
        vars:
          definition: "{{ lookup('file', '/'.join([deploy_dir, 'operator.yaml'])) | from_yaml }}"
        register: log

      - debug: var=log.stdout_lines

      - fail:
          msg: "Failed in verify.yml"

- hosts: operator
  gather_facts: no
  become: false
  tasks:
  - name: Output latest log
    command: cat /tmp/ansible-operator/runner/ansible.example.com/v1alpha1/Memcached/{{ namespace }}/example-memcached/artifacts/latest/stdout
    register: ansible_log
  - debug: var=ansible_log.stdout_lines
