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 = [ |
Hardik Windlass | 6f854a1 | 2021-07-12 13:20:21 +0000 | [diff] [blame] | 39 | 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 |
| 53 | def data = """ |
| 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 |
Matteo Scandolo | 42f6e57 | 2021-01-25 15:11:34 -0800 | [diff] [blame] | 77 | """ |
| 78 | writeFile(file: 'kind.cfg', text: data) |
| 79 | |
| 80 | // TODO skip cluster creation if cluster is already there |
| 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 """ |
Matteo Scandolo | 42f6e57 | 2021-01-25 15:11:34 -0800 | [diff] [blame] | 94 | # start the kind cluster |
| 95 | kind create cluster --name ${cfg.name} --config kind.cfg |
| 96 | |
| 97 | # remove NoSchedule taint from nodes |
| 98 | for MNODE in \$(kubectl get node --selector='node-role.kubernetes.io/master' -o json | jq -r '.items[].metadata.name'); do |
| 99 | kubectl taint node "\$MNODE" node-role.kubernetes.io/master:NoSchedule- |
| 100 | done |
| 101 | |
| 102 | mkdir -p $HOME/.volt |
| 103 | voltctl -s localhost:55555 config > $HOME/.volt/config |
| 104 | |
| 105 | mkdir -p $HOME/.kube |
| 106 | kind get kubeconfig --name ${cfg.name} > $HOME/.kube/config |
| 107 | |
Joey Armstrong | 16bd8e8 | 2023-01-12 15:20:06 -0500 | [diff] [blame] | 108 | # install kail |
| 109 | make -C "$WORKSPACE/voltha-system-tests" KAIL_PATH="$WORKSPACE/bin" kail |
Matteo Scandolo | 42f6e57 | 2021-01-25 15:11:34 -0800 | [diff] [blame] | 110 | """ |
Joey Armstrong | 7496127 | 2023-01-12 13:35:54 -0500 | [diff] [blame] | 111 | |
| 112 | println("** ${iam}: LEAVE") |
| 113 | return |
Matteo Scandolo | 42f6e57 | 2021-01-25 15:11:34 -0800 | [diff] [blame] | 114 | } |
Joey Armstrong | 16bd8e8 | 2023-01-12 15:20:06 -0500 | [diff] [blame] | 115 | |
| 116 | // [EOF] |