blob: f9a16e075d6bff662ebbe2dba2aecd187c37f683 [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**/
Aleksei Kasatkin9ce11842018-11-23 14:27:33 +010027import groovy.json.JsonSlurper
28
Victor Ryzhenkinef34a022018-06-22 19:36:13 +040029def common = new com.mirantis.mk.Common()
30def salt = new com.mirantis.mk.Salt()
31def python = new com.mirantis.mk.Python()
32
33def updates = TARGET_UPDATES.tokenize(",").collect{it -> it.trim()}
34def pepperEnv = "pepperEnv"
35
Aleksei Kasatkinff9d5b52018-10-26 11:47:46 +020036def POOL = "I@kubernetes:pool"
37def calicoImagesValid = false
38
Aleksei Kasatkin1f4f5ba2018-11-20 18:30:36 +010039ETCD_ENDPOINTS = ""
40
Victor Ryzhenkinef34a022018-06-22 19:36:13 +040041def overrideKubernetesImage(pepperEnv) {
42 def salt = new com.mirantis.mk.Salt()
43
44 def k8sSaltOverrides = """
45 kubernetes_hyperkube_image: ${KUBERNETES_HYPERKUBE_IMAGE}
46 kubernetes_pause_image: ${KUBERNETES_PAUSE_IMAGE}
47 """
48 stage("Override kubernetes images to target version") {
49 salt.setSaltOverrides(pepperEnv, k8sSaltOverrides)
50 }
51}
52
Aleksei Kasatkinff9d5b52018-10-26 11:47:46 +020053def overrideCalicoImages(pepperEnv) {
54 def salt = new com.mirantis.mk.Salt()
55
56 def calicoSaltOverrides = """
57 kubernetes_calico_image: ${KUBERNETES_CALICO_IMAGE}
58 kubernetes_calico_calicoctl_image: ${KUBERNETES_CALICO_CALICOCTL_IMAGE}
59 kubernetes_calico_cni_image: ${KUBERNETES_CALICO_CNI_IMAGE}
60 kubernetes_calico_kube_controllers_image: ${KUBERNETES_CALICO_KUBE_CONTROLLERS_IMAGE}
61 """
62 stage("Override calico images to target version") {
63 salt.setSaltOverrides(pepperEnv, calicoSaltOverrides)
64 }
65}
66
67def downloadCalicoUpgrader(pepperEnv, target) {
68 def salt = new com.mirantis.mk.Salt()
69
70 stage("Downloading calico-upgrade utility") {
71 salt.cmdRun(pepperEnv, target, "rm -f ./calico-upgrade")
72 salt.cmdRun(pepperEnv, target, "wget https://github.com/projectcalico/calico-upgrade/releases/download/${CALICO_UPGRADE_VERSION}/calico-upgrade")
73 salt.cmdRun(pepperEnv, target, "chmod +x ./calico-upgrade")
74 }
75}
76
77def pullCalicoImages(pepperEnv, target) {
78 def salt = new com.mirantis.mk.Salt()
79
80 stage("Pulling updated Calico docker images") {
81 salt.cmdRun(pepperEnv, target, "docker pull ${KUBERNETES_CALICO_IMAGE}")
82 salt.cmdRun(pepperEnv, target, "docker pull ${KUBERNETES_CALICO_CALICOCTL_IMAGE}")
83 salt.cmdRun(pepperEnv, target, "docker pull ${KUBERNETES_CALICO_CNI_IMAGE}")
84 salt.cmdRun(pepperEnv, target, "docker pull ${KUBERNETES_CALICO_KUBE_CONTROLLERS_IMAGE}")
85 }
86}
87
Victor Ryzhenkinef34a022018-06-22 19:36:13 +040088def performKubernetesComputeUpdate(pepperEnv, target) {
89 def salt = new com.mirantis.mk.Salt()
90
91 stage("Execute Kubernetes compute update on ${target}") {
92 salt.enforceState(pepperEnv, target, 'kubernetes.pool')
93 salt.runSaltProcessStep(pepperEnv, target, 'service.restart', ['kubelet'])
94 }
95}
96
97def performKubernetesControlUpdate(pepperEnv, target) {
98 def salt = new com.mirantis.mk.Salt()
99
100 stage("Execute Kubernetes control plane update on ${target}") {
101 salt.enforceStateWithExclude(pepperEnv, target, "kubernetes", "kubernetes.master.setup")
102 // Restart kubelet
103 salt.runSaltProcessStep(pepperEnv, target, 'service.restart', ['kubelet'])
104 }
105}
106
Aleksei Kasatkinff9d5b52018-10-26 11:47:46 +0200107def startCalicoUpgrade(pepperEnv, target) {
108 def salt = new com.mirantis.mk.Salt()
109
110 stage("Starting upgrade using calico-upgrade: migrate etcd schema and lock Calico") {
Aleksei Kasatkinff9d5b52018-10-26 11:47:46 +0200111 def cmd = "export APIV1_ETCD_ENDPOINTS=${ETCD_ENDPOINTS} && " +
112 "export APIV1_ETCD_CA_CERT_FILE=/var/lib/etcd/ca.pem && " +
113 "export APIV1_ETCD_CERT_FILE=/var/lib/etcd/etcd-client.crt && " +
114 "export APIV1_ETCD_KEY_FILE=/var/lib/etcd/etcd-client.key && " +
115 "export ETCD_ENDPOINTS=${ETCD_ENDPOINTS} && " +
116 "export ETCD_CA_CERT_FILE=/var/lib/etcd/ca.pem && " +
117 "export ETCD_CERT_FILE=/var/lib/etcd/etcd-client.crt && " +
118 "export ETCD_KEY_FILE=/var/lib/etcd/etcd-client.key && " +
119 "rm /root/upg_complete -f && " +
120 "./calico-upgrade start --no-prompts --ignore-v3-data > upgrade-start.log && " +
121 "until [ -f /root/upg_complete ]; do sleep 0.1; done && " +
122 "./calico-upgrade complete --no-prompts > upgrade-complete.log && " +
123 "rm /root/upg_complete -f"
124 // "saltArgs = ['async']" doesn't work, so we have to run "cmd.run --async"
125 salt.cmdRun(pepperEnv, "I@salt:master", "salt -C '${target}' cmd.run '${cmd}' --async")
126 salt.cmdRun(pepperEnv, target, "until [ -f /root/upgrade-start.log ]; do sleep 0.1; done")
127 }
128}
129
130def completeCalicoUpgrade(pepperEnv, target) {
131 def salt = new com.mirantis.mk.Salt()
132
133 stage("Complete upgrade using calico-upgrade: unlock Calico") {
134 salt.cmdRun(pepperEnv, target, "echo 'true' > /root/upg_complete")
135 salt.cmdRun(pepperEnv, target, "while [ -f /root/upg_complete ]; do sleep 0.1; done")
136 salt.cmdRun(pepperEnv, target, "cat /root/upgrade-start.log")
137 salt.cmdRun(pepperEnv, target, "cat /root/upgrade-complete.log")
138 }
139}
140
141def performCalicoConfigurationUpdateAndServicesRestart(pepperEnv, target) {
142 def salt = new com.mirantis.mk.Salt()
143
144 stage("Performing Calico configuration update and services restart") {
145 salt.enforceState(pepperEnv, target, "kubernetes.pool.calico")
146 salt.runSaltProcessStep(pepperEnv, target, 'service.restart', ['kubelet'])
147 }
148}
149
Victor Ryzhenkin42e4b382018-09-11 17:57:56 +0400150def cordonNode(pepperEnv, target) {
151 def salt = new com.mirantis.mk.Salt()
152 def originalTarget = "I@kubernetes:master and not ${target}"
153
154 stage("Cordoning ${target} kubernetes node") {
155 def nodeShortName = target.tokenize(".")[0]
156 salt.cmdRun(pepperEnv, originalTarget, "kubectl cordon ${nodeShortName}", true, 1)
157 }
158}
159
160def uncordonNode(pepperEnv, target) {
161 def salt = new com.mirantis.mk.Salt()
162 def originalTarget = "I@kubernetes:master and not ${target}"
163
164 stage("Uncordoning ${target} kubernetes node") {
165 def nodeShortName = target.tokenize(".")[0]
166 salt.cmdRun(pepperEnv, originalTarget, "kubectl uncordon ${nodeShortName}", true, 1)
167 }
168}
169
170def drainNode(pepperEnv, target) {
171 def salt = new com.mirantis.mk.Salt()
172 def originalTarget = "I@kubernetes:master and not ${target}"
173
174 stage("Draining ${target} kubernetes node") {
175 def nodeShortName = target.tokenize(".")[0]
176 salt.cmdRun(pepperEnv, originalTarget, "kubectl drain --force --ignore-daemonsets --grace-period 100 --timeout 300s --delete-local-data ${nodeShortName}", true, 1)
177 }
178}
179
180def regenerateCerts(pepperEnv, target) {
181 def salt = new com.mirantis.mk.Salt()
182
183 stage("Regenerate certs for ${target}") {
184 salt.enforceState(pepperEnv, target, 'salt.minion.cert')
185 }
186}
187
Victor Ryzhenkinae909182018-10-02 17:49:18 +0400188def updateAddons(pepperEnv, target) {
189 def salt = new com.mirantis.mk.Salt()
190
191 stage("Upgrading Addons at ${target}") {
Victor Ryzhenkin40625bc2018-10-04 16:15:27 +0400192 salt.enforceState(pepperEnv, target, "kubernetes.master.kube-addons")
Victor Ryzhenkinfd9677f2018-10-16 16:14:40 +0400193 }
194}
195
196def updateAddonManager(pepperEnv, target) {
197 def salt = new com.mirantis.mk.Salt()
198
199 stage("Upgrading AddonManager at ${target}") {
Victor Ryzhenkinae909182018-10-02 17:49:18 +0400200 salt.enforceState(pepperEnv, target, "kubernetes.master.setup")
201 }
202}
203
Victor Ryzhenkin42e4b382018-09-11 17:57:56 +0400204def upgradeDocker(pepperEnv, target) {
205 def salt = new com.mirantis.mk.Salt()
206
207 stage("Upgrading docker at ${target}") {
208 salt.enforceState(pepperEnv, target, 'docker.host')
209 }
210}
Victor Ryzhenkinef34a022018-06-22 19:36:13 +0400211
Victor Ryzhenkinae22a5a2018-10-12 15:52:27 +0400212def runConformance(pepperEnv, target, k8s_api, image) {
213 def salt = new com.mirantis.mk.Salt()
214 def containerName = 'conformance_tests'
215 output_file = image.replaceAll('/', '-') + '.output'
216 def output_file_full_path = "/tmp/" + image.replaceAll('/', '-') + '.output'
217 def artifacts_dir = '_artifacts/'
218 salt.cmdRun(pepperEnv, target, "docker rm -f ${containerName}", false)
219 salt.cmdRun(pepperEnv, target, "docker run -d --name ${containerName} --net=host -e API_SERVER=${k8s_api} ${image}")
220 sleep(10)
221
222 print("Waiting for tests to run...")
223 salt.runSaltProcessStep(pepperEnv, target, 'cmd.run', ["docker wait ${containerName}"], null, false)
224
225 print("Writing test results to output file...")
226 salt.runSaltProcessStep(pepperEnv, target, 'cmd.run', ["docker logs -t ${containerName} > ${output_file_full_path}"])
227 print("Conformance test output saved in " + output_file_full_path)
228
229 // collect output
230 sh "mkdir -p ${artifacts_dir}"
231 file_content = salt.getFileContent(pepperEnv, target, '/tmp/' + output_file)
232 writeFile file: "${artifacts_dir}${output_file}", text: file_content
233 sh "cat ${artifacts_dir}${output_file}"
234 try {
235 sh "cat ${artifacts_dir}${output_file} | grep 'Test Suite Failed' && exit 1 || exit 0"
236 } catch (Throwable e) {
237 print("Conformance tests failed. Please check output")
238 currentBuild.result = "FAILURE"
239 currentBuild.description = currentBuild.description ? e.message + " " + currentBuild.description : e.message
240 throw e
241 }
242}
243
244def buildImageURL(pepperEnv, target, mcp_repo) {
245 def salt = new com.mirantis.mk.Salt()
246 def raw_version = salt.cmdRun(pepperEnv, target, "kubectl version --short -o json")['return'][0].values()[0].replaceAll('Salt command execution success','')
247 print("Kubernetes version: " + raw_version)
248 def serialized_version = readJSON text: raw_version
249 def short_version = (serialized_version.serverVersion.gitVersion =~ /([v])(\d+\.)(\d+\.)(\d+\-)(\d+)/)[0][0]
250 print("Kubernetes short version: " + short_version)
251 def conformance_image = mcp_repo + "/mirantis/kubernetes/k8s-conformance:" + short_version
252 return conformance_image
253}
254
255def executeConformance(pepperEnv, target, k8s_api, mcp_repo) {
256 stage("Running conformance tests") {
257 def image = buildImageURL(pepperEnv, target, mcp_repo)
258 print("Using image: " + image)
259 runConformance(pepperEnv, target, k8s_api, image)
260 }
261}
262
Aleksei Kasatkinff9d5b52018-10-26 11:47:46 +0200263def checkCalicoUpgradeSuccessful(pepperEnv, target) {
264 def salt = new com.mirantis.mk.Salt()
265
266 stage("Checking cluster state after Calico upgrade") {
267 // TODO add auto-check of results
268 salt.cmdRun(pepperEnv, target, "calicoctl version | grep -i version")
269 salt.cmdRun(pepperEnv, target, "calicoctl node status")
Aleksei Kasatkin1f4f5ba2018-11-20 18:30:36 +0100270 salt.cmdRun(pepperEnv, target, "calicoctl node checksystem")
271 }
272}
273
274def checkCalicoUpgradePossibility(pepperEnv, target) {
275 def salt = new com.mirantis.mk.Salt()
276
277 stage("Verification of Calico upgrade possibility") {
278 // check Calico version
279 def versionResult = salt.cmdRun(
280 pepperEnv, target, "calicoctl version | grep 'Cluster Version'"
281 )['return'][0].values()[0].split("\n")[0].trim()
282 versionStr = (versionResult - "Cluster Version:").trim()
283 version = versionStr.tokenize(".")
284 if ((version.size() < 3) || (version[0] != "v2") || (version[1] != "6") || (version[2].toInteger() < 5)) {
285 error(
286 "Current Calico ${versionStr} cannot be upgraded to v3.x. " +
287 "Calico v2.6.x starting from v2.6.5 can be upgraded. " +
288 "For earlier versions, please update to v2.6.5 first."
289 )
290 }
291 print("Calico version was determined: ${versionStr}")
292
293 // check Calico is switched on
294 def readinessResult = salt.cmdRun(
295 pepperEnv, target, ". /var/lib/etcd/configenv && etcdctl get /calico/v1/Ready"
296 )['return'][0].values()[0].split("\n")[0].trim()
297 print("Calico readiness check result: ${readinessResult}")
298 if (readinessResult != "true") {
299 // try set it to true
300 readinessResult = salt.cmdRun(
301 pepperEnv, target, ". /var/lib/etcd/configenv && etcdctl set /calico/v1/Ready true"
302 )['return'][0].values()[0].split("\n")[0].trim()
303 print("Calico readiness result 2nd attempt: ${readinessResult}")
304 if (readinessResult != "true") {
305 error("Calico is not ready. '/calico/v1/Ready': '${readinessResult}'")
306 }
307 }
308
309 // Calico data upgrade dry-run
310 def cmd = "export APIV1_ETCD_ENDPOINTS=${ETCD_ENDPOINTS} && " +
311 "export APIV1_ETCD_CA_CERT_FILE=/var/lib/etcd/ca.pem && " +
312 "export APIV1_ETCD_CERT_FILE=/var/lib/etcd/etcd-client.crt && " +
313 "export APIV1_ETCD_KEY_FILE=/var/lib/etcd/etcd-client.key && " +
314 "export ETCD_ENDPOINTS=${ETCD_ENDPOINTS} && " +
315 "export ETCD_CA_CERT_FILE=/var/lib/etcd/ca.pem && " +
316 "export ETCD_CERT_FILE=/var/lib/etcd/etcd-client.crt && " +
317 "export ETCD_KEY_FILE=/var/lib/etcd/etcd-client.key && " +
318 "./calico-upgrade dry-run --ignore-v3-data"
319 def dryRunResult = salt.cmdRun(pepperEnv, target, cmd)['return'][0].values()[0]
320 // check dry-run result
321 def validationSuccessStr = "Successfully validated v1 to v3 conversion"
322 if (!dryRunResult.contains(validationSuccessStr)) {
323 error("Calico data upgrade dry-run has failed")
324 }
Aleksei Kasatkinff9d5b52018-10-26 11:47:46 +0200325 }
326}
Victor Ryzhenkinae22a5a2018-10-12 15:52:27 +0400327
Aleksei Kasatkin9ce11842018-11-23 14:27:33 +0100328def checkCalicoPolicySetting(pepperEnv, target) {
329 def common = new com.mirantis.mk.Common()
330 def salt = new com.mirantis.mk.Salt()
331
332 stage("Checking of Calico network policy setting") {
333 // check Calico policy enabled
334 def cniPolicy = false
335 def addonsPolicy = false
336 def kubeCtrlRunning = false
337
338 // check CNI config
339 def cniCfgResult = salt.cmdRun(
340 pepperEnv, target, "cat /etc/cni/net.d/10-calico.conf"
341 )['return'][0].values()[0].toString()
342 def cniCfg = new JsonSlurper().parseText(cniCfgResult)
343 if (cniCfg.get("policy") != null) {
344 if (cniCfg["policy"].get("type") == "k8s") {
345 cniPolicy = true
346 } else {
347 common.warningMsg("Calico policy type is unknown or not set.")
348 }
349 }
350
351 // check k8s addons
352 def addonsResult = salt.cmdRun(
353 pepperEnv, target, "ls /etc/kubernetes/addons"
354 )['return'][0].values()[0].toString()
355 if (addonsResult.contains("calico_policy")) {
356 addonsPolicy = true
357 }
358
359 // check kube-controllers is running
360 def kubeCtrlResult = salt.cmdRun(
361 pepperEnv, target, "kubectl get pod -n kube-system --selector=k8s-app=calico-kube-controllers"
362 )['return'][0].values()[0].toString()
363 if (kubeCtrlResult.contains("Running")) {
364 kubeCtrlRunning = true
365 }
366
367 // It's safe to enable Calico policy any time, but it may be unsafe to disable it.
368 // So, no need to disable Calico policy for v3.x if it's not in use currently.
369 // But if Calico policy is in use already, it should be enabled after upgrade as well.
370
371 // check for consistency
372 if ((cniPolicy != addonsPolicy) || (addonsPolicy != kubeCtrlRunning)) {
373 caution = "ATTENTION. Calico policy setting cannot be determined reliably (enabled in CNI config: ${cniPolicy}, " +
374 "presence in k8s addons: ${addonsPolicy}, kube-controllers is running: ${kubeCtrlRunning})."
375 currentBuild.description += "<br><b>${caution}</b><br><br>"
376 common.warningMsg(caution)
377 } else {
378 common.infoMsg("Current Calico policy state is detected as: ${cniPolicy}")
379 if (cniPolicy) {
380 // Calico policy is in use. Check policy setting for v3.x.
381 common.infoMsg("Calico policy is in use. It should be enabled for v3.x as well.")
382 def saltPolicyResult = salt.getPillar(
383 pepperEnv, target, "kubernetes:pool:network:calico:policy"
384 )["return"][0].values()[0].toString()
385
386 common.infoMsg("kubernetes.pool.network.calico.policy: ${saltPolicyResult}")
387 if (saltPolicyResult.toLowerCase().contains("true")) {
388 common.infoMsg("Calico policy setting for v3.x is detected as: true")
389 } else {
390 caution = "ATTENTION. Currently, Calico is running with policy switched on. " +
391 "Calico policy setting for v3.x is not set to true. " +
392 "After upgrade is completed, Calico policy will be switched off. " +
393 "You will need to switch it on manually if required."
394 currentBuild.description += "<br><b>${caution}</b><br><br>"
395 common.warningMsg(caution)
396 }
397 }
398 }
399
400 if (addonsPolicy) {
401 // Remove v2.6.x policy-related addons on masters to not interfere with v3.x kube-controllers
402 salt.cmdRun(pepperEnv, CTL_TARGET, "rm -rf /etc/kubernetes/addons/calico_policy")
403 }
404 }
405}
406
Victor Ryzhenkinef34a022018-06-22 19:36:13 +0400407timeout(time: 12, unit: 'HOURS') {
408 node() {
409 try {
410
411 stage("Setup virtualenv for Pepper") {
412 python.setupPepperVirtualenv(pepperEnv, SALT_MASTER_URL, SALT_MASTER_CREDENTIALS)
413 }
414
Victor Ryzhenkinae22a5a2018-10-12 15:52:27 +0400415 if (CONFORMANCE_RUN_BEFORE.toBoolean()) {
416 def target = CTL_TARGET
417 def mcp_repo = ARTIFACTORY_URL
418 def k8s_api = TEST_K8S_API_SERVER
419 firstTarget = salt.getFirstMinion(pepperEnv, target)
420 executeConformance(pepperEnv, firstTarget, k8s_api, mcp_repo)
421 }
422
Victor Ryzhenkinef34a022018-06-22 19:36:13 +0400423 if ((common.validInputParam('KUBERNETES_HYPERKUBE_IMAGE')) && (common.validInputParam('KUBERNETES_PAUSE_IMAGE'))) {
424 overrideKubernetesImage(pepperEnv)
425 }
426
Aleksei Kasatkinff9d5b52018-10-26 11:47:46 +0200427 if ((common.validInputParam('KUBERNETES_CALICO_IMAGE'))
428 && (common.validInputParam('KUBERNETES_CALICO_CALICOCTL_IMAGE'))
429 && (common.validInputParam('KUBERNETES_CALICO_CNI_IMAGE'))
430 && (common.validInputParam('KUBERNETES_CALICO_KUBE_CONTROLLERS_IMAGE'))
431 ) {
432 calicoImagesValid = true
433 overrideCalicoImages(pepperEnv)
434 }
435
Victor Ryzhenkinef34a022018-06-22 19:36:13 +0400436 /*
Aleksei Kasatkinff9d5b52018-10-26 11:47:46 +0200437 * Execute Calico upgrade if needed (only for v2 to v3 upgrade).
438 * This part causes workloads operations downtime.
439 * It is only required for Calico v2.x to v3.x upgrade when etcd is in use for Calico
440 * as Calico etcd schema has different formats for Calico v2.x and Calico v3.x.
441 */
442 if (UPGRADE_CALICO_V2_TO_V3.toBoolean()) {
443 // one CTL node will be used for running upgrade of Calico etcd schema
444 def ctl_node = salt.getMinionsSorted(pepperEnv, CTL_TARGET)[0]
445
Aleksei Kasatkin1f4f5ba2018-11-20 18:30:36 +0100446 // get ETCD_ENDPOINTS in use by Calico
447 def ep_str = salt.cmdRun(pepperEnv, ctl_node, "cat /etc/calico/calicoctl.cfg | grep etcdEndpoints")['return'][0].values()[0]
448 ETCD_ENDPOINTS = ep_str.split("\n")[0].tokenize(' ')[1]
449 print("ETCD_ENDPOINTS in use by Calico: '${ETCD_ENDPOINTS}'")
450
451 // download calico-upgrade utility
Aleksei Kasatkinff9d5b52018-10-26 11:47:46 +0200452 downloadCalicoUpgrader(pepperEnv, ctl_node)
Aleksei Kasatkin1f4f5ba2018-11-20 18:30:36 +0100453
454 // check the possibility of upgrading of Calico
455 checkCalicoUpgradePossibility(pepperEnv, ctl_node)
456
457 // prepare for upgrade. when done in advance, this will decrease downtime during upgrade
Aleksei Kasatkinff9d5b52018-10-26 11:47:46 +0200458 if (calicoImagesValid) {
459 pullCalicoImages(pepperEnv, POOL)
460 }
461
Aleksei Kasatkin9ce11842018-11-23 14:27:33 +0100462 // check and adjust Calico policy setting
463 checkCalicoPolicySetting(pepperEnv, ctl_node)
464
Aleksei Kasatkinff9d5b52018-10-26 11:47:46 +0200465 // this sequence implies workloads operations downtime
466 startCalicoUpgrade(pepperEnv, ctl_node)
467 performCalicoConfigurationUpdateAndServicesRestart(pepperEnv, POOL)
468 completeCalicoUpgrade(pepperEnv, ctl_node)
469 // after that no downtime is expected
470
471 checkCalicoUpgradeSuccessful(pepperEnv, POOL)
472 }
473
474 /*
475 * Execute k8s update
Victor Ryzhenkinef34a022018-06-22 19:36:13 +0400476 */
477 if (updates.contains("ctl")) {
478 def target = CTL_TARGET
479
480 if (PER_NODE.toBoolean()) {
481 def targetHosts = salt.getMinionsSorted(pepperEnv, target)
482
483 for (t in targetHosts) {
Victor Ryzhenkin42e4b382018-09-11 17:57:56 +0400484 if (SIMPLE_UPGRADE.toBoolean()) {
485 performKubernetesControlUpdate(pepperEnv, t)
486 } else {
487 cordonNode(pepperEnv, t)
488 drainNode(pepperEnv, t)
489 regenerateCerts(pepperEnv, t)
490 if (UPGRADE_DOCKER.toBoolean()) {
491 upgradeDocker(pepperEnv, t)
492 }
493 performKubernetesControlUpdate(pepperEnv, t)
Victor Ryzhenkinfd9677f2018-10-16 16:14:40 +0400494 updateAddonManager(pepperEnv, t)
Victor Ryzhenkin42e4b382018-09-11 17:57:56 +0400495 uncordonNode(pepperEnv, t)
496 }
Victor Ryzhenkinef34a022018-06-22 19:36:13 +0400497 }
498 } else {
499 performKubernetesControlUpdate(pepperEnv, target)
500 }
Victor Ryzhenkinfd9677f2018-10-16 16:14:40 +0400501 if (!SIMPLE_UPGRADE.toBoolean()) {
Aleksei Kasatkinff9d5b52018-10-26 11:47:46 +0200502 // Addons upgrade should be performed after all nodes will be upgraded
Victor Ryzhenkinfd9677f2018-10-16 16:14:40 +0400503 updateAddons(pepperEnv, target)
504 // Wait for 90 sec for addons reconciling
505 sleep(90)
506 }
Victor Ryzhenkinef34a022018-06-22 19:36:13 +0400507 }
508
509 if (updates.contains("cmp")) {
510 def target = CMP_TARGET
511
512 if (PER_NODE.toBoolean()) {
513 def targetHosts = salt.getMinionsSorted(pepperEnv, target)
514
515 for (t in targetHosts) {
Victor Ryzhenkin42e4b382018-09-11 17:57:56 +0400516 if (SIMPLE_UPGRADE.toBoolean()) {
517 performKubernetesComputeUpdate(pepperEnv, t)
518 } else {
519 cordonNode(pepperEnv, t)
520 drainNode(pepperEnv, t)
521 regenerateCerts(pepperEnv, t)
522 if (UPGRADE_DOCKER.toBoolean()) {
523 upgradeDocker(pepperEnv, t)
524 }
525 performKubernetesComputeUpdate(pepperEnv, t)
526 uncordonNode(pepperEnv, t)
527 }
Victor Ryzhenkinef34a022018-06-22 19:36:13 +0400528 }
529 } else {
530 performKubernetesComputeUpdate(pepperEnv, target)
531 }
532 }
Victor Ryzhenkinae22a5a2018-10-12 15:52:27 +0400533
534 if (CONFORMANCE_RUN_AFTER.toBoolean()) {
535 def target = CTL_TARGET
536 def mcp_repo = ARTIFACTORY_URL
537 def k8s_api = TEST_K8S_API_SERVER
538 firstTarget = salt.getFirstMinion(pepperEnv, target)
539 executeConformance(pepperEnv, firstTarget, k8s_api, mcp_repo)
540 }
Victor Ryzhenkinef34a022018-06-22 19:36:13 +0400541 } catch (Throwable e) {
542 // If there was an error or exception thrown, the build failed
543 currentBuild.result = "FAILURE"
544 currentBuild.description = currentBuild.description ? e.message + " " + currentBuild.description : e.message
545 throw e
546 }
547 }
548}