Created
June 7, 2024 09:12
-
-
Save alekonko/00b69c02176f5207967629cc17b5529f to your computer and use it in GitHub Desktop.
Revisions
-
alekonko created this gist
Jun 7, 2024 .There are no files selected for viewing
This file contains hidden or 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 charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,60 @@ #!/bin/bash usage() { echo "Usage: $0 -s <service-name> [-n <namespace>]" exit 1 } check_service_exists() { if [ -z "$NAMESPACE" ]; then kubectl get service "$SERVICE_NAME" > /dev/null 2>&1 else kubectl get service "$SERVICE_NAME" -n "$NAMESPACE" > /dev/null 2>&1 fi return $? } while getopts ":s:n:" opt; do case ${opt} in s ) SERVICE_NAME=$OPTARG ;; n ) NAMESPACE=$OPTARG ;; \? ) usage ;; esac done if [ -z "$SERVICE_NAME" ]; then echo "Errore: il nome del servizio è obbligatorio." usage fi if ! check_service_exists; then echo "Errore: il servizio '$SERVICE_NAME' non esiste." exit 1 fi if [ -z "$NAMESPACE" ]; then allocated_ip=$(kubectl get service $SERVICE_NAME -o jsonpath='{.status.loadBalancer.ingress[0].ip}') else allocated_ip=$(kubectl get service $SERVICE_NAME -n $NAMESPACE -o jsonpath='{.status.loadBalancer.ingress[0].ip}') fi if [ -z "$allocated_ip" ]; then echo "Errore: IP non trovato nel campo status.loadBalancer.ingress[0].ip" exit 1 fi patch_content="{\"spec\": {\"loadBalancerIP\": \"$allocated_ip\"}}" if [ -z "$NAMESPACE" ]; then kubectl patch service $SERVICE_NAME --patch "$patch_content" else kubectl patch service $SERVICE_NAME -n $NAMESPACE --patch "$patch_content" fi echo "Patch applicata con successo: $allocated_ip"