|
1 | 1 | --- |
2 | | -- name: Get current Kubernetes version |
3 | | - ansible.builtin.shell: kubectl version | grep 'Server Version' | awk '{print $3}' | sed 's/v//' |
4 | | - register: current_k8s_version_raw |
5 | | - |
6 | | -- name: Get target Kubernetes version |
7 | | - ansible.builtin.shell: echo {{ kubernetes_version }} | sed 's/v//' |
8 | | - register: target_k8s_version_raw |
9 | | - |
10 | | -- name: Set version facts |
11 | | - ansible.builtin.set_fact: |
12 | | - current_k8s_version: "{{ current_k8s_version_raw.stdout }}" |
13 | | - k8s_target_version: "{{ target_k8s_version_raw.stdout }}" |
14 | | - |
15 | | -- name: Display current and target versions |
16 | | - ansible.builtin.debug: |
17 | | - msg: | |
18 | | - Current Kubernetes version: {{ current_k8s_version }} |
19 | | - Target Kubernetes version: {{ k8s_target_version }} |
20 | | -
|
21 | | -- name: Parse version numbers |
22 | | - ansible.builtin.set_fact: |
23 | | - current_major: "{{ current_k8s_version.split('.')[0] }}" |
24 | | - current_minor: "{{ current_k8s_version.split('.')[1] }}" |
25 | | - target_major: "{{ k8s_target_version.split('.')[0] }}" |
26 | | - target_minor: "{{ k8s_target_version.split('.')[1] }}" |
27 | | - |
28 | | -# Do we need to fail? |
29 | | -- name: Check if versions are identical |
30 | | - ansible.builtin.fail: |
31 | | - msg: "Current version {{ current_k8s_version }} is already at target version {{ k8s_target_version }}" |
32 | | - when: current_k8s_version == k8s_target_version |
33 | | - |
34 | | -- name: Check if downgrade attempt |
35 | | - ansible.builtin.fail: |
36 | | - msg: | |
37 | | - ❌ Downgrade detected! |
38 | | - Current version: {{ current_k8s_version }} |
39 | | - Target version: {{ k8s_target_version }} |
40 | | - Please downgrade manually using kubeadm! |
41 | | - when: |
42 | | - - (target_major | int < current_major | int) or |
43 | | - (target_major | int == current_major | int and target_minor | int < current_minor | int) |
44 | | - |
45 | | -- name: Validate minor version upgrade (no version skipping) |
46 | | - ansible.builtin.fail: |
47 | | - msg: | |
48 | | - ❌ Invalid upgrade path detected! |
49 | | - Current version: {{ current_k8s_version }} |
50 | | - Target version: {{ k8s_target_version }} |
51 | | - |
52 | | - You are trying to skip minor versions. Kubernetes must be upgraded sequentially. |
53 | | - |
54 | | - Required upgrade path: |
55 | | - {% set minor_diff = (target_minor | int) - (current_minor | int) %} |
56 | | - {% for i in range(minor_diff) %} |
57 | | - {{ current_major }}.{{ (current_minor | int) + i }} → {{ current_major }}.{{ (current_minor | int) + i + 1 }} |
58 | | - {% endfor %} |
59 | | - |
60 | | - Please upgrade one minor version at a time. |
61 | | - Refer to: https://kubernetes.io/docs/tasks/administer-cluster/kubeadm/kubeadm-upgrade/ |
62 | | - when: |
63 | | - - (target_minor | int) - (current_minor | int) > 1 |
64 | | - |
65 | | -- name: Check all nodes are Ready |
66 | | - ansible.builtin.shell: kubectl get nodes --no-headers | grep -v " Ready" | wc -l |
67 | | - register: not_ready_nodes |
68 | | - changed_when: false |
69 | | - failed_when: not_ready_nodes.stdout | int > 0 |
70 | | - |
71 | | -- name: Verify all nodes are Ready |
72 | | - ansible.builtin.debug: |
73 | | - msg: "✅ All nodes are Ready" |
74 | | - |
75 | | -- name: Check system pods status |
76 | | - ansible.builtin.shell: | |
77 | | - kubectl get pods -n kube-system --no-headers | \ |
78 | | - grep -v -E "Running|Completed" | wc -l |
79 | | - register: unhealthy_pods |
80 | | - changed_when: false |
81 | | - failed_when: unhealthy_pods.stdout | int > 0 |
82 | | - |
83 | | -- name: Verify system pods are healthy |
84 | | - ansible.builtin.debug: |
85 | | - msg: "✅ All kube-system pods are Running" |
86 | | - |
87 | | -- name: Install pluto for API deprecation check |
88 | | - ansible.builtin.get_url: |
89 | | - url: "https://github.com/FairwindsOps/pluto/releases/download/v5.19.0/pluto_5.19.0_linux_amd64.tar.gz" |
90 | | - dest: "/tmp/pluto.tar.gz" |
91 | | - mode: '0644' |
92 | | - |
93 | | -- name: Extract pluto |
94 | | - ansible.builtin.unarchive: |
95 | | - src: "/tmp/pluto.tar.gz" |
96 | | - dest: "/tmp/" |
97 | | - remote_src: yes |
98 | | - |
99 | | -- name: Check for deprecated APIs |
100 | | - ansible.builtin.shell: | |
101 | | - /tmp/pluto detect-all-in-cluster --target-versions k8s=v{{ k8s_target_version }} -o wide |
102 | | - register: pluto_check |
103 | | - changed_when: false |
104 | | - failed_when: false |
105 | | - |
106 | | -- name: Display deprecated API warnings |
107 | | - ansible.builtin.debug: |
108 | | - msg: | |
109 | | - {% if 'API REMOVALS' in pluto_check.stdout or pluto_check.stdout | length > 100 %} |
110 | | - ⚠️ WARNING: Deprecated APIs detected! |
111 | | - {{ pluto_check.stdout }} |
112 | | - |
113 | | - Please address these before upgrading to avoid issues. |
114 | | - {% else %} |
115 | | - ✅ No deprecated APIs found |
116 | | - {% endif %} |
117 | | - when: pluto_check.stdout is defined |
118 | | - |
119 | | -- name: Fail if critical API deprecations found |
120 | | - ansible.builtin.fail: |
121 | | - msg: | |
122 | | - ❌ Critical API deprecations detected that will break in {{ k8s_target_version }} |
123 | | - Please remediate before upgrading. |
124 | | - when: |
125 | | - - pluto_check.stdout is defined |
126 | | - - "'REMOVED' in pluto_check.stdout" |
127 | | - |
128 | | -- name: Check Calico/Tigera operator health |
129 | | - ansible.builtin.shell: | |
130 | | - kubectl get tigerastatus -o jsonpath='{.items[*].status.conditions[?(@.type=="Available")].status}' | \ |
131 | | - grep -v "True" | wc -l |
132 | | - register: calico_status |
133 | | - changed_when: false |
134 | | - failed_when: calico_status.stdout | int > 0 |
135 | | - ignore_errors: yes |
136 | | - |
137 | | -- name: Verify Calico is healthy |
138 | | - ansible.builtin.debug: |
139 | | - msg: "✅ Calico/Tigera operator is healthy" |
140 | | - when: calico_status.rc == 0 |
141 | | - |
142 | | -- name: Check Calico pods if tigerastatus not available |
143 | | - ansible.builtin.shell: | |
144 | | - kubectl get pods -n calico-system --no-headers 2>/dev/null | \ |
145 | | - grep -v -E "Running|Completed" | wc -l |
146 | | - register: calico_pods_status |
147 | | - changed_when: false |
148 | | - when: calico_status.rc != 0 |
149 | | - |
150 | | -- name: Alternative Calico namespace check |
151 | | - ansible.builtin.shell: | |
152 | | - kubectl get pods -n tigera-operator --no-headers 2>/dev/null | \ |
153 | | - grep -v -E "Running|Completed" | wc -l |
154 | | - register: tigera_pods_status |
155 | | - changed_when: false |
156 | | - when: calico_status.rc != 0 |
157 | | - |
158 | | -- name: Verify Calico pods (alternative check) |
159 | | - ansible.builtin.debug: |
160 | | - msg: "✅ Calico pods are healthy" |
161 | | - when: |
162 | | - - calico_status.rc != 0 |
163 | | - - (calico_pods_status.stdout | default('0') | int == 0 or tigera_pods_status.stdout | default('0') | int == 0) |
164 | | - |
165 | | -- name: Pre-flight checks summary |
166 | | - ansible.builtin.debug: |
167 | | - msg: | |
168 | | - ==================================== |
169 | | - ✅ All pre-flight checks passed! |
170 | | - ==================================== |
171 | | - Current version: {{ current_k8s_version }} |
172 | | - Target version: {{ k8s_target_version }} |
173 | | - |
174 | | - Proceeding with upgrade... |
| 2 | + |
| 3 | +--- |
| 4 | +- name: Check if Kubernetes cluster is initialized |
| 5 | + ansible.builtin.stat: |
| 6 | + path: /etc/kubernetes/admin.conf |
| 7 | + register: k8s_is_installed |
| 8 | + |
| 9 | +- name: Pre-flight checks block |
| 10 | + when: k8s_is_installed |
| 11 | + block: |
| 12 | + - name: Get current Kubernetes version |
| 13 | + ansible.builtin.shell: kubectl version | grep 'Server Version' | awk '{print $3}' | sed 's/v//' |
| 14 | + register: current_k8s_version_raw |
| 15 | + |
| 16 | + - name: Get target Kubernetes version |
| 17 | + ansible.builtin.shell: echo {{ kubernetes_version }} | sed 's/v//' |
| 18 | + register: target_k8s_version_raw |
| 19 | + |
| 20 | + - name: Set version facts |
| 21 | + ansible.builtin.set_fact: |
| 22 | + current_k8s_version: "{{ current_k8s_version_raw.stdout }}" |
| 23 | + k8s_target_version: "{{ target_k8s_version_raw.stdout }}" |
| 24 | + |
| 25 | + - name: Display current and target versions |
| 26 | + ansible.builtin.debug: |
| 27 | + msg: | |
| 28 | + Current Kubernetes version: {{ current_k8s_version }} |
| 29 | + Target Kubernetes version: {{ k8s_target_version }} |
| 30 | +
|
| 31 | + - name: Parse version numbers |
| 32 | + ansible.builtin.set_fact: |
| 33 | + current_major: "{{ current_k8s_version.split('.')[0] }}" |
| 34 | + current_minor: "{{ current_k8s_version.split('.')[1] }}" |
| 35 | + target_major: "{{ k8s_target_version.split('.')[0] }}" |
| 36 | + target_minor: "{{ k8s_target_version.split('.')[1] }}" |
| 37 | + |
| 38 | + # Do we need to fail? |
| 39 | + - name: Check if versions are identical |
| 40 | + ansible.builtin.fail: |
| 41 | + msg: "Current version {{ current_k8s_version }} is already at target version {{ k8s_target_version }}" |
| 42 | + when: current_k8s_version == k8s_target_version |
| 43 | + |
| 44 | + - name: Check if downgrade attempt |
| 45 | + ansible.builtin.fail: |
| 46 | + msg: | |
| 47 | + ❌ Downgrade detected! |
| 48 | + Current version: {{ current_k8s_version }} |
| 49 | + Target version: {{ k8s_target_version }} |
| 50 | + Please downgrade manually using kubeadm! |
| 51 | + when: |
| 52 | + - (target_major | int < current_major | int) or |
| 53 | + (target_major | int == current_major | int and target_minor | int < current_minor | int) |
| 54 | + |
| 55 | + - name: Validate minor version upgrade (no version skipping) |
| 56 | + ansible.builtin.fail: |
| 57 | + msg: | |
| 58 | + ❌ Invalid upgrade path detected! |
| 59 | + Current version: {{ current_k8s_version }} |
| 60 | + Target version: {{ k8s_target_version }} |
| 61 | + |
| 62 | + You are trying to skip minor versions. Kubernetes must be upgraded sequentially. |
| 63 | + |
| 64 | + Required upgrade path: |
| 65 | + {% set minor_diff = (target_minor | int) - (current_minor | int) %} |
| 66 | + {% for i in range(minor_diff) %} |
| 67 | + {{ current_major }}.{{ (current_minor | int) + i }} → {{ current_major }}.{{ (current_minor | int) + i + 1 }} |
| 68 | + {% endfor %} |
| 69 | + |
| 70 | + Please upgrade one minor version at a time. |
| 71 | + Refer to: https://kubernetes.io/docs/tasks/administer-cluster/kubeadm/kubeadm-upgrade/ |
| 72 | + when: |
| 73 | + - (target_minor | int) - (current_minor | int) > 1 |
| 74 | + |
| 75 | + - name: Check all nodes are Ready |
| 76 | + ansible.builtin.shell: kubectl get nodes --no-headers | grep -v " Ready" | wc -l |
| 77 | + register: not_ready_nodes |
| 78 | + changed_when: false |
| 79 | + failed_when: not_ready_nodes.stdout | int > 0 |
| 80 | + |
| 81 | + - name: Verify all nodes are Ready |
| 82 | + ansible.builtin.debug: |
| 83 | + msg: "✅ All nodes are Ready" |
| 84 | + |
| 85 | + - name: Check system pods status |
| 86 | + ansible.builtin.shell: | |
| 87 | + kubectl get pods -n kube-system --no-headers | \ |
| 88 | + grep -v -E "Running|Completed" | wc -l |
| 89 | + register: unhealthy_pods |
| 90 | + changed_when: false |
| 91 | + failed_when: unhealthy_pods.stdout | int > 0 |
| 92 | + |
| 93 | + - name: Verify system pods are healthy |
| 94 | + ansible.builtin.debug: |
| 95 | + msg: "✅ All kube-system pods are Running" |
| 96 | + |
| 97 | + - name: Install pluto for API deprecation check |
| 98 | + ansible.builtin.get_url: |
| 99 | + url: "https://github.com/FairwindsOps/pluto/releases/download/v5.19.0/pluto_5.19.0_linux_amd64.tar.gz" |
| 100 | + dest: "/tmp/pluto.tar.gz" |
| 101 | + mode: '0644' |
| 102 | + |
| 103 | + - name: Extract pluto |
| 104 | + ansible.builtin.unarchive: |
| 105 | + src: "/tmp/pluto.tar.gz" |
| 106 | + dest: "/tmp/" |
| 107 | + remote_src: yes |
| 108 | + |
| 109 | + - name: Check for deprecated APIs |
| 110 | + ansible.builtin.shell: | |
| 111 | + /tmp/pluto detect-all-in-cluster --target-versions k8s=v{{ k8s_target_version }} -o wide |
| 112 | + register: pluto_check |
| 113 | + changed_when: false |
| 114 | + failed_when: false |
| 115 | + |
| 116 | + - name: Display deprecated API warnings |
| 117 | + ansible.builtin.debug: |
| 118 | + msg: | |
| 119 | + {% if 'API REMOVALS' in pluto_check.stdout or pluto_check.stdout | length > 100 %} |
| 120 | + ⚠️ WARNING: Deprecated APIs detected! |
| 121 | + {{ pluto_check.stdout }} |
| 122 | + |
| 123 | + Please address these before upgrading to avoid issues. |
| 124 | + {% else %} |
| 125 | + ✅ No deprecated APIs found |
| 126 | + {% endif %} |
| 127 | + when: pluto_check.stdout is defined |
| 128 | + |
| 129 | + - name: Fail if critical API deprecations found |
| 130 | + ansible.builtin.fail: |
| 131 | + msg: | |
| 132 | + ❌ Critical API deprecations detected that will break in {{ k8s_target_version }} |
| 133 | + Please remediate before upgrading. |
| 134 | + when: |
| 135 | + - pluto_check.stdout is defined |
| 136 | + - "'REMOVED' in pluto_check.stdout" |
| 137 | + |
| 138 | + - name: Check Calico/Tigera operator health |
| 139 | + ansible.builtin.shell: | |
| 140 | + kubectl get tigerastatus -o jsonpath='{.items[*].status.conditions[?(@.type=="Available")].status}' | \ |
| 141 | + grep -v "True" | wc -l |
| 142 | + register: calico_status |
| 143 | + changed_when: false |
| 144 | + failed_when: calico_status.stdout | int > 0 |
| 145 | + ignore_errors: yes |
| 146 | + |
| 147 | + - name: Verify Calico is healthy |
| 148 | + ansible.builtin.debug: |
| 149 | + msg: "✅ Calico/Tigera operator is healthy" |
| 150 | + when: calico_status.rc == 0 |
| 151 | + |
| 152 | + - name: Check Calico pods if tigerastatus not available |
| 153 | + ansible.builtin.shell: | |
| 154 | + kubectl get pods -n calico-system --no-headers 2>/dev/null | \ |
| 155 | + grep -v -E "Running|Completed" | wc -l |
| 156 | + register: calico_pods_status |
| 157 | + changed_when: false |
| 158 | + when: calico_status.rc != 0 |
| 159 | + |
| 160 | + - name: Alternative Calico namespace check |
| 161 | + ansible.builtin.shell: | |
| 162 | + kubectl get pods -n tigera-operator --no-headers 2>/dev/null | \ |
| 163 | + grep -v -E "Running|Completed" | wc -l |
| 164 | + register: tigera_pods_status |
| 165 | + changed_when: false |
| 166 | + when: calico_status.rc != 0 |
| 167 | + |
| 168 | + - name: Verify Calico pods (alternative check) |
| 169 | + ansible.builtin.debug: |
| 170 | + msg: "✅ Calico pods are healthy" |
| 171 | + when: |
| 172 | + - calico_status.rc != 0 |
| 173 | + - (calico_pods_status.stdout | default('0') | int == 0 or tigera_pods_status.stdout | default('0') | int == 0) |
| 174 | + |
| 175 | + - name: Pre-flight checks summary |
| 176 | + ansible.builtin.debug: |
| 177 | + msg: | |
| 178 | + ==================================== |
| 179 | + ✅ All pre-flight checks passed! |
| 180 | + ==================================== |
| 181 | + Current version: {{ current_k8s_version }} |
| 182 | + Target version: {{ k8s_target_version }} |
| 183 | + |
| 184 | + Proceeding with upgrade... |
0 commit comments