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.

128 lines
4.4 KiB

  1. ---
  2. # Wait for upgrade
  3. - name: Confirm node upgrade
  4. pause:
  5. echo: yes
  6. prompt: "Ready to upgrade node ?"
  7. when:
  8. - upgrade_node_confirm
  9. - name: Wait before upgrade node
  10. pause:
  11. seconds: "{{ upgrade_node_pause_seconds }}"
  12. when:
  13. - not upgrade_node_confirm
  14. - upgrade_node_pause_seconds != 0
  15. # Node Ready: type = ready, status = True
  16. # Node NotReady: type = ready, status = Unknown
  17. - name: See if node is in ready state
  18. shell: >-
  19. {{ bin_dir }}/kubectl get node {{ kube_override_hostname|default(inventory_hostname) }}
  20. -o jsonpath='{ range .status.conditions[?(@.type == "Ready")].status }{ @ }{ end }'
  21. register: kubectl_node_ready
  22. delegate_to: "{{ groups['kube_control_plane'][0] }}"
  23. failed_when: false
  24. changed_when: false
  25. # SchedulingDisabled: unschedulable = true
  26. # else unschedulable key doesn't exist
  27. - name: See if node is schedulable
  28. shell: >-
  29. {{ bin_dir }}/kubectl get node {{ kube_override_hostname|default(inventory_hostname) }}
  30. -o jsonpath='{ .spec.unschedulable }'
  31. register: kubectl_node_schedulable
  32. delegate_to: "{{ groups['kube_control_plane'][0] }}"
  33. failed_when: false
  34. changed_when: false
  35. - name: Set if node needs cordoning
  36. set_fact:
  37. needs_cordoning: >-
  38. {% if (kubectl_node_ready.stdout == "True" and not kubectl_node_schedulable.stdout) or upgrade_node_always_cordon -%}
  39. true
  40. {%- else -%}
  41. false
  42. {%- endif %}
  43. - name: Node draining
  44. block:
  45. - name: Cordon node
  46. command: "{{ bin_dir }}/kubectl cordon {{ kube_override_hostname|default(inventory_hostname) }}"
  47. delegate_to: "{{ groups['kube_control_plane'][0] }}"
  48. - name: Check kubectl version
  49. command: "{{ bin_dir }}/kubectl version --client --short"
  50. register: kubectl_version
  51. delegate_to: "{{ groups['kube_control_plane'][0] }}"
  52. run_once: yes
  53. changed_when: false
  54. when:
  55. - drain_nodes
  56. - drain_pod_selector
  57. - name: Ensure minimum version for drain label selector if necessary
  58. assert:
  59. that: "kubectl_version.stdout.split(' ')[-1] is version('v1.10.0', '>=')"
  60. when:
  61. - drain_nodes
  62. - drain_pod_selector
  63. - name: Drain node
  64. command: >-
  65. {{ bin_dir }}/kubectl drain
  66. --force
  67. --ignore-daemonsets
  68. --grace-period {{ hostvars['localhost']['drain_grace_period_after_failure'] | default(drain_grace_period) }}
  69. --timeout {{ hostvars['localhost']['drain_timeout_after_failure'] | default(drain_timeout) }}
  70. --delete-emptydir-data {{ kube_override_hostname|default(inventory_hostname) }}
  71. {% if drain_pod_selector %}--pod-selector '{{ drain_pod_selector }}'{% endif %}
  72. when: drain_nodes
  73. register: result
  74. failed_when:
  75. - result.rc != 0
  76. - not drain_fallback_enabled
  77. until: result.rc == 0
  78. retries: "{{ drain_retries }}"
  79. delay: "{{ drain_retry_delay_seconds }}"
  80. - name: Drain fallback
  81. block:
  82. - name: Set facts after regular drain has failed
  83. set_fact:
  84. drain_grace_period_after_failure: "{{ drain_fallback_grace_period }}"
  85. drain_timeout_after_failure: "{{ drain_fallback_timeout }}"
  86. delegate_to: localhost
  87. delegate_facts: yes
  88. run_once: yes
  89. - name: Drain node - fallback with disabled eviction
  90. command: >-
  91. {{ bin_dir }}/kubectl drain
  92. --force
  93. --ignore-daemonsets
  94. --grace-period {{ drain_fallback_grace_period }}
  95. --timeout {{ drain_fallback_timeout }}
  96. --delete-emptydir-data {{ kube_override_hostname|default(inventory_hostname) }}
  97. {% if drain_pod_selector %}--pod-selector '{{ drain_pod_selector }}'{% endif %}
  98. --disable-eviction
  99. register: drain_fallback_result
  100. until: drain_fallback_result.rc == 0
  101. retries: "{{ drain_fallback_retries }}"
  102. delay: "{{ drain_fallback_retry_delay_seconds }}"
  103. when:
  104. - drain_nodes
  105. - drain_fallback_enabled
  106. - result.rc != 0
  107. rescue:
  108. - name: Set node back to schedulable
  109. command: "{{ bin_dir }}/kubectl --kubeconfig {{ kube_config_dir }}/admin.conf uncordon {{ inventory_hostname }}"
  110. when: upgrade_node_uncordon_after_drain_failure
  111. - name: Fail after rescue
  112. fail:
  113. msg: "Failed to drain node {{ inventory_hostname }}"
  114. when: upgrade_node_fail_if_drain_fails
  115. delegate_to: "{{ groups['kube_control_plane'][0] }}"
  116. when:
  117. - needs_cordoning