blob: 9229d69fddb33fc78b6c37bd3271bfa4f74da608 [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
Jan Klare96abd862023-09-29 19:53:40 +0200124 # install ingress-nginx to allow usage of ingress to expose voltha
125 # endpoints (etcd and voltha-api)
126 kubectl apply -f https://raw.githubusercontent.com/kubernetes/ingress-nginx/helm-chart-4.2.5/deploy/static/provider/kind/deploy.yaml
Joey Armstrong74ec08c2023-08-31 11:25:57 -0400127""")
Matteo Scandolo42f6e572021-01-25 15:11:34 -0800128
Joey Armstrong74ec08c2023-08-31 11:25:57 -0400129 sh(label : 'Normalize config permissions',
130 script : """
Joey Armstrongf060aee2023-08-22 21:55:26 -0400131 ## ----------------------------------------------------------------------
132 ## This logic is problematic, when run on a node processing concurrent
133 ## jobs over-write will corrupt config for the other running job.
134 ## ----------------------------------------------------------------------
135 ## Future enhancement: Optimal answer would be to create and use configs
136 ## from a job-specific temp/config directory.
137 ## ----------------------------------------------------------------------
Matteo Scandolo42f6e572021-01-25 15:11:34 -0800138
Joey Armstrongf060aee2023-08-22 21:55:26 -0400139 umask 022
Matteo Scandolo42f6e572021-01-25 15:11:34 -0800140
Joey Armstrongf060aee2023-08-22 21:55:26 -0400141 echo
Joey Armstrong74ec08c2023-08-31 11:25:57 -0400142 echo "** Generate $HOME/.kube/config"
Joey Armstrongf060aee2023-08-22 21:55:26 -0400143 mkdir -p "$HOME/.kube"
Joey Armstrongf060aee2023-08-22 21:55:26 -0400144 chmod u=rwx "$HOME/.kube"
145 kind get kubeconfig --name ${cfg.name} > "$HOME/.kube/config"
Joey Armstrong74ec08c2023-08-31 11:25:57 -0400146 chmod -R u+w,go-rwx "$HOME/.kube"
Joey Armstrongf060aee2023-08-22 21:55:26 -0400147
148 echo
Joey Armstrong74ec08c2023-08-31 11:25:57 -0400149 echo "Display .kube/ and .volt/ configs"
Joey Armstrongf060aee2023-08-22 21:55:26 -0400150 /bin/ls -l "$HOME/.kube" "$HOME/.volt"
Joey Armstrong74ec08c2023-08-31 11:25:57 -0400151""")
Joey Armstrongf060aee2023-08-22 21:55:26 -0400152
Joey Armstrong74ec08c2023-08-31 11:25:57 -0400153 sh(label : 'Install kail',
154 script : """
155cat <<EOM
156
157** -----------------------------------------------------------------------
158** IAM: ${iam}
159** Install kail
160** -----------------------------------------------------------------------
161EOM
Joey Armstrong16bd8e82023-01-12 15:20:06 -0500162 make -C "$WORKSPACE/voltha-system-tests" KAIL_PATH="$WORKSPACE/bin" kail
Joey Armstrong74ec08c2023-08-31 11:25:57 -0400163""")
Joey Armstrong74961272023-01-12 13:35:54 -0500164
Roger Luethiff337d82023-09-20 12:09:44 +0200165 leave('main')
Joey Armstrong74961272023-01-12 13:35:54 -0500166 return
Matteo Scandolo42f6e572021-01-25 15:11:34 -0800167}
Joey Armstrong16bd8e82023-01-12 15:20:06 -0500168
169// [EOF]