blob: 02797280e4f5d9caa95b75c2c222d9acd5f2bb32 [file] [log] [blame]
Victor Ryzhenkinef34a022018-06-22 19:36:13 +04001/**
2 * Update kuberentes cluster
3 *
4 * Expected parameters:
5 * SALT_MASTER_CREDENTIALS Credentials to the Salt API.
6 * SALT_MASTER_URL Full Salt API address [https://10.10.10.1:8000].
7 * KUBERNETES_HYPERKUBE_IMAGE Target kubernetes version. May be null in case of reclass-system rollout
8 * KUBERNETES_PAUSE_IMAGE Kubernetes pause image should have same version as hyperkube. May be null in case of reclass-system rollout
9 * TARGET_UPDATES Comma separated list of nodes to update (Valid values are ctl,cmp)
10 * CTL_TARGET Salt targeted kubernetes CTL nodes (ex. I@kubernetes:master). Kubernetes control plane
11 * CMP_TARGET Salt targeted compute nodes (ex. cmp* and 'I@kubernetes:pool') Kubernetes computes
12 * PER_NODE Target nodes will be managed one by one (bool)
Victor Ryzhenkin42e4b382018-09-11 17:57:56 +040013 * SIMPLE_UPGRADE Use previous version of upgrade without conron/drain abilities
14 * UPGRADE_DOCKER Upgrade docker component
Victor Ryzhenkinae22a5a2018-10-12 15:52:27 +040015 * CONFORMANCE_RUN_AFTER Run Kubernetes conformance tests after update
16 * CONFORMANCE_RUN_BEFORE Run Kubernetes conformance tests before update
17 * TEST_K8S_API_SERVER Kubernetes API server address for test execution
18 * ARTIFACTORY_URL Artifactory URL where docker images located. Needed to correctly fetch conformance images.
Aleksei Kasatkinff9d5b52018-10-26 11:47:46 +020019 * UPGRADE_CALICO_V2_TO_V3 Perform Calico upgrade from v2 to v3.
20 * KUBERNETES_CALICO_IMAGE Target calico/node image. May be null in case of reclass-system rollout.
21 * KUBERNETES_CALICO_CALICOCTL_IMAGE Target calico/ctl image. May be null in case of reclass-system rollout.
22 * KUBERNETES_CALICO_CNI_IMAGE Target calico/cni image. May be null in case of reclass-system rollout.
23 * KUBERNETES_CALICO_KUBE_CONTROLLERS_IMAGE Target calico/kube-controllers image. May be null in case of reclass-system rollout.
24 * CALICO_UPGRADE_VERSION Version of "calico-upgrade" utility to be used ("v1.0.5" for Calico v3.1.3 target).
Victor Ryzhenkinef34a022018-06-22 19:36:13 +040025 *
26**/
27def common = new com.mirantis.mk.Common()
28def salt = new com.mirantis.mk.Salt()
29def python = new com.mirantis.mk.Python()
30
31def updates = TARGET_UPDATES.tokenize(",").collect{it -> it.trim()}
32def pepperEnv = "pepperEnv"
33
Aleksei Kasatkinff9d5b52018-10-26 11:47:46 +020034def POOL = "I@kubernetes:pool"
35def calicoImagesValid = false
36
Victor Ryzhenkinef34a022018-06-22 19:36:13 +040037def overrideKubernetesImage(pepperEnv) {
38 def salt = new com.mirantis.mk.Salt()
39
40 def k8sSaltOverrides = """
41 kubernetes_hyperkube_image: ${KUBERNETES_HYPERKUBE_IMAGE}
42 kubernetes_pause_image: ${KUBERNETES_PAUSE_IMAGE}
43 """
44 stage("Override kubernetes images to target version") {
45 salt.setSaltOverrides(pepperEnv, k8sSaltOverrides)
46 }
47}
48
Aleksei Kasatkinff9d5b52018-10-26 11:47:46 +020049def overrideCalicoImages(pepperEnv) {
50 def salt = new com.mirantis.mk.Salt()
51
52 def calicoSaltOverrides = """
53 kubernetes_calico_image: ${KUBERNETES_CALICO_IMAGE}
54 kubernetes_calico_calicoctl_image: ${KUBERNETES_CALICO_CALICOCTL_IMAGE}
55 kubernetes_calico_cni_image: ${KUBERNETES_CALICO_CNI_IMAGE}
56 kubernetes_calico_kube_controllers_image: ${KUBERNETES_CALICO_KUBE_CONTROLLERS_IMAGE}
57 """
58 stage("Override calico images to target version") {
59 salt.setSaltOverrides(pepperEnv, calicoSaltOverrides)
60 }
61}
62
63def downloadCalicoUpgrader(pepperEnv, target) {
64 def salt = new com.mirantis.mk.Salt()
65
66 stage("Downloading calico-upgrade utility") {
67 salt.cmdRun(pepperEnv, target, "rm -f ./calico-upgrade")
68 salt.cmdRun(pepperEnv, target, "wget https://github.com/projectcalico/calico-upgrade/releases/download/${CALICO_UPGRADE_VERSION}/calico-upgrade")
69 salt.cmdRun(pepperEnv, target, "chmod +x ./calico-upgrade")
70 }
71}
72
73def pullCalicoImages(pepperEnv, target) {
74 def salt = new com.mirantis.mk.Salt()
75
76 stage("Pulling updated Calico docker images") {
77 salt.cmdRun(pepperEnv, target, "docker pull ${KUBERNETES_CALICO_IMAGE}")
78 salt.cmdRun(pepperEnv, target, "docker pull ${KUBERNETES_CALICO_CALICOCTL_IMAGE}")
79 salt.cmdRun(pepperEnv, target, "docker pull ${KUBERNETES_CALICO_CNI_IMAGE}")
80 salt.cmdRun(pepperEnv, target, "docker pull ${KUBERNETES_CALICO_KUBE_CONTROLLERS_IMAGE}")
81 }
82}
83
Victor Ryzhenkinef34a022018-06-22 19:36:13 +040084def performKubernetesComputeUpdate(pepperEnv, target) {
85 def salt = new com.mirantis.mk.Salt()
86
87 stage("Execute Kubernetes compute update on ${target}") {
88 salt.enforceState(pepperEnv, target, 'kubernetes.pool')
89 salt.runSaltProcessStep(pepperEnv, target, 'service.restart', ['kubelet'])
90 }
91}
92
93def performKubernetesControlUpdate(pepperEnv, target) {
94 def salt = new com.mirantis.mk.Salt()
95
96 stage("Execute Kubernetes control plane update on ${target}") {
97 salt.enforceStateWithExclude(pepperEnv, target, "kubernetes", "kubernetes.master.setup")
98 // Restart kubelet
99 salt.runSaltProcessStep(pepperEnv, target, 'service.restart', ['kubelet'])
100 }
101}
102
Aleksei Kasatkinff9d5b52018-10-26 11:47:46 +0200103def startCalicoUpgrade(pepperEnv, target) {
104 def salt = new com.mirantis.mk.Salt()
105
106 stage("Starting upgrade using calico-upgrade: migrate etcd schema and lock Calico") {
107 // get ETCD_ENDPOINTS in use by Calico
108 def ep_str = salt.cmdRun(pepperEnv, target, "cat /etc/calico/calicoctl.cfg | grep etcdEndpoints")['return'][0].values()[0]
109 def ETCD_ENDPOINTS = ep_str.tokenize(' ')[1]
110 print("ETCD_ENDPOINTS in use by Calico: '${ETCD_ENDPOINTS}'")
111
112 def cmd = "export APIV1_ETCD_ENDPOINTS=${ETCD_ENDPOINTS} && " +
113 "export APIV1_ETCD_CA_CERT_FILE=/var/lib/etcd/ca.pem && " +
114 "export APIV1_ETCD_CERT_FILE=/var/lib/etcd/etcd-client.crt && " +
115 "export APIV1_ETCD_KEY_FILE=/var/lib/etcd/etcd-client.key && " +
116 "export ETCD_ENDPOINTS=${ETCD_ENDPOINTS} && " +
117 "export ETCD_CA_CERT_FILE=/var/lib/etcd/ca.pem && " +
118 "export ETCD_CERT_FILE=/var/lib/etcd/etcd-client.crt && " +
119 "export ETCD_KEY_FILE=/var/lib/etcd/etcd-client.key && " +
120 "rm /root/upg_complete -f && " +
121 "./calico-upgrade start --no-prompts --ignore-v3-data > upgrade-start.log && " +
122 "until [ -f /root/upg_complete ]; do sleep 0.1; done && " +
123 "./calico-upgrade complete --no-prompts > upgrade-complete.log && " +
124 "rm /root/upg_complete -f"
125 // "saltArgs = ['async']" doesn't work, so we have to run "cmd.run --async"
126 salt.cmdRun(pepperEnv, "I@salt:master", "salt -C '${target}' cmd.run '${cmd}' --async")
127 salt.cmdRun(pepperEnv, target, "until [ -f /root/upgrade-start.log ]; do sleep 0.1; done")
128 }
129}
130
131def completeCalicoUpgrade(pepperEnv, target) {
132 def salt = new com.mirantis.mk.Salt()
133
134 stage("Complete upgrade using calico-upgrade: unlock Calico") {
135 salt.cmdRun(pepperEnv, target, "echo 'true' > /root/upg_complete")
136 salt.cmdRun(pepperEnv, target, "while [ -f /root/upg_complete ]; do sleep 0.1; done")
137 salt.cmdRun(pepperEnv, target, "cat /root/upgrade-start.log")
138 salt.cmdRun(pepperEnv, target, "cat /root/upgrade-complete.log")
139 }
140}
141
142def performCalicoConfigurationUpdateAndServicesRestart(pepperEnv, target) {
143 def salt = new com.mirantis.mk.Salt()
144
145 stage("Performing Calico configuration update and services restart") {
146 salt.enforceState(pepperEnv, target, "kubernetes.pool.calico")
147 salt.runSaltProcessStep(pepperEnv, target, 'service.restart', ['kubelet'])
148 }
149}
150
Victor Ryzhenkin42e4b382018-09-11 17:57:56 +0400151def cordonNode(pepperEnv, target) {
152 def salt = new com.mirantis.mk.Salt()
153 def originalTarget = "I@kubernetes:master and not ${target}"
154
155 stage("Cordoning ${target} kubernetes node") {
156 def nodeShortName = target.tokenize(".")[0]
157 salt.cmdRun(pepperEnv, originalTarget, "kubectl cordon ${nodeShortName}", true, 1)
158 }
159}
160
161def uncordonNode(pepperEnv, target) {
162 def salt = new com.mirantis.mk.Salt()
163 def originalTarget = "I@kubernetes:master and not ${target}"
164
165 stage("Uncordoning ${target} kubernetes node") {
166 def nodeShortName = target.tokenize(".")[0]
167 salt.cmdRun(pepperEnv, originalTarget, "kubectl uncordon ${nodeShortName}", true, 1)
168 }
169}
170
171def drainNode(pepperEnv, target) {
172 def salt = new com.mirantis.mk.Salt()
173 def originalTarget = "I@kubernetes:master and not ${target}"
174
175 stage("Draining ${target} kubernetes node") {
176 def nodeShortName = target.tokenize(".")[0]
177 salt.cmdRun(pepperEnv, originalTarget, "kubectl drain --force --ignore-daemonsets --grace-period 100 --timeout 300s --delete-local-data ${nodeShortName}", true, 1)
178 }
179}
180
181def regenerateCerts(pepperEnv, target) {
182 def salt = new com.mirantis.mk.Salt()
183
184 stage("Regenerate certs for ${target}") {
185 salt.enforceState(pepperEnv, target, 'salt.minion.cert')
186 }
187}
188
Victor Ryzhenkinae909182018-10-02 17:49:18 +0400189def updateAddons(pepperEnv, target) {
190 def salt = new com.mirantis.mk.Salt()
191
192 stage("Upgrading Addons at ${target}") {
Victor Ryzhenkin40625bc2018-10-04 16:15:27 +0400193 salt.enforceState(pepperEnv, target, "kubernetes.master.kube-addons")
Victor Ryzhenkinfd9677f2018-10-16 16:14:40 +0400194 }
195}
196
197def updateAddonManager(pepperEnv, target) {
198 def salt = new com.mirantis.mk.Salt()
199
200 stage("Upgrading AddonManager at ${target}") {
Victor Ryzhenkinae909182018-10-02 17:49:18 +0400201 salt.enforceState(pepperEnv, target, "kubernetes.master.setup")
202 }
203}
204
Victor Ryzhenkin42e4b382018-09-11 17:57:56 +0400205def upgradeDocker(pepperEnv, target) {
206 def salt = new com.mirantis.mk.Salt()
207
208 stage("Upgrading docker at ${target}") {
209 salt.enforceState(pepperEnv, target, 'docker.host')
210 }
211}
Victor Ryzhenkinef34a022018-06-22 19:36:13 +0400212
Victor Ryzhenkinae22a5a2018-10-12 15:52:27 +0400213def runConformance(pepperEnv, target, k8s_api, image) {
214 def salt = new com.mirantis.mk.Salt()
215 def containerName = 'conformance_tests'
216 output_file = image.replaceAll('/', '-') + '.output'
217 def output_file_full_path = "/tmp/" + image.replaceAll('/', '-') + '.output'
218 def artifacts_dir = '_artifacts/'
219 salt.cmdRun(pepperEnv, target, "docker rm -f ${containerName}", false)
220 salt.cmdRun(pepperEnv, target, "docker run -d --name ${containerName} --net=host -e API_SERVER=${k8s_api} ${image}")
221 sleep(10)
222
223 print("Waiting for tests to run...")
224 salt.runSaltProcessStep(pepperEnv, target, 'cmd.run', ["docker wait ${containerName}"], null, false)
225
226 print("Writing test results to output file...")
227 salt.runSaltProcessStep(pepperEnv, target, 'cmd.run', ["docker logs -t ${containerName} > ${output_file_full_path}"])
228 print("Conformance test output saved in " + output_file_full_path)
229
230 // collect output
231 sh "mkdir -p ${artifacts_dir}"
232 file_content = salt.getFileContent(pepperEnv, target, '/tmp/' + output_file)
233 writeFile file: "${artifacts_dir}${output_file}", text: file_content
234 sh "cat ${artifacts_dir}${output_file}"
235 try {
236 sh "cat ${artifacts_dir}${output_file} | grep 'Test Suite Failed' && exit 1 || exit 0"
237 } catch (Throwable e) {
238 print("Conformance tests failed. Please check output")
239 currentBuild.result = "FAILURE"
240 currentBuild.description = currentBuild.description ? e.message + " " + currentBuild.description : e.message
241 throw e
242 }
243}
244
245def buildImageURL(pepperEnv, target, mcp_repo) {
246 def salt = new com.mirantis.mk.Salt()
247 def raw_version = salt.cmdRun(pepperEnv, target, "kubectl version --short -o json")['return'][0].values()[0].replaceAll('Salt command execution success','')
248 print("Kubernetes version: " + raw_version)
249 def serialized_version = readJSON text: raw_version
250 def short_version = (serialized_version.serverVersion.gitVersion =~ /([v])(\d+\.)(\d+\.)(\d+\-)(\d+)/)[0][0]
251 print("Kubernetes short version: " + short_version)
252 def conformance_image = mcp_repo + "/mirantis/kubernetes/k8s-conformance:" + short_version
253 return conformance_image
254}
255
256def executeConformance(pepperEnv, target, k8s_api, mcp_repo) {
257 stage("Running conformance tests") {
258 def image = buildImageURL(pepperEnv, target, mcp_repo)
259 print("Using image: " + image)
260 runConformance(pepperEnv, target, k8s_api, image)
261 }
262}
263
Aleksei Kasatkinff9d5b52018-10-26 11:47:46 +0200264def checkCalicoUpgradeSuccessful(pepperEnv, target) {
265 def salt = new com.mirantis.mk.Salt()
266
267 stage("Checking cluster state after Calico upgrade") {
268 // TODO add auto-check of results
269 salt.cmdRun(pepperEnv, target, "calicoctl version | grep -i version")
270 salt.cmdRun(pepperEnv, target, "calicoctl node status")
271 }
272}
Victor Ryzhenkinae22a5a2018-10-12 15:52:27 +0400273
Victor Ryzhenkinef34a022018-06-22 19:36:13 +0400274timeout(time: 12, unit: 'HOURS') {
275 node() {
276 try {
277
278 stage("Setup virtualenv for Pepper") {
279 python.setupPepperVirtualenv(pepperEnv, SALT_MASTER_URL, SALT_MASTER_CREDENTIALS)
280 }
281
Victor Ryzhenkinae22a5a2018-10-12 15:52:27 +0400282 if (CONFORMANCE_RUN_BEFORE.toBoolean()) {
283 def target = CTL_TARGET
284 def mcp_repo = ARTIFACTORY_URL
285 def k8s_api = TEST_K8S_API_SERVER
286 firstTarget = salt.getFirstMinion(pepperEnv, target)
287 executeConformance(pepperEnv, firstTarget, k8s_api, mcp_repo)
288 }
289
Victor Ryzhenkinef34a022018-06-22 19:36:13 +0400290 if ((common.validInputParam('KUBERNETES_HYPERKUBE_IMAGE')) && (common.validInputParam('KUBERNETES_PAUSE_IMAGE'))) {
291 overrideKubernetesImage(pepperEnv)
292 }
293
Aleksei Kasatkinff9d5b52018-10-26 11:47:46 +0200294 if ((common.validInputParam('KUBERNETES_CALICO_IMAGE'))
295 && (common.validInputParam('KUBERNETES_CALICO_CALICOCTL_IMAGE'))
296 && (common.validInputParam('KUBERNETES_CALICO_CNI_IMAGE'))
297 && (common.validInputParam('KUBERNETES_CALICO_KUBE_CONTROLLERS_IMAGE'))
298 ) {
299 calicoImagesValid = true
300 overrideCalicoImages(pepperEnv)
301 }
302
Victor Ryzhenkinef34a022018-06-22 19:36:13 +0400303 /*
Aleksei Kasatkinff9d5b52018-10-26 11:47:46 +0200304 * Execute Calico upgrade if needed (only for v2 to v3 upgrade).
305 * This part causes workloads operations downtime.
306 * It is only required for Calico v2.x to v3.x upgrade when etcd is in use for Calico
307 * as Calico etcd schema has different formats for Calico v2.x and Calico v3.x.
308 */
309 if (UPGRADE_CALICO_V2_TO_V3.toBoolean()) {
310 // one CTL node will be used for running upgrade of Calico etcd schema
311 def ctl_node = salt.getMinionsSorted(pepperEnv, CTL_TARGET)[0]
312
313 // prepare for upgrade. when done in advance, this will decrease downtime during upgrade
314 downloadCalicoUpgrader(pepperEnv, ctl_node)
315 if (calicoImagesValid) {
316 pullCalicoImages(pepperEnv, POOL)
317 }
318
319 // this sequence implies workloads operations downtime
320 startCalicoUpgrade(pepperEnv, ctl_node)
321 performCalicoConfigurationUpdateAndServicesRestart(pepperEnv, POOL)
322 completeCalicoUpgrade(pepperEnv, ctl_node)
323 // after that no downtime is expected
324
325 checkCalicoUpgradeSuccessful(pepperEnv, POOL)
326 }
327
328 /*
329 * Execute k8s update
Victor Ryzhenkinef34a022018-06-22 19:36:13 +0400330 */
331 if (updates.contains("ctl")) {
332 def target = CTL_TARGET
333
334 if (PER_NODE.toBoolean()) {
335 def targetHosts = salt.getMinionsSorted(pepperEnv, target)
336
337 for (t in targetHosts) {
Victor Ryzhenkin42e4b382018-09-11 17:57:56 +0400338 if (SIMPLE_UPGRADE.toBoolean()) {
339 performKubernetesControlUpdate(pepperEnv, t)
340 } else {
341 cordonNode(pepperEnv, t)
342 drainNode(pepperEnv, t)
343 regenerateCerts(pepperEnv, t)
344 if (UPGRADE_DOCKER.toBoolean()) {
345 upgradeDocker(pepperEnv, t)
346 }
347 performKubernetesControlUpdate(pepperEnv, t)
Victor Ryzhenkinfd9677f2018-10-16 16:14:40 +0400348 updateAddonManager(pepperEnv, t)
Victor Ryzhenkin42e4b382018-09-11 17:57:56 +0400349 uncordonNode(pepperEnv, t)
350 }
Victor Ryzhenkinef34a022018-06-22 19:36:13 +0400351 }
352 } else {
353 performKubernetesControlUpdate(pepperEnv, target)
354 }
Victor Ryzhenkinfd9677f2018-10-16 16:14:40 +0400355 if (!SIMPLE_UPGRADE.toBoolean()) {
Aleksei Kasatkinff9d5b52018-10-26 11:47:46 +0200356 // Addons upgrade should be performed after all nodes will be upgraded
Victor Ryzhenkinfd9677f2018-10-16 16:14:40 +0400357 updateAddons(pepperEnv, target)
358 // Wait for 90 sec for addons reconciling
359 sleep(90)
360 }
Victor Ryzhenkinef34a022018-06-22 19:36:13 +0400361 }
362
363 if (updates.contains("cmp")) {
364 def target = CMP_TARGET
365
366 if (PER_NODE.toBoolean()) {
367 def targetHosts = salt.getMinionsSorted(pepperEnv, target)
368
369 for (t in targetHosts) {
Victor Ryzhenkin42e4b382018-09-11 17:57:56 +0400370 if (SIMPLE_UPGRADE.toBoolean()) {
371 performKubernetesComputeUpdate(pepperEnv, t)
372 } else {
373 cordonNode(pepperEnv, t)
374 drainNode(pepperEnv, t)
375 regenerateCerts(pepperEnv, t)
376 if (UPGRADE_DOCKER.toBoolean()) {
377 upgradeDocker(pepperEnv, t)
378 }
379 performKubernetesComputeUpdate(pepperEnv, t)
380 uncordonNode(pepperEnv, t)
381 }
Victor Ryzhenkinef34a022018-06-22 19:36:13 +0400382 }
383 } else {
384 performKubernetesComputeUpdate(pepperEnv, target)
385 }
386 }
Victor Ryzhenkinae22a5a2018-10-12 15:52:27 +0400387
388 if (CONFORMANCE_RUN_AFTER.toBoolean()) {
389 def target = CTL_TARGET
390 def mcp_repo = ARTIFACTORY_URL
391 def k8s_api = TEST_K8S_API_SERVER
392 firstTarget = salt.getFirstMinion(pepperEnv, target)
393 executeConformance(pepperEnv, firstTarget, k8s_api, mcp_repo)
394 }
Victor Ryzhenkinef34a022018-06-22 19:36:13 +0400395 } catch (Throwable e) {
396 // If there was an error or exception thrown, the build failed
397 currentBuild.result = "FAILURE"
398 currentBuild.description = currentBuild.description ? e.message + " " + currentBuild.description : e.message
399 throw e
400 }
401 }
402}