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) |
Victor Ryzhenkin | 42e4b38 | 2018-09-11 17:57:56 +0400 | [diff] [blame] | 13 | * SIMPLE_UPGRADE Use previous version of upgrade without conron/drain abilities |
| 14 | * UPGRADE_DOCKER Upgrade docker component |
Victor Ryzhenkin | ef34a02 | 2018-06-22 19:36:13 +0400 | [diff] [blame] | 15 | * |
| 16 | **/ |
| 17 | def common = new com.mirantis.mk.Common() |
| 18 | def salt = new com.mirantis.mk.Salt() |
| 19 | def python = new com.mirantis.mk.Python() |
| 20 | |
| 21 | def updates = TARGET_UPDATES.tokenize(",").collect{it -> it.trim()} |
| 22 | def pepperEnv = "pepperEnv" |
| 23 | |
| 24 | def overrideKubernetesImage(pepperEnv) { |
| 25 | def salt = new com.mirantis.mk.Salt() |
| 26 | |
| 27 | def k8sSaltOverrides = """ |
| 28 | kubernetes_hyperkube_image: ${KUBERNETES_HYPERKUBE_IMAGE} |
| 29 | kubernetes_pause_image: ${KUBERNETES_PAUSE_IMAGE} |
| 30 | """ |
| 31 | stage("Override kubernetes images to target version") { |
| 32 | salt.setSaltOverrides(pepperEnv, k8sSaltOverrides) |
| 33 | } |
| 34 | } |
| 35 | |
| 36 | def performKubernetesComputeUpdate(pepperEnv, target) { |
| 37 | def salt = new com.mirantis.mk.Salt() |
| 38 | |
| 39 | stage("Execute Kubernetes compute update on ${target}") { |
| 40 | salt.enforceState(pepperEnv, target, 'kubernetes.pool') |
| 41 | salt.runSaltProcessStep(pepperEnv, target, 'service.restart', ['kubelet']) |
| 42 | } |
| 43 | } |
| 44 | |
| 45 | def performKubernetesControlUpdate(pepperEnv, target) { |
| 46 | def salt = new com.mirantis.mk.Salt() |
| 47 | |
| 48 | stage("Execute Kubernetes control plane update on ${target}") { |
| 49 | salt.enforceStateWithExclude(pepperEnv, target, "kubernetes", "kubernetes.master.setup") |
| 50 | // Restart kubelet |
| 51 | salt.runSaltProcessStep(pepperEnv, target, 'service.restart', ['kubelet']) |
| 52 | } |
| 53 | } |
| 54 | |
Victor Ryzhenkin | 42e4b38 | 2018-09-11 17:57:56 +0400 | [diff] [blame] | 55 | def cordonNode(pepperEnv, target) { |
| 56 | def salt = new com.mirantis.mk.Salt() |
| 57 | def originalTarget = "I@kubernetes:master and not ${target}" |
| 58 | |
| 59 | stage("Cordoning ${target} kubernetes node") { |
| 60 | def nodeShortName = target.tokenize(".")[0] |
| 61 | salt.cmdRun(pepperEnv, originalTarget, "kubectl cordon ${nodeShortName}", true, 1) |
| 62 | } |
| 63 | } |
| 64 | |
| 65 | def uncordonNode(pepperEnv, target) { |
| 66 | def salt = new com.mirantis.mk.Salt() |
| 67 | def originalTarget = "I@kubernetes:master and not ${target}" |
| 68 | |
| 69 | stage("Uncordoning ${target} kubernetes node") { |
| 70 | def nodeShortName = target.tokenize(".")[0] |
| 71 | salt.cmdRun(pepperEnv, originalTarget, "kubectl uncordon ${nodeShortName}", true, 1) |
| 72 | } |
| 73 | } |
| 74 | |
| 75 | def drainNode(pepperEnv, target) { |
| 76 | def salt = new com.mirantis.mk.Salt() |
| 77 | def originalTarget = "I@kubernetes:master and not ${target}" |
| 78 | |
| 79 | stage("Draining ${target} kubernetes node") { |
| 80 | def nodeShortName = target.tokenize(".")[0] |
| 81 | salt.cmdRun(pepperEnv, originalTarget, "kubectl drain --force --ignore-daemonsets --grace-period 100 --timeout 300s --delete-local-data ${nodeShortName}", true, 1) |
| 82 | } |
| 83 | } |
| 84 | |
| 85 | def regenerateCerts(pepperEnv, target) { |
| 86 | def salt = new com.mirantis.mk.Salt() |
| 87 | |
| 88 | stage("Regenerate certs for ${target}") { |
| 89 | salt.enforceState(pepperEnv, target, 'salt.minion.cert') |
| 90 | } |
| 91 | } |
| 92 | |
Victor Ryzhenkin | ae90918 | 2018-10-02 17:49:18 +0400 | [diff] [blame] | 93 | def updateAddons(pepperEnv, target) { |
| 94 | def salt = new com.mirantis.mk.Salt() |
| 95 | |
| 96 | stage("Upgrading Addons at ${target}") { |
Victor Ryzhenkin | 40625bc | 2018-10-04 16:15:27 +0400 | [diff] [blame] | 97 | salt.enforceState(pepperEnv, target, "kubernetes.master.kube-addons") |
Victor Ryzhenkin | ae90918 | 2018-10-02 17:49:18 +0400 | [diff] [blame] | 98 | salt.enforceState(pepperEnv, target, "kubernetes.master.setup") |
| 99 | } |
| 100 | } |
| 101 | |
Victor Ryzhenkin | 42e4b38 | 2018-09-11 17:57:56 +0400 | [diff] [blame] | 102 | def upgradeDocker(pepperEnv, target) { |
| 103 | def salt = new com.mirantis.mk.Salt() |
| 104 | |
| 105 | stage("Upgrading docker at ${target}") { |
| 106 | salt.enforceState(pepperEnv, target, 'docker.host') |
| 107 | } |
| 108 | } |
Victor Ryzhenkin | ef34a02 | 2018-06-22 19:36:13 +0400 | [diff] [blame] | 109 | |
| 110 | timeout(time: 12, unit: 'HOURS') { |
| 111 | node() { |
| 112 | try { |
| 113 | |
| 114 | stage("Setup virtualenv for Pepper") { |
| 115 | python.setupPepperVirtualenv(pepperEnv, SALT_MASTER_URL, SALT_MASTER_CREDENTIALS) |
| 116 | } |
| 117 | |
| 118 | if ((common.validInputParam('KUBERNETES_HYPERKUBE_IMAGE')) && (common.validInputParam('KUBERNETES_PAUSE_IMAGE'))) { |
| 119 | overrideKubernetesImage(pepperEnv) |
| 120 | } |
| 121 | |
| 122 | /* |
| 123 | * Execute update |
| 124 | */ |
| 125 | if (updates.contains("ctl")) { |
| 126 | def target = CTL_TARGET |
| 127 | |
| 128 | if (PER_NODE.toBoolean()) { |
| 129 | def targetHosts = salt.getMinionsSorted(pepperEnv, target) |
| 130 | |
| 131 | for (t in targetHosts) { |
Victor Ryzhenkin | 42e4b38 | 2018-09-11 17:57:56 +0400 | [diff] [blame] | 132 | if (SIMPLE_UPGRADE.toBoolean()) { |
| 133 | performKubernetesControlUpdate(pepperEnv, t) |
| 134 | } else { |
| 135 | cordonNode(pepperEnv, t) |
| 136 | drainNode(pepperEnv, t) |
| 137 | regenerateCerts(pepperEnv, t) |
| 138 | if (UPGRADE_DOCKER.toBoolean()) { |
| 139 | upgradeDocker(pepperEnv, t) |
| 140 | } |
| 141 | performKubernetesControlUpdate(pepperEnv, t) |
Victor Ryzhenkin | ae90918 | 2018-10-02 17:49:18 +0400 | [diff] [blame] | 142 | updateAddons(pepperEnv, t) |
Victor Ryzhenkin | 42e4b38 | 2018-09-11 17:57:56 +0400 | [diff] [blame] | 143 | uncordonNode(pepperEnv, t) |
| 144 | } |
Victor Ryzhenkin | ef34a02 | 2018-06-22 19:36:13 +0400 | [diff] [blame] | 145 | } |
| 146 | } else { |
| 147 | performKubernetesControlUpdate(pepperEnv, target) |
| 148 | } |
| 149 | } |
| 150 | |
| 151 | if (updates.contains("cmp")) { |
| 152 | def target = CMP_TARGET |
| 153 | |
| 154 | if (PER_NODE.toBoolean()) { |
| 155 | def targetHosts = salt.getMinionsSorted(pepperEnv, target) |
| 156 | |
| 157 | for (t in targetHosts) { |
Victor Ryzhenkin | 42e4b38 | 2018-09-11 17:57:56 +0400 | [diff] [blame] | 158 | if (SIMPLE_UPGRADE.toBoolean()) { |
| 159 | performKubernetesComputeUpdate(pepperEnv, t) |
| 160 | } else { |
| 161 | cordonNode(pepperEnv, t) |
| 162 | drainNode(pepperEnv, t) |
| 163 | regenerateCerts(pepperEnv, t) |
| 164 | if (UPGRADE_DOCKER.toBoolean()) { |
| 165 | upgradeDocker(pepperEnv, t) |
| 166 | } |
| 167 | performKubernetesComputeUpdate(pepperEnv, t) |
| 168 | uncordonNode(pepperEnv, t) |
| 169 | } |
Victor Ryzhenkin | ef34a02 | 2018-06-22 19:36:13 +0400 | [diff] [blame] | 170 | } |
| 171 | } else { |
| 172 | performKubernetesComputeUpdate(pepperEnv, target) |
| 173 | } |
| 174 | } |
| 175 | } catch (Throwable e) { |
| 176 | // If there was an error or exception thrown, the build failed |
| 177 | currentBuild.result = "FAILURE" |
| 178 | currentBuild.description = currentBuild.description ? e.message + " " + currentBuild.description : e.message |
| 179 | throw e |
| 180 | } |
| 181 | } |
| 182 | } |