Skip to content

Commit

Permalink
add delete namespace feature
Browse files Browse the repository at this point in the history
  • Loading branch information
manolo committed Jan 19, 2025
1 parent 93e499f commit 063db86
Show file tree
Hide file tree
Showing 4 changed files with 70 additions and 27 deletions.
9 changes: 5 additions & 4 deletions scripts/pit/lib/lib-ccenter.sh
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ installCC() {
[ -n "$CC_KEY" -a -n "$CC_CERT" ] && args="--set app.tlsSecret=$CC_TLS_A --set keycloak.tlsSecret=$CC_TLS_K"
runCmd "$TEST" "Installing Vaadin Control Center" \
"helm install control-center oci://docker.io/vaadin/control-center \
-n $CC_NS --create-namespace --set livenessProbe.failureThreshold=10 \
-n $CC_NS --create-namespace --set livenessProbe.failureThreshold=20 \
--set domain=$CC_DOMAIN \
--set user.email=$CC_EMAIL \
--set app.host=$CC_CONTROL --set keycloak.host=$CC_AUTH $args \
Expand Down Expand Up @@ -90,17 +90,18 @@ runControlCenter() {
start)
checkCommands kind helm docker kubectl || return 1
## Clean up from a previous run
stopForwardIngress $CC_NS
stopCloudProvider
deleteCluster $CC_CLUSTER
deleteNamespace $CC_CLUSTER $CC_NS
## Start a new cluster
createCluster $CC_CLUSTER $CC_NS || return 1
startCloudProvider || return 1
## Install Control Center
installCC || waitForCC 400 || return 1
installCC || waitForCC 800 || return 1
tmp_email=`kubectl get secret control-center-user -o go-template="{{ .data.email | base64decode | println }}"`
computeTemporaryPassword
forwardIngress $CC_NS || return 1
waitUntilHttpResponse https://$CC_CONTROL 443 120 || return 1
waitUntilHttpResponse https://$CC_AUTH 443 120 || return 1
## Update TLS certificates
[ -n "$CC_KEY" -a -n "$CC_CERT" ] && installTls || NO_TLS="--notls"
## Run Playwright tests
Expand Down
59 changes: 40 additions & 19 deletions scripts/pit/lib/lib-k8s-kind.sh
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
startCloudProvider() {
[ -z "$TEST" ] && docker container inspect kind-cloud-provider >/dev/null 2>&1 && log "Docker Kind Cloud Provider already running" && return
runCmd "$TEST" "Starting Docker KinD Cloud Provider" \
"docker run --quiet --name kind-cloud-provider --rm -d --network kind \
"docker run --quiet --name kind-cloud-provider --rm -d --network kind -p 443:443 \
-v /var/run/docker.sock:/var/run/docker.sock \
rophy/cloud-provider-kind:0.4.0-20241026-r1"
}
Expand All @@ -15,32 +15,38 @@ stopCloudProvider() {
docker ps | grep envoyproxy/envoy | awk '{print $1}' | xargs docker kill 2>/dev/null
}

##
# $1: command
setSuid() {
W=`which $1` || return 1
R=`realpath $W` || return 1
O=`ls -l "$R" | awk '{print $3}'`
P=`ls -l "$R" | awk '{print $1}'`
echo "$O $P"
[ "$O" = "root" ] || runCmd "$TEST" "Changing owner to root to: $R" "sudo chown root $R" || return 1
expr "$P" : "^-..s" >/dev/null || runCmd "$TEST" "Setting sUI to $R" "sudo chmod u+s $R" || return 1
}

##
# $1: namespace
# $2: service
# $3: port in guest
# $4: target port in host
startPortForward() {
[ -z "$3" ] && echo "args err usage: startPortForward name-space service port" && return 1
H=`getPids "kubectl port-forward $2"`
[ -n "$H" ] && return 0
KUBECTL=`which kubectl`
[ -n "$H" ] && log "Already running k8s port-forward $1 $2 $3 -> $4 with pid $H" && return 0
[ -z "$TEST" ] && log "Starting k8s port-forward $1 $2 $3 -> $4"
if isLinux || isMac ; then
[ -z "$TEST" ] && log "listening to ports <1024 requires sudo, type your password if requested"
[ -n "$TEST" ] && cmd "## Start k8s port-forward service $2 port:$3 -> localhost:$4"
cmd "sudo KUBECONFIG=\$HOME/.kube/config kubectl port-forward $2 $4:$3 -n $1"
[ -z "$TEST" ] && sudo -n true || sudo true
sudo -n true || return 1
[ -z "$TEST" ] && sudo -n KUBECONFIG="$HOME/.kube/config" $KUBECTL port-forward $2 $4:$3 -n $1 &
else
[ -n "$TEST" ] && cmd "## Start k8s port-forward service $2 port:$3 -> localhost:$4"
cmd "KUBECONFIG=\$HOME/.kube/config kubectl port-forward $2 $4:$3 -n $1"
[ -z "$TEST" ] && $KUBECTL port-forward $2 $4:$3 -n $1 &
fi
[ -z "$TEST" ] && sleep 2 || return 0
[ "$4" -le 1024 ] && setSuid kubectl || return 1
runCmd "$TEST" "Starting k8s port-forward $1 $2 $3 -> $4" \
"kubectl port-forward $2 $4:$3 -n $1 &"
}

