Joey Armstrong | 7496127 | 2023-01-12 13:35:54 -0500 | [diff] [blame] | 1 | #!/usr/bin/env groovy |
| 2 | // ----------------------------------------------------------------------- |
Joey Armstrong | 16bd8e8 | 2023-01-12 15:20:06 -0500 | [diff] [blame] | 3 | // Copyright 2021-2023 Open Networking Foundation (ONF) and the ONF Contributors |
| 4 | // |
| 5 | // Licensed under the Apache License, Version 2.0 (the "License"); |
| 6 | // you may not use this file except in compliance with the License. |
| 7 | // You may obtain a copy of the License at |
| 8 | // |
| 9 | // http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | // |
| 11 | // Unless required by applicable law or agreed to in writing, software |
| 12 | // distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | // See the License for the specific language governing permissions and |
| 15 | // limitations under the License. |
| 16 | // ----------------------------------------------------------------------- |
| 17 | // Intent: sets up a kubernetes cluster (using kind) |
Joey Armstrong | 7496127 | 2023-01-12 13:35:54 -0500 | [diff] [blame] | 18 | // ----------------------------------------------------------------------- |
Matteo Scandolo | 42f6e57 | 2021-01-25 15:11:34 -0800 | [diff] [blame] | 19 | |
Joey Armstrong | 7496127 | 2023-01-12 13:35:54 -0500 | [diff] [blame] | 20 | // ----------------------------------------------------------------------- |
| 21 | // ----------------------------------------------------------------------- |
Joey Armstrong | 74ec08c | 2023-08-31 11:25:57 -0400 | [diff] [blame] | 22 | String getIam(String func) { |
Joey Armstrong | 7496127 | 2023-01-12 13:35:54 -0500 | [diff] [blame] | 23 | // Cannot rely on a stack trace due to jenkins manipulation |
| 24 | String src = 'vars/createKubernetesCluster.groovy' |
| 25 | String iam = [src, func].join('::') |
| 26 | return iam |
| 27 | } |
| 28 | |
| 29 | // ----------------------------------------------------------------------- |
Joey Armstrong | 74ec08c | 2023-08-31 11:25:57 -0400 | [diff] [blame] | 30 | // Intent: Log progress message |
Joey Armstrong | 7496127 | 2023-01-12 13:35:54 -0500 | [diff] [blame] | 31 | // ----------------------------------------------------------------------- |
Joey Armstrong | 74ec08c | 2023-08-31 11:25:57 -0400 | [diff] [blame] | 32 | void enter(String name) { |
| 33 | // Announce ourselves for log usability |
| 34 | String iam = getIam(name) |
| 35 | println("${iam}: ENTER") |
| 36 | return |
| 37 | } |
| 38 | |
| 39 | // ----------------------------------------------------------------------- |
| 40 | // Intent: Log progress message |
| 41 | // ----------------------------------------------------------------------- |
| 42 | void leave(String name) { |
| 43 | // Announce ourselves for log usability |
| 44 | String iam = getIam(name) |
| 45 | println("${iam}: LEAVE") |
| 46 | return |
| 47 | } |
| 48 | |
| 49 | // ----------------------------------------------------------------------- |
| 50 | // ----------------------------------------------------------------------- |
| 51 | def call(Map config=[:]) { |
Joey Armstrong | 7496127 | 2023-01-12 13:35:54 -0500 | [diff] [blame] | 52 | |
| 53 | String iam = getIam('main') |
Joey Armstrong | 74ec08c | 2023-08-31 11:25:57 -0400 | [diff] [blame] | 54 | enter('main') |
Joey Armstrong | 7496127 | 2023-01-12 13:35:54 -0500 | [diff] [blame] | 55 | |
Matteo Scandolo | 42f6e57 | 2021-01-25 15:11:34 -0800 | [diff] [blame] | 56 | // note that I can't define this outside the function as there's no global scope in Groovy |
| 57 | def defaultConfig = [ |
Joey Armstrong | 74ec08c | 2023-08-31 11:25:57 -0400 | [diff] [blame] | 58 | branch: 'master', // branch=master ?!? |
| 59 | nodes: 1, |
| 60 | name: 'kind-ci' |
Matteo Scandolo | 42f6e57 | 2021-01-25 15:11:34 -0800 | [diff] [blame] | 61 | ] |
| 62 | |
Matteo Scandolo | 42f6e57 | 2021-01-25 15:11:34 -0800 | [diff] [blame] | 63 | def cfg = defaultConfig + config |
| 64 | |
| 65 | println "Deploying Kind cluster with the following parameters: ${cfg}." |
| 66 | |
| 67 | // TODO support different configs |
Joey Armstrong | f060aee | 2023-08-22 21:55:26 -0400 | [diff] [blame] | 68 | def data = ''' |
Matteo Scandolo | 42f6e57 | 2021-01-25 15:11:34 -0800 | [diff] [blame] | 69 | kind: Cluster |
| 70 | apiVersion: kind.x-k8s.io/v1alpha4 |
| 71 | nodes: |
Matteo Scandolo | 9b644ba | 2021-04-19 11:21:07 -0700 | [diff] [blame] | 72 | - role: worker |
| 73 | - role: worker |
Matteo Scandolo | 42f6e57 | 2021-01-25 15:11:34 -0800 | [diff] [blame] | 74 | - role: control-plane |
Matteo Scandolo | 9b644ba | 2021-04-19 11:21:07 -0700 | [diff] [blame] | 75 | kubeadmConfigPatches: |
| 76 | - | |
| 77 | kind: InitConfiguration |
| 78 | nodeRegistration: |
| 79 | kubeletExtraArgs: |
| 80 | node-labels: "ingress-ready=true" |
| 81 | extraPortMappings: |
| 82 | - containerPort: 80 |
| 83 | hostPort: 80 |
| 84 | protocol: TCP |
| 85 | - containerPort: 443 |
| 86 | hostPort: 443 |
| 87 | protocol: TCP |
| 88 | - containerPort: 30115 |
| 89 | hostPort: 30115 |
| 90 | - containerPort: 30120 |
| 91 | hostPort: 30120 |
Joey Armstrong | f060aee | 2023-08-22 21:55:26 -0400 | [diff] [blame] | 92 | ''' |
Matteo Scandolo | 42f6e57 | 2021-01-25 15:11:34 -0800 | [diff] [blame] | 93 | writeFile(file: 'kind.cfg', text: data) |
| 94 | |
Joey Armstrong | f060aee | 2023-08-22 21:55:26 -0400 | [diff] [blame] | 95 | // TODO: Skip kind install, make install-kind-command has done it already |
Matteo Scandolo | 42f6e57 | 2021-01-25 15:11:34 -0800 | [diff] [blame] | 96 | sh """ |
Joey Armstrong | 74ec08c | 2023-08-31 11:25:57 -0400 | [diff] [blame] | 97 | mkdir -p $WORKSPACE/bin |
Matteo Scandolo | 42f6e57 | 2021-01-25 15:11:34 -0800 | [diff] [blame] | 98 | |
| 99 | # download kind (should we add it to the base image?) |
Andrea Campanella | 0e540ba | 2021-10-01 11:16:03 +0200 | [diff] [blame] | 100 | curl -Lo ./kind https://kind.sigs.k8s.io/dl/v0.11.0/kind-linux-amd64 |
Matteo Scandolo | 42f6e57 | 2021-01-25 15:11:34 -0800 | [diff] [blame] | 101 | chmod +x ./kind |
| 102 | mv ./kind $WORKSPACE/bin/kind |
Matteo Scandolo | b47a6fd | 2021-10-27 17:02:49 -0700 | [diff] [blame] | 103 | """ |
Joey Armstrong | 7496127 | 2023-01-12 13:35:54 -0500 | [diff] [blame] | 104 | |
Matteo Scandolo | b47a6fd | 2021-10-27 17:02:49 -0700 | [diff] [blame] | 105 | // install voltctl |
| 106 | installVoltctl("${cfg.branch}") |
Joey Armstrong | 7496127 | 2023-01-12 13:35:54 -0500 | [diff] [blame] | 107 | |
Joey Armstrong | 74ec08c | 2023-08-31 11:25:57 -0400 | [diff] [blame] | 108 | sh(label : 'Start kind cluster', |
| 109 | script : """ |
Joey Armstrong | f060aee | 2023-08-22 21:55:26 -0400 | [diff] [blame] | 110 | cat <<EOM |
| 111 | |
| 112 | ** ----------------------------------------------------------------------- |
Joey Armstrong | 74ec08c | 2023-08-31 11:25:57 -0400 | [diff] [blame] | 113 | ** IAM: ${iam} |
Joey Armstrong | f060aee | 2023-08-22 21:55:26 -0400 | [diff] [blame] | 114 | ** Starting kind cluster |
| 115 | ** ----------------------------------------------------------------------- |
| 116 | EOM |
Matteo Scandolo | 42f6e57 | 2021-01-25 15:11:34 -0800 | [diff] [blame] | 117 | # start the kind cluster |
| 118 | kind create cluster --name ${cfg.name} --config kind.cfg |
| 119 | |
| 120 | # remove NoSchedule taint from nodes |
| 121 | for MNODE in \$(kubectl get node --selector='node-role.kubernetes.io/master' -o json | jq -r '.items[].metadata.name'); do |
| 122 | kubectl taint node "\$MNODE" node-role.kubernetes.io/master:NoSchedule- |
| 123 | done |
Joey Armstrong | 74ec08c | 2023-08-31 11:25:57 -0400 | [diff] [blame] | 124 | """) |
Matteo Scandolo | 42f6e57 | 2021-01-25 15:11:34 -0800 | [diff] [blame] | 125 | |
Joey Armstrong | 74ec08c | 2023-08-31 11:25:57 -0400 | [diff] [blame] | 126 | sh(label : 'Normalize config permissions', |
| 127 | script : """ |
Joey Armstrong | f060aee | 2023-08-22 21:55:26 -0400 | [diff] [blame] | 128 | ## ---------------------------------------------------------------------- |
| 129 | ## This logic is problematic, when run on a node processing concurrent |
| 130 | ## jobs over-write will corrupt config for the other running job. |
| 131 | ## ---------------------------------------------------------------------- |
| 132 | ## Future enhancement: Optimal answer would be to create and use configs |
| 133 | ## from a job-specific temp/config directory. |
| 134 | ## ---------------------------------------------------------------------- |
Matteo Scandolo | 42f6e57 | 2021-01-25 15:11:34 -0800 | [diff] [blame] | 135 | |
Joey Armstrong | f060aee | 2023-08-22 21:55:26 -0400 | [diff] [blame] | 136 | umask 022 |
Matteo Scandolo | 42f6e57 | 2021-01-25 15:11:34 -0800 | [diff] [blame] | 137 | |
Joey Armstrong | f060aee | 2023-08-22 21:55:26 -0400 | [diff] [blame] | 138 | echo |
Joey Armstrong | 74ec08c | 2023-08-31 11:25:57 -0400 | [diff] [blame] | 139 | echo "** Generate $HOME/.volt/config" |
Joey Armstrong | f060aee | 2023-08-22 21:55:26 -0400 | [diff] [blame] | 140 | mkdir -p "$HOME/.volt" |
Joey Armstrong | f060aee | 2023-08-22 21:55:26 -0400 | [diff] [blame] | 141 | chmod u=rwx "$HOME/.volt" |
| 142 | voltctl -s localhost:55555 config > "$HOME/.volt/config" |
Joey Armstrong | 74ec08c | 2023-08-31 11:25:57 -0400 | [diff] [blame] | 143 | chmod -R u+w,go-rwx "$HOME/.volt" |
Joey Armstrong | f060aee | 2023-08-22 21:55:26 -0400 | [diff] [blame] | 144 | |
| 145 | echo |
Joey Armstrong | 74ec08c | 2023-08-31 11:25:57 -0400 | [diff] [blame] | 146 | echo "** Generate $HOME/.kube/config" |
Joey Armstrong | f060aee | 2023-08-22 21:55:26 -0400 | [diff] [blame] | 147 | mkdir -p "$HOME/.kube" |
Joey Armstrong | f060aee | 2023-08-22 21:55:26 -0400 | [diff] [blame] | 148 | chmod u=rwx "$HOME/.kube" |
| 149 | kind get kubeconfig --name ${cfg.name} > "$HOME/.kube/config" |
Joey Armstrong | 74ec08c | 2023-08-31 11:25:57 -0400 | [diff] [blame] | 150 | chmod -R u+w,go-rwx "$HOME/.kube" |
Joey Armstrong | f060aee | 2023-08-22 21:55:26 -0400 | [diff] [blame] | 151 | |
| 152 | echo |
Joey Armstrong | 74ec08c | 2023-08-31 11:25:57 -0400 | [diff] [blame] | 153 | echo "Display .kube/ and .volt/ configs" |
Joey Armstrong | f060aee | 2023-08-22 21:55:26 -0400 | [diff] [blame] | 154 | /bin/ls -l "$HOME/.kube" "$HOME/.volt" |
Joey Armstrong | 74ec08c | 2023-08-31 11:25:57 -0400 | [diff] [blame] | 155 | """) |
Joey Armstrong | f060aee | 2023-08-22 21:55:26 -0400 | [diff] [blame] | 156 | |
Joey Armstrong | 74ec08c | 2023-08-31 11:25:57 -0400 | [diff] [blame] | 157 | sh(label : 'Install kail', |
| 158 | script : """ |
| 159 | cat <<EOM |
| 160 | |
| 161 | ** ----------------------------------------------------------------------- |
| 162 | ** IAM: ${iam} |
| 163 | ** Install kail |
| 164 | ** ----------------------------------------------------------------------- |
| 165 | EOM |
Joey Armstrong | 16bd8e8 | 2023-01-12 15:20:06 -0500 | [diff] [blame] | 166 | make -C "$WORKSPACE/voltha-system-tests" KAIL_PATH="$WORKSPACE/bin" kail |
Joey Armstrong | 74ec08c | 2023-08-31 11:25:57 -0400 | [diff] [blame] | 167 | """) |
Joey Armstrong | 7496127 | 2023-01-12 13:35:54 -0500 | [diff] [blame] | 168 | |
Roger Luethi | ff337d8 | 2023-09-20 12:09:44 +0200 | [diff] [blame] | 169 | leave('main') |
Joey Armstrong | 7496127 | 2023-01-12 13:35:54 -0500 | [diff] [blame] | 170 | return |
Matteo Scandolo | 42f6e57 | 2021-01-25 15:11:34 -0800 | [diff] [blame] | 171 | } |
Joey Armstrong | 16bd8e8 | 2023-01-12 15:20:06 -0500 | [diff] [blame] | 172 | |
| 173 | // [EOF] |