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

  roles:
  - lib_openshift
  - openshift_facts

  tasks:
  - name: Check for GlusterFS project
    command: >
      {{ hostvars[groups.oo_first_master.0]['first_master_client_binary'] }} get project {{ openshift_storage_glusterfs_namespace }}
      --config={{ openshift.common.config_base }}/master/admin.kubeconfig
      --no-headers
    register: glusterfs_check
    ignore_errors: true
    delegate_to: "{{ groups.oo_first_master.0 }}"
    when:
    - openshift_storage_glusterfs_namespace is defined
    - openshift_storage_glusterfs_namespace != ""
    - groups.oo_glusterfs_to_config | default([]) | count > 0

  - name: Check Gluster Health Before Restart
    include_role:
      name: openshift_storage_glusterfs
      tasks_from: check_cluster_health.yml
    when:
    - openshift_storage_glusterfs_namespace is defined
    - openshift_storage_glusterfs_namespace != ""
    - groups.oo_glusterfs_to_config | default([]) | count > 0
    - glusterfs_check.rc == 0

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

  - name: Drain Node for Kubelet restart
    command: >
      {{ hostvars[groups.oo_first_master.0]['first_master_client_binary'] }} adm drain {{ l_kubelet_node_name | 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:
    - openshift_node_restart_drain | default(false) | bool
    - inventory_hostname in groups.oo_nodes_to_config
    register: l_docker_upgrade_drain_result
    until: not (l_docker_upgrade_drain_result is failed)
    retries: "{{ 1 if ( openshift_upgrade_nodes_drain_timeout | default(0) | int ) == 0 else 0 }}"
    delay: 5
    failed_when:
    - l_docker_upgrade_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

  - import_role:
      name: openshift_node
      tasks_from: sdn_delete.yml
    when:
    - openshift_use_crio | default(false) | bool
    - openshift_node_restart_drain | default(false) | 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: "{{ l_kubelet_node_name | lower }}"
    register: node_output
    delegate_to: "{{ groups.oo_first_master.0 }}"
    when: inventory_hostname in groups.oo_nodes_to_config
    until:
    - node_output.module_results is defined
    - node_output.module_results.returncode is defined
    - node_output.module_results.returncode == 0
    - node_output.module_results.results is defined
    - node_output.module_results.results | length > 0
    - node_output.module_results.results[0].status is defined
    - node_output.module_results.results[0].status.conditions is defined
    - node_output.module_results.results[0].status.conditions | selectattr('type', 'match', '^Ready$') | map(attribute='status') | join | bool == True
    # Give the node nine minutes to come back online.
    retries: 36
    delay: 15

  - name: Set node schedulability
    oc_adm_manage_node:
      node: "{{ l_kubelet_node_name | 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

  - name: Check Gluster Health After Restart
    include_role:
      name: openshift_storage_glusterfs
      tasks_from: check_cluster_health.yml
    when:
    - openshift_storage_glusterfs_namespace is defined
    - openshift_storage_glusterfs_namespace != ""
    - groups.oo_glusterfs_to_config | default([]) | count > 0
    - glusterfs_check.rc == 0
