Skip to content

Commit

Permalink
scripts: Add scripts for deploying and testing clusters
Browse files Browse the repository at this point in the history
Signed-off-by: Din Music <[email protected]>
  • Loading branch information
MusicDin committed Apr 6, 2024
1 parent 5d14419 commit 51b2901
Show file tree
Hide file tree
Showing 3 changed files with 236 additions and 0 deletions.
82 changes: 82 additions & 0 deletions scripts/deploy-cluster.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
#!/bin/sh
set -eu

# Check input arguments.
if [ "${1:-}" = "" ] || [ "${2:-}" = "" ] || [ "${3:-}" = "" ] || [ "${4:-}" = "" ]; then
echo "Usage: ${0} <cluster_name> <distro> <network_plugin> <k8s_version>"
exit 1
fi

CLUSTER="${1}"
DISTRO="${2}"
NETWORK_PLUGIN="${3}"
K8S_VERSION="${4}"

echo "==> DEPLOY: Cluster ${DISTRO}/${NETWORK_PLUGIN}/${K8S_VERSION}"

# Create config.
cat <<-EOF > config.yaml
hosts:
- name: localhost
connection:
type: local
cluster:
name: ${CLUSTER}
network:
mode: nat
cidr: 192.168.113.0/24
nodeTemplate:
user: k8s
updateOnBoot: true
cpuMode: host-passthrough
ssh:
addToKnownHosts: true
os:
distro: ${DISTRO}
nodes:
loadBalancer:
default:
cpu: 1
ram: 1
mainDiskSize: 8
instances:
- id: 1
ip: 192.168.113.100
master:
default:
cpu: 2
ram: 2
mainDiskSize: 16
instances:
- id: 1
ip: 192.168.113.10
worker:
default:
cpu: 2
ram: 2
mainDiskSize: 16
instances:
- id: 1
ip: 192.168.113.20
- id: 2
ip: 192.168.113.21
kubernetes:
version: ${K8S_VERSION}
networkPlugin: ${NETWORK_PLUGIN}
EOF

echo "Config:"
echo "---"
cat config.yaml
echo "---"

# Apply config and export kubeconfig.
mkdir -p "${HOME}/.kube"
kubitect apply --config config.yaml
kubitect export kubeconfig --cluster "${CLUSTER}" > "${HOME}/.kube/config"

echo "==> DEBUG: Cluster info"
kubectl cluster-info
kubectl get nodes
64 changes: 64 additions & 0 deletions scripts/deploy-node.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
#!/bin/sh
set -eu

# Check input arguments.
if [ "${1:-}" = "" ] || [ "${2:-}" = "" ] || [ "${3:-}" = "" ] || [ "${4:-}" = "" ]; then
echo "Usage: ${0} <cluster_name> <distro> <network_plugin> <k8s_version>"
exit 1
fi

CLUSTER="${1}"
DISTRO="${2}"
NETWORK_PLUGIN="${3}"
K8S_VERSION="${4}"

echo "==> DEPLOY: Cluster (Single Node) ${DISTRO}/${NETWORK_PLUGIN}/${K8S_VERSION}"

# Create config.
cat <<-EOF > config.yaml
hosts:
- name: localhost
connection:
type: local
cluster:
name: ${CLUSTER}
network:
mode: nat
cidr: 192.168.113.0/24
nodeTemplate:
user: k8s
updateOnBoot: true
cpuMode: host-passthrough
ssh:
addToKnownHosts: true
os:
distro: ${DISTRO}
nodes:
master:
default:
cpu: 2
ram: 4
mainDiskSize: 32
instances:
- id: 1
ip: 192.168.113.10
kubernetes:
version: ${K8S_VERSION}
networkPlugin: ${NETWORK_PLUGIN}
EOF

echo "Config:"
echo "---"
cat config.yaml
echo "---"

# Apply config and export kubeconfig.
mkdir -p "${HOME}/.kube"
kubitect apply --config config.yaml
kubitect export kubeconfig --cluster "${CLUSTER}" > "${HOME}/.kube/config"

echo "==> DEBUG: Cluster info"
kubectl cluster-info
kubectl get nodes
90 changes: 90 additions & 0 deletions scripts/test-cluster.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
#!/bin/sh
set -eu

TIMEOUT=600 # seconds

defer() {
if [ "${FAIL}" = "1" ]; then
echo "==> DEBUG: Cluster events"
kubectl get events --all-namespaces

echo "==> FAIL"
exit 1
fi

echo "==> PASS"
exit 0
}

FAIL=1
trap defer EXIT HUP INT TERM

echo "==> TEST: Cluster readiness"

startTime=$(date +%s)
nodes=$(kubectl get nodes | awk 'NR>1 {print $1}')

for node in $nodes; do
while :; do
isReady=$(kubectl get node "${node}" \
-o jsonpath='{.status.conditions[?(@.type=="Ready")].status}'
)

if [ "${isReady}" = "True" ]; then
echo "===> PASS: Node ${node} is ready."
break
fi

currentTime=$(date +%s)
elapsedTime=$((currentTime - timeStart))

if [ "${elapsedTime}" -gt "${TIMEOUT}" ]; then
echo "FAIL: Node ${node} is NOT READY after ${TIMEOUT} seconds!"
kubectl get nodes
break
fi

sleep 10
done
done

echo "==> TEST: Running pods"

startTime=$(date +%s)

while :; do
failedPods=$(kubectl get pods \
--all-namespaces \
--field-selector="status.phase!=Succeeded,status.phase!=Running" \
--output custom-columns="NAMESPACE:metadata.namespace,POD:metadata.name,STATUS:status.phase"
)

if [ "$(echo "${failedPods}" | awk 'NR>1')" = "" ]; then
echo "===> PASS: All pods are running."
break
fi

currentTime=$(date +%s)
elapsedTime=$((currentTime - startTime))

if [ "${elapsedTime}" -gt "${TIMEOUT}" ]; then
echo "==> FAIL: Pods not running after ${TIMEOUT} seconds!"
echo "${failedPods}"
break
fi

sleep 10
done

echo "==> TEST: DNS"
kubectl run dns-test --image=busybox:1.28.4 --restart=Never -- sleep 180
kubectl wait --for=condition=Ready pod/dns-test --timeout=60s

kubectl exec dns-test -- nslookup kubernetes.default
echo "===> PASS: Local lookup (kubernetes.default)."

kubectl exec dns-test -- nslookup kubitect.io
echo "===> PASS: External lookup (kubitect.io)."

# All tests have passed.
FAIL=0

0 comments on commit 51b2901

Please sign in to comment.