Skip to content

Commit df90329

Browse files
committed
enhancing KIND support
1 parent aeb7c1c commit df90329

File tree

1 file changed

+152
-25
lines changed

1 file changed

+152
-25
lines changed

install-scripts/kind.sh

Lines changed: 152 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ echo_error() {
6262
printf "%s [ERROR] %s\n" "$timestamp" "$message" >> "$LOGFILE"
6363
}
6464

65-
# Function to run helm commands with a timeout of 15 minutes in debug mode
65+
# Function to run helm install commands with a timeout of 15 minutes in debug mode
6666
helm_install_with_timeout() {
6767
helm install "$@" --debug --timeout=15m 2>&1 | tee -a "$DEBUG_LOGFILE" >> "$LOGFILE"
6868
}
@@ -78,24 +78,23 @@ check_command() {
7878
fi
7979
}
8080

81-
8281
# Function to ensure Helm repository is added and updated
8382
ensure_helm_repo() {
8483
REPO_NAME="opengovernance"
8584
REPO_URL="https://opengovern.github.io/charts/"
8685

8786
if ! helm repo list | grep -q "^$REPO_NAME "; then
88-
echo_info "Adding Helm repository '$REPO_NAME'."
89-
helm repo add "$REPO_NAME" "$REPO_URL" || { echo_error "Failed to add Helm repository '$REPO_NAME'."; exit 1; }
87+
# Suppress output from ensure_helm_repo
88+
helm repo add "$REPO_NAME" "$REPO_URL" >/dev/null 2>&1 || { echo_error "Failed to add Helm repository '$REPO_NAME'."; exit 1; }
9089
else
91-
echo_info "Helm repository '$REPO_NAME' already exists."
90+
# Suppress output from ensure_helm_repo
91+
:
9292
fi
9393

94-
echo_info "Updating Helm repositories."
95-
helm repo update || { echo_error "Failed to update Helm repositories."; exit 1; }
94+
# Suppress output from ensure_helm_repo
95+
helm repo update >/dev/null 2>&1 || { echo_error "Failed to update Helm repositories."; exit 1; }
9696
}
9797

98-
9998
# Function to check prerequisites
10099
check_prerequisites() {
101100
# List of required commands
@@ -108,13 +107,148 @@ check_prerequisites() {
108107
check_command "$cmd"
109108
done
110109

111-
112110
# Ensure Helm repository is added and updated
113111
ensure_helm_repo
114112

115113
echo_info "Checking Prerequisites...Completed"
116114
}
117115

116+
# Function to list existing Kind clusters
117+
list_kind_clusters() {
118+
kind get clusters
119+
}
120+
121+
# Function to check if a Kind cluster exists
122+
kind_cluster_exists() {
123+
kind get clusters | grep -qw "$1"
124+
}
125+
126+
# Function to create a Kind cluster
127+
create_kind_cluster() {
128+
cluster_name="$1"
129+
echo_info "Creating Kind Cluster $cluster_name"
130+
kind create cluster --name "$cluster_name" || { echo_error "Failed to create Kind cluster '$cluster_name'."; exit 1; }
131+
kubectl config use-context "kind-$cluster_name" || { echo_error "Failed to set kubectl context to 'kind-$cluster_name'."; exit 1; }
132+
}
133+
134+
# Function to prompt the user for selecting an existing cluster or creating a new one
135+
select_kind_cluster() {
136+
existing_clusters=$(list_kind_clusters)
137+
cluster_count=$(echo "$existing_clusters" | wc -l | tr -d ' ')
138+
139+
if [ "$cluster_count" -eq 0 ]; then
140+
# No existing clusters, create a new one
141+
echo_info "No existing Kind clusters found. Creating a new Kind cluster named '$NAMESPACE'."
142+
create_kind_cluster "$NAMESPACE"
143+
else
144+
# Existing clusters found, prompt user
145+
echo_prompt "Existing Kind clusters detected:"
146+
echo "$existing_clusters" > /dev/tty
147+
echo_prompt "Do you want to use an existing Kind cluster? (y/N): "
148+
149+
read user_input
150+
151+
case "$user_input" in
152+
[Yy]* )
153+
# Prompt user to select which cluster to use
154+
echo_prompt "Enter the name of the Kind cluster you want to use:"
155+
read selected_cluster
156+
157+
if kind_cluster_exists "$selected_cluster"; then
158+
echo_info "Using existing Kind cluster '$selected_cluster'."
159+
kubectl config use-context "kind-$selected_cluster" || { echo_error "Failed to set kubectl context to 'kind-$selected_cluster'."; exit 1; }
160+
else
161+
echo_error "Kind cluster '$selected_cluster' does not exist."
162+
exit 1
163+
fi
164+
;;
165+
* )
166+
# Create a new cluster
167+
echo_info "Creating a new Kind cluster named '$NAMESPACE'."
168+
create_kind_cluster "$NAMESPACE"
169+
;;
170+
esac
171+
fi
172+
}
173+
174+
# -----------------------------
175+
# Readiness Check Functions
176+
# -----------------------------
177+
178+
check_opengovernance_readiness() {
179+
# Check the readiness of all pods in the specified namespace
180+
not_ready_pods=$(kubectl get pods -n "$NAMESPACE" --no-headers | awk '{print $3}' | grep -v -E 'Running|Completed' || true)
181+
182+
if [ -z "$not_ready_pods" ]; then
183+
APP_HEALTHY="true"
184+
else
185+
echo_error "Some OpenGovernance pods are not healthy."
186+
kubectl get pods -n "$NAMESPACE" >> "$LOGFILE" 2>&1
187+
APP_HEALTHY="false"
188+
fi
189+
}
190+
191+
# Function to check pods and migrator jobs
192+
check_pods_and_jobs() {
193+
attempts=0
194+
max_attempts=24 # 24 attempts * 30 seconds = 12 minutes
195+
sleep_time=30
196+
197+
while [ "$attempts" -lt "$max_attempts" ]; do
198+
check_opengovernance_readiness
199+
if [ "${APP_HEALTHY:-false}" = "true" ]; then
200+
return 0
201+
fi
202+
attempts=$((attempts + 1))
203+
echo_info "Waiting for pods to become ready... ($attempts/$max_attempts)"
204+
sleep "$sleep_time"
205+
done
206+
207+
echo_error "OpenGovernance did not become ready within expected time."
208+
exit 1
209+
}
210+
211+
# -----------------------------
212+
# Port-Forwarding Function
213+
# -----------------------------
214+
215+
basic_install_with_port_forwarding() {
216+
echo_info "Setting up port-forwarding to access OpenGovernance locally."
217+
218+
# Start port-forwarding in the background
219+
kubectl port-forward -n "$NAMESPACE" service/nginx-proxy 8080:80 >> "$LOGFILE" 2>&1 &
220+
PORT_FORWARD_PID=$!
221+
222+
# Give port-forwarding some time to establish
223+
sleep 5
224+
225+
# Check if port-forwarding is still running
226+
if kill -0 "$PORT_FORWARD_PID" >/dev/null 2>&1; then
227+
echo_info "Port-forwarding established successfully."
228+
echo_prompt "OpenGovernance is accessible at http://localhost:8080"
229+
echo_prompt "To sign in, use the following default credentials:"
230+
echo_prompt " Username: [email protected]"
231+
echo_prompt " Password: password"
232+
echo_prompt "You can terminate port-forwarding by killing the background process (PID: $PORT_FORWARD_PID)."
233+
else
234+
echo_primary ""
235+
echo_primary "========================================="
236+
echo_primary "Port-Forwarding Instructions"
237+
echo_primary "========================================="
238+
echo_primary "OpenGovernance is running but not accessible via Ingress."
239+
echo_primary "You can access it using port-forwarding as follows:"
240+
echo_primary ""
241+
echo_primary "kubectl port-forward -n \"$NAMESPACE\" service/nginx-proxy 8080:80"
242+
echo_primary ""
243+
echo_primary "Then, access it at http://localhost:8080"
244+
echo_primary ""
245+
echo_primary "To sign in, use the following default credentials:"
246+
echo_primary " Username: [email protected]"
247+
echo_primary " Password: password"
248+
echo_primary ""
249+
fi
250+
}
251+
118252
# -----------------------------
119253
# Main Deployment Steps
120254
# -----------------------------
@@ -125,25 +259,18 @@ echo_primary "Starting OpenGovernance deployment script."
125259
# Check prerequisites
126260
check_prerequisites
127261

