Skip to content

Commit fecf1ff

Browse files
committed
feat: add helper script for standing up three test clusters
Utilize kind to stand up three test clusters for the purpose of validating the deployment of UnderStack. This does nothing past standing the three clusters up, deploying ArgoCD and configuring the other clusters to be accessed by ArgoCD. Lastly it includes a cleanup operation to tear everything back down.
1 parent 12ab25d commit fecf1ff

1 file changed

Lines changed: 166 additions & 0 deletions

File tree

scripts/e2e-test-setup.sh

Lines changed: 166 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,166 @@
1+
#!/bin/bash
2+
set -euo pipefail
3+
4+
SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
5+
#PROJECT_ROOT="$(cd "${SCRIPT_DIR}/.." && pwd)"
6+
7+
# Cluster names
8+
MGMT_CLUSTER="mgmt"
9+
GLOBAL_CLUSTER="global"
10+
SITE_CLUSTER="site"
11+
12+
cleanup() {
13+
echo "Cleaning up clusters..."
14+
kind delete cluster --name "${SITE_CLUSTER}" || true
15+
kind delete cluster --name "${GLOBAL_CLUSTER}" || true
16+
kind delete cluster --name "${MGMT_CLUSTER}" || true
17+
}
18+
19+
create_clusters() {
20+
echo "Creating management cluster..."
21+
kind create cluster --name "${MGMT_CLUSTER}"
22+
23+
echo "Creating global cluster..."
24+
kind create cluster --name "${GLOBAL_CLUSTER}"
25+
26+
echo "Creating site cluster..."
27+
kind create cluster --name "${SITE_CLUSTER}"
28+
}
29+
30+
install_argocd() {
31+
echo "Installing ArgoCD..."
32+
kubectl --context "kind-${MGMT_CLUSTER}" create namespace argocd
33+
kubectl --context "kind-${MGMT_CLUSTER}" apply -n argocd -f https://raw.githubusercontent.com/argoproj/argo-cd/stable/manifests/install.yaml
34+
35+
# Wait for ArgoCD to be ready
36+
kubectl --context "kind-${MGMT_CLUSTER}" wait --for=condition=available --timeout=300s deployment/argocd-server -n argocd
37+
}
38+
39+
setup_cluster_access() {
40+
echo "Setting up cluster access..."
41+
42+
# Register global cluster
43+
register_cluster "${GLOBAL_CLUSTER}" "global"
44+
45+
# Register site cluster
46+
register_cluster "${SITE_CLUSTER}" "site"
47+
48+
# Verify clusters are registered
49+
verify_clusters
50+
}
51+
52+
verify_clusters() {
53+
echo "Verifying cluster registration..."
54+
55+
local max_attempts=30
56+
local attempt=0
57+
58+
while [ $attempt -lt $max_attempts ]; do
59+
local registered_clusters
60+
registered_clusters=$(kubectl --context "kind-${MGMT_CLUSTER}" get secrets -n argocd -l argocd.argoproj.io/secret-type=cluster -o name | wc -l)
61+
62+
if [ "$registered_clusters" -ge 2 ]; then
63+
echo "✓ All clusters registered successfully"
64+
kubectl --context "kind-${MGMT_CLUSTER}" get secrets -n argocd -l argocd.argoproj.io/secret-type=cluster -o custom-columns=NAME:.metadata.name,CLUSTER:.stringData.name
65+
return 0
66+
fi
67+
68+
echo "Waiting for clusters to register... ($((attempt + 1))/$max_attempts)"
69+
sleep 2
70+
((attempt++))
71+
done
72+
73+
echo "✗ Cluster registration verification failed"
74+
return 1
75+
}
76+
77+
register_cluster() {
78+
local cluster_name="$1"
79+
local cluster_role="$2"
80+
echo "Registering ${cluster_name} cluster with ArgoCD..."
81+
82+
# Get cluster config
83+
TARGET_SERVER=$(kubectl --context "kind-${cluster_name}" config view --minify -o jsonpath='{.clusters[0].cluster.server}')
84+
TARGET_CA=$(kubectl --context "kind-${cluster_name}" config view --raw --minify --flatten -o jsonpath='{.clusters[0].cluster.certificate-authority-data}')
85+
86+
# Create service account in target cluster
87+
kubectl --context "kind-${cluster_name}" apply -f - <<EOF
88+
apiVersion: v1
89+
kind: ServiceAccount
90+
metadata:
91+
name: argocd-manager
92+
namespace: kube-system
93+
---
94+
apiVersion: rbac.authorization.k8s.io/v1
95+
kind: ClusterRoleBinding
96+
metadata:
97+
name: argocd-manager
98+
roleRef:
99+
apiGroup: rbac.authorization.k8s.io
100+
kind: ClusterRole
101+
name: cluster-admin
102+
subjects:
103+
- kind: ServiceAccount
104+
name: argocd-manager
105+
namespace: kube-system
106+
EOF
107+
108+
# Get token
109+
TOKEN=$(kubectl --context "kind-${cluster_name}" create token argocd-manager -n kube-system)
110+
111+
# Add cluster to ArgoCD
112+
kubectl --context "kind-${MGMT_CLUSTER}" apply -f - <<EOF
113+
apiVersion: v1
114+
kind: Secret
115+
metadata:
116+
name: ${cluster_name}-secret
117+
namespace: argocd
118+
labels:
119+
argocd.argoproj.io/secret-type: cluster
120+
annotations:
121+
understack.rackspace.com/env: test
122+
understack.rackspace.com/partition: test
123+
understack.racksapce.com/role: ${cluster_role}
124+
type: Opaque
125+
stringData:
126+
name: ${cluster_name}
127+
server: ${TARGET_SERVER}
128+
config: |
129+
{
130+
"bearerToken": "${TOKEN}",
131+
"tlsClientConfig": {
132+
"caData": "${TARGET_CA}"
133+
}
134+
}
135+
EOF
136+
}
137+
138+
main() {
139+
if [ $# -eq 0 ]; then
140+
echo "Usage: $0 [setup|cleanup]"
141+
echo ""
142+
echo "Commands:"
143+
echo " setup - Create clusters, install ArgoCD, and deploy UnderStack"
144+
echo " cleanup - Delete all test clusters"
145+
exit 1
146+
fi
147+
148+
case "${1}" in
149+
"cleanup")
150+
cleanup
151+
;;
152+
"setup")
153+
create_clusters
154+
install_argocd
155+
setup_cluster_access
156+
echo "Setup of three clusters is complete!"
157+
echo "ArgoCD UI: kubectl --context kind-mgmt port-forward svc/argocd-server -n argocd 8080:443"
158+
;;
159+
*)
160+
echo "Usage: $0 [setup|cleanup]"
161+
exit 1
162+
;;
163+
esac
164+
}
165+
166+
main "$@"

0 commit comments

Comments
 (0)