blob: 399532aa040cd201f5e2ea8476065f8b02bb7593 [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// -----------------------------------------------------------------------
Joey Armstrong74ec08c2023-08-31 11:25:57 -040022String getIam(String func) {
Joey Armstrong74961272023-01-12 13:35:54 -050023 // Cannot rely on a stack trace due to jenkins manipulation
24 String src = 'vars/createKubernetesCluster.groovy'
25 String iam = [src, func].join('::')
26 return iam
27}
28
29// -----------------------------------------------------------------------
Joey Armstrong74ec08c2023-08-31 11:25:57 -040030// Intent: Log progress message
Joey Armstrong74961272023-01-12 13:35:54 -050031// -----------------------------------------------------------------------
Joey Armstrong74ec08c2023-08-31 11:25:57 -040032void enter(String name) {
33 // Announce ourselves for log usability
34 String iam = getIam(name)
35 println("${iam}: ENTER")
36 return
37}
38
39// -----------------------------------------------------------------------
40// Intent: Log progress message
41// -----------------------------------------------------------------------
42void leave(String name) {
43 // Announce ourselves for log usability
44 String iam = getIam(name)
45 println("${iam}: LEAVE")
46 return
47}
48
49// -----------------------------------------------------------------------
50// -----------------------------------------------------------------------
51def call(Map config=[:]) {
Joey Armstrong74961272023-01-12 13:35:54 -050052
53 String iam = getIam('main')
Joey Armstrong74ec08c2023-08-31 11:25:57 -040054 enter('main')
Joey Armstrong74961272023-01-12 13:35:54 -050055
Matteo Scandolo42f6e572021-01-25 15:11:34 -080056 // note that I can't define this outside the function as there's no global scope in Groovy
57 def defaultConfig = [
Joey Armstrong74ec08c2023-08-31 11:25:57 -040058 branch: 'master', // branch=master ?!?
59 nodes: 1,
60 name: 'kind-ci'
Matteo Scandolo42f6e572021-01-25 15:11:34 -080061 ]
62
Matteo Scandolo42f6e572021-01-25 15:11:34 -080063 def cfg = defaultConfig + config
64
65 println "Deploying Kind cluster with the following parameters: ${cfg}."
66
67 // TODO support different configs
Joey Armstrongf060aee2023-08-22 21:55:26 -040068 def data = '''
Matteo Scandolo42f6e572021-01-25 15:11:34 -080069kind: Cluster
70apiVersion: kind.x-k8s.io/v1alpha4
71nodes:
Matteo Scandolo9b644ba2021-04-19 11:21:07 -070072- role: worker
73- role: worker
Matteo Scandolo42f6e572021-01-25 15:11:34 -080074- role: control-plane
Matteo Scandolo9b644ba2021-04-19 11:21:07 -070075 kubeadmConfigPatches:
76 - |
77 kind: InitConfiguration
78 nodeRegistration:
79 kubeletExtraArgs:
80 node-labels: "ingress-ready=true"
81 extraPortMappings:
82 - containerPort: 80
83 hostPort: 80
84 protocol: TCP
85 - containerPort: 443
86 hostPort: 443
87 protocol: TCP
88 - containerPort: 30115
89 hostPort: 30115
90 - containerPort: 30120
91 hostPort: 30120
Joey Armstrongf060aee2023-08-22 21:55:26 -040092'''
Matteo Scandolo42f6e572021-01-25 15:11:34 -080093 writeFile(file: 'kind.cfg', text: data)
94
Joey Armstrongf060aee2023-08-22 21:55:26 -040095 // TODO: Skip kind install, make install-kind-command has done it already
Matteo Scandolo42f6e572021-01-25 15:11:34 -080096 sh """
Joey Armstrong74ec08c2023-08-31 11:25:57 -040097 mkdir -p $WORKSPACE/bin
Matteo Scandolo42f6e572021-01-25 15:11:34 -080098
99 # download kind (should we add it to the base image?)
Andrea Campanella0e540ba2021-10-01 11:16:03 +0200100 curl -Lo ./kind https://kind.sigs.k8s.io/dl/v0.11.0/kind-linux-amd64
Matteo Scandolo42f6e572021-01-25 15:11:34 -0800101 chmod +x ./kind
102 mv ./kind $WORKSPACE/bin/kind
Matteo Scandolob47a6fd2021-10-27 17:02:49 -0700103 """
Joey Armstrong74961272023-01-12 13:35:54 -0500104
Matteo Scandolob47a6fd2021-10-27 17:02:49 -0700105 // install voltctl
106 installVoltctl("${cfg.branch}")
Joey Armstrong74961272023-01-12 13:35:54 -0500107
Joey Armstrong74ec08c2023-08-31 11:25:57 -0400108 sh(label : 'Start kind cluster',
109 script : """
Joey Armstrongf060aee2023-08-22 21:55:26 -0400110cat <<EOM
111
112** -----------------------------------------------------------------------
Joey Armstrong74ec08c2023-08-31 11:25:57 -0400113** IAM: ${iam}
Joey Armstrongf060aee2023-08-22 21:55:26 -0400114** Starting kind cluster
115** -----------------------------------------------------------------------
116EOM
Matteo Scandolo42f6e572021-01-25 15:11:34 -0800117 # start the kind cluster
118 kind create cluster --name ${cfg.name} --config kind.cfg
119
120 # remove NoSchedule taint from nodes
121 for MNODE in \$(kubectl get node --selector='node-role.kubernetes.io/master' -o json | jq -r '.items[].metadata.name'); do
122 kubectl taint node "\$MNODE" node-role.kubernetes.io/master:NoSchedule-
123 done
Joey Armstrong74ec08c2023-08-31 11:25:57 -0400124""")
Matteo Scandolo42f6e572021-01-25 15:11:34 -0800125
Joey Armstrong74ec08c2023-08-31 11:25:57 -0400126 sh(label : 'Normalize config permissions',
127 script : """
Joey Armstrongf060aee2023-08-22 21:55:26 -0400128 ## ----------------------------------------------------------------------
129 ## This logic is problematic, when run on a node processing concurrent
130 ## jobs over-write will corrupt config for the other running job.
131 ## ----------------------------------------------------------------------
132 ## Future enhancement: Optimal answer would be to create and use configs
133 ## from a job-specific temp/config directory.
134 ## ----------------------------------------------------------------------
Matteo Scandolo42f6e572021-01-25 15:11:34 -0800135
Joey Armstrongf060aee2023-08-22 21:55:26 -0400136 umask 022
Matteo Scandolo42f6e572021-01-25 15:11:34 -0800137
Joey Armstrongf060aee2023-08-22 21:55:26 -0400138 echo
Joey Armstrong74ec08c2023-08-31 11:25:57 -0400139 echo "** Generate $HOME/.kube/config"
Joey Armstrongf060aee2023-08-22 21:55:26 -0400140 mkdir -p "$HOME/.kube"
Joey Armstrongf060aee2023-08-22 21:55:26 -0400141 chmod u=rwx "$HOME/.kube"
142 kind get kubeconfig --name ${cfg.name} > "$HOME/.kube/config"
Joey Armstrong74ec08c2023-08-31 11:25:57 -0400143 chmod -R u+w,go-rwx "$HOME/.kube"
Joey Armstrongf060aee2023-08-22 21:55:26 -0400144
145 echo
Joey Armstrong74ec08c2023-08-31 11:25:57 -0400146 echo "Display .kube/ and .volt/ configs"
Joey Armstrongf060aee2023-08-22 21:55:26 -0400147 /bin/ls -l "$HOME/.kube" "$HOME/.volt"
Joey Armstrong74ec08c2023-08-31 11:25:57 -0400148""")
Joey Armstrongf060aee2023-08-22 21:55:26 -0400149
Joey Armstrong74ec08c2023-08-31 11:25:57 -0400150 sh(label : 'Install kail',
151 script : """
152cat <<EOM
153
154** -----------------------------------------------------------------------
155** IAM: ${iam}
156** Install kail
157** -----------------------------------------------------------------------
158EOM
Joey Armstrong16bd8e82023-01-12 15:20:06 -0500159 make -C "$WORKSPACE/voltha-system-tests" KAIL_PATH="$WORKSPACE/bin" kail
Joey Armstrong74ec08c2023-08-31 11:25:57 -0400160""")
Joey Armstrong74961272023-01-12 13:35:54 -0500161
Roger Luethiff337d82023-09-20 12:09:44 +0200162 leave('main')
Joey Armstrong74961272023-01-12 13:35:54 -0500163 return
Matteo Scandolo42f6e572021-01-25 15:11:34 -0800164}
Joey Armstrong16bd8e82023-01-12 15:20:06 -0500165
166// [EOF]