Skip to content

Commit 1f831a4

Browse files
authored
Merge pull request #28 from kishen-v/patch-os
Introduce tasks to patch OS with latest updates
2 parents cdc829f + 6324af7 commit 1f831a4

File tree

3 files changed

+87
-0
lines changed

3 files changed

+87
-0
lines changed
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
- name: Update nodes with latest patches and updates
2+
hosts:
3+
- masters
4+
- workers
5+
roles:
6+
- role: update-node-os
7+
8+
- name: Reboot Kubernetes nodes one-by-one
9+
hosts:
10+
- masters
11+
- workers
12+
serial: 1
13+
become: yes
14+
roles:
15+
- role: reboot-sequentially
Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
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
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
- name: Update packages and kernel to latest available versions
2+
package:
3+
name: '*'
4+
state: latest
5+
when: ansible_pkg_mgr in ['yum', 'dnf']
6+
7+
- name: Check if reboot required
8+
shell: needs-restarting -r
9+
register: reboot_check
10+
ignore_errors: yes
11+
when: ansible_distribution in ['CentOS', 'RedHat']

0 commit comments

Comments
 (0)