Created
May 17, 2024 12:20
-
-
Save khusseini/5f53c4cabe1000a2c939a61a6045bb0f to your computer and use it in GitHub Desktop.
Create eks cluster and bootstrap gitlab project with flux
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/env bash | |
set -e | |
set -a | |
source .env | |
set +a | |
# Check if the cluster already exists | |
CLUSTER_STATUS=$(eksctl get cluster --name ${EKS_CLUSTER_NAME} --region ${EKS_CLUSTER_REGION} --output json | jq -r '.[0].Status') | |
if [ "$CLUSTER_STATUS" == "CREATING" ]; then | |
echo "Cluster ${EKS_CLUSTER_NAME} is currently being created. Waiting for it to become active..." | |
eksctl utils wait-nodes --cluster ${EKS_CLUSTER_NAME} --nodes-to-wait-for 1 | |
elif [ "$CLUSTER_STATUS" == "DELETING" ]; then | |
echo "Cluster ${EKS_CLUSTER_NAME} is currently being deleted. Exiting." | |
exit 1 | |
elif [ "$CLUSTER_STATUS" == "ACTIVE" ]; then | |
echo "Cluster ${EKS_CLUSTER_NAME} already exists and is active." | |
else | |
echo "Creating cluster ${EKS_CLUSTER_NAME}..." | |
eksctl create cluster \ | |
--name ${EKS_CLUSTER_NAME} \ | |
--version ${EKS_CLUSTER_VERSION} \ | |
--region ${EKS_CLUSTER_REGION} \ | |
--node-type t3.large \ | |
--nodes 1 \ | |
--node-volume-size 128 \ | |
--nodes-min 1 \ | |
--nodes-max 3 | |
fi | |
eksctl utils write-kubeconfig --cluster=${EKS_CLUSTER_NAME} --region=${EKS_CLUSTER_REGION} | |
kubectl cluster-info | |
TIMEOUT=0 | |
until kubectl get nodes --no-headers | awk '{print $2}' | grep -q 'Ready' || [ $TIMEOUT -eq 60 ]; do | |
echo "Waiting for nodes to be ready..." | |
sleep 10 | |
TIMEOUT=$((TIMEOUT + 10)) | |
done | |
if [ $TIMEOUT -eq 60 ]; then | |
echo "Timeout waiting for nodes to be ready" | |
exit 1 | |
fi | |
# Wait for Metrics Server to be ready | |
wait_for_metrics_server() { | |
local -r metrics_server_namespace="kube-system" | |
local -r metrics_server_deployment="metrics-server" | |
local -r timeout_seconds=300 # 5 minutes | |
local start_time=$(date +%s) | |
local elapsed_time=0 | |
local get_deployment_cmd=$(kubectl get deployment -n $metrics_server_namespace $metrics_server_deployment) | |
local get_pods_cmd=$(kubectl get pods -n $metrics_server_namespace -l app.kubernetes.io/name=metrics-server -o jsonpath='{.items[*].status.containerStatuses[*].ready}') | |
echo "Waiting for Metrics Server to be ready..." | |
while true; do | |
# Check if the Metrics Server deployment exists | |
if [ -z "$get_deployment_cmd" ]; then | |
echo "Metrics Server deployment not found." | |
return 1 | |
fi | |
# Check if the Metrics Server pods are ready | |
if echo "$get_pods_cmd" | tr ' ' '\n' | grep -vq 'false'; then | |
echo "Metrics Server is ready!" | |
return 0 | |
fi | |
# Check if the timeout has been reached | |
elapsed_time=$(($(date +%s) - $start_time)) | |
if [ "$elapsed_time" -gt "$timeout_seconds" ]; then | |
echo "Timeout reached while waiting for Metrics Server to be ready." | |
return 1 | |
fi | |
sleep 5 | |
done | |
} | |
# Deploy Metrics Server (replace this with your actual deployment command) | |
kubectl apply -f https://github.com/kubernetes-sigs/metrics-server/releases/latest/download/components.yaml | |
# Wait for Metrics Server to be ready | |
wait_for_metrics_server | |
echo ${GITLAB_TOKEN} | flux bootstrap gitlab \ | |
--owner=${GITLAB_GROUP} \ | |
--repository=${GITLAB_PROJECT} \ | |
--branch=${GITLAB_BRANCH} \ | |
--path=./workspace/clusters/management \ | |
--timeout=15m \ | |
--interval=20m |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment