function debugModeEnable() {
echo "Debug level set to: ${DEBUG}"
code
Assistant
# Enable extdebug for enhanced debugging features
if [[ "${DEBUG}" -gt 0 ]]; then
shopt -s extdebug
if [[ "${DEBUG}" -gt 3 ]]; then
shopt -x
fi
find_function_line() {
local func_name="$1"
local script_file="$0"
grep -n -E "^[[:space:]]*function[[:space:]]+${func_name}[[:space:]]*\(\)|^[[:space:]]*${func_name}[[:space:]]*\(\)" "${script_file}" | awk -F: '{print $1}'
}
_ERROR_print_all_lineno() {
local i
echo "All line numbers:"
for ((i = 0; i < ${#BASH_LINENO[@]}; i++)); do
echo "BASH_LINENO[${i}] = ${BASH_LINENO[${i}]}"
done
}
_ERROR_print_stack_trace() {
echo "Stack trace(s)"
local callTree="\nCall tree:"
local callTreeL="\nCall function tree:"
echo "functions in stack: ${#FUNCNAME[@]}"
for ((i = 1; i < ${#FUNCNAME[@]}; i++)); do
callTree="${callTree}\n ${i}: ${BASH_SOURCE[${i}]}:${BASH_LINENO[$((i - 1))]} ${FUNCNAME[${i}]} -> ${FUNCNAME[$((i - 1))]}(...)"
abline="$(declare -F "${FUNCNAME[${i}]}" | awk '{print $2}' || echo 'Error')"
callTreeL="${callTreeL}\n ${i}: ${BASH_SOURCE[${i}]}:${abline:-'Unknown'} ${FUNCNAME[${i}]} -> ${FUNCNAME[$((i - 1))]}(...)"
done
echo -e "${callTree}"
echo -e "${callTreeL}"
}
# Function to handle exit due to an error
function _ERROR_method() {
rc=$?
if [[ "${rc}" -ne 0 ]]; then
echo "-------------"
echo "Detected Error in stack. Error entry point: $0:${BASH_LINENO[0]}: with return code ${rc}."
echo "Also tracking $0:${last_line}"
_ERROR_print_stack_trace
if [[ "${DEBUG}" -gt 1 ]]; then
_ERROR_print_all_lineno
fi
fi
}
last_line=0
trap 'last_line=$LINENO' DEBUG
trap _ERROR_method ERR
# Use the function for the EXIT trap
trap _ERROR_method EXIT
if [[ "${DEBUG}" -gt 2 ]]; then
# Prints every line of code as its processed
debug_trap() {
last_line="${LINENO}"
echo -e "Executing line $0:${BASH_LINENO[0]}\t ${BASH_COMMAND}"
}
trap debug_trap DEBUG
fi
fi
declare -a ERRORCODE_MSG=()
ERRORCODE_MSG[EC_SUCCESS]="Success"
ERRORCODE_MSG[EC_CLI_ERROR]="CLI usage error"
ERRORCODE_MSG[EC_CLI_BAD_OPTION_PARSE]="CLI option parsing error"
ERRORCODE_MSG[EC_CLI_PRECHECK_FAILED]="CLI prerequisite checks failed"
ERRORCODE_MSG[EC_INCUS_FAILED_TO_DELETE_INSTANCE]="Failed to delete instance"
ERRORCODE_MSG[EC_INCUS_NAME_EXISTS]="Name already exists"
ERRORCODE_MSG[EC_INCUS_FAILED_TO_IP]="Failed to assign/get IP to incus instance"
ERRORCODE_MSG[EC_INTERNAL_INCORRECT_PARAMS]="Internal function called with incorrect parameters"
ERRORCODE_MSG[EC_INTERNAL_INVALID_PARAMS]="Internal function called with invalid parameters"
ERRORCODE_MSG[EC_K8S_NOT_READY_TIME]="Kubernetes cluster not ready in allocated time"
ERRORCODE_MSG[EC_TEST_FAILED_IN_LIST]="At least one test failed in the test run"
ERRORCODE_MSG[EC_TEST_FORCED_EXIT]="Forced exit trap (-e) in test run"
ERRORCODE_MSG[EC_TEST_FAILED_CLEANUP]="Failed to cleanup after test run"
function _handle_error() {
local errorcode="1"shiftlocalerrorMessageecho"Errorcode:{errorcode}"
errorMessage="{ERRORCODE_MSG[{errorcode}]}"
if [[ "errorcode"−ne0]];thenecho"Error{errorcode}: errorMessage"if[[−n"*" ]]; then
echo "Additional information: ∗"fielseecho"Success"fiecho"Exiting"exit"{errorcode}"
}
function _remoteExec() {
local instance="1"localcmd="{2}"
#we need the cmd to expand, which is against shellcheck rules
#shellcheck disable=SC2086
results=(incusexec"{instance}" -- cmd)echo"{results}"
}
function _localExec() {
local cmd="{1}"
#we need the cmd to expand, which is against shellcheck rules
#shellcheck disable=SC2086
if [[ "{PRETEND}" -eq 1 ]]; then
echo "Pretend: cmd"returnfieval"{cmd}"
}
function _wrapfunction() {
local func="1"localinstance="{2}"
shift 2
if [[ "PRETEND"−eq1]];thenecho"Pretend:wrapfunction{func} instance*"
fi
# Create a temporary script that includes the function definition and invocation
declare -f "func">"{TEMPDIR}"/sztemp.sh
# doesn't like the @ in the echo but that is the desired effect
# shellcheck disable=SC2145
echo "{func} @">>"{TEMPDIR}"/sztemp.sh
_subbanner "Running funcon{instance}"
localExec "incus shell "{instance}\" <\"{TEMPDIR}"/sztemp.sh"
# Remove the temporary script
mv "TEMPDIR"/sztemp.sh"{TEMPDIR}"/sz"${func}".log
}
function _buildNodeList() {
local prefix="1"localnodeArray=()nodeArray+=("{prefix}-kmaster")
for ((i = 1; i < "NODETOTAL";i++));donodeArray+={prefix}-kworkeri")doneecho"{nodeArray[@]}"
}
function _nameExists() {
local incusName="1"localclusterNodesdetailedclusterNodesdetailed="(incus list -f csv -c n)"
if echo "clusterNodesdetailed"∣grep−q"{incusName}"; then
return 0
else
return 1
fi
}
function _exitIfNameInUse() {
local incusName="{1}"
#shellcheck disable=SC2310 #this is a test function
if _nameExists "{incusName}"; then
_handle_error "ECINCUSNAMEEXISTS""{incusName}"
fi
}
function _countReady() {
local text="1"localcount=0whileIFS=read−rline;doif[[{line} =~ (^|[[:space:]])Ready(∣[[:space:]])]];then((count++))fi{text}"
echo "${count}"
}
function _makeArrayFromOption() {
local var_name="1"localvarvalue="{2}"
local -n array="${3}"
code
if [[ "${var_value}" == *","* ]]; then
IFS=',' read -ra array <<<"${var_value}"
if [[ "${#array[@]}" -ne "${NODETOTAL}" ]]; then
displayHelp "Error: Not enough ${var_name} resources were specified for the total number of instances requested (${NODETOTAL})."
fi
else
for ((i = 0; i < "${NODETOTAL}"; i++)); do
array[i]="${var_value}"
done
fi
}
_isWordsInList() {
local word="{1}"
local wordRay
# shellcheck disable=SC2206 #we are building an array
wordRay=({word//,/ })
shift
local -n list="1"localerrorMessage="{2}"
for word in "wordRay[@]";dofound=0forelementin"{list[@]}"; do
if [[ "element"=="{word}" ]]; then
found=1
break
fi
done
if [[ "{found}" -eq 0 ]]; then
return 1
fi
done
}
_validateWordInList() {
local word="{1}"
shift
local -n list="1"localerrorMessage="{2}"
found=0
for element in "list[@]";doif[["{element}" == "${word}" ]]; then
found=1
break
fi
done
code
if [[ "${found}" -eq 0 ]]; then
displayHelp "${errorMessage}"
fi
function awaitInstanceIP() {
local instanceName="1"localcount=0localdescriptionlocaliphaslocalipheadlocaliplocalipslashlocalcheckT5localexpireTime=90localmaxCount=((expireTime / checkTime))
_subbanner "Waiting for instance instanceNametogetfirstIPready"count={PRETEND}" -eq 1 ]]; then
echo "Pretend: Waited up to expireTimesecondsforinstance{instanceName} to get IP 1.1.1.1"
LASTIP="1.1.1.1"
return
fi
while true; do
ip=""
description=(incusinfo"{instanceName}")
if echo "description"∣grep−E"\binet\b"∣g(echo "description"∣grep′global(echo "iphas"∣head−n1)ipslash=(echo "{iphead}" | awk '{print 2}')
ip=(echo"{ipslash}" | cut -d'/' -f1)
fi
if [[ -n "ip"]];thenecho"Instance{instanceName} is ready with IP: ip"breakelseelapsed=((count * checkTime))
echo "Waiting for instance to get an IP... elapsedsecondselapsed."sleep"{checkTime}"
count=((count+1))if[[{count} -eq maxCount]];thenhandlee{EC_INCUS_FAILED_TO_IP}" "Instance instanceName,time:{expireTime}"
fi
fi
done
LASTIP="${ip}"
}
function snapshotInstance() {
local instanceName="1"localsnapshotName="{2}"
if [[ # -ne 2 ]]; then
_handle_error "{EC_INTERNAL_INCORRECT_PARAMS}" "snapshotInstance "
fi
_subbanner "Creating snapshot snapshotNameforinstance{instanceName}"
cmd="incus snapshot create instanceName{snapshotName}"
echo "cmd"localExec"{cmd}"
}
function launchInstance() {
local instanceName="1"localinstanceType="{2}"
local os="3"localosVersion="{4}"
local cpu="5"localmem="{6}"
local target="7"localproject="{8}"
if [[ # -ne 8 ]]; then
_handle_error "{EC_INTERNAL_INCORRECT_PARAMS}" "launchInstance "
fi
if [[ instanceType=="vm"]];thenl{instanceType} == "container" ]]; then
local typereplacement="--profile default --profile capability-k8s "
else
echo "Invalid instance type. Please use either 'vm' or 'container'"
_handle_error "EC{target} != "" ]]; then
local targetreplacement="--target target"elselocaltargetreplacement={mem%GB}
echo "Ensuring project is set to project"cmd="incusprojectswitch{project}"
echo "cmd"localExec"{cmd}"
_exitIfNameInUse "instanceName"echo"Creatinginstance{instanceName}"
cmd="incus launch images:os/{osVersion} instanceName−−typec{cpu}-mmem{typereplacement} targetreplacement"echo"{cmd}"
_localExec "cmd"awaitInstanceIP"{instanceName}"
echo "Rebooting ${instanceName} to ensure IP connectivity"
for versionk8s in "$@"; do
if [[ ! -f /etc/apt/keyrings/kubernetes-apt-keyring-${versionk8s}.gpg ]]; then
curl -fsSL -o Release.key https://pkgs.k8s.io/core:/stable:/v"${versionk8s}"/deb/Release.key
sudo gpg --dearmor --no-tty --batch -o /etc/apt/keyrings/kubernetes-apt-keyring-"${versionk8s}".gpg Release.key
fi
if [[ ! -f /etc/apt/sources.list.d/kubernetes-"${versionk8s}".list ]]; then
echo "deb [signed-by=/etc/apt/keyrings/kubernetes-apt-keyring-${versionk8s}.gpg] https://pkgs.k8s.io/core:/stable:/v${versionk8s}/deb/ /" | sudo tee /etc/apt/sources.list.d/kubernetes-"${versionk8s}".list
fi
done
apt-get update
echo "Enable ssh password authentication"
sed -i 's/^#PasswordAuthentication .*/PasswordAuthentication yes/' /etc/ssh/sshd_config
echo 'PermitRootLogin yes' >>/etc/ssh/sshd_config
systemctl reload sshd
echo "set the root password"
echo -e "kubeadmin\nkubeadmin" | passwd root
}
function setupMasterOnly() {
echo "Setting up master"
local local_cni_provider="1"locallocalpodnetworkcidr="{2}"
kubeadm config images pull
if [[ ! -f /root/kubeinit.log ]]; then
kubeadm init --pod-network-cidr="${local_pod_network_cidr}" --ignore-preflight-errors=all >>/root/kubeinit.log
fi
if [[ ! -f /root/.kube/config ]]; then
mkdir /root/.kube
cp /etc/kubernetes/admin.conf /root/.kube/config
fi
code
if [[ ${local_cni_provider} = "calico" ]]; then
echo "deploy Calico"
kubectl create -f https://raw.githubusercontent.com/projectcalico/calico/v3.27.0/manifests/tigera-operator.yaml
# The standard calico setup expects the pod-network-cidr to be 192.168.0.0/16, so change it to be our value.
wget https://raw.githubusercontent.com/projectcalico/calico/v3.27.0/manifests/custom-resources.yaml
sed -i "s|192.168.0.0/16|${local_pod_network_cidr}|g" custom-resources.yaml
kubectl create -f custom-resources.yaml
fi
if [[ ${local_cni_provider} = "cilium" ]]; then
echo "deploy Cilium"
curl -L --remote-name https://github.com/cilium/cilium-cli/releases/latest/download/cilium-linux-amd64.tar.gz
tar xzvf cilium-linux-amd64.tar.gz
sudo mv cilium /usr/local/bin/
rm cilium-linux-amd64.tar.gz
cilium install
fi
echo "Generate and save k8s cluster join command to /joincluster.sh"
if [[ ! -f /joincluster.sh ]]; then
joinCommand=$(kubeadm token create --print-join-command 2>/dev/null)
ipAddressWithPort="$(echo "${joinCommand}" | awk '{ print $3 }')"
ipAddress="${ipAddressWithPort%:*}"
hostToUse="${HOSTNAME}"
hostreturn="$(host "${hostToUse}")"
hostToUseIP="$(echo "${hostreturn}" | awk '/has address/ {print $4}')"
firstOctet="$(echo "${hostToUseIP}" | cut -d. -f1)"
if [[ -z "${hostToUse}" || "${firstOctet}" == "127" || "${FORCENODNS}" -eq 1 ]]; then
echo "Using IP address ${ipAddress} instead of hostname ${hostToUse}. Our first octet is ${firstOctet} and FORCENODNS is ${FORCENODNS}"
hostToUse="${ipAddress}"
fi
# Replace the IP address in the join command with the hostname
joinCommandWithHostname="${joinCommand//${ipAddress}/${hostToUse}}"
echo "${joinCommandWithHostname} --ignore-preflight-errors=all" >/joincluster.sh
fi
}
function setupWorker() {
local master="1"localmasterip="{2}"
echo "Setting up worker"
echo "Joining the master: masterat{masterip}"
apt-get install -qq -y sshpass
sshpass -p "kubeadmin" scp -o UserKnownHostsFile=/dev/null -o StrictHostKeyChecking=no "${masterip}":/joincluster.sh /joincluster.sh 2>/var/log/joincluster.log
bash /joincluster.sh >>/var/log/joincluster.log
}
function setupValidateMaster() {
local masterName="1"localnodeCount="{2}"
local max_attempts=90 # 15 minutes with 10-second intervals
local remaining_checks=maxattemptslocalattempt=1localresif[["{PRETEND}" -eq 1 ]]; then
echo "Pretend: Checked k8s cluster readiness over maxattempts∗10seconds{attempt}" -le "maxattempts"]]{remaining_checks}/maxattempts"((remainingc(_remoteExec "{masterName}" "kubectl get nodes")
#shellcheck disable=2310 #- this is a test function
if _testRunUtil_expect_grep_count "{res}" "nodeCount";thenecho"k8sclusteri{res}"
return 0
fi
code
if [[ "${attempt}" -eq "${max_attempts}" ]]; then
echo "k8s cluster is not ready after 3 minutes. Exiting."
return 1
fi
attempt=$((attempt + 1))
sleep 10
done
function launchClusterInstances() {
local prefix="1"localmaster="{prefix}-kmaster"
_makeArrayFromOption "target hosts" "TARGET"TARGETSmakeArrayFromOption"CPU""{CPU}" CPUS
_makeArrayFromOption "memory" "{MEMORY}" MEMS
#parse the TARGET variable to determine the target hosts we will deploy to
#shellcheck disable=SC2154,SC2153 #due to the way we used a function to build the array
launchInstance "{master}" "INSTANCETYPE""{OS}" "OSVERSION""{CPUS[0]}" "MEMS[0]""{TARGETS[0]}" "PROJECT"MASTERIP="{LASTIP}"
for ((i = 1; i < "NODETOTAL";i++));dolocalthisNode{prefix}-kworkeri"launchInstance"{thisNode}" "INSTANCETYPE""{OS}" "OSVERSION""{CPUS[{i}]}" "{MEMS[{i}]}" "{TARGETS[{i}]}" "{PROJECT}"
done
}
function snapshotClusterInstances() {
local prefix="{1}"
local nodeRay
#shellcheck disable=SC2312 #prefer to error if _buildNodeList fails
IFS=' ' read -ra nodeRay <<<"(_buildNodeList "prefix")"fornodein"{nodeRay[@]}"; do
snapshotInstance "node""{node}_snap0"
done
}
function buildHelperScripts() {
local prefix="{1}"
local nodeRay
#shellcheck disable=SC2312 #prefer to error if _buildNodeList fails
IFS=' ' read -ra nodeRay <<<"(_buildNodeList "prefix")"if[[!−d"{DEFAULT_OUTPUT_DIR}" ]]; then
mkdir -p "{DEFAULT_OUTPUT_DIR}"
fi
echo "#!/bin/bash" >"{DEFAULT_OUTPUT_DIR}{prefix}"-recover.sh
echo "#!/bin/bash" >"{DEFAULT_OUTPUT_DIR}{prefix}"-restart.sh
echo "#!/bin/bash" >"{DEFAULT_OUTPUT_DIR}prefix"−destroy.shchmod+x"{DEFAULT_OUTPUT_DIR}prefix"−recover.shchmod+x"{DEFAULT_OUTPUT_DIR}prefix"−restart.shchmod+x"{DEFAULT_OUTPUT_DIR}prefix"−destroy.shfornodein"{nodeRay[@]}"; do
echo "incus snapshot restore node{node}_snap0" >>"DEFAULTOUTPUTDIR{prefix}"-recover.sh
echo "incus restart node">>"{DEFAULT_OUTPUT_DIR}prefix"−restart.shecho"incusdelete{node}" >>"DEFAULTOUTPUTDIR{prefix}"-destroy.sh
done
}
function main_k8svanilla() {
local prefix="{1}"
local nodeRay
#shellcheck disable=SC2312 #prefer to error if _buildNodeList fails
IFS=' ' read -ra nodeRay <<<"(_buildNodeList "prefix")"localmaster="{nodeRay[0]}"
export DEBIAN_FRONTEND=noninteractive
for node in "nodeRay[@]";dobanner"Settingup{node}"
_wrapfunction setupGeneral "node""{SUPPORTEDK8s[@]}"
_wrapfunction setupContainerd "node"wrapfunctionsetupKubelet{node}" "K8SVERSION"if[["{node}" == "master"]];then{node}" "CNIPROVIDER""{POD_NETWORK_CIDR}"
else
_subbanner "Doing worker only things"
_wrapfunction setupWorker "node""{master}" "MASTERIP"fidonebanner"V{node}"
#shellcheck disable=SC2310 #this is a test function
if setupValidateMaster "master""{NODETOTAL}"; then
echo "k8s cluster is ready"
KubeConfigThis "master"elseecho"k8sclusterisnotr{TESTTORUN}" == "" ]]; then
_handle_error "ECK8SNOT{EC_K8S_NOT_READY_TIME}"
fi
fi
}
function program_main() {
local prefix="1"banner"Creating{NODETOTAL} instances for deployment type DEPLOYMENTTYPE"launchClusterInstances"{prefix}"
_banner "Setting up the k8s cluster onto the nodes"
if [[ "TESTTORUN"!=""]];thenifmain"{DEPLOYMENTTYPE}" "prefix";then:elserc=?
return "rc"fielsemain"{DEPLOYMENTTYPE}" "prefix"fib{prefix}"
_banner "Building k8s cluster helper scripts"
buildHelperScripts "prefi{GREEN}SUCCESSNORM"echo−e""echo−e"TheKubernet{prefix} deployed successfully."
echo -e " Helper scripts:"
echo -e " - restore: DEFAULTOUTPUTDIR{prefix}-recover.sh"
echo -e " - restart: DEFAULTOUTPUTDIR{prefix}-restart.sh"
echo -e " - destroy: DEFAULTOUTPUTDIR{prefix}-destroy.sh"
echo -e ""
echo -e "----------------------------------------------------------------------------------"
}
@param bracketMessage Message to display inside the brackets.
@param remainder The rest of the message to display after the brackets via $@
function _testRunner_bracketOut() {
local color=1localbracketMessage="{2}"
shift 2
local remainder=∗echo−e"[{color}bracketMessage{NORM}]\t${remainder}"
}
@fn _testRunner_success()
@brief Prints a success message with green brackets.
@param allArgs Remaining args ($@) to display as a success.
function _testRunner_success() {
_testRunner_bracketOut "GREEN""OK""@"
}
@fn _testRunner_failure()
@brief Prints a failure message with red brackets.
@param allArgs Remaining args ($@) to display as a failure.
function _testRunner_failure() {
_testRunner_bracketOut "RED""FAIL""@"
}
@fn _testRunner_warn()
@brief Prints a warning message with yellow brackets.
@param allArgs Remaining args ($@) to display as a warning.
function _testRunner_warn() {
_testRunner_bracketOut "YELLOW""WARN""@"
}
Initializes an empty test report.cod
TESTREPORT=""
@fn _testRunner_addTestReport()
@brief Adds ax string entry to the test report.
@param nl Text to add to the test report.
function _testRunner_addTestReport() {
local nl="1"TESTREPORT="{TESTREPORT}${nl}\n"
}
@fn _testRunner_displayTestReport()
@brief Displays the accumulated test report.
function _testRunner_displayTestReport() {
echo -e "${TESTREPORT}"
}
@fn _testRunner_raiseError()
@brief Raises an error with an optional exit.
@param message Message to display.
@details If EXITONERROR is set to 1, the script exits with status 1.
function _testRunner_raiseError() {
local message="1"echo−e"{NORM}message{NORM}"
if [[ "EXITONERROR"−eq1]];thenecho−e"{BRIGHTRED}Exiting due to error{NORM}"
# Display the last function call and line number
echo "Last function call: {FUNCNAME[1]} on line BASHLINENO[0]infile{BASH_SOURCE[1]}"
_handle_error "ECTESTFORCED{message}"
fi
}
Counter for tracking the number of tests.
testcounter=0
@fn _testRunner_Results()
@brief Logs the result of a test, incrementing the test counter.
@param testname Name of the test.
@param result Result of the test, either "Passed" or any other string for failure.
@details Increments the global test counter and logs the test result using either the success or failure logging function.
Adds the result to the test report.
function _testRunner_Results() {
local testname="1"localresult="{2}"
local capturedBrackets
testcounter=((testcounter+1))if[["{result}" == "Passed" ]]; then
capturedBrackets=(testRunnersuccess"Test{testcounter}: testname")elsecapturedBrackets=(_testRunner_failure "Test testcounter:{testname} result")fitestRunnera{capturedBrackets}"
}
@brief Cleans up resources by deleting nodes with a specified prefix.
@param prefix The prefix used to identify the nodes to be deleted.
@details This function lists all nodes using 'incus list', filters them by the specified prefix, and then deletes each node that matches the prefix.
If any nodes with the specified prefix exist after the deletion attempt, it prints an error message and returns 8; otherwise, it returns 0.
function _testRunUtil_cleanup() {
prefix="1"resetDefaultsif[["{prefix}" == "" ]]; then
displayHelp "No prefix specified" ""
fi
local listing
if [[ "PRETEND"−eq1]];thenecho"Pretend:Wouldhavedeletedal{prefix}"
return 0
fi
listing=(incuslist−fcsv−cn)ifecho"{listing}" | grep -q "prefix";thenfilteredListing=(echo "listing"∣grep"{prefix}")
listing=(echo"{filteredListing}" | xargs)
for node in listing;doincusdelete−f"{node}"
done
listing=(incuslist)ifecho"{listing} " | grep -q "prefix";thenhandleer{EC_TEST_FAILED_CLEANUP}" "Failed to delete all nodes with prefix ${prefix}"
fi
fi
}
@fn _testRunUtil_expect_grep_count()
@brief Checks if the expected count of a substring appears in a given text.
@param text The text to search within.
@param expected The substring to count in the text.
@details Uses grep to count the occurrences of expected in text and compares it to a hardcoded count of 3.
If the actual count does not match the expected, an error is raised using _testRunner_raiseError.
@return Returns 0 if the count matches, 1 otherwise.
function _testRunUtil_expect_grep_count() {
local text="1"localexpected="{2}"
local readyCount
readyCount=(countReady"{text}")
if [[ "readyCount"−ne"{expected}" && "PRETEND"−ne1]];thenecho"text:{text}"
echo "readyCount: readyCount"testRunner{expected} instances of 'Ready' but found ${readyCount}"
return 1
fi
}
function _testRunUtil_correctReady() {
local prefix="1"if[["{PRETEND}" -eq 1 ]]; then
echo "Pretend: Would have checked all nodes with prefix prefixarereadywith{NODETOTAL} nodes"
_testRunner_Results "FUNCNAME[1]""Passed"return0fires=(_remoteExec "{prefix}-kmaster" "kubectl get nodes")
#shellcheck disable=2310 #- this is a test function
if _testRunUtil_expect_grep_count "{res}" "NODETOTAL";thentestRunner{FUNCNAME[1]}" "Passed"
return 0
else
_testRunner_Results "FUNCNAME[1]""failedtogetr{prefix}" "${res}"
return 1
fi
}
@fn _testRunUtil_getNodeMemory()
@brief Retrieves the memory allocated to a node in GB.
@param node The node identifier.
@details Fetches the node configuration using incus config show command, parses the memory line, extracts the memory value in MiB, and converts it to GB.
@return Echoes the memory allocated to the node in GB.
function _testRunUtil_getNodeMemory() {
local node
local config
local memline
local memCountwq
local memValue
local memCount
node="1"config=(incus config show "node")memline=(echo "config"∣grepmemory)memCountwq=(echo "{memline}" | awk '{print 2}')
memValue={memCountwq%MiB}
memCount=(echo "memValue/1024"∣bc)echo"{memCount}"
}
@fn _testRunUtil_getNodeCPU()
@brief Retrieves the number of CPUs allocated to a node.
@param node The node identifier.
@details Fetches the node configuration using incus config show command, parses the CPU line, and extracts the CPU count.
@return Echoes the number of CPUs allocated to the node.
function _testRunUtil_getNodeCPU() {
# Check nbr of arguments received
if [[ "#" -ne 1 ]]; then
echo "_testRunUtil_getNodeCPU: wrong number of arguments #, expected 1"
return 1
fi
local node
local config
local cpuline
local cpuCount
node="1"config=(incus config show "node")cpuline=(echo "config"∣grepcpu)cpuCount=(echo "{cpuline}" | awk '{print 2}')
cpuCount="{cpuCount//\"/}" # Remove double quotes
echo "{cpuCount}"
}
@param prefix The prefix to use for the node list.
@details Calls the _buildNodeList function with the provided prefix and compares the output to the expected node list.
If the output matches the expected list, the test is marked as "Passed"; otherwise, it is marked as "Failed".
@return Returns 0 if the test passes, indicating success; otherwise, returns 1.
function test_util_buildNodeList() {
local prefix="{1}"
NODETOTAL=6
local nodeList
#shellcheck disable=SC2312 #prefer to error if _buildNodeList fails
mapfile -t nodeList < <(_buildNodeList "{prefix}")
if [[ "nodeList[∗]"=="{prefix}-kmaster prefix−kworker1{prefix}-kworker2 prefix−kworker3{prefix}-kworker4 prefix−kworker5"]];then{FUNCNAME[0]}" "Passed"
return 0
else
_testRunner_Results "${FUNCNAME[0]}" "Failed"
return 1
fi
}
@fn test_util_cleanup()
@brief Cleans up resources after tests.
@details This function cleans up the k8s cluster resources based on the provided prefix. It calls the _testRunUtil_cleanup utility function to perform the cleanup. If the cleanup is successful, it logs the result as "Passed"; otherwise, it logs "Failed".
@param prefix The prefix used for naming the k8s cluster resources to be cleaned up.
@return Returns 0 if the cleanup is successful, indicating success; otherwise, returns 1.
function test_util_cleanup() {
prefix="{1}"
#shellcheck disable=SC2310 #this is a test function
if _testRunUtil_cleanup "{prefix}"; then
_testRunner_Results "FUNCNAME[0]""Passed"return0elsetestRunnerResults"{FUNCNAME[0]}" "Failed"
return 1
fi
}
@fn test_util_expectedGrepNotReady()
@brief Verifies that not all nodes are in the Ready state.
@details This function checks if the k8s cluster nodes are not all in the Ready state by simulating the output of a k8s cluster status command. It uses the _testRunUtil_expect_grep_count utility function to count the occurrences of "Ready" statuses. If the count matches the expected number, indicating that not all nodes are Ready, the test is marked as failed, as it expects at least one node to not be in the Ready state.
@return Returns 0 if the test passes (at least one node is not Ready), indicating success; otherwise, returns 1.
function test_util_expectedGrepNotReady() {
local isNotReady="NAME STATUS ROLES AGE VERSION
tr-tra-kmaster Ready control-plane 2m7s v1.29.4
tr-tra-kworker1 Ready 61s v1.29.4
tr-tra-kworker2 NotReady 1s v1.29.4"
# shellcheck disable=2310 #- this is a test function
if _testRunUtil_expect_grep_count "isNotReady"3;thentestRunnerResults"{FUNCNAME[0]}" "Failed" "Should not count NotReady as Ready"
return 1
else
_testRunner_Results "${FUNCNAME[0]}" "Passed"
fi
}
@fn test_util_expectedGrepReady()
@brief Verifies all nodes are in the Ready state.
@details This function simulates checking the status of k8s cluster nodes to ensure they are all in the "Ready" state. It uses a predefined string that represents the output of a hypothetical k8s cluster status command and checks if the count of "Ready" statuses matches the expected number. If the count is correct, the test passes; otherwise, it fails.
@return Returns 0 if all nodes are in the Ready state, indicating success; otherwise, returns 1.
function test_util_expectedGrepReady() {
local isReady="NAME STATUS ROLES AGE VERSION
tr-tra-kmaster Ready control-plane 2m7s v1.29.4
tr-tra-kworker1 Ready 61s v1.29.4
tr-tra-kworker2 Ready 1s v1.29.4"
# shellcheck disable=2310 #- this is a test function
if _testRunUtil_expect_grep_count "isReady"3;thentestRunnerResults"{FUNCNAME[0]}" "Passed"
else
_testRunner_Results "${FUNCNAME[0]}" "Failed"
return 1
fi
}
@fn xtest_util_forceFail()
@brief Forces a test failure.
@details This function is designed to always fail. It is useful for testing the test framework itself or ensuring that failure handling works as expected. It explicitly logs the test as "Failed" and returns a non-zero exit code.
@return Always returns 1, indicating failure.
function xtest_util_forceFail() {
_testRunner_Results "${FUNCNAME[0]}" "Failed" "This test is expected to fail"
return 1
}
@fn test_util_forcePass()
@brief Forces a test pass.
@details This function is designed to always pass. It is useful for testing the test framework itself or ensuring that success handling works as expected. It explicitly logs the test as "Passed" and returns a zero exit code.
@return Always returns 0, indicating success.
function test_util_forcePass() {
_testRunner_Results "${FUNCNAME[0]}" "Passed"
return 0
}
@fn test_util_validateWordInList()
@brief Validates if a word is in a list using the function _validateWordInList.
@details This function tests if a word is in a list by comparing the word to the list. If the word is found in the list, the test passes; otherwise, it fails.
@param prefix The prefix used for naming the k8s cluster resources and is always required for a test_ function
@return Returns 0 if the word is found in the list, indicating success; otherwise, returns 1.
function test_util_validateSingleWordInList() {
local prefix="{1}"
#shellcheck disable=SC2310 #this is a test function
if _isWordsInList "1.27" SUPPORTEDK8s "Invalid version"; then
_testRunner_Results "{FUNCNAME[0]}" "Passed"
return 0
else
_testRunner_Results "${FUNCNAME[0]}" "Failed"
return 1
fi
}
@fn test_util_validateMultipleWordsInList()
@brief Validates if multiple words are in a csv list using the function _validateWordInList.
@details This function tests if a word is in a list by comparing the word to the list. If the word is found in the list, the test passes; otherwise, it fails.
@param prefix The prefix used for naming the k8s cluster resources and is always required for a test_ function
@return Returns 0 if the word is found in the list, indicating success; otherwise, returns 1.
function test_util_validateMultipleWordsInList() {
local prefix="{1}"
#shellcheck disable=SC2310 #this is a test function
if _isWordsInList "1.27,1.28" SUPPORTEDK8s "Invalid version xxx"; then
_testRunner_Results "{FUNCNAME[0]}" "Passed"
return 0
else
_testRunner_Results "${FUNCNAME[0]}" "Failed"
return 1
fi
}
@brief Tests launching an instance in a non Incus cluster environment.
@details This function tests launching an instance without a target, which simulates a scenario where no incus cluster is available. It calls the launchInstance function with an empty target and checks if the IP address is empty. If the IP address is empty, the test passes; otherwise, it fails.
@param prefix The prefix used for naming the k8s cluster resources.
@return Returns 0 if the IP address is empty, indicating success; otherwise, returns 1.
function test_incus_launchNoTarget() {
local prefix="1"localinstanceName="{prefix}-kmaster"
local instanceType="vm"
local os="ubuntu"
local osVersion="22.04"
local cpu="2"
local memory="2"
local target=""
local project="default"
LASTIP=""
launchInstance "instanceName""{instanceType}" "os""{osVersion}" "cpu""{memory}" "target""{project}"
if [[ "LASTIP"==""]];thentestRu{FUNCNAME[0]}" "Failed" "No IP address returned"
return 1
else
_testRunner_Results "${FUNCNAME[0]}" "Passed"
return 0
fi
}
@fn diagnose_failing_nodes()
@brief Try to get diagnostics for nodes that are not ready.
@details This function runs remotely kubectl describe node & kubectl logs. Runs locally incus info on the node.
@param prefix The prefix used for naming the k8s cluster resources.
@param res The results of the failing kubectl get nodes that triggers this action.
@return Returns 0 (success) by default (even if _remoteExec or incus fails). Returns with 1 if wrong number arguments provided.
function diagnose_failing_nodes() {
# Check nbr of arguments received
if [[ "#" -ne 2 ]]; then
echo "diagnose_failing_nodes: wrong number of arguments"
return 1
fi
local prefix
prefix="{1}"
local res
res="2"failing=(echo "res"∣grep−v"Ready")fornodein{failing}; do
#shellcheck disable=2310 #- this is a test function
_remoteExec "prefix−kmaster""kubectldescribenode{node}" || true
#shellcheck disable=2310 #- this is a test function
_remoteExec "prefix−kmaster""kubectllogs{node}" || true
incus info "${node}" || true
done
return 0
}
@brief Tests the k8s cluster setup with Calico as the CNI provider.
@details This function sets the CNI provider to Calico, deploys a k8s cluster, and checks if all nodes are in the "Ready" state.
@param prefix The prefix used for naming the k8s cluster resources.
@return Returns 0 if all nodes are in the "Ready" state, indicating success; otherwise, returns 1.
function test_k8s_calico() {
prefix="1"CNIPROVIDER="calico"programmain"{prefix}"
_testRunUtil_correctReady "${prefix}"
}
@fn test_k8s_creationWithDns()
@brief Tests the creation of the environment with DNS enabled.
@param prefix The prefix used for naming the nodes in the k8s cluster.
@details This function initiates the environment setup with DNS capabilities and checks if the nodes are in the 'Ready' state.
It relies on the main function to set up the environment and _remoteExec to execute commands on the remote node.
@note This function assumes a working DNS environment where nodes can resolve each other.
@return Exits with 1 on failure if EXITONERROR is set; otherwise, returns 0 on success and 1 for failure.
function test_k8s_creationWithDns() {
local prefix="1"localresCPU=2MEMORY=4programmain"{prefix}"
_testRunUtil_correctReady "${prefix}"
}
@fn test_k8s_creationWithoutDns()
@brief Tests the creation of the environment without DNS.
@param prefix The prefix used for naming the nodes in the k8s cluster.
@details This function initiates the environment setup without DNS capabilities and checks if the nodes are in the 'Ready' state.
It sets FORCENODNS to 1 to simulate an environment without DNS and relies on the main function for setup.
@note This function simulates a non-DNS environment and may give a false positive if DNS is actually required for operation.
@return Exits with 1 on failure if EXITONERROR is set; otherwise, returns 0 on success and 1 for failure.
function test_k8s_creationWithoutDns() {
local prefix="1"FORCENODNS=1localresprogrammain"{prefix}"
_testRunUtil_correctReady "${prefix}"
}
@fn test_k8s_defaults()
@brief Tests the default k8s cluster setup.
@details This function tests the default setup by deploying a k8s cluster and checking if all nodes are in the "Ready" state.
@param prefix The prefix used for naming the k8s cluster resources.
@return Returns 0 if all nodes are in the "Ready" state, indicating success; otherwise, returns 1.
function test_k8s_defaults() {
local prefix="1"localresprogrammain"{prefix}"
_testRunUtil_correctReady "${prefix}"
}
@fn test_incus_ram_cpu_variants()
@brief Tests RAM and CPU variants for instances when the comma separated variable format.
@param prefix The prefix used for naming the instances resources.
@details This function tests the RAM and CPU variants for instances by deploying a k8s cluster with different CPU and RAM values.
It compares the expected values with the actual values for each node and logs the test as "Passed" if the values match; otherwise, it logs the failure and returns 1.
@return Returns 0 if the test passes (all nodes have the expected CPU and RAM values), otherwise returns 1.
function test_incus_ram_cpu_variants() {
local prefix="1"localCPUMEMORYcpumemnodenodeRaynodeCPUnodeMemoryresCPU="2,4,6"MEMORY="2,4,6"launchClusterInstances"{prefix}"
#shellcheck disable=SC2312 #prefer to error if _buildNodeList fails
IFS=' ' read -ra nodeRay <<<"(buildNodeList"{prefix}")"
# Convert CSV strings to arrays
IFS=',' read -r -a CPU_ARRAY <<<"CPU"IFS=′,′rea{MEMORY}"
# Loop over the indices of the arrays
for i in "!CPUARRAY[@]";docpu="{CPU_ARRAY[{i}]}"
mem="{MEMORY_ARRAY[{i}]}"
node="{nodeRay[{i}]}"
# echo "Checking node node with CPU cpuandMEMORYmem"
# in pretend mode, we don't actually check the values
if [[ "PRETEND"−eq1]];thennodeCPU="{cpu}"
nodeMemory="mem"elsenodeCPU=(_testRunUtil_getNodeCPU "node")nodeMemory=(_testRunUtil_getNodeMemory "node")fiif[["{cpu}" -eq "{nodeCPU}" && "{mem}" -eq "nodeMemory"]];then:else{FUNCNAME[0]}" "Node nodehasincorrectCPU{nodeCPU} (cpuexpected)orMEMORY{nodeMemory} (memexpected)values"return1fidon{FUNCNAME[0]}" "Passed"
}
@fn test_k8s_version()
@brief Tests if the Kubernetes cluster is set up with the specified version.
@param prefix The prefix used for naming the k8s cluster resources.
@param version The Kubernetes version to test for.
@details This function sets the Kubernetes version if specified, initiates the k8s cluster setup using the 'main' function, and then checks if all nodes are in the "Ready" state.
It uses '_remoteExec' to execute 'kubectl get nodes' on the master node and '_testRunUtil_expect_grep_count' to verify that all nodes are ready.
If the nodes are ready, it logs the test as passed; otherwise, it logs the failure and returns 1.
@return Returns 0 if the test passes (all nodes are ready), otherwise returns 1.
function test_k8s_version() {
local prefix="1"localversion="{2:-}"
if [[ "version"!=""]];thenK8SVERSION="{version}"
fi
program_main "prefix"testRunUtilc{prefix}"
}
@fn test_k8s_versions()
@brief Tests k8s cluster setup with different Kubernetes versions.
@param prefix The prefix used for naming the k8s cluster resources.
@details Iterates over a list of supported Kubernetes versions, sets each version, and initiates the k8s cluster setup.
For each version, it checks if all nodes are in the "Ready" state by executing 'kubectl get nodes' on the master node.
Utilizes '_testRunUtil_expect_grep_count' to verify the count of "Ready" nodes matches the expected count.
If a version passes the test, it logs the result as "Passed"; otherwise, it logs the failure and returns 1.
@note This function returns after the first failure without testing remaining versions.
@return Returns 0 if all nodes are ready for a specific version, otherwise returns 1 on the first failure.
function test_k8s_versions() {
local prefix="1"forversionin"{SUPPORTEDK8s[@]}"; do
K8SVERSION="version"programmain"{prefix}"
_testRunUtil_correctReady "${prefix}"
done
}
@fn testSuite()
Executes a suite of tests based on the provided test name and prefix.
This function dynamically identifies and runs test functions within the script.
It supports running a specific test, multiple tests, or all tests, excluding those
that are intentionally masked out (e.g., due to external resource constraints).
@param testname The name of the test suite to run.
Use "all" to run all available tests.
Multiple test names can be specified, separated by commas.
@param prefix The prefix to use for the test run.
This prefix is used for identifying or grouping test runs, potentially for isolation or reporting.
@param additionalArgs Additional arguments to be passed to the test functions.
@details The function first checks for the presence of mandatory parameters (test name and prefix).
It then collects all defined functions in the script and filters out those that are test functions
(i.e., functions whose names start with "test_"). Depending on the test name parameter, it either runs
all test functions (excluding any specifically masked out tests) or runs only the specified test(s).
Each test function is called with the prefix and any additional arguments. If any test function returns
a non-zero exit code, the overall return code is set to 8. After running the tests, a test report is displayed,
and the script exits with the determined return code.
@note The function uses global variables for managing test execution state and assumes the existence of helper functions
like _banner, _subbanner, _testRunUtil_cleanup, and _testRunner_displayTestReport for logging and cleanup tasks.
@return Exits with 0 if all tests pass, or 8 if any test fails.
function testSuite() {
local testname="1"localprefix="TR−{2}"
local foundTest=0
local allFunctions
local returnCodes=0
shift 2
if [[ "testname"==""]];thendisplayHelp"Notestsuitespecified""<testname><{prefix}" == "" ]]; then
displayHelp "No prefix specified" ""
fi
_banner "Test Suite"
allFunctions="$(declare -F)"
testfunctions=()
code
# Loop through all function names in allFunctions
for funcname in ${allFunctions}; do
# Check if the function name starts with 'test_'
if [[ "${funcname}" == test_* ]]; then
# Add the function name to the testfunctions array
testfunctions+=("${funcname}")
fi
done
# Function to run tests based on pattern
run_tests() {
local pattern="$1"
# Convert wildcard pattern to a regular expression
local regex_pattern pattern # Example pattern
regex_pattern="${pattern//\*/.*}" # Replace '*' with '.*' using native Bash string manipulation
_banner "Running Test Suites matching pattern: ${pattern}"
for test in "${testfunctions[@]}"; do
# Mask out the test_versions test because it currently uses too many docker hub pulls
if [[ "${test}" != "test_k8s_versions" && "${test}" =~ ${regex_pattern} ]]; then
_subbanner "Running ${test} with prefix ${prefix}"
if ! ${test} "${prefix}"; then
returnCodes="${EC_TEST_FAILED_IN_LIST}"
fi
_testRunUtil_cleanup "${prefix}"
_subbanner "Completed ${test} with prefix ${prefix}"
fi
done
}
matchedPattern=""
for key in "${!TESTMACROS[@]}"; do
pattern="${TESTMACROS[${key}]}"
if [[ "${testname}" == "${key}" ]]; then
matchedPattern=${pattern}
break
fi
done
if [[ -n "${matchedPattern}" ]]; then
run_tests "${matchedPattern}"
else
IFS=',' read -r -a testNameArray <<<"${testname}"
for testNameEntry in "${testNameArray[@]}"; do
foundTest=0
for funcname in "${testfunctions[@]}"; do
# Check if the function name is the same as the current testNameEntry
if [[ "${funcname}" == "${testNameEntry}" ]]; then
foundTest=1
_subbanner "Running ${testNameEntry} with prefix ${prefix} and args $*"
if ! ${testNameEntry} "${prefix}" "$@"; then
returnCodes="${EC_TEST_FAILED_IN_LIST}"
fi
_testRunUtil_cleanup "${prefix}"
_subbanner "Completed ${testNameEntry} with prefix ${prefix}"
break # This break exits the for loop for testfunctions
fi
done
if [[ "${foundTest}" -eq 0 ]]; then
displayHelp "Invalid test suite ${testNameEntry}"
fi
done
fi
_banner "Test Suite Report"
_testRunner_displayTestReport
return "${returnCodes}"
function displayHelp() {
local error="1:−"localerrorArg="{2:-}"
code
echo -e "# ----------------------------------------------------------------------------------------------------- #"
echo -e "# _ #"
echo -e "# __ ___ __ __ _ _ __ __ _| | ___ _ __ #"
echo -e "# \ \ /\ / / '__/ _' | '_ \ / _' | |/ _ \ '__| #"
echo -e "# \ V V /| | | (_| | | | | (_| | | __/ | #"
echo -e "# \_/\_/ |_| \__,_|_| |_|\__, |_|\___|_| #"
echo -e "# |___/ #"
echo -e "# incus environments #"
echo -e "# ----------------------------------------------------------------------------------------------------- #"
if [[ "${error}" != "" ]]; then
echo -en "${BOLD}${RED}Error:\t${NORM} ${BRIGHTRED}${error}${NORM}\n\n"
fi
if [[ "${errorArg}" != "" ]]; then
echo -en "Usage:\t${CLICALL}${BRIGHTRED} ${errorArg}${NORM}\n"
else
echo -e "Usage:\t$0 [options] <prefix>"
fi
echo ""
echo "This script creates K8s clusters using instances (both vm and containers) managed in incus."
echo ""
echo "Options:"
echo " --cpu <cpu>: CPU count for the nodes. Default: ${DEFAULT_CPU}."
echo " --example: --cpu 2 assigns 2 cpus to all nodes. (-kmaster and -kworker(n))"
echo " -- multiple cpu counts provided via comma separated list. (must align with node count from --num-nodes)"
echo " --example: --cpu 2,4,6 assigns 2 -kmaster and 4 -kworker1 and 6 -kworker2."
echo " --containers: use containers instead of the default ${DEFAULT_INSTANCETYPE}"
echo -n " --deployment-type <deploymentType>: One of "
_helplist "${DEPLOYMENTTYPES[@]}"
echo ". Default: \"${DEFAULT_DEPLOYMENTTYPE}\""
echo " --debug-level <Debug_level>: set the debug level. Default: ${DEBUG}"
echo " -- 0: No debug output"
echo " -- 1: Minimal debug output with stack trace"
echo " -- 2: additional stack trace info"
echo " -- 3: developer based, printing of each line number"
echo " -- 4: shopt -x tracing"
echo " --exit-on-error: Exit on error during test module. Essentially disables auto-cleanup."
echo " --help: Display this help message."
echo " --incus-project <incus project>: Default: ${DEFAULT_PROJECT}"
echo " --memory <memory>: Memory for the nodes in GB. Default: ${DEFAULT_MEMORY}"
echo " --example: --memory 4 assigns 4GB of memory to all nodes (-kmaster and -kworker(n))."
echo " -- multiple memory specs provided via comma separated list. Must align with node count from --num-nodes."
echo " --example: --memory 2,4,4 assigns 2GB to -kmaster and 4GB to -kworker1 and -kworker2."
echo " --num-nodes <number_Nodes>: Number of nodes to create for the k8s cluster. Default: ${DEFAULT_NODETOTAL}"
vcniText=$(_helplist "${CNI_PROVIDERS[@]}")
echo " --network-provider <network_provider>: The network provider. Options are ${vcniText}. Default: ${DEFAULT_CNI_PROVIDER}"
echo " --os <OS>: Operating system for the instances. Default: ${DEFAULT_OS}. (Ubuntu is the only supported OS as of version 0.2)"
echo " --os-version <OS version>: Operating system version for the instances. Default: ${DEFAULT_OSVERSION}"
echo " --output-dir <output_dir>: Directory to store the recovery script. Default: ${DEFAULT_OUTPUT_DIR}"
echo " --pod-cidr-network <pod-cidr-network>: Default: ${DEFAULT_POD_NETWORK_CIDR}"
echo " --pretend: Run the script in pretend mode. No changes will be made."
echo " --tests <tests>: Run a specific test, multiple tests or macro defined tests."
echo " --example: --tests all will run all tests that start with test_"
echo " --example: --tests k8s will run all tests that start with test_k8s_"
echo " --example: --tests util will run all tests that start with test_util_"
echo " --example: --tests test_util_cleanup will run the test_util_cleanup test"
echo " --example: --tests test_util_cleanup,test_util_forcePass will run those two tests"
echo " --target-hosts <target incus host(s)>: to deploy to."
if [[ ${DEFAULT_TARGET} != "" ]]; then
vtargetText=$(_helplist "${VALIDTARGETS[@]}")
local countValidTargets=${#VALIDTARGETS[@]}
echo " -- Availability: ${vtargetText}. Default: ${DEFAULT_TARGET}"
echo " --example: --target-hosts ${VALIDTARGETS[((countValidTargets - 1))]} will deploy all nodes (-kmaster, kworker1, kworker2) to the ${VALIDTARGETS[((countValidTargets - 1))]} host."
echo " -- multiple hosts provided via comma separated list. (alignment with --num-nodes flag required)"
if [[ countValidTargets -ge 3 ]]; then
echo " --example: --target-hosts ${VALIDTARGETS[((countValidTargets - 3))]},${VALIDTARGETS[((countValidTargets - 2))]},${VALIDTARGETS[((countValidTargets - 1))]} will -kmaster to ${VALIDTARGETS[((countValidTargets - 3))]}, -kworker1 to ${VALIDTARGETS[((countValidTargets - 2))]} and -kworker2 to ${VALIDTARGETS[((countValidTargets - 1))]}."
elif [[ countValidTargets -eq 2 ]]; then
echo " --example: --target-hosts ${VALIDTARGETS[((countValidTargets - 2))]},${VALIDTARGETS[((countValidTargets - 1))]},${VALIDTARGETS[((countValidTargets - 1))]} will deploy -kmaster to ${VALIDTARGETS[((countValidTargets - 2))]} and -kworker(n) to ${VALIDTARGETS[((countValidTargets - 1))]}"
else
echo " --example: --target-hosts ${VALIDTARGETS[((countValidTargets - 1))]} will deploy all nodes to the ${VALIDTARGETS[((countValidTargets - 1))]}."
fi
echo " -- Not available on this host. Not a Incus cluster member."
fi
echo " --k8s-version <K8s version>: Sets the version of Kubernetes to run. Currently defaults to: ${DEFAULT_K8SVERSION}."
echo " Supported versions are:"
for version in "${SUPPORTEDK8sSversions[@]}"; do
echo " -- ${version}"
done
echo " --version: display the version of this script."
echo "Arguments:"
echo " <K8s_cluster_prefix>: a prefix for each node in the k8s cluster. All nodes base name will be -kmaster or -kworker(n)."
echo -e "Example 1:"
echo "Creates a 4-node Kubernetes cluster with VMs (2 CPUs, 4GB RAM each). Master on H91 (Tcluster-kmaster), workers on H92-H94 (Tcluster-kworker1-3). Generates a recovery script at ${HOME}/K8sWrangler/Tcluster-recover.sh."
echo " -- command: $0 --cpu 2 --memory 4 --num-nodes 4 --target-hosts H91,H92,H93,H94 Tcluster"
echo ""
echo "Error Codes:"
for code in "${ERRORCODES[@]}"; do
msg_index=${code}
echo " -- ${!code} ${code}: ${ERRORCODE_MSG[${msg_index}]}"
done
echo ""
if [[ "${error}" == "" ]]; then
exit 0
fi
_handle_error "${EC_CLI_ERROR}" "${error}"
}
function processArgs() {
OPTIONS=(getopt−oh−−longcpu:,containers,deployment−type:@")
#shellcheck disable=2181 #we want to use this compare in brackets
if [[ ? -ne 0 ]]; then
echo "Failed parsing options." >&2
_handle_error "{EC_CLI_BAD_OPTION_PARSE}" "Failed parsing options."
fi
#echo "OPTIONS=OPTIONS"evalset−−"{OPTIONS}"
while true; do
case "1"in−−containers)DEF2"
shift 2
;;
--debug-level)
DEBUG="2"debugModeEnableshift2;;−−2" DEPLOYMENTTYPES "Invalid deployment type 2"DEFAULTDEPLOYMENTTYPE=2"
shift 2
;;
--exit-on-error)
EXITONERROR=1
shift
;;
-h | --help)
displayHelp
;;
--incus-project)
DEFAULT_PROJECT="2"plist="(incus DEFAULT_project list -f csv -c "n")"
echo "plist"∣grep−q"{DEFAULT_PROJECT}"
if ! echo "plist"∣grep−q"{DEFAULT_PROJECT}"; then
displayHelp "Project DEFAULTP2" SUPPORTEDK8sSversions "Invalid Kubernetes version 2"DEFAULTK8SVERSION="2"
shift 2
;;
--memory)
DEFAULT_MEMORY="2"shift2;;−−network−provid2" CNI_PROVIDERS "Invalid CNI provider 2"DEFAULTCNIPROVID2"
shift 2
;;
--num-nodes)
DEFAULT_NODETOTAL="2"if[["{DEFAULT_NODETOTAL}" -lt 3 ]]; then
displayHelp "You must specify at least 3 nodes (--num-nodes 3)"
fi
shift 2
;;
--os)
DEFAULT_OS="2"shift2;;−−os−version)DEFAUL2"
shift 2
;;
--output-dir)
DEFAULT_OUTPUT_DIR="2"shift2;;−−pr2"
shift 2
;;
--target-hosts)
if [[ "2" VALIDTARGETS "Invalid target-hosts 2"DEFAULTTARGET="2"
fi
shift 2
;;
--tests)
TESTTORUN="2"shift2;;−−version)echo"Version:{SCRIPTVERSION}"
exit 0
;;
--)
shift
break
;;
*)
echo "Invalid Option: 1" 1>&2
exit 1
;;
esac
done
if [[ # -ne 1 ]]; then
displayHelp "Prefix not specified" ""
fi
MYPREFIX="1"shiftresetDefaultsif[["{MYPREFIX}" == "" ]]; then
displayHelp "Prefix not specified" ""
fi
if [[ "TESTTORUN"!=""]];then{MYPREFIX}"
testSuite "TESTTORUN""{MYPREFIX}" "@"rc=?
_destroyTmpDir
_handle_error "rc""Tests:{TESTTORUN} on MYPREFIX"fimakeTmpDirpr{MYPREFIX}"
_destroyTmpDir
}
function prereqs() {
#make sure incus is installed
local contflag=0
local capture
if ! command -v incus &>/dev/null; then
capture+="(testRunnerfailure"incusisinstalled")\n"contflag="{EC_CLI_PRECHECK_FAILED}"
else
capture+="(_testRunner_success "incus is installed")\n"
fi
# test if the current user is a member of incus group
if ! groups | grep -qE "\bincus\b"; then
capture+="(_testRunner_failure "User is a member of the incus group")\n"
contflag="ECCLIPRECHE(_testRunner_success "User is a member of the incus group")\n"
fi
# test if the current user is a member of incus-admin
if ! groups | grep -q "\bincus-admin\b"; then
capture+="(testRunner{EC_CLI_PRECHECK_FAILED}"
else
capture+="(testRunner{contflag}" -ne 0 ]]; then
echo -e "capture"fireturn"{contflag}"
}
export DEBIAN_FRONTEND=noninteractive
#shellcheck disable=SC2124
CLICALL="0@"
#shellcheck disable=SC2310 #this is a test function
if prereqs; then
processArgs "@"elsehandleerror"{EC_CLI_PRECHECK_FAILED}"
fi
Here's how the provided bash script for setting up a k8s cluster can be converted into a Go (Golang) program.
Since the original script is quite extensive, I'll focus on translating the debugModeEnable function and setting up some parts of the script. Then I'll provide the structure for the rest of the script.
go
// main.go
package main
import (
"flag"
"fmt"
"log"
"os"
"os/exec"
"regexp"
"strings"
)
var debug int
var pretend int
var deploymentType string
var cpu int
var memory int
var nodeTotal int
var instanceType string
var osType string
var osVersion string
var project string
var k8sVersion string
var cniProvider string
var podNetworkCidr string
const scriptVersion = "1.0.0"
func init() {
flag.IntVar(&cpu, "cpu", 2, "CPU count for the nodes.")
flag.IntVar(&memory, "memory", 2, "Memory for the nodes in GB.")
flag.IntVar(&nodeTotal, "num-nodes", 3, "Number of nodes to create for the k8s cluster.")
flag.StringVar(&deploymentType, "deployment-type", "k8svanilla", "Deployment type for the k8s cluster.")
flag.StringVar(&instanceType, "instance-type", "vm", "Instance type, either 'vm' or 'container'.")
flag.StringVar(&osType, "os", "ubuntu", "Operating system for the instances.")
flag.StringVar(&osVersion, "os-version", "22.04", "Operating system version for the instances.")
flag.StringVar(&project, "incus-project", "default", "Incus project.")
flag.StringVar(&k8sVersion, "k8s-version", "1.29.4-2.1", "Kubernetes version.")
flag.StringVar(&cniProvider, "network-provider", "cilium", "CNI provider.")
flag.StringVar(&podNetworkCidr, "pod-cidr-network", "192.168.0.0/16", "Pod CIDR network.")
flag.IntVar(&debug, "debug-level", 0, "Debug level")
flag.IntVar(&pretend, "pretend", 0, "Run in pretend mode.")
flag.Parse()
}
func debugModeEnable() {
fmt.Printf("Debug level set to: %d\n", debug)
if debug > 0 {
// Extend debugging features as needed
if debug > 3 {
// For extdebug behavior
}
}
}
// Utility functions
func execCommand(cmdName string, cmdArgs ...string) (string, error) {
if pretend == 1 {
fmt.Printf("Pretend: %s %s\n", cmdName, strings.Join(cmdArgs, " "))
return "", nil
}
cmd := exec.Command(cmdName, cmdArgs...)
output, err := cmd.CombinedOutput()
if err != nil {
return "", fmt.Errorf("failed to execute %s %v: %w", cmdName, cmdArgs, err)
}
return string(output), nil
}
// Main logic functions
func launchClusterInstances(prefix string) {
// Logic to launch cluster instances
}
func main() {
debugModeEnable()
if len(flag.Args()) != 1 {
log.Fatalf("Usage: %s <prefix>", os.Args[0])
}
prefix := flag.Args()[0]
if err := prereqs(); err != nil {
log.Fatalf("Prerequisites failed: %v", err)
}
if pretend == 1 {
fmt.Println("Running in pretend mode. No actual changes will be made.")
}
// Launch the cluster instances
launchClusterInstances(prefix)
// Additional logic...
}
// Function to check prerequisites
func prereqs() error {
// Check if incus is installed
if _, err := exec.LookPath("incus"); err != nil {
return fmt.Errorf("incus is not installed")
}
// Check if the current user is a member of the incus group
groups, err := execCommand("groups")
if err != nil {
return err
}
if !regexp.MustCompile(`\bincus\b`).MatchString(groups) {
return fmt.Errorf("user is not a member of the incus group")
}
// Check if the current user is a member of the incus-admin group
if !regexp.MustCompile(`\bincus-admin\b`).MatchString(groups) {
return fmt.Errorf("user is not a member of the incus-admin group")
}
return nil
}
Explanation:
Command-line Arguments Parsing:
The flag package is used to parse command-line arguments.
Various command-line flags are set to capture parameters such as CPU, memory, and others.
Debug Mode:
debugModeEnable() simulates enabling debug based on the debug level provided.
Utility Functions:
execCommand(cmdName string, cmdArgs ...string) wraps around the system command execution.
If pretend is set, it simply prints the command instead of executing it.
Main Logic:
launchClusterInstances(prefix string) is a placeholder for the logic to launch cluster instances.
The main() function triggers the overall flow, including prerequisite checks.
Prerequisite Check:
prereqs() checks if necessary tools (like incus) are installed and verifies group memberships.
The rest of the functions (awaitInstanceIP, snapshotInstance, launchInstance, etc.) should be implemented in a similar manner by breaking them down into appropriate Go functions. Given the length and complexity of the original bash script, converting each function to Go will require a structured approach, ensuring each operation and its error handling is properly translated.