Joey Armstrong | 7496127 | 2023-01-12 13:35:54 -0500 | [diff] [blame] | 1 | #!/usr/bin/env groovy |
| 2 | // ----------------------------------------------------------------------- |
Joey Armstrong | 518f357 | 2024-02-11 07:56:25 -0500 | [diff] [blame] | 3 | // Copyright 2021-2024 Open Networking Foundation (ONF) and the ONF Contributors |
Joey Armstrong | 16bd8e8 | 2023-01-12 15:20:06 -0500 | [diff] [blame] | 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 |
Jan Klare | 96abd86 | 2023-09-29 19:53:40 +0200 | [diff] [blame] | 124 | # install ingress-nginx to allow usage of ingress to expose voltha |
| 125 | # endpoints (etcd and voltha-api) |
| 126 | kubectl apply -f https://raw.githubusercontent.com/kubernetes/ingress-nginx/helm-chart-4.2.5/deploy/static/provider/kind/deploy.yaml |
Jan Klare | 974ea55 | 2023-10-02 11:32:05 +0200 | [diff] [blame] | 127 | # remove validating webhook for now, since some of the pipelines seem to |
| 128 | # fail with errors related to calling this hook and although there is a |
| 129 | # lot of talk in the internet about this issue, the solution very much |
| 130 | # depends on your exact setup (and it might not be worth investigating the |
| 131 | # details for a k8s version which has been EOL since some time now) |
| 132 | # https://stackoverflow.com/questions/61616203/nginx-ingress-controller-failed-calling-webhook |
| 133 | # This should be removed and properly fixed once we upgrade to a recent |
| 134 | # k8s version and a fitting ingress-nginx deployment |
| 135 | kubectl delete -A ValidatingWebhookConfiguration ingress-nginx-admission |
| 136 | |
Joey Armstrong | 74ec08c | 2023-08-31 11:25:57 -0400 | [diff] [blame] | 137 | """) |
Matteo Scandolo | 42f6e57 | 2021-01-25 15:11:34 -0800 | [diff] [blame] | 138 | |
Joey Armstrong | 74ec08c | 2023-08-31 11:25:57 -0400 | [diff] [blame] | 139 | sh(label : 'Normalize config permissions', |
| 140 | script : """ |
Joey Armstrong | f060aee | 2023-08-22 21:55:26 -0400 | [diff] [blame] | 141 | ## ---------------------------------------------------------------------- |
| 142 | ## This logic is problematic, when run on a node processing concurrent |
| 143 | ## jobs over-write will corrupt config for the other running job. |
| 144 | ## ---------------------------------------------------------------------- |
| 145 | ## Future enhancement: Optimal answer would be to create and use configs |
| 146 | ## from a job-specific temp/config directory. |
| 147 | ## ---------------------------------------------------------------------- |
Matteo Scandolo | 42f6e57 | 2021-01-25 15:11:34 -0800 | [diff] [blame] | 148 | |
Joey Armstrong | f060aee | 2023-08-22 21:55:26 -0400 | [diff] [blame] | 149 | umask 022 |
Matteo Scandolo | 42f6e57 | 2021-01-25 15:11:34 -0800 | [diff] [blame] | 150 | |
Joey Armstrong | f060aee | 2023-08-22 21:55:26 -0400 | [diff] [blame] | 151 | echo |
Joey Armstrong | 74ec08c | 2023-08-31 11:25:57 -0400 | [diff] [blame] | 152 | echo "** Generate $HOME/.kube/config" |
Joey Armstrong | f060aee | 2023-08-22 21:55:26 -0400 | [diff] [blame] | 153 | mkdir -p "$HOME/.kube" |
Joey Armstrong | f060aee | 2023-08-22 21:55:26 -0400 | [diff] [blame] | 154 | chmod u=rwx "$HOME/.kube" |
| 155 | kind get kubeconfig --name ${cfg.name} > "$HOME/.kube/config" |
Joey Armstrong | 74ec08c | 2023-08-31 11:25:57 -0400 | [diff] [blame] | 156 | chmod -R u+w,go-rwx "$HOME/.kube" |
Joey Armstrong | f060aee | 2023-08-22 21:55:26 -0400 | [diff] [blame] | 157 | |
| 158 | echo |
Joey Armstrong | 74ec08c | 2023-08-31 11:25:57 -0400 | [diff] [blame] | 159 | echo "Display .kube/ and .volt/ configs" |
Joey Armstrong | f060aee | 2023-08-22 21:55:26 -0400 | [diff] [blame] | 160 | /bin/ls -l "$HOME/.kube" "$HOME/.volt" |
Joey Armstrong | 74ec08c | 2023-08-31 11:25:57 -0400 | [diff] [blame] | 161 | """) |
Joey Armstrong | f060aee | 2023-08-22 21:55:26 -0400 | [diff] [blame] | 162 | |
Joey Armstrong | 74ec08c | 2023-08-31 11:25:57 -0400 | [diff] [blame] | 163 | sh(label : 'Install kail', |
| 164 | script : """ |
| 165 | cat <<EOM |
| 166 | |
| 167 | ** ----------------------------------------------------------------------- |
| 168 | ** IAM: ${iam} |
| 169 | ** Install kail |
| 170 | ** ----------------------------------------------------------------------- |
| 171 | EOM |
Joey Armstrong | 16bd8e8 | 2023-01-12 15:20:06 -0500 | [diff] [blame] | 172 | make -C "$WORKSPACE/voltha-system-tests" KAIL_PATH="$WORKSPACE/bin" kail |
Joey Armstrong | 74ec08c | 2023-08-31 11:25:57 -0400 | [diff] [blame] | 173 | """) |
Joey Armstrong | 7496127 | 2023-01-12 13:35:54 -0500 | [diff] [blame] | 174 | |
Roger Luethi | ff337d8 | 2023-09-20 12:09:44 +0200 | [diff] [blame] | 175 | leave('main') |
Joey Armstrong | 7496127 | 2023-01-12 13:35:54 -0500 | [diff] [blame] | 176 | return |
Matteo Scandolo | 42f6e57 | 2021-01-25 15:11:34 -0800 | [diff] [blame] | 177 | } |
Joey Armstrong | 16bd8e8 | 2023-01-12 15:20:06 -0500 | [diff] [blame] | 178 | |
| 179 | // [EOF] |