diff --git a/config/generator/config-template.yaml b/config/generator/config-dev.yaml similarity index 100% rename from config/generator/config-template.yaml rename to config/generator/config-dev.yaml diff --git a/config/generator/env/clusters.sh b/config/generator/env/clusters.sh new file mode 100755 index 0000000..fe58e66 --- /dev/null +++ b/config/generator/env/clusters.sh @@ -0,0 +1,394 @@ +#!/bin/bash +# +# This script manages the creation, deletion, and configuration of Kubernetes clusters using kind (Kubernetes IN Docker). +# It sets up multiple clusters with Calico for inter-cluster networking and configures BGP for routing between clusters. +# +# Usage: +# ./clusters.sh {create|delete|cloudprovider} +# +# Commands: +# create - Creates the clusters, installs and configures Calico, and sets up BGP and DNS. +# delete - Deletes all the clusters. +# cloudprovider - Runs the cloud-provider-kind. +# +# Prerequisites: +# - jq +# - kind +# - kubectl +# - cloud-provider-kind +# +# Environment Variables: +# CLUSTERS - Array of cluster names. +# POD_SUBNETS - Array of pod subnets for each cluster. +# SVC_SUBNETS - Array of service subnets for each cluster. +# AS_NUMBERS - Array of BGP Autonomous System (AS) numbers for each cluster. +# DNS_DOMAINS - Array of DNS domains for each cluster. +# DEFAULT_DNS_IPS - Array of default DNS IPs for each cluster. +# NODES - Array of node roles for each cluster. +# +CLUSTERS=("kind-cluster1" "kind-cluster2") +POD_SUBNETS=("192.168.0.0" "192.169.0.0") +SVC_SUBNETS=("10.96.0.0" "10.97.0.0") +AS_NUMBERS=("65001" "65002") +DNS_DOMAINS=("cluster1.local" "cluster2.local") +DEFAULT_DNS_IPS=("10.96.0.10" "10.97.0.10") + +NODES=("control-plane" "worker") + +usage() { + echo "Usage: $0 {create|delete|cloudprovider}" + exit 1 +} + +main () { + if [ $# -ne 1 ]; then + usage + fi + + case "$1" in + create) + create_clusters + ;; + delete) + delete_clusters + ;; + cloudprovider) + run_cloud_provider_kind + ;; + *) + echo "Error: Invalid command '$1'" + usage + ;; + esac +} + +create_clusters() { + validate + create_clusters_all + merge_kubeconfig + install_calico_all + configure_calico_all + wait_pods_ready_all + configure_dns_all + wait_pods_ready_all + configure_bgp_all + wait_pods_ready_all + configure_bgp_peers_all + + echo "Clusters with Calico with inter-cluster networking are now set up!" +} + +validate() { + if ! command -v jq &> /dev/null; then + echo "jq is not installed. Please install it before running this script." + exit 1 + fi + + if ! command -v kind &> /dev/null; then + echo "kind is not installed. Please install it before running this script." + exit 1 + fi + + if ! command -v kubectl &> /dev/null; then + echo "kubectl is not installed. Please install it before running this script." + exit 1 + fi + + if ! command -v cloud-provider-kind &> /dev/null; then + echo "cloud-provider-kind is not installed. Please install it before running this script." + exit 1 + fi + + mkdir -p target +} + +create_clusters_all() { + echo "### Creating clusters... ###" + + for i in "${!CLUSTERS[@]}"; do + cluster="${CLUSTERS[$i]}" + pod_subnet="${POD_SUBNETS[$i]}" + svc_subnet="${SVC_SUBNETS[$i]}" + dns_domain="${DNS_DOMAINS[$i]}" + create_cluster $cluster $pod_subnet $svc_subnet $dns_domain + done + kind get clusters + echo "" +} + +create_cluster() { + local cluster_name=$1 + local pod_subnet=$2 + local svc_subnet=$3 + local dns_domain=$4 + + echo "Creating cluster: $cluster_name" + + kind create cluster --name $cluster_name --config - < ./target/kubeconfig.${cluster}.yaml + KUBECONFIG=./target/kubeconfig.${cluster}.yaml:${KUBECONFIG}: + done + + export KUBECONFIG=${KUBECONFIG} + kubectl config view --merge --flatten > ./target/merged-kubeconfig.yaml + + export KUBECONFIG=$(pwd)/target/merged-kubeconfig.yaml + echo "KUBECONFIG set to: ${KUBECONFIG}" + echo "" +} + +install_calico_all() { + echo "### Installing Calico... ###" + for i in "${!CLUSTERS[@]}"; do + cluster="${CLUSTERS[$i]}" + install_calico $cluster + done + echo "" + sleep 1 +} + +install_calico() { + local cluster=$1 + + echo "Installing Calico on $cluster..." + kubectl --context kind-${cluster} create ns calico-system + kubectl --context kind-${cluster} create -f https://raw.githubusercontent.com/projectcalico/calico/v3.30.1/manifests/operator-crds.yaml + kubectl --context kind-${cluster} create -f https://raw.githubusercontent.com/projectcalico/calico/v3.30.1/manifests/tigera-operator.yaml +} + +configure_calico_all() { + echo "### Configuring Calico... ###" + + for i in "${!CLUSTERS[@]}"; do + cluster="${CLUSTERS[$i]}" + pod_subnet="${POD_SUBNETS[$i]}" + configure_calico $cluster $pod_subnet + done + echo "" +} + +configure_calico() { + local cluster=$1 + local pod_subnet=$2 + + echo "Configuring Calico on $cluster..." + + kubectl --context kind-${cluster} create -f -<