blob: fd22546aea22ab7128a45b69e5b702590311187a [file] [log] [blame]
Joey Armstrong74961272023-01-12 13:35:54 -05001#!/usr/bin/env groovy
2// -----------------------------------------------------------------------
Matteo Scandolo42f6e572021-01-25 15:11:34 -08003// sets up a kubernetes cluster (using kind)
Joey Armstrong74961272023-01-12 13:35:54 -05004// -----------------------------------------------------------------------
Matteo Scandolo42f6e572021-01-25 15:11:34 -08005
Joey Armstrong74961272023-01-12 13:35:54 -05006// -----------------------------------------------------------------------
7// -----------------------------------------------------------------------
8def getIam(String func)
9{
10 // Cannot rely on a stack trace due to jenkins manipulation
11 String src = 'vars/createKubernetesCluster.groovy'
12 String iam = [src, func].join('::')
13 return iam
14}
15
16// -----------------------------------------------------------------------
17// -----------------------------------------------------------------------
Matteo Scandolo42f6e572021-01-25 15:11:34 -080018def call(Map config) {
Joey Armstrong74961272023-01-12 13:35:54 -050019
20 String iam = getIam('main')
21 println("** ${iam}: ENTER")
22
Matteo Scandolo42f6e572021-01-25 15:11:34 -080023 // note that I can't define this outside the function as there's no global scope in Groovy
24 def defaultConfig = [
Hardik Windlass6f854a12021-07-12 13:20:21 +000025 branch: "master",
Matteo Scandolo42f6e572021-01-25 15:11:34 -080026 nodes: 1,
27 name: "kind-ci"
28 ]
29
30 if (!config) {
31 config = [:]
32 }
33
34 def cfg = defaultConfig + config
35
36 println "Deploying Kind cluster with the following parameters: ${cfg}."
37
38 // TODO support different configs
39 def data = """
40kind: Cluster
41apiVersion: kind.x-k8s.io/v1alpha4
42nodes:
Matteo Scandolo9b644ba2021-04-19 11:21:07 -070043- role: worker
44- role: worker
Matteo Scandolo42f6e572021-01-25 15:11:34 -080045- role: control-plane
Matteo Scandolo9b644ba2021-04-19 11:21:07 -070046 kubeadmConfigPatches:
47 - |
48 kind: InitConfiguration
49 nodeRegistration:
50 kubeletExtraArgs:
51 node-labels: "ingress-ready=true"
52 extraPortMappings:
53 - containerPort: 80
54 hostPort: 80
55 protocol: TCP
56 - containerPort: 443
57 hostPort: 443
58 protocol: TCP
59 - containerPort: 30115
60 hostPort: 30115
61 - containerPort: 30120
62 hostPort: 30120
Matteo Scandolo42f6e572021-01-25 15:11:34 -080063 """
64 writeFile(file: 'kind.cfg', text: data)
65
66 // TODO skip cluster creation if cluster is already there
67 sh """
Joey Armstrong74961272023-01-12 13:35:54 -050068 mkdir -p "$WORKSPACE/bin"
Matteo Scandolo42f6e572021-01-25 15:11:34 -080069
70 # download kind (should we add it to the base image?)
Andrea Campanella0e540ba2021-10-01 11:16:03 +020071 curl -Lo ./kind https://kind.sigs.k8s.io/dl/v0.11.0/kind-linux-amd64
Matteo Scandolo42f6e572021-01-25 15:11:34 -080072 chmod +x ./kind
73 mv ./kind $WORKSPACE/bin/kind
Matteo Scandolob47a6fd2021-10-27 17:02:49 -070074 """
Joey Armstrong74961272023-01-12 13:35:54 -050075
Matteo Scandolob47a6fd2021-10-27 17:02:49 -070076 // install voltctl
77 installVoltctl("${cfg.branch}")
Joey Armstrong74961272023-01-12 13:35:54 -050078
Matteo Scandolob47a6fd2021-10-27 17:02:49 -070079 sh """
Matteo Scandolo42f6e572021-01-25 15:11:34 -080080 # start the kind cluster
81 kind create cluster --name ${cfg.name} --config kind.cfg
82
83 # remove NoSchedule taint from nodes
84 for MNODE in \$(kubectl get node --selector='node-role.kubernetes.io/master' -o json | jq -r '.items[].metadata.name'); do
85 kubectl taint node "\$MNODE" node-role.kubernetes.io/master:NoSchedule-
86 done
87
88 mkdir -p $HOME/.volt
89 voltctl -s localhost:55555 config > $HOME/.volt/config
90
91 mkdir -p $HOME/.kube
92 kind get kubeconfig --name ${cfg.name} > $HOME/.kube/config
93
Joey Armstrong74961272023-01-12 13:35:54 -050094 make -C "$WORKSPACE/voltha-system-tests" KAIL_PATH="$WORKSPACE/bin" kail || true
95
Matteo Scandolo42f6e572021-01-25 15:11:34 -080096 # download kail
97 bash <( curl -sfL https://raw.githubusercontent.com/boz/kail/master/godownloader.sh) -b "$WORKSPACE/bin"
98 """
Joey Armstrong74961272023-01-12 13:35:54 -050099
100 println("** ${iam}: LEAVE")
101 return
Matteo Scandolo42f6e572021-01-25 15:11:34 -0800102}