---
- name: Node Join Checkpoint Start
  hosts: all
  gather_facts: false
  tasks:
  - name: Set Node Join 'In Progress'
    run_once: true
    set_stats:
      data:
        installer_phase_node_join:
          title: "Node Join"
          playbook: "playbooks/openshift-node/join.yml"
          status: "In Progress"
          start: "{{ lookup('pipe', 'date +%Y%m%d%H%M%SZ') }}"

- name: Distribute bootstrap and start nodes
  hosts: oo_nodes_to_bootstrap
  gather_facts: no
  tasks:
  - import_role:
      name: openshift_node
      tasks_from: distribute_bootstrap.yml

- name: Approve any pending CSR requests from inventory nodes
  hosts: oo_first_master
  gather_facts: no
  tasks:
  - name: Dump all candidate bootstrap hostnames
    debug:
      msg: "{{ groups['oo_nodes_to_bootstrap'] | default([]) }}"

  - name: Find all hostnames for bootstrapping
    set_fact:
      l_nodes_to_join: "{{ groups['oo_nodes_to_bootstrap'] | default([]) | map('extract', hostvars) | map(attribute='openshift.node.nodename') | list }}"

  - name: Dump the bootstrap hostnames
    debug:
      msg: "{{ l_nodes_to_join }}"

  - name: Approve bootstrap nodes
    oc_adm_csr:
      nodes: "{{ l_nodes_to_join }}"
      timeout: 60
      fail_on_timeout: true
    register: approve_out
    ignore_errors: true
    when:
    - l_nodes_to_join|length > 0

  - when: approve_out is failed
    block:
    - name: Get CSRs
      command: >
        {{ openshift_client_binary }} describe csr --config={{ openshift.common.config_base }}/master/admin.kubeconfig
    - name: Report approval errors
      fail:
        msg: Node approval failed

- name: Ensure any inventory labels are applied to the nodes
  hosts: oo_nodes_to_bootstrap
  vars:
    openshift_node_master_api_url: "{{ hostvars[groups.oo_first_master.0].openshift.master.api_url }}"
  roles:
  - role: openshift_manage_node
    openshift_master_host: "{{ groups.oo_first_master.0 }}"
    openshift_manage_node_is_master: "{{ ('oo_masters_to_config' in group_names) | bool }}"

- name: Node Join Checkpoint End
  hosts: all
  gather_facts: false
  tasks:
  - name: Set Node Join 'Complete'
    run_once: true
    set_stats:
      data:
        installer_phase_node_join:
          status: "Complete"
          end: "{{ lookup('pipe', 'date +%Y%m%d%H%M%SZ') }}"
