blob: 9653840672320586e83aab0df3fe67b400d14fef [file] [log] [blame]
#!/usr/bin/env groovy
// -----------------------------------------------------------------------
// Copyright 2021-2023 Open Networking Foundation (ONF) and the ONF Contributors
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
// -----------------------------------------------------------------------
// Intent: sets up a kubernetes cluster (using kind)
// -----------------------------------------------------------------------
// -----------------------------------------------------------------------
// -----------------------------------------------------------------------
def getIam(String func)
{
// Cannot rely on a stack trace due to jenkins manipulation
String src = 'vars/createKubernetesCluster.groovy'
String iam = [src, func].join('::')
return iam
}
// -----------------------------------------------------------------------
// -----------------------------------------------------------------------
def call(Map config) {
String iam = getIam('main')
println("** ${iam}: ENTER")
// note that I can't define this outside the function as there's no global scope in Groovy
def defaultConfig = [
branch: "master", // branch=master ?!?
nodes: 1,
name: "kind-ci"
]
if (!config) {
config = [:]
}
def cfg = defaultConfig + config
println "Deploying Kind cluster with the following parameters: ${cfg}."
// TODO support different configs
def data = '''
kind: Cluster
apiVersion: kind.x-k8s.io/v1alpha4
nodes:
- role: worker
- role: worker
- role: control-plane
kubeadmConfigPatches:
- |
kind: InitConfiguration
nodeRegistration:
kubeletExtraArgs:
node-labels: "ingress-ready=true"
extraPortMappings:
- containerPort: 80
hostPort: 80
protocol: TCP
- containerPort: 443
hostPort: 443
protocol: TCP
- containerPort: 30115
hostPort: 30115
- containerPort: 30120
hostPort: 30120
'''
writeFile(file: 'kind.cfg', text: data)
// TODO: Skip kind install, make install-kind-command has done it already
sh """
mkdir -p "$WORKSPACE/bin"
# download kind (should we add it to the base image?)
curl -Lo ./kind https://kind.sigs.k8s.io/dl/v0.11.0/kind-linux-amd64
chmod +x ./kind
mv ./kind $WORKSPACE/bin/kind
"""
// install voltctl
installVoltctl("${cfg.branch}")
sh """
cat <<EOM
** -----------------------------------------------------------------------
** Starting kind cluster
** -----------------------------------------------------------------------
EOM
# start the kind cluster
kind create cluster --name ${cfg.name} --config kind.cfg
# remove NoSchedule taint from nodes
for MNODE in \$(kubectl get node --selector='node-role.kubernetes.io/master' -o json | jq -r '.items[].metadata.name'); do
kubectl taint node "\$MNODE" node-role.kubernetes.io/master:NoSchedule-
done
## ----------------------------------------------------------------------
## This logic is problematic, when run on a node processing concurrent
## jobs over-write will corrupt config for the other running job.
## ----------------------------------------------------------------------
## Future enhancement: Optimal answer would be to create and use configs
## from a job-specific temp/config directory.
## ----------------------------------------------------------------------
umask 022
echo
echo "** Generate ~/.volt/config"
mkdir -p "$HOME/.volt"
chmod -R u+w,go-rwx "$HOME/.volt"
chmod u=rwx "$HOME/.volt"
voltctl -s localhost:55555 config > "$HOME/.volt/config"
echo
echo "** Generate ~/.kube/config"
mkdir -p "$HOME/.kube"
chmod -R u+w,go-rwx "$HOME/.kube"
chmod u=rwx "$HOME/.kube"
kind get kubeconfig --name ${cfg.name} > "$HOME/.kube/config"
echo
echo "Display .kube/ and .volt"
/bin/ls -l "$HOME/.kube" "$HOME/.volt"
echo
echo "Install Kail"
make -C "$WORKSPACE/voltha-system-tests" KAIL_PATH="$WORKSPACE/bin" kail
"""
println("** ${iam}: LEAVE")
return
}
// [EOF]