blob: 64a7d99884ae53399177bf8511ffb094be36f015 [file] [log] [blame]
Victor Ryzhenkinef34a022018-06-22 19:36:13 +04001/**
2 * Update kuberentes cluster
3 *
4 * Expected parameters:
Victor Ryzhenkineb543bf2019-01-18 06:34:26 +04005 * 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_SOURCE Versioned hyperkube binary to update control plane from. Should be null if update rolling via reclass-system level
8 * KUBERNETES_HYPERKUBE_SOURCE_HASH Versioned hyperkube binary to update control plane from. Should be null if update rolling via reclass-system level
9 * KUBERNETES_PAUSE_IMAGE Kubernetes pause image should have same version as hyperkube. May be null in case of reclass-system rollout
10 * TARGET_UPDATES Comma separated list of nodes to update (Valid values are ctl,cmp)
11 * CTL_TARGET Salt targeted kubernetes CTL nodes (ex. I@kubernetes:master). Kubernetes control plane
12 * CMP_TARGET Salt targeted compute nodes (ex. cmp* and 'I@kubernetes:pool') Kubernetes computes
13 * PER_NODE Target nodes will be managed one by one (bool)
14 * SIMPLE_UPGRADE Use previous version of upgrade without conron/drain abilities
15 * 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.
19 * 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_SOURCE Versioned calico/ctl binary. Should be null if update rolling via reclass-system level
22 * KUBERNETES_CALICO_CALICOCTL_SOURCE_HASH Calico/ctl binary md5 hash. Should be null if update rolling via reclass-system level
23 * KUBERNETES_CALICO_CNI_SOURCE Versioned calico/cni binary. Should be null if update rolling via reclass-system level
24 * KUBERNETES_CALICO_CNI_SOURCE_HASH Сalico/cni binary hash. Should be null if update rolling via reclass-system level
25 * KUBERNETES_CALICO_BIRDCL_SOURCE Versioned calico/bird binary. Should be null if update rolling via reclass-system level
26 * KUBERNETES_CALICO_BIRDCL_SOURCE_HASH Сalico/bird binary hash. Should be null if update rolling via reclass-system level
27 * KUBERNETES_CALICO_CNI_IPAM_SOURCE Versioned calico/ipam binary. Should be null if update rolling via reclass-system level
28 * KUBERNETES_CALICO_CNI_IPAM_SOURCE_HASH Сalico/ipam binary hash. Should be null if update rolling via reclass-system level
29 * KUBERNETES_CALICO_KUBE_CONTROLLERS_IMAGE Target calico/kube-controllers image. May be null in case of reclass-system rollout.
30 * CALICO_UPGRADE_VERSION Version of "calico-upgrade" utility to be used ("v1.0.5" for Calico v3.1.3 target).
ashestakovc030dac2019-01-11 11:34:54 +020031 * KUBERNETES_ETCD_SOURCE Target etcd binary. May be null in case of reclass-system rollout.
32 * KUBERNETES_ETCD_SOURCE_HASH Target etcd binary checksum. May be null in case of reclass-system rollout.
Victor Ryzhenkinef34a022018-06-22 19:36:13 +040033 *
34**/
Aleksei Kasatkin9ce11842018-11-23 14:27:33 +010035import groovy.json.JsonSlurper
36
Victor Ryzhenkinef34a022018-06-22 19:36:13 +040037def common = new com.mirantis.mk.Common()
38def salt = new com.mirantis.mk.Salt()
39def python = new com.mirantis.mk.Python()
Victor Ryzhenkin723bd062018-12-11 17:09:06 +040040def test = new com.mirantis.mk.Test()
Victor Ryzhenkinef34a022018-06-22 19:36:13 +040041
42def updates = TARGET_UPDATES.tokenize(",").collect{it -> it.trim()}
43def pepperEnv = "pepperEnv"
44
Aleksei Kasatkinff9d5b52018-10-26 11:47:46 +020045def POOL = "I@kubernetes:pool"
Aleksei Kasatkinff9d5b52018-10-26 11:47:46 +020046
Aleksei Kasatkin1f4f5ba2018-11-20 18:30:36 +010047ETCD_ENDPOINTS = ""
48
Victor Ryzhenkinef34a022018-06-22 19:36:13 +040049def overrideKubernetesImage(pepperEnv) {
50 def salt = new com.mirantis.mk.Salt()
51
52 def k8sSaltOverrides = """
Victor Ryzhenkineb543bf2019-01-18 06:34:26 +040053 kubernetes_hyperkube_source: ${KUBERNETES_HYPERKUBE_SOURCE}
54 kubernetes_hyperkube_source_hash: ${KUBERNETES_HYPERKUBE_SOURCE_HASH}
Victor Ryzhenkinef34a022018-06-22 19:36:13 +040055 kubernetes_pause_image: ${KUBERNETES_PAUSE_IMAGE}
56 """
57 stage("Override kubernetes images to target version") {
58 salt.setSaltOverrides(pepperEnv, k8sSaltOverrides)
59 }
60}
61
Aleksei Kasatkinff9d5b52018-10-26 11:47:46 +020062def overrideCalicoImages(pepperEnv) {
63 def salt = new com.mirantis.mk.Salt()
64
65 def calicoSaltOverrides = """
66 kubernetes_calico_image: ${KUBERNETES_CALICO_IMAGE}
Victor Ryzhenkineb543bf2019-01-18 06:34:26 +040067 kubernetes_calico_calicoctl_source: ${KUBERNETES_CALICO_CALICOCTL_SOURCE}
68 kubernetes_calico_calicoctl_source_hash: ${KUBERNETES_CALICO_CALICOCTL_SOURCE_HASH}
69 kubernetes_calico_birdcl_source: ${KUBERNETES_CALICO_BIRDCL_SOURCE}
70 kubernetes_calico_birdcl_source_hash: ${KUBERNETES_CALICO_BIRDCL_SOURCE_HASH}
71 kubernetes_calico_cni_source: ${KUBERNETES_CALICO_CNI_SOURCE}
72 kubernetes_calico_cni_source_hash: ${KUBERNETES_CALICO_CNI_SOURCE_HASH}
73 kubernetes_calico_cni_ipam_source: ${KUBERNETES_CALICO_CNI_IPAM_SOURCE}
74 kubernetes_calico_cni_ipam_source_hash: ${KUBERNETES_CALICO_CNI_IPAM_SOURCE_HASH}
Aleksei Kasatkinff9d5b52018-10-26 11:47:46 +020075 kubernetes_calico_kube_controllers_image: ${KUBERNETES_CALICO_KUBE_CONTROLLERS_IMAGE}
76 """
77 stage("Override calico images to target version") {
78 salt.setSaltOverrides(pepperEnv, calicoSaltOverrides)
79 }
80}
81
82def downloadCalicoUpgrader(pepperEnv, target) {
83 def salt = new com.mirantis.mk.Salt()
84
85 stage("Downloading calico-upgrade utility") {
86 salt.cmdRun(pepperEnv, target, "rm -f ./calico-upgrade")
87 salt.cmdRun(pepperEnv, target, "wget https://github.com/projectcalico/calico-upgrade/releases/download/${CALICO_UPGRADE_VERSION}/calico-upgrade")
88 salt.cmdRun(pepperEnv, target, "chmod +x ./calico-upgrade")
89 }
90}
91
ashestakovc030dac2019-01-11 11:34:54 +020092def overrideEtcdSource(pepperEnv) {
93 def salt = new com.mirantis.mk.Salt()
94
95 def k8sSaltOverrides = """
96 kubernetes_etcd_source: ${KUBERNETES_ETCD_SOURCE}
97 kubernetes_etcd_source_hash: ${KUBERNETES_ETCD_SOURCE_HASH}
98 """
99 stage("Override etcd binaries to target version") {
100 salt.setSaltOverrides(pepperEnv, k8sSaltOverrides)
101 }
102}
103
104def performEtcdUpdateAndServicesRestart(pepperEnv, target) {
105 def salt = new com.mirantis.mk.Salt()
106
107 stage("Performing etcd update and services restart on ${target}") {
108 salt.enforceState(pepperEnv, target, "etcd.server.service")
109 salt.cmdRun(pepperEnv, target, ". /var/lib/etcd/configenv && etcdctl cluster-health")
110 }
111}
112
Victor Ryzhenkinef34a022018-06-22 19:36:13 +0400113def performKubernetesComputeUpdate(pepperEnv, target) {
114 def salt = new com.mirantis.mk.Salt()
115
116 stage("Execute Kubernetes compute update on ${target}") {
117 salt.enforceState(pepperEnv, target, 'kubernetes.pool')
118 salt.runSaltProcessStep(pepperEnv, target, 'service.restart', ['kubelet'])
119 }
120}
121
122def performKubernetesControlUpdate(pepperEnv, target) {
123 def salt = new com.mirantis.mk.Salt()
124
125 stage("Execute Kubernetes control plane update on ${target}") {
Victor Ryzhenkind324a012019-01-23 05:39:34 +0400126 salt.enforceStateWithExclude(pepperEnv, target, "kubernetes", "kubernetes.master.setup,kubernetes.master.kube-addons")
Victor Ryzhenkinef34a022018-06-22 19:36:13 +0400127 // Restart kubelet
128 salt.runSaltProcessStep(pepperEnv, target, 'service.restart', ['kubelet'])
129 }
130}
131
Aleksei Kasatkinff9d5b52018-10-26 11:47:46 +0200132def startCalicoUpgrade(pepperEnv, target) {
133 def salt = new com.mirantis.mk.Salt()
134
135 stage("Starting upgrade using calico-upgrade: migrate etcd schema and lock Calico") {
Aleksei Kasatkinff9d5b52018-10-26 11:47:46 +0200136 def cmd = "export APIV1_ETCD_ENDPOINTS=${ETCD_ENDPOINTS} && " +
137 "export APIV1_ETCD_CA_CERT_FILE=/var/lib/etcd/ca.pem && " +
138 "export APIV1_ETCD_CERT_FILE=/var/lib/etcd/etcd-client.crt && " +
139 "export APIV1_ETCD_KEY_FILE=/var/lib/etcd/etcd-client.key && " +
140 "export ETCD_ENDPOINTS=${ETCD_ENDPOINTS} && " +
141 "export ETCD_CA_CERT_FILE=/var/lib/etcd/ca.pem && " +
142 "export ETCD_CERT_FILE=/var/lib/etcd/etcd-client.crt && " +
143 "export ETCD_KEY_FILE=/var/lib/etcd/etcd-client.key && " +
144 "rm /root/upg_complete -f && " +
145 "./calico-upgrade start --no-prompts --ignore-v3-data > upgrade-start.log && " +
146 "until [ -f /root/upg_complete ]; do sleep 0.1; done && " +
147 "./calico-upgrade complete --no-prompts > upgrade-complete.log && " +
148 "rm /root/upg_complete -f"
149 // "saltArgs = ['async']" doesn't work, so we have to run "cmd.run --async"
150 salt.cmdRun(pepperEnv, "I@salt:master", "salt -C '${target}' cmd.run '${cmd}' --async")
151 salt.cmdRun(pepperEnv, target, "until [ -f /root/upgrade-start.log ]; do sleep 0.1; done")
152 }
153}
154
155def completeCalicoUpgrade(pepperEnv, target) {
156 def salt = new com.mirantis.mk.Salt()
157
158 stage("Complete upgrade using calico-upgrade: unlock Calico") {
159 salt.cmdRun(pepperEnv, target, "echo 'true' > /root/upg_complete")
160 salt.cmdRun(pepperEnv, target, "while [ -f /root/upg_complete ]; do sleep 0.1; done")
161 salt.cmdRun(pepperEnv, target, "cat /root/upgrade-start.log")
162 salt.cmdRun(pepperEnv, target, "cat /root/upgrade-complete.log")
163 }
164}
165
Aleksei Kasatkind9d682e2018-12-12 14:51:59 +0100166def performCalicoConfigurationUpdateAndServicesRestart(pepperEnv, target, ctl_node) {
Aleksei Kasatkinff9d5b52018-10-26 11:47:46 +0200167 def salt = new com.mirantis.mk.Salt()
168
169 stage("Performing Calico configuration update and services restart") {
Aleksei Kasatkind9d682e2018-12-12 14:51:59 +0100170 if (containerDenabled(pepperEnv, ctl_node)) {
171 salt.enforceState(pepperEnv, target, "kubernetes.pool")
172 } else {
173 salt.enforceState(pepperEnv, target, "kubernetes.pool.calico")
174 }
Aleksei Kasatkinff9d5b52018-10-26 11:47:46 +0200175 salt.runSaltProcessStep(pepperEnv, target, 'service.restart', ['kubelet'])
176 }
177}
178
Victor Ryzhenkin42e4b382018-09-11 17:57:56 +0400179def cordonNode(pepperEnv, target) {
180 def salt = new com.mirantis.mk.Salt()
181 def originalTarget = "I@kubernetes:master and not ${target}"
182
183 stage("Cordoning ${target} kubernetes node") {
184 def nodeShortName = target.tokenize(".")[0]
185 salt.cmdRun(pepperEnv, originalTarget, "kubectl cordon ${nodeShortName}", true, 1)
186 }
187}
188
189def uncordonNode(pepperEnv, target) {
190 def salt = new com.mirantis.mk.Salt()
191 def originalTarget = "I@kubernetes:master and not ${target}"
192
193 stage("Uncordoning ${target} kubernetes node") {
194 def nodeShortName = target.tokenize(".")[0]
195 salt.cmdRun(pepperEnv, originalTarget, "kubectl uncordon ${nodeShortName}", true, 1)
196 }
197}
198
199def drainNode(pepperEnv, target) {
200 def salt = new com.mirantis.mk.Salt()
201 def originalTarget = "I@kubernetes:master and not ${target}"
202
203 stage("Draining ${target} kubernetes node") {
204 def nodeShortName = target.tokenize(".")[0]
205 salt.cmdRun(pepperEnv, originalTarget, "kubectl drain --force --ignore-daemonsets --grace-period 100 --timeout 300s --delete-local-data ${nodeShortName}", true, 1)
206 }
207}
208
209def regenerateCerts(pepperEnv, target) {
210 def salt = new com.mirantis.mk.Salt()
211
212 stage("Regenerate certs for ${target}") {
213 salt.enforceState(pepperEnv, target, 'salt.minion.cert')
214 }
215}
216
Victor Ryzhenkinae909182018-10-02 17:49:18 +0400217def updateAddons(pepperEnv, target) {
218 def salt = new com.mirantis.mk.Salt()
219
220 stage("Upgrading Addons at ${target}") {
Victor Ryzhenkin40625bc2018-10-04 16:15:27 +0400221 salt.enforceState(pepperEnv, target, "kubernetes.master.kube-addons")
Victor Ryzhenkinfd9677f2018-10-16 16:14:40 +0400222 }
223}
224
225def updateAddonManager(pepperEnv, target) {
226 def salt = new com.mirantis.mk.Salt()
227
228 stage("Upgrading AddonManager at ${target}") {
Victor Ryzhenkinae909182018-10-02 17:49:18 +0400229 salt.enforceState(pepperEnv, target, "kubernetes.master.setup")
230 }
231}
232
Victor Ryzhenkind324a012019-01-23 05:39:34 +0400233def buildDaemonsetMap(pepperEnv, target) {
234 def salt = new com.mirantis.mk.Salt()
235 def daemonset_lists
236 daemonset_lists = salt.cmdRun(pepperEnv, target, "kubectl get ds --all-namespaces | tail -n+2 | awk '{print \$2, \$1}'"
237 )['return'][0].values()[0].replaceAll('Salt command execution success','').tokenize("\n")
238 def daemonset_map = []
239 for (ds in daemonset_lists) {
240 a = ds.tokenize(" ")
241 daemonset_map << a
242 }
243 print("Built daemonset map")
244 print(daemonset_map)
245 return daemonset_map
246}
247
248def purgeDaemonsetPods(pepperEnv, target, daemonSetMap) {
249 def salt = new com.mirantis.mk.Salt()
250 def originalTarget = "I@kubernetes:master and not ${target}"
251 def nodeShortName = target.tokenize(".")[0]
252 firstTarget = salt.getFirstMinion(pepperEnv, originalTarget)
253
254 if (daemonSetMap) {
255 stage("Purging daemonset-managed pods on ${target}") {
256 for (ds in daemonSetMap) {
257 print("Purging "+ ds[0] +" inside "+ ds[1] +" namespace")
258 salt.cmdRun(pepperEnv, firstTarget, "kubectl get po -n ${ds[1]} -o wide | grep ${nodeShortName}" +
259 " | grep ${ds[0]} | awk '{print \$1}' | xargs --no-run-if-empty kubectl delete po -n ${ds[1]} --grace-period=0 --force")
260 }
261 }
262 }
263}
264
265def isNodeReady(pepperEnv, target) {
266 def salt = new com.mirantis.mk.Salt()
267 def originalTarget = "I@kubernetes:master and not ${target}"
268 def nodeShortName = target.tokenize(".")[0]
269 firstTarget = salt.getFirstMinion(pepperEnv, originalTarget)
270
271 status = salt.cmdRun(pepperEnv, firstTarget, "kubectl get no | grep ${nodeShortName} | awk '{print \$2}'"
272 )['return'][0].values()[0].replaceAll('Salt command execution success',''
273 ).replaceAll(',SchedulingDisabled','').trim()
274
275 if (status == "Ready") {
276 return true
277 } else {
278 return false
279 }
280}
281
282def rebootKubernetesNode(pepperEnv, target, times=15, delay=10) {
283 def common = new com.mirantis.mk.Common()
284 def debian = new com.mirantis.mk.Debian()
285
286 stage("Rebooting ${target}") {
287 debian.osReboot(pepperEnv, target)
Victor Ryzhenkin1da4dd92019-03-28 02:16:39 +0400288 /*
289 * Kubernetes controller manager will mark kubernetes node as NotReady
290 * only after 40 seconds of it's downtime.
291 * Let's wait for 60 sec to be sure that node will reach it's
292 * correct status.
293 */
294 sleep(60)
Victor Ryzhenkind324a012019-01-23 05:39:34 +0400295 common.retry(times, delay) {
296 if(!isNodeReady(pepperEnv, target)) {
297 error("Node still not in Ready state...")
298 }
299 }
300 }
301}
302
Victor Ryzhenkin42e4b382018-09-11 17:57:56 +0400303def upgradeDocker(pepperEnv, target) {
304 def salt = new com.mirantis.mk.Salt()
305
306 stage("Upgrading docker at ${target}") {
307 salt.enforceState(pepperEnv, target, 'docker.host')
308 }
309}
Victor Ryzhenkinef34a022018-06-22 19:36:13 +0400310
Victor Ryzhenkinae22a5a2018-10-12 15:52:27 +0400311def runConformance(pepperEnv, target, k8s_api, image) {
312 def salt = new com.mirantis.mk.Salt()
313 def containerName = 'conformance_tests'
314 output_file = image.replaceAll('/', '-') + '.output'
315 def output_file_full_path = "/tmp/" + image.replaceAll('/', '-') + '.output'
316 def artifacts_dir = '_artifacts/'
317 salt.cmdRun(pepperEnv, target, "docker rm -f ${containerName}", false)
318 salt.cmdRun(pepperEnv, target, "docker run -d --name ${containerName} --net=host -e API_SERVER=${k8s_api} ${image}")
319 sleep(10)
320
321 print("Waiting for tests to run...")
322 salt.runSaltProcessStep(pepperEnv, target, 'cmd.run', ["docker wait ${containerName}"], null, false)
323
324 print("Writing test results to output file...")
325 salt.runSaltProcessStep(pepperEnv, target, 'cmd.run', ["docker logs -t ${containerName} > ${output_file_full_path}"])
326 print("Conformance test output saved in " + output_file_full_path)
327
328 // collect output
329 sh "mkdir -p ${artifacts_dir}"
330 file_content = salt.getFileContent(pepperEnv, target, '/tmp/' + output_file)
331 writeFile file: "${artifacts_dir}${output_file}", text: file_content
332 sh "cat ${artifacts_dir}${output_file}"
333 try {
334 sh "cat ${artifacts_dir}${output_file} | grep 'Test Suite Failed' && exit 1 || exit 0"
335 } catch (Throwable e) {
336 print("Conformance tests failed. Please check output")
337 currentBuild.result = "FAILURE"
338 currentBuild.description = currentBuild.description ? e.message + " " + currentBuild.description : e.message
339 throw e
340 }
341}
342
343def buildImageURL(pepperEnv, target, mcp_repo) {
344 def salt = new com.mirantis.mk.Salt()
345 def raw_version = salt.cmdRun(pepperEnv, target, "kubectl version --short -o json")['return'][0].values()[0].replaceAll('Salt command execution success','')
346 print("Kubernetes version: " + raw_version)
347 def serialized_version = readJSON text: raw_version
348 def short_version = (serialized_version.serverVersion.gitVersion =~ /([v])(\d+\.)(\d+\.)(\d+\-)(\d+)/)[0][0]
349 print("Kubernetes short version: " + short_version)
350 def conformance_image = mcp_repo + "/mirantis/kubernetes/k8s-conformance:" + short_version
351 return conformance_image
352}
353
354def executeConformance(pepperEnv, target, k8s_api, mcp_repo) {
355 stage("Running conformance tests") {
356 def image = buildImageURL(pepperEnv, target, mcp_repo)
357 print("Using image: " + image)
358 runConformance(pepperEnv, target, k8s_api, image)
359 }
360}
361
Victor Ryzhenkin723bd062018-12-11 17:09:06 +0400362def containerDinstalled(pepperEnv, target) {
363 def salt = new com.mirantis.mk.Salt()
364 return salt.cmdRun(pepperEnv, target, "containerd --version 2>1 1>/dev/null && echo 'true' || echo 'false'"
365 )['return'][0].values()[0].replaceAll('Salt command execution success','').trim().toBoolean()
366}
367
368def containerDenabled(pepperEnv, target) {
369 def salt = new com.mirantis.mk.Salt()
Victor Ryzhenkin71ecdf42018-12-11 22:22:50 +0400370 return salt.getPillar(pepperEnv, target, "kubernetes:common:containerd:enabled"
Victor Ryzhenkin723bd062018-12-11 17:09:06 +0400371 )["return"][0].values()[0].toBoolean()
372}
373
374def conformancePodDefExists(pepperEnv, target) {
375 def salt = new com.mirantis.mk.Salt()
376 return salt.cmdRun(pepperEnv, target, "test -e /srv/kubernetes/conformance.yml && echo 'true' || echo 'false'"
377 )['return'][0].values()[0].replaceAll('Salt command execution success','').trim().toBoolean()
378}
379
Victor Ryzhenkineb543bf2019-01-18 06:34:26 +0400380def printVersionInfo(pepperEnv, target) {
381 def salt = new com.mirantis.mk.Salt()
382 def common = new com.mirantis.mk.Common()
383
384 stage("Gather version and runtime information") {
385 common.infoMsg("Version and runtime info:")
386 salt.cmdRun(pepperEnv, target, "kubectl get no -o wide")
387 common.infoMsg("Cluster health info:")
388 salt.cmdRun(pepperEnv, target, "kubectl get cs")
389 common.infoMsg("ETCD health info:")
Victor Ryzhenkin7fda5c42019-01-18 22:17:57 +0400390 salt.cmdRun(pepperEnv, target, ". /var/lib/etcd/configenv && etcdctl cluster-health")
Victor Ryzhenkineb543bf2019-01-18 06:34:26 +0400391 common.infoMsg("Calico peers info:")
392 salt.cmdRun(pepperEnv, target, "calicoctl node status")
393 }
394}
395
Aleksei Kasatkin5ccea272018-12-06 17:34:58 +0100396def calicoEnabled(pepperEnv, target) {
397 def salt = new com.mirantis.mk.Salt()
398 return salt.getPillar(pepperEnv, target, "kubernetes:pool:network:calico:enabled"
399 )["return"][0].values()[0].toBoolean()
400}
401
402def checkCalicoClusterState(pepperEnv, target) {
403 def common = new com.mirantis.mk.Common()
Aleksei Kasatkinff9d5b52018-10-26 11:47:46 +0200404 def salt = new com.mirantis.mk.Salt()
405
Aleksei Kasatkin5ccea272018-12-06 17:34:58 +0100406 stage("Checking Calico cluster state after upgrade") {
407 // check Calico cluster and cli clients versions
408 def checkVer = [
409 "Client Version:": [verStr: "", dif: false, wrong: false],
410 "Cluster Version:": [verStr: "", dif: false, wrong: false]
411 ]
412 def checkVerPassed = true
413 def versionResults = salt.cmdRun(pepperEnv, target, "calicoctl version | grep -i version")['return'][0]
414 versionResults.each { k, v ->
415 // println("Node:\n${k}\nResult:\n${v}")
416 for (verLine in v.split("\n")) {
417 for (verType in checkVer.keySet()) {
418 if (verLine.contains(verType)) {
419 def verRec = checkVer[verType]
420 ver = (verLine - verType).trim()
421 if (!verRec.verStr) {
422 verRec.verStr = ver
423 }
424 if (verRec.verStr != ver) {
425 verRec.dif = true
426 checkVerPassed = false
427 }
428 version = ver.tokenize(".")
429 if ((version.size() < 3) || (version[0] != "v3")) {
430 verRec.wrong = true
431 checkVerPassed = false
432 }
433 checkVer[verType] = verRec
434 }
435 }
436 }
437 }
438 if (checkVerPassed) {
439 common.infoMsg("Calico version verification passed")
440 }
441 else {
442 def warningMsg = "Calico version verification failed.\n"
443 checkVer.each { k, rec ->
444 if (rec.dif) {
445 warningMsg += "${k} versions are different across nodes.\n"
446 }
447 if (rec.wrong) {
448 warningMsg += "${k} (some) versions are wrong - should be v3.x.\n"
449 }
450 }
451 common.warningMsg(warningMsg)
452 currentBuild.description += "<br><b>${warningMsg}</b><br><br>"
453 }
454
455 // check Calico nodes' statuses
456 def nodeStatusResults = salt.cmdRun(pepperEnv, target, "calicoctl node status")['return'][0]
457 def nodesRunning = true
458 def peersNotFound = []
459 def peersNotOnline = []
460 nodeStatusResults.each { k, v ->
461 // println("Node:\n${k}\nResult:\n${v}")
462 if (!v.contains("Calico process is running")) {
463 nodesRunning = false
464 def warningMsg = "Node ${k}: Calico node is not running."
465 common.warningMsg(warningMsg)
466 currentBuild.description += "<br><b>${warningMsg}</b><br><br>"
467 }
468 def nodePeersFound = false
469 def nodePeersOnline = true
470 for (nodeLine in v.split("\n")) {
471 if (nodeLine.contains("|") && (!nodeLine.contains("STATE"))) {
472 def col = nodeLine.tokenize("|").collect{it.trim()}
473 if (col.size() == 5) {
474 nodePeersFound = true
475 if ((col[2] != "up") || (col[4] != "Established")) {
476 def warningMsg = "Node ${k}: BGP peer '${col[0]}' is out of reach. Peer state: '${col[2]}', connection info: '${col[4]}'."
477 common.warningMsg(warningMsg)
478 currentBuild.description += "<br><b>${warningMsg}</b><br><br>"
479 nodePeersOnline = false
480 }
481 }
482 }
483 }
484 if (!nodePeersFound) {
485 peersNotFound += k
486 }
487 if (!nodePeersOnline) {
488 peersNotOnline += k
489 }
490 }
491 if (nodesRunning) {
492 common.infoMsg("All the Calico nodes are running")
493 }
494 if (peersNotFound) {
495 def warningMsg = "BGP peers not found for the node(s): " + peersNotFound.join(', ') + "."
496 common.warningMsg(warningMsg)
497 currentBuild.description += "<br><b>${warningMsg}</b><br><br>"
498 } else {
499 common.infoMsg("BGP peers were found for all the nodes")
500 }
501 if (!peersNotOnline) {
502 common.infoMsg("All reported BGP peers are reachable")
503 }
504
505 // check that 'calico-kube-controllers' is running
506 // one CTL node will be used to get pod's state using kubectl
507 def ctl_node = salt.getMinionsSorted(pepperEnv, CTL_TARGET)[0]
508 def kubeCtrlResult = salt.cmdRun(
509 pepperEnv, ctl_node, "kubectl get pod -n kube-system --selector=k8s-app=calico-kube-controllers"
510 )['return'][0].values()[0].toString()
511 if (kubeCtrlResult.contains("calico-kube-controllers")) {
512 for (line in kubeCtrlResult.split("\n")) {
513 if (line.contains("calico-kube-controllers")) {
514 col = line.tokenize(" ")
515 if ((col[1] != "1/1") || (col[2] != "Running")) {
516 def warningMsg = "Calico kube-controllers pod is not running properly."
517 common.warningMsg(warningMsg)
518 currentBuild.description += "<br><b>${warningMsg}</b><br><br>"
519 }
520 else {
521 common.infoMsg("Calico kube-controllers pod is running.")
522 }
523 break
524 }
525 }
526 } else {
527 def warningMsg = "Calico kube-controllers pod was not scheduled."
528 common.warningMsg(warningMsg)
529 currentBuild.description += "<br><b>${warningMsg}</b><br><br>"
530 }
Aleksei Kasatkin1f4f5ba2018-11-20 18:30:36 +0100531 }
532}
533
534def checkCalicoUpgradePossibility(pepperEnv, target) {
535 def salt = new com.mirantis.mk.Salt()
536
537 stage("Verification of Calico upgrade possibility") {
538 // check Calico version
539 def versionResult = salt.cmdRun(
540 pepperEnv, target, "calicoctl version | grep 'Cluster Version'"
541 )['return'][0].values()[0].split("\n")[0].trim()
542 versionStr = (versionResult - "Cluster Version:").trim()
543 version = versionStr.tokenize(".")
544 if ((version.size() < 3) || (version[0] != "v2") || (version[1] != "6") || (version[2].toInteger() < 5)) {
545 error(
546 "Current Calico ${versionStr} cannot be upgraded to v3.x. " +
547 "Calico v2.6.x starting from v2.6.5 can be upgraded. " +
548 "For earlier versions, please update to v2.6.5 first."
549 )
550 }
551 print("Calico version was determined: ${versionStr}")
552
553 // check Calico is switched on
554 def readinessResult = salt.cmdRun(
555 pepperEnv, target, ". /var/lib/etcd/configenv && etcdctl get /calico/v1/Ready"
556 )['return'][0].values()[0].split("\n")[0].trim()
557 print("Calico readiness check result: ${readinessResult}")
558 if (readinessResult != "true") {
559 // try set it to true
560 readinessResult = salt.cmdRun(
561 pepperEnv, target, ". /var/lib/etcd/configenv && etcdctl set /calico/v1/Ready true"
562 )['return'][0].values()[0].split("\n")[0].trim()
563 print("Calico readiness result 2nd attempt: ${readinessResult}")
564 if (readinessResult != "true") {
565 error("Calico is not ready. '/calico/v1/Ready': '${readinessResult}'")
566 }
567 }
568
569 // Calico data upgrade dry-run
570 def cmd = "export APIV1_ETCD_ENDPOINTS=${ETCD_ENDPOINTS} && " +
571 "export APIV1_ETCD_CA_CERT_FILE=/var/lib/etcd/ca.pem && " +
572 "export APIV1_ETCD_CERT_FILE=/var/lib/etcd/etcd-client.crt && " +
573 "export APIV1_ETCD_KEY_FILE=/var/lib/etcd/etcd-client.key && " +
574 "export ETCD_ENDPOINTS=${ETCD_ENDPOINTS} && " +
575 "export ETCD_CA_CERT_FILE=/var/lib/etcd/ca.pem && " +
576 "export ETCD_CERT_FILE=/var/lib/etcd/etcd-client.crt && " +
577 "export ETCD_KEY_FILE=/var/lib/etcd/etcd-client.key && " +
578 "./calico-upgrade dry-run --ignore-v3-data"
579 def dryRunResult = salt.cmdRun(pepperEnv, target, cmd)['return'][0].values()[0]
580 // check dry-run result
581 def validationSuccessStr = "Successfully validated v1 to v3 conversion"
582 if (!dryRunResult.contains(validationSuccessStr)) {
583 error("Calico data upgrade dry-run has failed")
584 }
Aleksei Kasatkinff9d5b52018-10-26 11:47:46 +0200585 }
586}
Victor Ryzhenkinae22a5a2018-10-12 15:52:27 +0400587
Aleksei Kasatkin9ce11842018-11-23 14:27:33 +0100588def checkCalicoPolicySetting(pepperEnv, target) {
589 def common = new com.mirantis.mk.Common()
590 def salt = new com.mirantis.mk.Salt()
591
592 stage("Checking of Calico network policy setting") {
593 // check Calico policy enabled
594 def cniPolicy = false
595 def addonsPolicy = false
596 def kubeCtrlRunning = false
597
598 // check CNI config
599 def cniCfgResult = salt.cmdRun(
600 pepperEnv, target, "cat /etc/cni/net.d/10-calico.conf"
601 )['return'][0].values()[0].toString()
602 def cniCfg = new JsonSlurper().parseText(cniCfgResult)
603 if (cniCfg.get("policy") != null) {
604 if (cniCfg["policy"].get("type") == "k8s") {
605 cniPolicy = true
606 } else {
607 common.warningMsg("Calico policy type is unknown or not set.")
608 }
609 }
610
611 // check k8s addons
612 def addonsResult = salt.cmdRun(
613 pepperEnv, target, "ls /etc/kubernetes/addons"
614 )['return'][0].values()[0].toString()
615 if (addonsResult.contains("calico_policy")) {
616 addonsPolicy = true
617 }
618
619 // check kube-controllers is running
620 def kubeCtrlResult = salt.cmdRun(
621 pepperEnv, target, "kubectl get pod -n kube-system --selector=k8s-app=calico-kube-controllers"
622 )['return'][0].values()[0].toString()
623 if (kubeCtrlResult.contains("Running")) {
624 kubeCtrlRunning = true
625 }
626
627 // It's safe to enable Calico policy any time, but it may be unsafe to disable it.
628 // So, no need to disable Calico policy for v3.x if it's not in use currently.
629 // But if Calico policy is in use already, it should be enabled after upgrade as well.
630
631 // check for consistency
632 if ((cniPolicy != addonsPolicy) || (addonsPolicy != kubeCtrlRunning)) {
633 caution = "ATTENTION. Calico policy setting cannot be determined reliably (enabled in CNI config: ${cniPolicy}, " +
634 "presence in k8s addons: ${addonsPolicy}, kube-controllers is running: ${kubeCtrlRunning})."
635 currentBuild.description += "<br><b>${caution}</b><br><br>"
636 common.warningMsg(caution)
637 } else {
638 common.infoMsg("Current Calico policy state is detected as: ${cniPolicy}")
639 if (cniPolicy) {
640 // Calico policy is in use. Check policy setting for v3.x.
641 common.infoMsg("Calico policy is in use. It should be enabled for v3.x as well.")
642 def saltPolicyResult = salt.getPillar(
643 pepperEnv, target, "kubernetes:pool:network:calico:policy"
644 )["return"][0].values()[0].toString()
645
646 common.infoMsg("kubernetes.pool.network.calico.policy: ${saltPolicyResult}")
647 if (saltPolicyResult.toLowerCase().contains("true")) {
648 common.infoMsg("Calico policy setting for v3.x is detected as: true")
649 } else {
650 caution = "ATTENTION. Currently, Calico is running with policy switched on. " +
651 "Calico policy setting for v3.x is not set to true. " +
652 "After upgrade is completed, Calico policy will be switched off. " +
653 "You will need to switch it on manually if required."
654 currentBuild.description += "<br><b>${caution}</b><br><br>"
655 common.warningMsg(caution)
656 }
657 }
658 }
659
660 if (addonsPolicy) {
661 // Remove v2.6.x policy-related addons on masters to not interfere with v3.x kube-controllers
662 salt.cmdRun(pepperEnv, CTL_TARGET, "rm -rf /etc/kubernetes/addons/calico_policy")
663 }
664 }
665}
666
Victor Ryzhenkinef34a022018-06-22 19:36:13 +0400667timeout(time: 12, unit: 'HOURS') {
668 node() {
669 try {
670
671 stage("Setup virtualenv for Pepper") {
672 python.setupPepperVirtualenv(pepperEnv, SALT_MASTER_URL, SALT_MASTER_CREDENTIALS)
673 }
674
Victor Ryzhenkind324a012019-01-23 05:39:34 +0400675 def ctl_node = salt.getMinionsSorted(pepperEnv, CTL_TARGET)[0]
676 def daemonsetMap = buildDaemonsetMap(pepperEnv, ctl_node)
677
Victor Ryzhenkinae22a5a2018-10-12 15:52:27 +0400678 if (CONFORMANCE_RUN_BEFORE.toBoolean()) {
679 def target = CTL_TARGET
680 def mcp_repo = ARTIFACTORY_URL
681 def k8s_api = TEST_K8S_API_SERVER
682 firstTarget = salt.getFirstMinion(pepperEnv, target)
Victor Ryzhenkin723bd062018-12-11 17:09:06 +0400683 def containerd_enabled = containerDenabled(pepperEnv, firstTarget)
684 def containerd_installed = containerDinstalled(pepperEnv, firstTarget)
685 def conformance_pod_ready = conformancePodDefExists(pepperEnv, firstTarget)
686 if (containerd_enabled && containerd_installed && conformance_pod_ready) {
687 def config = ['master': pepperEnv,
688 'target': firstTarget,
689 'junitResults': false,
690 'autodetect': true]
691 test.executeConformance(config)
692 } else {
693 executeConformance(pepperEnv, firstTarget, k8s_api, mcp_repo)
694 }
Victor Ryzhenkinae22a5a2018-10-12 15:52:27 +0400695 }
696
Victor Ryzhenkinef34a022018-06-22 19:36:13 +0400697 if ((common.validInputParam('KUBERNETES_HYPERKUBE_IMAGE')) && (common.validInputParam('KUBERNETES_PAUSE_IMAGE'))) {
698 overrideKubernetesImage(pepperEnv)
699 }
700
Aleksei Kasatkinff9d5b52018-10-26 11:47:46 +0200701 if ((common.validInputParam('KUBERNETES_CALICO_IMAGE'))
Victor Ryzhenkineb543bf2019-01-18 06:34:26 +0400702 && (common.validInputParam('KUBERNETES_CALICO_CALICOCTL_SOURCE'))
703 && (common.validInputParam('KUBERNETES_CALICO_CALICOCTL_SOURCE_HASH'))
704 && (common.validInputParam('KUBERNETES_CALICO_CNI_SOURCE'))
705 && (common.validInputParam('KUBERNETES_CALICO_CNI_SOURCE_HASH'))
706 && (common.validInputParam('KUBERNETES_CALICO_BIRDCL_SOURCE'))
707 && (common.validInputParam('KUBERNETES_CALICO_BIRDCL_SOURCE_HASH'))
708 && (common.validInputParam('KUBERNETES_CALICO_CNI_IPAM_SOURCE'))
709 && (common.validInputParam('KUBERNETES_CALICO_CNI_IPAM_SOURCE_HASH'))
Aleksei Kasatkinff9d5b52018-10-26 11:47:46 +0200710 && (common.validInputParam('KUBERNETES_CALICO_KUBE_CONTROLLERS_IMAGE'))
711 ) {
Aleksei Kasatkinff9d5b52018-10-26 11:47:46 +0200712 overrideCalicoImages(pepperEnv)
713 }
714
Victor Ryzhenkinef34a022018-06-22 19:36:13 +0400715 /*
Aleksei Kasatkinff9d5b52018-10-26 11:47:46 +0200716 * Execute Calico upgrade if needed (only for v2 to v3 upgrade).
717 * This part causes workloads operations downtime.
718 * It is only required for Calico v2.x to v3.x upgrade when etcd is in use for Calico
719 * as Calico etcd schema has different formats for Calico v2.x and Calico v3.x.
720 */
721 if (UPGRADE_CALICO_V2_TO_V3.toBoolean()) {
Aleksei Kasatkin1f4f5ba2018-11-20 18:30:36 +0100722 // get ETCD_ENDPOINTS in use by Calico
723 def ep_str = salt.cmdRun(pepperEnv, ctl_node, "cat /etc/calico/calicoctl.cfg | grep etcdEndpoints")['return'][0].values()[0]
724 ETCD_ENDPOINTS = ep_str.split("\n")[0].tokenize(' ')[1]
725 print("ETCD_ENDPOINTS in use by Calico: '${ETCD_ENDPOINTS}'")
726
727 // download calico-upgrade utility
Aleksei Kasatkinff9d5b52018-10-26 11:47:46 +0200728 downloadCalicoUpgrader(pepperEnv, ctl_node)
Aleksei Kasatkin1f4f5ba2018-11-20 18:30:36 +0100729
730 // check the possibility of upgrading of Calico
731 checkCalicoUpgradePossibility(pepperEnv, ctl_node)
732
Aleksei Kasatkin9ce11842018-11-23 14:27:33 +0100733 // check and adjust Calico policy setting
734 checkCalicoPolicySetting(pepperEnv, ctl_node)
735
Aleksei Kasatkinff9d5b52018-10-26 11:47:46 +0200736 // this sequence implies workloads operations downtime
737 startCalicoUpgrade(pepperEnv, ctl_node)
Aleksei Kasatkind9d682e2018-12-12 14:51:59 +0100738 performCalicoConfigurationUpdateAndServicesRestart(pepperEnv, POOL, ctl_node)
Aleksei Kasatkinff9d5b52018-10-26 11:47:46 +0200739 completeCalicoUpgrade(pepperEnv, ctl_node)
Aleksei Kasatkin5ccea272018-12-06 17:34:58 +0100740 // no downtime is expected after this point
Aleksei Kasatkinff9d5b52018-10-26 11:47:46 +0200741 }
742
743 /*
ashestakovc030dac2019-01-11 11:34:54 +0200744 * Execute etcd update
745 */
746 if ((common.validInputParam('KUBERNETES_ETCD_SOURCE')) && (common.validInputParam('KUBERNETES_ETCD_SOURCE_HASH'))) {
747 overrideEtcdSource(pepperEnv)
748 }
Victor Ryzhenkinc275d152019-01-24 13:52:21 +0400749 def targetHostsEtcd = salt.getMinionsSorted(pepperEnv, "I@etcd:server")
750 for (t in targetHostsEtcd) {
ashestakovc030dac2019-01-11 11:34:54 +0200751 performEtcdUpdateAndServicesRestart(pepperEnv, t)
752 }
753
754 /*
Aleksei Kasatkinff9d5b52018-10-26 11:47:46 +0200755 * Execute k8s update
Victor Ryzhenkinef34a022018-06-22 19:36:13 +0400756 */
757 if (updates.contains("ctl")) {
758 def target = CTL_TARGET
759
760 if (PER_NODE.toBoolean()) {
761 def targetHosts = salt.getMinionsSorted(pepperEnv, target)
762
763 for (t in targetHosts) {
Victor Ryzhenkin42e4b382018-09-11 17:57:56 +0400764 if (SIMPLE_UPGRADE.toBoolean()) {
765 performKubernetesControlUpdate(pepperEnv, t)
766 } else {
767 cordonNode(pepperEnv, t)
768 drainNode(pepperEnv, t)
769 regenerateCerts(pepperEnv, t)
Victor Ryzhenkin42e4b382018-09-11 17:57:56 +0400770 performKubernetesControlUpdate(pepperEnv, t)
Victor Ryzhenkinfd9677f2018-10-16 16:14:40 +0400771 updateAddonManager(pepperEnv, t)
Victor Ryzhenkind324a012019-01-23 05:39:34 +0400772 if (daemonsetMap) {
773 purgeDaemonsetPods(pepperEnv, t, daemonsetMap)
774 rebootKubernetesNode(pepperEnv, t)
775 }
Victor Ryzhenkin42e4b382018-09-11 17:57:56 +0400776 uncordonNode(pepperEnv, t)
777 }
Victor Ryzhenkinef34a022018-06-22 19:36:13 +0400778 }
779 } else {
780 performKubernetesControlUpdate(pepperEnv, target)
781 }
Victor Ryzhenkinfd9677f2018-10-16 16:14:40 +0400782 if (!SIMPLE_UPGRADE.toBoolean()) {
Aleksei Kasatkinff9d5b52018-10-26 11:47:46 +0200783 // Addons upgrade should be performed after all nodes will be upgraded
Victor Ryzhenkinfd9677f2018-10-16 16:14:40 +0400784 updateAddons(pepperEnv, target)
785 // Wait for 90 sec for addons reconciling
786 sleep(90)
787 }
Victor Ryzhenkinef34a022018-06-22 19:36:13 +0400788 }
789
790 if (updates.contains("cmp")) {
791 def target = CMP_TARGET
792
793 if (PER_NODE.toBoolean()) {
794 def targetHosts = salt.getMinionsSorted(pepperEnv, target)
795
796 for (t in targetHosts) {
Victor Ryzhenkin42e4b382018-09-11 17:57:56 +0400797 if (SIMPLE_UPGRADE.toBoolean()) {
798 performKubernetesComputeUpdate(pepperEnv, t)
799 } else {
800 cordonNode(pepperEnv, t)
801 drainNode(pepperEnv, t)
802 regenerateCerts(pepperEnv, t)
Victor Ryzhenkin42e4b382018-09-11 17:57:56 +0400803 performKubernetesComputeUpdate(pepperEnv, t)
Victor Ryzhenkind324a012019-01-23 05:39:34 +0400804 if (daemonsetMap) {
805 purgeDaemonsetPods(pepperEnv, t, daemonsetMap)
806 rebootKubernetesNode(pepperEnv, t)
807 }
Victor Ryzhenkin42e4b382018-09-11 17:57:56 +0400808 uncordonNode(pepperEnv, t)
809 }
Victor Ryzhenkinef34a022018-06-22 19:36:13 +0400810 }
811 } else {
812 performKubernetesComputeUpdate(pepperEnv, target)
813 }
814 }
Victor Ryzhenkinae22a5a2018-10-12 15:52:27 +0400815
Aleksei Kasatkin5ccea272018-12-06 17:34:58 +0100816 if (calicoEnabled(pepperEnv, ctl_node)) {
817 checkCalicoClusterState(pepperEnv, POOL)
818 }
Victor Ryzhenkineb543bf2019-01-18 06:34:26 +0400819 printVersionInfo(pepperEnv, ctl_node)
Aleksei Kasatkin5ccea272018-12-06 17:34:58 +0100820
Victor Ryzhenkinae22a5a2018-10-12 15:52:27 +0400821 if (CONFORMANCE_RUN_AFTER.toBoolean()) {
822 def target = CTL_TARGET
823 def mcp_repo = ARTIFACTORY_URL
824 def k8s_api = TEST_K8S_API_SERVER
825 firstTarget = salt.getFirstMinion(pepperEnv, target)
Victor Ryzhenkin723bd062018-12-11 17:09:06 +0400826 def containerd_enabled = containerDenabled(pepperEnv, firstTarget)
827 def containerd_installed = containerDinstalled(pepperEnv, firstTarget)
828 def conformance_pod_ready = conformancePodDefExists(pepperEnv, firstTarget)
829 if (containerd_enabled && containerd_installed && conformance_pod_ready) {
830 def config = ['master': pepperEnv,
831 'target': firstTarget,
832 'junitResults': false,
833 'autodetect': true]
834 test.executeConformance(config)
835 } else {
836 executeConformance(pepperEnv, firstTarget, k8s_api, mcp_repo)
837 }
Victor Ryzhenkinae22a5a2018-10-12 15:52:27 +0400838 }
Victor Ryzhenkinef34a022018-06-22 19:36:13 +0400839 } catch (Throwable e) {
840 // If there was an error or exception thrown, the build failed
841 currentBuild.result = "FAILURE"
842 currentBuild.description = currentBuild.description ? e.message + " " + currentBuild.description : e.message
843 throw e
844 }
845 }
Victor Ryzhenkind324a012019-01-23 05:39:34 +0400846}