---
- name: Filter list of nodes to be upgraded if necessary
  hosts: oo_first_master

  roles:
  - lib_openshift

  tasks:
  - when: openshift_upgrade_nodes_label is defined
    block:
    - name: Retrieve list of openshift nodes matching upgrade label
      oc_obj:
        state: list
        kind: node
        selector: "{{ openshift_upgrade_nodes_label }}"
      register: nodes_to_upgrade

    - name: Fail if no nodes match openshift_upgrade_nodes_label
      fail:
        msg: "openshift_upgrade_nodes_label was specified but no nodes matched"
      when: nodes_to_upgrade.module_results.results[0]['items'] | length == 0

    # We got a list of nodes with the label, now we need to match these with inventory hosts
    # using their openshift.common.hostname fact.
    - name: Map labelled nodes to inventory hosts
      add_host:
        name: "{{ item }}"
        groups: temp_nodes_to_upgrade
        ansible_ssh_user: "{{ g_ssh_user | default(omit) }}"
        ansible_become: "{{ g_sudo | default(omit) }}"
      with_items: " {{ groups['oo_nodes_to_config'] }}"
      when:
      - hostvars[item].l_kubelet_node_name | lower in nodes_to_upgrade.module_results.results[0]['items'] | map(attribute='metadata.name') | list
      changed_when: false

    # Make sure the filtering based on hostname worked
    # to avoid issues as seen in https://bugzilla.redhat.com/show_bug.cgi?id=1649074
    - name: Fail if temp_nodes_to_upgrade is empty with openshift_upgrade_nodes_label
      fail:
        msg: "openshift_upgrade_nodes_label was specified but no nodes matched after filtering (check the hostnames of the node)"
      when: ( groups["temp_nodes_to_upgrade"] | default([])) | length == 0

  # Build up the oo_nodes_to_upgrade group, use the list filtered by label if
  # present, otherwise hit all nodes:
  - name: Evaluate oo_nodes_to_upgrade
    add_host:
      name: "{{ item }}"
      groups: oo_nodes_to_upgrade
      ansible_ssh_user: "{{ g_ssh_user | default(omit) }}"
      ansible_become: "{{ g_sudo | default(omit) }}"
    when: item not in not_nodes
    vars:
      not_nodes: "{{ groups['oo_etcd_to_config'] | difference( groups['oo_masters'] | union(groups['oo_nodes_to_config']) ) }}"
    with_items: "{{ groups['temp_nodes_to_upgrade'] | default(groups['oo_nodes_to_config']) }}"
    changed_when: False