##
# $1: service
stopPortForward() {
H=`getPids kubectl "port-forward service/$1"`
[ -z "$H" ] && return 0
log "Stoping k8s port-forward $1 (pid $H)"
sudo bash -c "kill -9 $H" || return 1
runCmd "$TEST" "Stoping k8s port-forward $1" "kill -TERM $H" || return 1
}

forwardIngress() {
Expand All @@ -51,18 +57,33 @@ stopForwardIngress() {
stopPortForward control-center-ingress-nginx-controller
}

##
# $1: cluster name
# $2: namespace
createCluster() {
kind get clusters | grep -q "^$1$" && return 0
runCmd "$TEST" "Creating KinD cluster: $1" \
"kind create cluster --name $1" || return 1
runCmd "$TEST" "Setting default namespace $2" \
"kubectl config set-context --current --namespace=$2"
}

##
# $1: cluster name
deleteCluster() {
kind get clusters | grep -q "^$1$" || return 0
runCmd "$TEST" "Deleting Cluster $1" \
"kind delete cluster --name $1" || return 1
}

##
# $1: cluster name
# $2: namespace
deleteNamespace() {
kind get clusters | grep "^$1$" || return 0
kubectl get ns | grep "^$2" || return 0
runCmd "$TEST" "Cleaning KinD Cluster $1, Namespace: $2" "kubectl delete ns $2"
}



Expand Down
25 changes: 22 additions & 3 deletions scripts/pit/lib/lib-utils.sh
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ getPids() {
if [ -n "$H" ]
then
_P=`echo "$H" | grep "$1" | awk '{print $1}'`
else
else
_P=`ps -feaw | grep "$1" | grep -v grep | awk '{print $2}'`
fi
[ -n "$_P" ] && echo "$_P" | tr "\n" " " && return 0 || return 1
Expand Down Expand Up @@ -184,7 +184,16 @@ runCmd() {
_cmd="${*}"
cmd "$_cmd"
[ true = "$_skip" -o test = "$_skip" ] && return 0
eval "$_cmd"
if expr "$_cmd" : ".*&$" >/dev/null
then
_cmd=`echo "$_cmd" | sed -e 's/&$//'`
eval "$_cmd" &
_pid=$!
sleep 2
kill -0 $_pid 2>/dev/null || return 1
else
eval "$_cmd"
fi
}

## Run a command and outputs its stdout/stderr to a file
Expand Down Expand Up @@ -356,7 +365,7 @@ checkHttpServlet() {
[ -n "$TEST" ] && return 0
rm -f $__cfile
log "Checking whether url $__url returns HTTP 200"
runToFile "curl -s --fail -I -L -H Accept:text/html $__url" "$__cfile" "$VERBOSE"
runToFile "curl -s --fail -I -L --insecure -H Accept:text/html $__url" "$__cfile" "$VERBOSE"
[ $? != 0 ] && reportOutErrors "$__ofile" "Server Logs" && return 1 || return 0
}

Expand Down Expand Up @@ -393,6 +402,16 @@ waitUntilFrontendCompiled() {
done
}

waitUntilHttpResponse() {
log "Waiting for HTTP response in $1"
while true; do
H=`curl -s -I -L --insecure --fail $1 | grep ^HTTP`
[ -n "$H" ] && return 0
printf "."
sleep 3
done
}

## get a property value from pom.xml, normally used for version of some dependency
## $1: property name
getMavenVersion() {
Expand Down
4 changes: 3 additions & 1 deletion scripts/pit/run.sh
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
#!/usr/bin/env bash

## MAIN script to be run for PiT tests
## for a list of arguments run the script with --help option

Expand Down Expand Up @@ -114,7 +116,7 @@ main() {
fi
run runDemo "$i" "$tmp"
done


cd "$pwd"

Expand Down

0 comments on commit 063db86

Please sign in to comment.