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 | // ----------------------------------------------------------------------- |
| 22 | def getIam(String func) |
| 23 | { |
| 24 | // Cannot rely on a stack trace due to jenkins manipulation |
| 25 | String src = 'vars/createKubernetesCluster.groovy' |
| 26 | String iam = [src, func].join('::') |
| 27 | return iam |
| 28 | } |
| 29 | |
| 30 | // ----------------------------------------------------------------------- |
| 31 | // ----------------------------------------------------------------------- |
Matteo Scandolo | 42f6e57 | 2021-01-25 15:11:34 -0800 | [diff] [blame] | 32 | def call(Map config) { |
Joey Armstrong | 7496127 | 2023-01-12 13:35:54 -0500 | [diff] [blame] | 33 | |
| 34 | String iam = getIam('main') |
| 35 | println("** ${iam}: ENTER") |
| 36 | |
Matteo Scandolo | 42f6e57 | 2021-01-25 15:11:34 -0800 | [diff] [blame] | 37 | // note that I can't define this outside the function as there's no global scope in Groovy |
| 38 | def defaultConfig = [ |
Joey Armstrong | f060aee | 2023-08-22 21:55:26 -0400 | [diff] [blame] | 39 | branch: "master", // branch=master ?!? |
Matteo Scandolo | 42f6e57 | 2021-01-25 15:11:34 -0800 | [diff] [blame] | 40 | nodes: 1, |
| 41 | name: "kind-ci" |
| 42 | ] |
| 43 | |
| 44 | if (!config) { |
| 45 | config = [:] |
| 46 | } |
| 47 | |
| 48 | def cfg = defaultConfig + config |
| 49 | |
| 50 | println "Deploying Kind cluster with the following parameters: ${cfg}." |
| 51 | |
| 52 | // TODO support different configs |
Joey Armstrong | f060aee | 2023-08-22 21:55:26 -0400 | [diff] [blame] | 53 | def data = ''' |
Matteo Scandolo | 42f6e57 | 2021-01-25 15:11:34 -0800 | [diff] [blame] | 54 | kind: Cluster |
| 55 | apiVersion: kind.x-k8s.io/v1alpha4 |
| 56 | nodes: |
Matteo Scandolo | 9b644ba | 2021-04-19 11:21:07 -0700 | [diff] [blame] | 57 | - role: worker |
| 58 | - role: worker |
Matteo Scandolo | 42f6e57 | 2021-01-25 15:11:34 -0800 | [diff] [blame] | 59 | - role: control-plane |
Matteo Scandolo | 9b644ba | 2021-04-19 11:21:07 -0700 | [diff] [blame] | 60 | kubeadmConfigPatches: |
| 61 | - | |
| 62 | kind: InitConfiguration |
| 63 | nodeRegistration: |
| 64 | kubeletExtraArgs: |
| 65 | node-labels: "ingress-ready=true" |
| 66 | extraPortMappings: |
| 67 | - containerPort: 80 |
| 68 | hostPort: 80 |
| 69 | protocol: TCP |
| 70 | - containerPort: 443 |
| 71 | hostPort: 443 |
| 72 | protocol: TCP |
| 73 | - containerPort: 30115 |
| 74 | hostPort: 30115 |
| 75 | - containerPort: 30120 |
| 76 | hostPort: 30120 |
Joey Armstrong | f060aee | 2023-08-22 21:55:26 -0400 | [diff] [blame] | 77 | ''' |
Matteo Scandolo | 42f6e57 | 2021-01-25 15:11:34 -0800 | [diff] [blame] | 78 | writeFile(file: 'kind.cfg', text: data) |
| 79 | |
Joey Armstrong | f060aee | 2023-08-22 21:55:26 -0400 | [diff] [blame] | 80 | // TODO: Skip kind install, make install-kind-command has done it already |
Matteo Scandolo | 42f6e57 | 2021-01-25 15:11:34 -0800 | [diff] [blame] | 81 | sh """ |
Joey Armstrong | 7496127 | 2023-01-12 13:35:54 -0500 | [diff] [blame] | 82 | mkdir -p "$WORKSPACE/bin" |
Matteo Scandolo | 42f6e57 | 2021-01-25 15:11:34 -0800 | [diff] [blame] | 83 | |
| 84 | # download kind (should we add it to the base image?) |
Andrea Campanella | 0e540ba | 2021-10-01 11:16:03 +0200 | [diff] [blame] | 85 | 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] | 86 | chmod +x ./kind |
| 87 | mv ./kind $WORKSPACE/bin/kind |
Matteo Scandolo | b47a6fd | 2021-10-27 17:02:49 -0700 | [diff] [blame] | 88 | """ |
Joey Armstrong | 7496127 | 2023-01-12 13:35:54 -0500 | [diff] [blame] | 89 | |
Matteo Scandolo | b47a6fd | 2021-10-27 17:02:49 -0700 | [diff] [blame] | 90 | // install voltctl |
| 91 | installVoltctl("${cfg.branch}") |
Joey Armstrong | 7496127 | 2023-01-12 13:35:54 -0500 | [diff] [blame] | 92 | |
Matteo Scandolo | b47a6fd | 2021-10-27 17:02:49 -0700 | [diff] [blame] | 93 | sh """ |
Joey Armstrong | f060aee | 2023-08-22 21:55:26 -0400 | [diff] [blame] | 94 | cat <<EOM |
| 95 | |
| 96 | ** ----------------------------------------------------------------------- |
| 97 | ** Starting kind cluster |
| 98 | ** ----------------------------------------------------------------------- |
| 99 | EOM |
Matteo Scandolo | 42f6e57 | 2021-01-25 15:11:34 -0800 | [diff] [blame] | 100 | # start the kind cluster |
| 101 | kind create cluster --name ${cfg.name} --config kind.cfg |
| 102 | |
| 103 | # remove NoSchedule taint from nodes |
| 104 | for MNODE in \$(kubectl get node --selector='node-role.kubernetes.io/master' -o json | jq -r '.items[].metadata.name'); do |
| 105 | kubectl taint node "\$MNODE" node-role.kubernetes.io/master:NoSchedule- |
| 106 | done |
| 107 | |
Joey Armstrong | f060aee | 2023-08-22 21:55:26 -0400 | [diff] [blame] | 108 | ## ---------------------------------------------------------------------- |
| 109 | ## This logic is problematic, when run on a node processing concurrent |
| 110 | ## jobs over-write will corrupt config for the other running job. |
| 111 | ## ---------------------------------------------------------------------- |
| 112 | ## Future enhancement: Optimal answer would be to create and use configs |
| 113 | ## from a job-specific temp/config directory. |
| 114 | ## ---------------------------------------------------------------------- |
Matteo Scandolo | 42f6e57 | 2021-01-25 15:11:34 -0800 | [diff] [blame] | 115 | |
Joey Armstrong | f060aee | 2023-08-22 21:55:26 -0400 | [diff] [blame] | 116 | umask 022 |
Matteo Scandolo | 42f6e57 | 2021-01-25 15:11:34 -0800 | [diff] [blame] | 117 | |
Joey Armstrong | f060aee | 2023-08-22 21:55:26 -0400 | [diff] [blame] | 118 | echo |
| 119 | echo "** Generate ~/.volt/config" |
| 120 | mkdir -p "$HOME/.volt" |
| 121 | chmod -R u+w,go-rwx "$HOME/.volt" |
| 122 | chmod u=rwx "$HOME/.volt" |
| 123 | voltctl -s localhost:55555 config > "$HOME/.volt/config" |
| 124 | |
| 125 | echo |
| 126 | echo "** Generate ~/.kube/config" |
| 127 | mkdir -p "$HOME/.kube" |
| 128 | chmod -R u+w,go-rwx "$HOME/.kube" |
| 129 | chmod u=rwx "$HOME/.kube" |
| 130 | kind get kubeconfig --name ${cfg.name} > "$HOME/.kube/config" |
| 131 | |
| 132 | echo |
| 133 | echo "Display .kube/ and .volt" |
| 134 | /bin/ls -l "$HOME/.kube" "$HOME/.volt" |
| 135 | |
| 136 | echo |
| 137 | echo "Install Kail" |
Joey Armstrong | 16bd8e8 | 2023-01-12 15:20:06 -0500 | [diff] [blame] | 138 | make -C "$WORKSPACE/voltha-system-tests" KAIL_PATH="$WORKSPACE/bin" kail |
Matteo Scandolo | 42f6e57 | 2021-01-25 15:11:34 -0800 | [diff] [blame] | 139 | """ |
Joey Armstrong | 7496127 | 2023-01-12 13:35:54 -0500 | [diff] [blame] | 140 | |
| 141 | println("** ${iam}: LEAVE") |
| 142 | return |
Matteo Scandolo | 42f6e57 | 2021-01-25 15:11:34 -0800 | [diff] [blame] | 143 | } |
Joey Armstrong | 16bd8e8 | 2023-01-12 15:20:06 -0500 | [diff] [blame] | 144 | |
| 145 | // [EOF] |