You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

75 lines
2.5 KiB

  1. ---
  2. # Node Ready: type = ready, status = True
  3. # Node NotReady: type = ready, status = Unknown
  4. - name: See if node is in ready state
  5. shell: >-
  6. {{ bin_dir }}/kubectl get node {{ kube_override_hostname|default(inventory_hostname) }}
  7. -o jsonpath='{ range .status.conditions[?(@.type == "Ready")].status }{ @ }{ end }'
  8. register: kubectl_node_ready
  9. delegate_to: "{{ groups['kube-master'][0] }}"
  10. failed_when: false
  11. changed_when: false
  12. # SchedulingDisabled: unschedulable = true
  13. # else unschedulable key doesn't exist
  14. - name: See if node is schedulable
  15. shell: >-
  16. {{ bin_dir }}/kubectl get node {{ kube_override_hostname|default(inventory_hostname) }}
  17. -o jsonpath='{ .spec.unschedulable }'
  18. register: kubectl_node_schedulable
  19. delegate_to: "{{ groups['kube-master'][0] }}"
  20. failed_when: false
  21. changed_when: false
  22. - name: Set if node needs cordoning
  23. set_fact:
  24. needs_cordoning: >-
  25. {% if kubectl_node_ready.stdout == "True" and not kubectl_node_schedulable.stdout -%}
  26. true
  27. {%- else -%}
  28. false
  29. {%- endif %}
  30. - name: Node draining
  31. block:
  32. - name: Cordon node
  33. command: "{{ bin_dir }}/kubectl cordon {{ kube_override_hostname|default(inventory_hostname) }}"
  34. delegate_to: "{{ groups['kube-master'][0] }}"
  35. - name: Check kubectl version
  36. command: "{{ bin_dir }}/kubectl version --client --short"
  37. register: kubectl_version
  38. delegate_to: "{{ groups['kube-master'][0] }}"
  39. run_once: yes
  40. changed_when: false
  41. when:
  42. - drain_nodes
  43. - drain_pod_selector
  44. - name: Ensure minimum version for drain label selector if necessary
  45. assert:
  46. that: "kubectl_version.stdout.split(' ')[-1] is version('v1.10.0', '>=')"
  47. when:
  48. - drain_nodes
  49. - drain_pod_selector
  50. - name: Drain node
  51. command: >-
  52. {{ bin_dir }}/kubectl drain
  53. --force
  54. --ignore-daemonsets
  55. --grace-period {{ drain_grace_period }}
  56. --timeout {{ drain_timeout }}
  57. --delete-local-data {{ kube_override_hostname|default(inventory_hostname) }}
  58. {% if drain_pod_selector %}--pod-selector '{{ drain_pod_selector }}'{% endif %}
  59. when:
  60. - drain_nodes
  61. rescue:
  62. - name: Set node back to schedulable
  63. command: "{{ bin_dir }}/kubectl --kubeconfig /etc/kubernetes/admin.conf uncordon {{ inventory_hostname }}"
  64. - name: Fail after rescue
  65. fail:
  66. msg: "Failed to drain node {{ inventory_hostname }}"
  67. delegate_to: "{{ groups['kube-master'][0] }}"
  68. when:
  69. - needs_cordoning