Victor Ryzhenkin | ef34a02 | 2018-06-22 19:36:13 +0400 | [diff] [blame] | 1 | /** |
| 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) |
| 13 | * |
| 14 | **/ |
| 15 | def common = new com.mirantis.mk.Common() |
| 16 | def salt = new com.mirantis.mk.Salt() |
| 17 | def python = new com.mirantis.mk.Python() |
| 18 | |
| 19 | def updates = TARGET_UPDATES.tokenize(",").collect{it -> it.trim()} |
| 20 | def pepperEnv = "pepperEnv" |
| 21 | |
| 22 | def overrideKubernetesImage(pepperEnv) { |
| 23 | def salt = new com.mirantis.mk.Salt() |
| 24 | |
| 25 | def k8sSaltOverrides = """ |
| 26 | kubernetes_hyperkube_image: ${KUBERNETES_HYPERKUBE_IMAGE} |
| 27 | kubernetes_pause_image: ${KUBERNETES_PAUSE_IMAGE} |
| 28 | """ |
| 29 | stage("Override kubernetes images to target version") { |
| 30 | salt.setSaltOverrides(pepperEnv, k8sSaltOverrides) |
| 31 | } |
| 32 | } |
| 33 | |
| 34 | def performKubernetesComputeUpdate(pepperEnv, target) { |
| 35 | def salt = new com.mirantis.mk.Salt() |
| 36 | |
| 37 | stage("Execute Kubernetes compute update on ${target}") { |
| 38 | salt.enforceState(pepperEnv, target, 'kubernetes.pool') |
| 39 | salt.runSaltProcessStep(pepperEnv, target, 'service.restart', ['kubelet']) |
| 40 | } |
| 41 | } |
| 42 | |
| 43 | def performKubernetesControlUpdate(pepperEnv, target) { |
| 44 | def salt = new com.mirantis.mk.Salt() |
| 45 | |
| 46 | stage("Execute Kubernetes control plane update on ${target}") { |
| 47 | salt.enforceStateWithExclude(pepperEnv, target, "kubernetes", "kubernetes.master.setup") |
| 48 | // Restart kubelet |
| 49 | salt.runSaltProcessStep(pepperEnv, target, 'service.restart', ['kubelet']) |
| 50 | } |
| 51 | } |
| 52 | |
| 53 | |
| 54 | timeout(time: 12, unit: 'HOURS') { |
| 55 | node() { |
| 56 | try { |
| 57 | |
| 58 | stage("Setup virtualenv for Pepper") { |
| 59 | python.setupPepperVirtualenv(pepperEnv, SALT_MASTER_URL, SALT_MASTER_CREDENTIALS) |
| 60 | } |
| 61 | |
| 62 | if ((common.validInputParam('KUBERNETES_HYPERKUBE_IMAGE')) && (common.validInputParam('KUBERNETES_PAUSE_IMAGE'))) { |
| 63 | overrideKubernetesImage(pepperEnv) |
| 64 | } |
| 65 | |
| 66 | /* |
| 67 | * Execute update |
| 68 | */ |
| 69 | if (updates.contains("ctl")) { |
| 70 | def target = CTL_TARGET |
| 71 | |
| 72 | if (PER_NODE.toBoolean()) { |
| 73 | def targetHosts = salt.getMinionsSorted(pepperEnv, target) |
| 74 | |
| 75 | for (t in targetHosts) { |
| 76 | performKubernetesControlUpdate(pepperEnv, t) |
| 77 | } |
| 78 | } else { |
| 79 | performKubernetesControlUpdate(pepperEnv, target) |
| 80 | } |
| 81 | } |
| 82 | |
| 83 | if (updates.contains("cmp")) { |
| 84 | def target = CMP_TARGET |
| 85 | |
| 86 | if (PER_NODE.toBoolean()) { |
| 87 | def targetHosts = salt.getMinionsSorted(pepperEnv, target) |
| 88 | |
| 89 | for (t in targetHosts) { |
| 90 | performKubernetesComputeUpdate(pepperEnv, t) |
| 91 | } |
| 92 | } else { |
| 93 | performKubernetesComputeUpdate(pepperEnv, target) |
| 94 | } |
| 95 | } |
| 96 | } catch (Throwable e) { |
| 97 | // If there was an error or exception thrown, the build failed |
| 98 | currentBuild.result = "FAILURE" |
| 99 | currentBuild.description = currentBuild.description ? e.message + " " + currentBuild.description : e.message |
| 100 | throw e |
| 101 | } |
| 102 | } |
| 103 | } |