From 4bc5e8d912ad0802d82d9a6d30256a7ac7e6ed75 Mon Sep 17 00:00:00 2001 From: Ho Kim Date: Tue, 3 Jan 2023 22:03:32 +0900 Subject: [PATCH] Skip removing nodes if cluster is not set (#9430) --- roles/remove-node/post-remove/tasks/main.yml | 6 ++++-- roles/remove-node/pre-remove/tasks/main.yml | 9 +++++++-- roles/remove-node/remove-etcd-node/tasks/main.yml | 1 + 3 files changed, 12 insertions(+), 4 deletions(-) diff --git a/roles/remove-node/post-remove/tasks/main.yml b/roles/remove-node/post-remove/tasks/main.yml index 36b1e9ff8..ff8a06d84 100644 --- a/roles/remove-node/post-remove/tasks/main.yml +++ b/roles/remove-node/post-remove/tasks/main.yml @@ -2,8 +2,10 @@ - name: remove-node | Delete node command: "{{ kubectl }} delete node {{ kube_override_hostname|default(inventory_hostname) }}" delegate_to: "{{ groups['kube_control_plane']|first }}" - # ignore servers that are not nodes - when: inventory_hostname in groups['k8s_cluster'] and kube_override_hostname|default(inventory_hostname) in nodes.stdout_lines + when: + - groups['kube_control_plane'] | length > 0 + # ignore servers that are not nodes + - inventory_hostname in groups['k8s_cluster'] and kube_override_hostname|default(inventory_hostname) in nodes.stdout_lines retries: "{{ delete_node_retries }}" # Sometimes the api-server can have a short window of indisponibility when we delete a master node delay: "{{ delete_node_delay_seconds }}" diff --git a/roles/remove-node/pre-remove/tasks/main.yml b/roles/remove-node/pre-remove/tasks/main.yml index add5120df..b45e809f8 100644 --- a/roles/remove-node/pre-remove/tasks/main.yml +++ b/roles/remove-node/pre-remove/tasks/main.yml @@ -3,6 +3,8 @@ command: >- {{ kubectl }} get nodes -o go-template={% raw %}'{{ range .items }}{{ .metadata.name }}{{ "\n" }}{{ end }}'{% endraw %} register: nodes + when: + - groups['kube_control_plane'] | length > 0 delegate_to: "{{ groups['kube_control_plane']|first }}" changed_when: false run_once: true @@ -15,8 +17,10 @@ --grace-period {{ drain_grace_period }} --timeout {{ drain_timeout }} --delete-emptydir-data {{ kube_override_hostname|default(inventory_hostname) }} - # ignore servers that are not nodes - when: kube_override_hostname|default(inventory_hostname) in nodes.stdout_lines + when: + - groups['kube_control_plane'] | length > 0 + # ignore servers that are not nodes + - kube_override_hostname|default(inventory_hostname) in nodes.stdout_lines register: result failed_when: result.rc != 0 and not allow_ungraceful_removal delegate_to: "{{ groups['kube_control_plane']|first }}" @@ -34,5 +38,6 @@ retries: 3 delay: "{{ drain_grace_period }}" when: + - groups['kube_control_plane'] | length > 0 - not allow_ungraceful_removal - kube_override_hostname|default(inventory_hostname) in nodes.stdout_lines diff --git a/roles/remove-node/remove-etcd-node/tasks/main.yml b/roles/remove-node/remove-etcd-node/tasks/main.yml index 7500d6d34..e9ef0cf6b 100644 --- a/roles/remove-node/remove-etcd-node/tasks/main.yml +++ b/roles/remove-node/remove-etcd-node/tasks/main.yml @@ -5,6 +5,7 @@ -o jsonpath='{range .status.addresses[?(@.type=="InternalIP")]}{@.address}{"\n"}{end}' register: remove_node_ip when: + - groups['kube_control_plane'] | length > 0 - inventory_hostname in groups['etcd'] - ip is not defined - access_ip is not defined