---

- name: "Create pod"
  tags: create
  hosts: localhost
  vars:
    pod_count: 10
    loop_count: 5
  gather_facts: no
  tasks:
    - name: Create a test pod
      k8s:
        state: present
        definition:
          apiVersion: v1
          kind: Pod
          metadata:
            name: test-pod-{{item}}
            namespace: default
          spec:
            containers:
            - name: python2
              image: python:2
              args: [ "sleep", "100000" ]
            - name: python3
              image: python:3
              args: [ "sleep", "100000" ]
      loop: "{{ range(pod_count|int)|list }}"

    - name: "Wait pod to be running"
      debug: { msg: "pod is running" }
      # status and availableReplicas might not be there. Using default value (d(default_value))
      until: "pod_def.status.containerStatuses[0].ready"
      # Waiting 100 s
      retries: 50
      delay: 2
      vars:
        pod_def: "{{lookup('k8s', kind='Pod', namespace='default', resource_name='test-pod-' ~ item)}}"
      loop: "{{ range(pod_count|int)|list }}"

    - name: "Add pod to pods group"
      add_host:
        name: "test-pod-{{item}}"
        groups: [ "pods" ]
        ansible_connection: "kubectl"
      changed_when: no
      tags: "always"
      loop: "{{ range(pod_count|int)|list }}"

- name: "Test kubectl connection (default strategy)"
  tags: default
  hosts: pods
  strategy: "linear"
  vars:
    pod_count: 10
    loop_count: 5
  gather_facts: no
  tasks:
    - name: "Simple shell with linear"
      shell: ls /tmp
      loop: "{{ range(loop_count|int)|list }}"

    - name: "Simple file with linear"
      file:
        path: "/etc"
        state: directory
      loop: "{{ range(loop_count|int)|list }}"

    - block:
        - name: "Check python version on python3 container"
          command: python --version
          vars:
            ansible_kubectl_container: python3
          register: _

        - assert: { that: "'Python 3' in _.stdout" }
          fail_msg: |
            _={{ _ }}

        - debug: var=_.stdout,_.stderr
          run_once: yes

        - name: "Check python version on default container"
          command: python --version
          register: _

        - assert: { that: "'Python 2' in _.stderr" }
          fail_msg: |
            _={{ _ }}

        - debug: var=_.stdout,_.stderr
          run_once: yes

- name: "Test kubectl connection (mitogen strategy)"
  tags: mitogen
  hosts: pods
  strategy: "mitogen_linear"
  vars:
    pod_count: 10
    loop_count: 5
  gather_facts: no
  tasks:
    - name: "Simple shell with mitogen"
      shell: ls /tmp
      loop: "{{ range(loop_count|int)|list }}"

    - name: "Simple file with mitogen"
      file:
        path: "/etc"
        state: directory
      loop: "{{ range(loop_count|int)|list }}"

    - block:
        - name: "Check python version on python3 container"
          command: python --version
          vars:
            ansible_kubectl_container: python3
          register: _

        - assert:
            that: "'Python 3' in _.stdout"
            fail_msg: |
              _={{ _ }}

        - debug: var=_.stdout,_.stderr
          run_once: yes

        - name: "Check python version on default container"
          command: python --version
          register: _

        - assert:
            that: "'Python 2' in _.stderr"
            fail_msg: |
              _={{ _ }}

        - debug: var=_.stdout,_.stderr
          run_once: yes
      tags: check

- name: "Destroy pod"
  tags: cleanup
  hosts: pods
  gather_facts: no
  vars:
    ansible_connection: "local"
  tasks:
    - name: Destroy pod
      k8s:
        state: absent
        definition:
          apiVersion: v1
          kind: Pod
          metadata:
            name: "{{inventory_hostname}}"
            namespace: default
