blob: ee0531743a07ce6325d3b290c5bf96bab2eb652a [file] [log] [blame]
Joey Armstrong74961272023-01-12 13:35:54 -05001#!/usr/bin/env groovy
2// -----------------------------------------------------------------------
Joey Armstrong518f3572024-02-11 07:56:25 -05003// Copyright 2021-2024 Open Networking Foundation (ONF) and the ONF Contributors
Joey Armstrong16bd8e82023-01-12 15:20:06 -05004//
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
Jan Klare974ea552023-10-02 11:32:05 +0200127 # remove validating webhook for now, since some of the pipelines seem to
128 # fail with errors related to calling this hook and although there is a
129 # lot of talk in the internet about this issue, the solution very much
130 # depends on your exact setup (and it might not be worth investigating the
131 # details for a k8s version which has been EOL since some time now)
132 # https://stackoverflow.com/questions/61616203/nginx-ingress-controller-failed-calling-webhook
133 # This should be removed and properly fixed once we upgrade to a recent
134 # k8s version and a fitting ingress-nginx deployment
135 kubectl delete -A ValidatingWebhookConfiguration ingress-nginx-admission
136
Joey Armstrong74ec08c2023-08-31 11:25:57 -0400137""")
Matteo Scandolo42f6e572021-01-25 15:11:34 -0800138
Joey Armstrong74ec08c2023-08-31 11:25:57 -0400139 sh(label : 'Normalize config permissions',
140 script : """
Joey Armstrongf060aee2023-08-22 21:55:26 -0400141 ## ----------------------------------------------------------------------
142 ## This logic is problematic, when run on a node processing concurrent
143 ## jobs over-write will corrupt config for the other running job.
144 ## ----------------------------------------------------------------------
145 ## Future enhancement: Optimal answer would be to create and use configs
146 ## from a job-specific temp/config directory.
147 ## ----------------------------------------------------------------------
Matteo Scandolo42f6e572021-01-25 15:11:34 -0800148
Joey Armstrongf060aee2023-08-22 21:55:26 -0400149 umask 022
Matteo Scandolo42f6e572021-01-25 15:11:34 -0800150
Joey Armstrongf060aee2023-08-22 21:55:26 -0400151 echo
Joey Armstrong74ec08c2023-08-31 11:25:57 -0400152 echo "** Generate $HOME/.kube/config"
Joey Armstrongf060aee2023-08-22 21:55:26 -0400153 mkdir -p "$HOME/.kube"
Joey Armstrongf060aee2023-08-22 21:55:26 -0400154 chmod u=rwx "$HOME/.kube"
155 kind get kubeconfig --name ${cfg.name} > "$HOME/.kube/config"
Joey Armstrong74ec08c2023-08-31 11:25:57 -0400156 chmod -R u+w,go-rwx "$HOME/.kube"
Joey Armstrongf060aee2023-08-22 21:55:26 -0400157
158 echo
Joey Armstrong74ec08c2023-08-31 11:25:57 -0400159 echo "Display .kube/ and .volt/ configs"
Joey Armstrongf060aee2023-08-22 21:55:26 -0400160 /bin/ls -l "$HOME/.kube" "$HOME/.volt"
Joey Armstrong74ec08c2023-08-31 11:25:57 -0400161""")
Joey Armstrongf060aee2023-08-22 21:55:26 -0400162
Joey Armstrong74ec08c2023-08-31 11:25:57 -0400163 sh(label : 'Install kail',
164 script : """
165cat <<EOM
166
167** -----------------------------------------------------------------------
168** IAM: ${iam}
169** Install kail
170** -----------------------------------------------------------------------
171EOM
Joey Armstrong16bd8e82023-01-12 15:20:06 -0500172 make -C "$WORKSPACE/voltha-system-tests" KAIL_PATH="$WORKSPACE/bin" kail
Joey Armstrong74ec08c2023-08-31 11:25:57 -0400173""")
Joey Armstrong74961272023-01-12 13:35:54 -0500174
Roger Luethiff337d82023-09-20 12:09:44 +0200175 leave('main')
Joey Armstrong74961272023-01-12 13:35:54 -0500176 return
Matteo Scandolo42f6e572021-01-25 15:11:34 -0800177}
Joey Armstrong16bd8e82023-01-12 15:20:06 -0500178
179// [EOF]