blob: a0df8958e0ec04b27f8233dc459eba7029716629 [file] [log] [blame]
Joey Armstrong74961272023-01-12 13:35:54 -05001#!/usr/bin/env groovy
2// -----------------------------------------------------------------------
Joey Armstrong16bd8e82023-01-12 15:20:06 -05003// 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 Armstrong74961272023-01-12 13:35:54 -050018// -----------------------------------------------------------------------
Matteo Scandolo42f6e572021-01-25 15:11:34 -080019
Joey Armstrong74961272023-01-12 13:35:54 -050020// -----------------------------------------------------------------------
21// -----------------------------------------------------------------------
22def 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 Scandolo42f6e572021-01-25 15:11:34 -080032def call(Map config) {
Joey Armstrong74961272023-01-12 13:35:54 -050033
34 String iam = getIam('main')
35 println("** ${iam}: ENTER")
36
Matteo Scandolo42f6e572021-01-25 15:11:34 -080037 // note that I can't define this outside the function as there's no global scope in Groovy
38 def defaultConfig = [
Hardik Windlass6f854a12021-07-12 13:20:21 +000039 branch: "master",
Matteo Scandolo42f6e572021-01-25 15:11:34 -080040 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 = """
54kind: Cluster
55apiVersion: kind.x-k8s.io/v1alpha4
56nodes:
Matteo Scandolo9b644ba2021-04-19 11:21:07 -070057- role: worker
58- role: worker
Matteo Scandolo42f6e572021-01-25 15:11:34 -080059- role: control-plane
Matteo Scandolo9b644ba2021-04-19 11:21:07 -070060 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 Scandolo42f6e572021-01-25 15:11:34 -080077 """
78 writeFile(file: 'kind.cfg', text: data)
79
80 // TODO skip cluster creation if cluster is already there
81 sh """
Joey Armstrong74961272023-01-12 13:35:54 -050082 mkdir -p "$WORKSPACE/bin"
Matteo Scandolo42f6e572021-01-25 15:11:34 -080083
84 # download kind (should we add it to the base image?)
Andrea Campanella0e540ba2021-10-01 11:16:03 +020085 curl -Lo ./kind https://kind.sigs.k8s.io/dl/v0.11.0/kind-linux-amd64
Matteo Scandolo42f6e572021-01-25 15:11:34 -080086 chmod +x ./kind
87 mv ./kind $WORKSPACE/bin/kind
Matteo Scandolob47a6fd2021-10-27 17:02:49 -070088 """
Joey Armstrong74961272023-01-12 13:35:54 -050089
Matteo Scandolob47a6fd2021-10-27 17:02:49 -070090 // install voltctl
91 installVoltctl("${cfg.branch}")
Joey Armstrong74961272023-01-12 13:35:54 -050092
Matteo Scandolob47a6fd2021-10-27 17:02:49 -070093 sh """
Matteo Scandolo42f6e572021-01-25 15:11:34 -080094 # 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 Armstrong16bd8e82023-01-12 15:20:06 -0500108 # install kail
109 make -C "$WORKSPACE/voltha-system-tests" KAIL_PATH="$WORKSPACE/bin" kail
Matteo Scandolo42f6e572021-01-25 15:11:34 -0800110 """
Joey Armstrong74961272023-01-12 13:35:54 -0500111
112 println("** ${iam}: LEAVE")
113 return
Matteo Scandolo42f6e572021-01-25 15:11:34 -0800114}
Joey Armstrong16bd8e82023-01-12 15:20:06 -0500115
116// [EOF]