|
| 1 | +- block: |
| 2 | + - name: Resolve Kubernetes node name from inventory IP |
| 3 | + shell: | |
| 4 | + kubectl get nodes -o jsonpath="{range .items[*]}{.metadata.name} {.status.addresses[?(@.type=='InternalIP')].address}{'\n'}{end}" --kubeconfig {{ kubeconfig_path }} |\ |
| 5 | + grep {{ inventory_hostname }} | awk '{print $1}' |
| 6 | + register: node_name |
| 7 | + delegate_to: "{{ groups['masters'][0] }}" |
| 8 | + |
| 9 | + - name: Cordon the kubernetes node |
| 10 | + shell: | |
| 11 | + kubectl cordon {{ node_name.stdout }} |
| 12 | + register: drain_output |
| 13 | + changed_when: "'already cordoned' not in drain_output.stdout" |
| 14 | + delegate_to: "{{ groups['masters'][0] }}" |
| 15 | + |
| 16 | + - name: Check and wait if there are any running jobs that need to complete before draining. |
| 17 | + shell: | |
| 18 | + kubectl get pods -n test-pods \ |
| 19 | + --kubeconfig {{ kubeconfig_path }} \ |
| 20 | + --field-selector spec.nodeName={{ node_name.stdout }},status.phase=Running \ |
| 21 | + -o go-template={% raw %}'{{range .items}}{{if or (not .metadata.ownerReferences) (ne (index .metadata.ownerReferences 0).kind "DaemonSet")}}{{.metadata.name}}{{"\n"}} {{end}}{{end}}'{% endraw %} \ |
| 22 | + | wc -l |
| 23 | + register: running_pod_count |
| 24 | + retries: 360 |
| 25 | + delay: 30 |
| 26 | + until: running_pod_count.stdout | int == 0 |
| 27 | + delegate_to: "{{ groups['masters'][0] }}" |
| 28 | + |
| 29 | + - name: Drain Kubernetes Node |
| 30 | + shell: | |
| 31 | + kubectl drain {{ node_name.stdout }} --ignore-daemonsets --delete-emptydir-data --kubeconfig {{ kubeconfig_path }} |
| 32 | + register: drain_output |
| 33 | + changed_when: "'already cordoned' not in drain_output.stdout" |
| 34 | + delegate_to: "{{ groups['masters'][0] }}" |
| 35 | + |
| 36 | + - name: Wait for all pods to be evicted |
| 37 | + shell: | |
| 38 | + kubectl get pods -n test-pods --field-selector spec.nodeName={{ node_name.stdout }},status.phase=Running -o go-template='{% raw %}{{range .items}}{{if or (not .metadata.ownerReferences) (ne (index .metadata.ownerReferences 0).kind "DaemonSet")}}{{.metadata.name}}{{"\\n"}}{{end}}{{end}}{% endraw %}' | wc -l |
| 39 | + register: pods_remaining |
| 40 | + until: pods_remaining.stdout | int == 0 |
| 41 | + retries: 10 |
| 42 | + delay: 15 |
| 43 | + delegate_to: "{{ groups['masters'][0] }}" |
| 44 | + |
| 45 | + - name: Reboot node |
| 46 | + reboot: |
| 47 | + |
| 48 | + - name: Wait for node to become Ready |
| 49 | + shell: | |
| 50 | + kubectl get node {{ node_name.stdout }} --kubeconfig {{ kubeconfig_path }} -o jsonpath='{.status.conditions[?(@.type=="Ready")].status}' |
| 51 | + register: node_status |
| 52 | + until: node_status.stdout == "True" |
| 53 | + retries: 20 |
| 54 | + delay: 15 |
| 55 | + delegate_to: "{{ groups['masters'][0] }}" |
| 56 | + |
| 57 | + - name: Uncordon the node |
| 58 | + shell: kubectl uncordon {{ node_name.stdout }} --kubeconfig {{ kubeconfig_path }} |
| 59 | + delegate_to: "{{ groups['masters'][0] }}" |
| 60 | + |
| 61 | + when: reboot_check is defined and reboot_check.rc == 1 |
0 commit comments