blob: 224040f69f7901af5825630b3e06082881fd10c0 [file] [log] [blame]
Victor Ryzhenkinef34a022018-06-22 19:36:13 +04001/**
2 * Update kuberentes cluster
3 *
4 * Expected parameters:
Victor Ryzhenkin3401ee62019-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).
ashestakov138b0a52019-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 Ryzhenkin3401ee62019-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 Ryzhenkin3401ee62019-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
ashestakov138b0a52019-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 Ryzhenkinc2024132019-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 Ryzhenkinc2024132019-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
Victor Ryzhenkineb7e7482019-03-28 02:11:28 +0400271 status = salt.cmdRun(pepperEnv, firstTarget, "kubectl get no ${nodeShortName} | tail -n+2 | awk '{print \$2}'"
Victor Ryzhenkinc2024132019-01-23 05:39:34 +0400272 )['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 Ryzhenkin6a2cf782019-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 Ryzhenkinc2024132019-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) {
Victor Ryzhenkin7e28c152019-03-28 02:23:42 +0400355 def image = buildImageURL(pepperEnv, target, mcp_repo)
356 print("Using image: " + image)
357 runConformance(pepperEnv, target, k8s_api, image)
Victor Ryzhenkinae22a5a2018-10-12 15:52:27 +0400358}
359
Victor Ryzhenkin723bd062018-12-11 17:09:06 +0400360def containerDinstalled(pepperEnv, target) {
361 def salt = new com.mirantis.mk.Salt()
362 return salt.cmdRun(pepperEnv, target, "containerd --version 2>1 1>/dev/null && echo 'true' || echo 'false'"
363 )['return'][0].values()[0].replaceAll('Salt command execution success','').trim().toBoolean()
364}
365
366def containerDenabled(pepperEnv, target) {
367 def salt = new com.mirantis.mk.Salt()
Victor Ryzhenkin71ecdf42018-12-11 22:22:50 +0400368 return salt.getPillar(pepperEnv, target, "kubernetes:common:containerd:enabled"
Victor Ryzhenkin723bd062018-12-11 17:09:06 +0400369 )["return"][0].values()[0].toBoolean()
370}
371
372def conformancePodDefExists(pepperEnv, target) {
373 def salt = new com.mirantis.mk.Salt()
374 return salt.cmdRun(pepperEnv, target, "test -e /srv/kubernetes/conformance.yml && echo 'true' || echo 'false'"
375 )['return'][0].values()[0].replaceAll('Salt command execution success','').trim().toBoolean()
376}
377
Victor Ryzhenkin3401ee62019-01-18 06:34:26 +0400378def printVersionInfo(pepperEnv, target) {
379 def salt = new com.mirantis.mk.Salt()
380 def common = new com.mirantis.mk.Common()
381
382 stage("Gather version and runtime information") {
383 common.infoMsg("Version and runtime info:")
384 salt.cmdRun(pepperEnv, target, "kubectl get no -o wide")
385 common.infoMsg("Cluster health info:")
386 salt.cmdRun(pepperEnv, target, "kubectl get cs")
387 common.infoMsg("ETCD health info:")
Victor Ryzhenkin3029c8b2019-01-18 22:17:57 +0400388 salt.cmdRun(pepperEnv, target, ". /var/lib/etcd/configenv && etcdctl cluster-health")
Victor Ryzhenkin3401ee62019-01-18 06:34:26 +0400389 common.infoMsg("Calico peers info:")
390 salt.cmdRun(pepperEnv, target, "calicoctl node status")
391 }
392}
393
Aleksei Kasatkin5ccea272018-12-06 17:34:58 +0100394def calicoEnabled(pepperEnv, target) {
395 def salt = new com.mirantis.mk.Salt()
396 return salt.getPillar(pepperEnv, target, "kubernetes:pool:network:calico:enabled"
397 )["return"][0].values()[0].toBoolean()
398}
399
400def checkCalicoClusterState(pepperEnv, target) {
401 def common = new com.mirantis.mk.Common()
Aleksei Kasatkinff9d5b52018-10-26 11:47:46 +0200402 def salt = new com.mirantis.mk.Salt()
403
Aleksei Kasatkin5ccea272018-12-06 17:34:58 +0100404 stage("Checking Calico cluster state after upgrade") {
405 // check Calico cluster and cli clients versions
406 def checkVer = [
407 "Client Version:": [verStr: "", dif: false, wrong: false],
408 "Cluster Version:": [verStr: "", dif: false, wrong: false]
409 ]
410 def checkVerPassed = true
411 def versionResults = salt.cmdRun(pepperEnv, target, "calicoctl version | grep -i version")['return'][0]
412 versionResults.each { k, v ->
413 // println("Node:\n${k}\nResult:\n${v}")
414 for (verLine in v.split("\n")) {
415 for (verType in checkVer.keySet()) {
416 if (verLine.contains(verType)) {
417 def verRec = checkVer[verType]
418 ver = (verLine - verType).trim()
419 if (!verRec.verStr) {
420 verRec.verStr = ver
421 }
422 if (verRec.verStr != ver) {
423 verRec.dif = true
424 checkVerPassed = false
425 }
426 version = ver.tokenize(".")
427 if ((version.size() < 3) || (version[0] != "v3")) {
428 verRec.wrong = true
429 checkVerPassed = false
430 }
431 checkVer[verType] = verRec
432 }
433 }
434 }
435 }
436 if (checkVerPassed) {
437 common.infoMsg("Calico version verification passed")
438 }
439 else {
440 def warningMsg = "Calico version verification failed.\n"
441 checkVer.each { k, rec ->
442 if (rec.dif) {
443 warningMsg += "${k} versions are different across nodes.\n"
444 }
445 if (rec.wrong) {
446 warningMsg += "${k} (some) versions are wrong - should be v3.x.\n"
447 }
448 }
449 common.warningMsg(warningMsg)
450 currentBuild.description += "<br><b>${warningMsg}</b><br><br>"
451 }
452
453 // check Calico nodes' statuses
454 def nodeStatusResults = salt.cmdRun(pepperEnv, target, "calicoctl node status")['return'][0]
455 def nodesRunning = true
456 def peersNotFound = []
457 def peersNotOnline = []
458 nodeStatusResults.each { k, v ->
459 // println("Node:\n${k}\nResult:\n${v}")
460 if (!v.contains("Calico process is running")) {
461 nodesRunning = false
462 def warningMsg = "Node ${k}: Calico node is not running."
463 common.warningMsg(warningMsg)
464 currentBuild.description += "<br><b>${warningMsg}</b><br><br>"
465 }
466 def nodePeersFound = false
467 def nodePeersOnline = true
468 for (nodeLine in v.split("\n")) {
469 if (nodeLine.contains("|") && (!nodeLine.contains("STATE"))) {
470 def col = nodeLine.tokenize("|").collect{it.trim()}
471 if (col.size() == 5) {
472 nodePeersFound = true
473 if ((col[2] != "up") || (col[4] != "Established")) {
474 def warningMsg = "Node ${k}: BGP peer '${col[0]}' is out of reach. Peer state: '${col[2]}', connection info: '${col[4]}'."
475 common.warningMsg(warningMsg)
476 currentBuild.description += "<br><b>${warningMsg}</b><br><br>"
477 nodePeersOnline = false
478 }
479 }
480 }
481 }
482 if (!nodePeersFound) {
483 peersNotFound += k
484 }
485 if (!nodePeersOnline) {
486 peersNotOnline += k
487 }
488 }
489 if (nodesRunning) {
490 common.infoMsg("All the Calico nodes are running")
491 }
492 if (peersNotFound) {
493 def warningMsg = "BGP peers not found for the node(s): " + peersNotFound.join(', ') + "."
494 common.warningMsg(warningMsg)
495 currentBuild.description += "<br><b>${warningMsg}</b><br><br>"
496 } else {
497 common.infoMsg("BGP peers were found for all the nodes")
498 }
499 if (!peersNotOnline) {
500 common.infoMsg("All reported BGP peers are reachable")
501 }
502
503 // check that 'calico-kube-controllers' is running
504 // one CTL node will be used to get pod's state using kubectl
505 def ctl_node = salt.getMinionsSorted(pepperEnv, CTL_TARGET)[0]
506 def kubeCtrlResult = salt.cmdRun(
507 pepperEnv, ctl_node, "kubectl get pod -n kube-system --selector=k8s-app=calico-kube-controllers"
508 )['return'][0].values()[0].toString()
509 if (kubeCtrlResult.contains("calico-kube-controllers")) {
510 for (line in kubeCtrlResult.split("\n")) {
511 if (line.contains("calico-kube-controllers")) {
512 col = line.tokenize(" ")
513 if ((col[1] != "1/1") || (col[2] != "Running")) {
514 def warningMsg = "Calico kube-controllers pod is not running properly."
515 common.warningMsg(warningMsg)
516 currentBuild.description += "<br><b>${warningMsg}</b><br><br>"
517 }
518 else {
519 common.infoMsg("Calico kube-controllers pod is running.")
520 }
521 break
522 }
523 }
524 } else {
525 def warningMsg = "Calico kube-controllers pod was not scheduled."
526 common.warningMsg(warningMsg)
527 currentBuild.description += "<br><b>${warningMsg}</b><br><br>"
528 }
Aleksei Kasatkin1f4f5ba2018-11-20 18:30:36 +0100529 }
530}
531
532def checkCalicoUpgradePossibility(pepperEnv, target) {
533 def salt = new com.mirantis.mk.Salt()
534
535 stage("Verification of Calico upgrade possibility") {
536 // check Calico version
537 def versionResult = salt.cmdRun(
538 pepperEnv, target, "calicoctl version | grep 'Cluster Version'"
539 )['return'][0].values()[0].split("\n")[0].trim()
540 versionStr = (versionResult - "Cluster Version:").trim()
541 version = versionStr.tokenize(".")
542 if ((version.size() < 3) || (version[0] != "v2") || (version[1] != "6") || (version[2].toInteger() < 5)) {
543 error(
544 "Current Calico ${versionStr} cannot be upgraded to v3.x. " +
545 "Calico v2.6.x starting from v2.6.5 can be upgraded. " +
546 "For earlier versions, please update to v2.6.5 first."
547 )
548 }
549 print("Calico version was determined: ${versionStr}")
550
551 // check Calico is switched on
552 def readinessResult = salt.cmdRun(
553 pepperEnv, target, ". /var/lib/etcd/configenv && etcdctl get /calico/v1/Ready"
554 )['return'][0].values()[0].split("\n")[0].trim()
555 print("Calico readiness check result: ${readinessResult}")
556 if (readinessResult != "true") {
557 // try set it to true
558 readinessResult = salt.cmdRun(
559 pepperEnv, target, ". /var/lib/etcd/configenv && etcdctl set /calico/v1/Ready true"
560 )['return'][0].values()[0].split("\n")[0].trim()
561 print("Calico readiness result 2nd attempt: ${readinessResult}")
562 if (readinessResult != "true") {
563 error("Calico is not ready. '/calico/v1/Ready': '${readinessResult}'")
564 }
565 }
566
567 // Calico data upgrade dry-run
568 def cmd = "export APIV1_ETCD_ENDPOINTS=${ETCD_ENDPOINTS} && " +
569 "export APIV1_ETCD_CA_CERT_FILE=/var/lib/etcd/ca.pem && " +
570 "export APIV1_ETCD_CERT_FILE=/var/lib/etcd/etcd-client.crt && " +
571 "export APIV1_ETCD_KEY_FILE=/var/lib/etcd/etcd-client.key && " +
572 "export ETCD_ENDPOINTS=${ETCD_ENDPOINTS} && " +
573 "export ETCD_CA_CERT_FILE=/var/lib/etcd/ca.pem && " +
574 "export ETCD_CERT_FILE=/var/lib/etcd/etcd-client.crt && " +
575 "export ETCD_KEY_FILE=/var/lib/etcd/etcd-client.key && " +
576 "./calico-upgrade dry-run --ignore-v3-data"
577 def dryRunResult = salt.cmdRun(pepperEnv, target, cmd)['return'][0].values()[0]
578 // check dry-run result
579 def validationSuccessStr = "Successfully validated v1 to v3 conversion"
580 if (!dryRunResult.contains(validationSuccessStr)) {
581 error("Calico data upgrade dry-run has failed")
582 }
Aleksei Kasatkinff9d5b52018-10-26 11:47:46 +0200583 }
584}
Victor Ryzhenkinae22a5a2018-10-12 15:52:27 +0400585
Aleksei Kasatkin9ce11842018-11-23 14:27:33 +0100586def checkCalicoPolicySetting(pepperEnv, target) {
587 def common = new com.mirantis.mk.Common()
588 def salt = new com.mirantis.mk.Salt()
589
590 stage("Checking of Calico network policy setting") {
591 // check Calico policy enabled
592 def cniPolicy = false
593 def addonsPolicy = false
594 def kubeCtrlRunning = false
595
596 // check CNI config
597 def cniCfgResult = salt.cmdRun(
598 pepperEnv, target, "cat /etc/cni/net.d/10-calico.conf"
599 )['return'][0].values()[0].toString()
600 def cniCfg = new JsonSlurper().parseText(cniCfgResult)
601 if (cniCfg.get("policy") != null) {
602 if (cniCfg["policy"].get("type") == "k8s") {
603 cniPolicy = true
604 } else {
605 common.warningMsg("Calico policy type is unknown or not set.")
606 }
607 }
608
609 // check k8s addons
610 def addonsResult = salt.cmdRun(
611 pepperEnv, target, "ls /etc/kubernetes/addons"
612 )['return'][0].values()[0].toString()
613 if (addonsResult.contains("calico_policy")) {
614 addonsPolicy = true
615 }
616
617 // check kube-controllers is running
618 def kubeCtrlResult = salt.cmdRun(
619 pepperEnv, target, "kubectl get pod -n kube-system --selector=k8s-app=calico-kube-controllers"
620 )['return'][0].values()[0].toString()
621 if (kubeCtrlResult.contains("Running")) {
622 kubeCtrlRunning = true
623 }
624
625 // It's safe to enable Calico policy any time, but it may be unsafe to disable it.
626 // So, no need to disable Calico policy for v3.x if it's not in use currently.
627 // But if Calico policy is in use already, it should be enabled after upgrade as well.
628
629 // check for consistency
630 if ((cniPolicy != addonsPolicy) || (addonsPolicy != kubeCtrlRunning)) {
631 caution = "ATTENTION. Calico policy setting cannot be determined reliably (enabled in CNI config: ${cniPolicy}, " +
632 "presence in k8s addons: ${addonsPolicy}, kube-controllers is running: ${kubeCtrlRunning})."
633 currentBuild.description += "<br><b>${caution}</b><br><br>"
634 common.warningMsg(caution)
635 } else {
636 common.infoMsg("Current Calico policy state is detected as: ${cniPolicy}")
637 if (cniPolicy) {
638 // Calico policy is in use. Check policy setting for v3.x.
639 common.infoMsg("Calico policy is in use. It should be enabled for v3.x as well.")
640 def saltPolicyResult = salt.getPillar(
641 pepperEnv, target, "kubernetes:pool:network:calico:policy"
642 )["return"][0].values()[0].toString()
643
644 common.infoMsg("kubernetes.pool.network.calico.policy: ${saltPolicyResult}")
645 if (saltPolicyResult.toLowerCase().contains("true")) {
646 common.infoMsg("Calico policy setting for v3.x is detected as: true")
647 } else {
648 caution = "ATTENTION. Currently, Calico is running with policy switched on. " +
649 "Calico policy setting for v3.x is not set to true. " +
650 "After upgrade is completed, Calico policy will be switched off. " +
651 "You will need to switch it on manually if required."
652 currentBuild.description += "<br><b>${caution}</b><br><br>"
653 common.warningMsg(caution)
654 }
655 }
656 }
657
658 if (addonsPolicy) {
659 // Remove v2.6.x policy-related addons on masters to not interfere with v3.x kube-controllers
660 salt.cmdRun(pepperEnv, CTL_TARGET, "rm -rf /etc/kubernetes/addons/calico_policy")
661 }
662 }
663}
664
Victor Ryzhenkinef34a022018-06-22 19:36:13 +0400665timeout(time: 12, unit: 'HOURS') {
666 node() {
667 try {
668
669 stage("Setup virtualenv for Pepper") {
670 python.setupPepperVirtualenv(pepperEnv, SALT_MASTER_URL, SALT_MASTER_CREDENTIALS)
671 }
672
Victor Ryzhenkinc2024132019-01-23 05:39:34 +0400673 def ctl_node = salt.getMinionsSorted(pepperEnv, CTL_TARGET)[0]
674 def daemonsetMap = buildDaemonsetMap(pepperEnv, ctl_node)
675
Victor Ryzhenkinae22a5a2018-10-12 15:52:27 +0400676 if (CONFORMANCE_RUN_BEFORE.toBoolean()) {
Victor Ryzhenkin7e28c152019-03-28 02:23:42 +0400677 stage("Perform conformance run before upgrade") {
678 def target = CTL_TARGET
679 def mcp_repo = ARTIFACTORY_URL
680 def k8s_api = TEST_K8S_API_SERVER
681 firstTarget = salt.getFirstMinion(pepperEnv, target)
682 def containerd_enabled = containerDenabled(pepperEnv, firstTarget)
683 def containerd_installed = containerDinstalled(pepperEnv, firstTarget)
684 def conformance_pod_ready = conformancePodDefExists(pepperEnv, firstTarget)
685 if (containerd_enabled && containerd_installed && conformance_pod_ready) {
686 def config = ['master': pepperEnv,
687 'target': firstTarget,
688 'junitResults': false,
689 'autodetect': true]
690 test.executeConformance(config)
691 } else {
692 executeConformance(pepperEnv, firstTarget, k8s_api, mcp_repo)
693 }
Victor Ryzhenkin723bd062018-12-11 17:09:06 +0400694 }
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 Ryzhenkin3401ee62019-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 /*
ashestakov138b0a52019-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 Ryzhenkin2c77cda2019-01-24 13:52:21 +0400749 def targetHostsEtcd = salt.getMinionsSorted(pepperEnv, "I@etcd:server")
750 for (t in targetHostsEtcd) {
ashestakov138b0a52019-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 Ryzhenkinc2024132019-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 Ryzhenkinc2024132019-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 Ryzhenkin3401ee62019-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()) {
Victor Ryzhenkin7e28c152019-03-28 02:23:42 +0400822 stage("Perform conformance run after upgrade") {
823 def target = CTL_TARGET
824 def mcp_repo = ARTIFACTORY_URL
825 def k8s_api = TEST_K8S_API_SERVER
826 firstTarget = salt.getFirstMinion(pepperEnv, target)
827 def containerd_enabled = containerDenabled(pepperEnv, firstTarget)
828 def containerd_installed = containerDinstalled(pepperEnv, firstTarget)
829 def conformance_pod_ready = conformancePodDefExists(pepperEnv, firstTarget)
830 if (containerd_enabled && containerd_installed && conformance_pod_ready) {
831 def config = ['master': pepperEnv,
832 'target': firstTarget,
833 'junitResults': false,
834 'autodetect': true]
835 test.executeConformance(config)
836 } else {
837 executeConformance(pepperEnv, firstTarget, k8s_api, mcp_repo)
838 }
Victor Ryzhenkin723bd062018-12-11 17:09:06 +0400839 }
Victor Ryzhenkinae22a5a2018-10-12 15:52:27 +0400840 }
Victor Ryzhenkinef34a022018-06-22 19:36:13 +0400841 } catch (Throwable e) {
842 // If there was an error or exception thrown, the build failed
843 currentBuild.result = "FAILURE"
844 currentBuild.description = currentBuild.description ? e.message + " " + currentBuild.description : e.message
845 throw e
846 }
847 }
Victor Ryzhenkinc2024132019-01-23 05:39:34 +0400848}