---
- name: Restart nodes
  hosts: oo_nodes_to_config
  serial: "{{ openshift_restart_nodes_serial | default(1) }}"

  roles:
  - lib_openshift
  - openshift_facts

  tasks:
  - name: Mark node unschedulable
    oc_adm_manage_node:
      node: "{{ openshift.node.nodename | lower }}"
      schedulable: False
    delegate_to: "{{ groups.oo_first_master.0 }}"
    retries: 10
    delay: 5
    register: node_unschedulable
    until: node_unschedulable is succeeded
    when:
    - inventory_hostname in groups.oo_nodes_to_config
    - openshift_node_restart_drain | default(false) | bool

  - name: Drain Node for Kubelet restart
    command: >
      {{ hostvars[groups.oo_first_master.0]['first_master_client_binary'] }} adm drain {{ openshift.node.nodename | lower }}
      --config={{ openshift.common.config_base }}/master/admin.kubeconfig
      --force --delete-local-data --ignore-daemonsets
      --timeout={{ openshift_upgrade_nodes_drain_timeout | default(0) }}s
    delegate_to: "{{ groups.oo_first_master.0 }}"
    when:
    - inventory_hostname in groups.oo_nodes_to_config
    - openshift_node_restart_drain | default(false) | bool
    register: l_drain_result
    until: not (l_drain_result is failed)
    retries: "{{ 1 if ( openshift_upgrade_nodes_drain_timeout | default(0) | int ) == 0 else 0 }}"
    delay: 5
    failed_when:
    - l_drain_result is failed
    - openshift_upgrade_nodes_drain_timeout | default(0) | int == 0

  - name: Restart docker
    service:
      name: docker
      state: restarted
    register: l_docker_restart_docker_in_node_result
    until: not (l_docker_restart_docker_in_node_result is failed)
    retries: 3
    delay: 30
    when:
    - openshift_node_restart_docker_required | default(True)
    - not openshift_use_crio_only | bool

  - name: Restart openvswitch service
    service:
      name: openvswitch
      state: started
    when:
    - openshift_is_containerized | bool
    - openshift_use_openshift_sdn | bool

  - name: Restart containerized services
    service:
      name: "{{ item }}"
      state: started
    with_items:
    - etcd_container
    - "{{ openshift_service_type }}-master-api"
    - "{{ openshift_service_type }}-master-controllers"
    - "{{ openshift_service_type }}-node"
    failed_when: false
    when: openshift_is_containerized | bool

  - name: Wait for master API to come back online
    wait_for:
      host: "{{ openshift.common.hostname }}"
      state: started
      delay: 10
      port: "{{ openshift.master.api_port }}"
      timeout: 600
    when: inventory_hostname in groups.oo_masters_to_config

  - name: restart node
    service:
      name: "{{ openshift_service_type }}-node"
      state: restarted

  - name: Wait for node to be ready
    oc_obj:
      state: list
      kind: node
      name: "{{ openshift.common.hostname | lower }}"
    register: node_output
    delegate_to: "{{ groups.oo_first_master.0 }}"
    when: inventory_hostname in groups.oo_nodes_to_config
    until:
    - node_output.results is defined
    - node_output.results.returncode is defined
    - node_output.results.returncode == 0
    - node_output.results.results is defined
    - node_output.results.results | length > 0
    - node_output.results.results[0].status is defined
    - node_output.results.results[0].status.conditions is defined
    - node_output.results.results[0].status.conditions | selectattr('type', 'match', '^Ready$') | map(attribute='status') | join | bool == True
    # Give the node three minutes to come back online.
    retries: 24
    delay: 5

  - name: Set node schedulability
    oc_adm_manage_node:
      node: "{{ openshift.node.nodename | lower }}"
      schedulable: True
    delegate_to: "{{ groups.oo_first_master.0 }}"
    retries: 10
    delay: 5
    register: node_schedulable
    until: node_schedulable is succeeded
    when:
    - node_unschedulable is changed
    - openshift_node_restart_drain | default(false) | bool