128-
# Step 1: Create a Kind cluster named 'opengovernance'
129-
echo_info "Creating Kind cluster named '$NAMESPACE'."
130-
kind create cluster --name "$NAMESPACE" || { echo_error "Failed to create Kind cluster '$NAMESPACE'."; exit 1; }
131-
132-
# Step 2: Tell kubectl to use the 'opengovernance' context
133-
CONTEXT_NAME="kind-$NAMESPACE"
134-
echo_info "Setting kubectl context to '$CONTEXT_NAME'."
135-
kubectl config use-context "$CONTEXT_NAME" || { echo_error "Failed to set kubectl context to '$CONTEXT_NAME'."; exit 1; }
262+
# Handle Kind clusters
263+
select_kind_cluster
136264

137-
# Step 3: Add the Helm repository and update
138-
echo_info "Adding Helm repository 'opengovernance'."
139-
helm repo add opengovernance https://opengovern.github.io/charts/ || { echo_error "Failed to add Helm repository 'opengovernance'."; exit 1; }
265+
# Step 1: Install the 'opengovernance' chart into the specified namespace
266+
echo_primary "Installing 'opengovernance' Helm chart into namespace '$NAMESPACE'."
267+
helm_install_with_timeout -n "$NAMESPACE" opengovernance opengovernance/opengovernance --create-namespace || { echo_error "Helm installation failed."; exit 1; }
140268

141-
echo_info "Updating Helm repositories."
142-
helm repo update || { echo_error "Failed to update Helm repositories."; exit 1; }
269+
# Step 2: Check if the application is ready
270+
check_pods_and_jobs
143271

144-
# Step 4: Install the 'opengovernance' chart into the specified namespace
145-
echo_info "Installing 'opengovernance' Helm chart into namespace '$NAMESPACE'."
146-
helm_install_with_timeout -n "$NAMESPACE" opengovernance opengovernance/opengovernance --create-namespace || { echo_error "Helm installation failed."; exit 1; }
272+
# Step 3: Set up port-forwarding after successful Helm install and readiness
273+
basic_install_with_port_forwarding
147274

148275
# Completion message
149276
echo_primary "OpenGovernance deployment completed successfully."

0 commit comments

Comments
 (0)