blob: 9653840672320586e83aab0df3fe67b400d14fef [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 = [
Joey Armstrongf060aee2023-08-22 21:55:26 -040039 branch: "master", // 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
Joey Armstrongf060aee2023-08-22 21:55:26 -040053 def data = '''
Matteo Scandolo42f6e572021-01-25 15:11:34 -080054kind: 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
Joey Armstrongf060aee2023-08-22 21:55:26 -040077'''
Matteo Scandolo42f6e572021-01-25 15:11:34 -080078 writeFile(file: 'kind.cfg', text: data)
79
Joey Armstrongf060aee2023-08-22 21:55:26 -040080 // TODO: Skip kind install, make install-kind-command has done it already
Matteo Scandolo42f6e572021-01-25 15:11:34 -080081 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 """
Joey Armstrongf060aee2023-08-22 21:55:26 -040094cat <<EOM
95
96** -----------------------------------------------------------------------
97** Starting kind cluster
98** -----------------------------------------------------------------------
99EOM
Matteo Scandolo42f6e572021-01-25 15:11:34 -0800100 # 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 Armstrongf060aee2023-08-22 21:55:26 -0400108 ## ----------------------------------------------------------------------
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 Scandolo42f6e572021-01-25 15:11:34 -0800115
Joey Armstrongf060aee2023-08-22 21:55:26 -0400116 umask 022
Matteo Scandolo42f6e572021-01-25 15:11:34 -0800117
Joey Armstrongf060aee2023-08-22 21:55:26 -0400118 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 Armstrong16bd8e82023-01-12 15:20:06 -0500138 make -C "$WORKSPACE/voltha-system-tests" KAIL_PATH="$WORKSPACE/bin" kail
Matteo Scandolo42f6e572021-01-25 15:11:34 -0800139 """
Joey Armstrong74961272023-01-12 13:35:54 -0500140
141 println("** ${iam}: LEAVE")
142 return
Matteo Scandolo42f6e572021-01-25 15:11:34 -0800143}
Joey Armstrong16bd8e82023-01-12 15:20:06 -0500144
145// [EOF]