#!/bin/bash # Aggiunge un listener HTTPS direttamente sulla risorsa K8s Gateway # main-gateway nel namespace nginx-gateway, tramite kubectl patch. # # Uso: # ./add-listener.sh # # Esempio: # ./add-listener.sh sonarqube.italiadatacenter.com https-sonarqube sonarqube-secret set -euo pipefail GATEWAY_NAME="main-gateway" GATEWAY_NS="nginx-gateway" HOSTNAME_VAL="${1:-}" NAME_VAL="${2:-}" SECRET_NAME="${3:-}" if [[ -z "$HOSTNAME_VAL" || -z "$NAME_VAL" || -z "$SECRET_NAME" ]]; then echo "Uso: $0 " echo "Es.: $0 sonarqube.italiadatacenter.com https-sonarqube sonarqube-secret" exit 1 fi # Controlla idempotenza: verifica se il listener esiste già per nome o hostname EXISTING=$(kubectl get gateway "$GATEWAY_NAME" -n "$GATEWAY_NS" \ -o jsonpath='{.spec.listeners[*].name}') if echo "$EXISTING" | grep -qw "$NAME_VAL"; then echo "Attenzione: listener con name '${NAME_VAL}' già presente. Nessuna modifica." exit 0 fi EXISTING_HOSTS=$(kubectl get gateway "$GATEWAY_NAME" -n "$GATEWAY_NS" \ -o jsonpath='{.spec.listeners[*].hostname}') if echo "$EXISTING_HOSTS" | grep -qw "$HOSTNAME_VAL"; then echo "Attenzione: listener con hostname '${HOSTNAME_VAL}' già presente. Nessuna modifica." exit 0 fi # JSON Patch: aggiunge il nuovo listener in append alla lista PATCH=$(cat <