#!/bin/bash applySed=0 checkReplicas() { if [[ "$1" == "-r" ]]; then shift if [[ "$1" =~ ^[0-9]+$ ]]; then replicas="$1" applySed=1 else echo "Error: Invalid REPLICAS value. Please provide a positive integer." exit 1 fi shift else replicas=1 fi } applySaltMinion() { if [ "$applySed" -eq 1 ]; then echo "Applying Salt-minion yaml with $replicas replicas..." sed "s/replicas:.*/replicas: $replicas/" salt-minion.yaml | kubectl apply -f - else echo "Applying Salt-minion yaml..." kubectl apply -f salt-minion.yaml fi } deleteSalt() { echo "Deleting Salt infrastructure..." kubectl delete -f salt-minion.yaml kubectl delete -f salt-master.yaml echo "Salt infrastructure deleted." } waitUntilSaltMasterInitialized() { attempts=0 max_attempts=10 while [ "$attempts" -lt "$max_attempts" ]; do echo "Checking if Salt-master has initialized..." sleep 5 if kubectl exec "$saltmaster" -it -- /bin/sh -c "salt-key -L" | grep -q "minion"; then echo "Salt-master is up and running. Accepting minion keys..." kubectl exec "$saltmaster" -it -- /bin/sh -c "salt-key -A -y" break fi ((attempts++)) done } deploySalt() { if [[ "$1" == "-d" ]]; then deleteSalt else checkReplicas "$@" applySaltMinion echo "Applying Salt-master yaml..." kubectl apply -f salt-master.yaml echo "Checking for Salt-master pod name..." saltmaster=$(kubectl get pods | grep salt-master | cut -d ' ' -f 1) kubectl wait --for=condition=Ready "pod/$saltmaster" --timeout=300s waitUntilSaltMasterInitialized fi } clear # Call the main function deploySalt "$@"