add tls sync

This commit is contained in:
2026-01-22 02:28:10 +01:00
parent c94d6254fa
commit 50b7092809
16 changed files with 979 additions and 75 deletions

View File

@@ -43,32 +43,94 @@ echo "=== Synchronisation automatique des secrets TLS ==="
echo "Source: $SOURCE_CLUSTER (namespace: $SOURCE_NS)"
echo ""
# Fonction pour déterminer les namespaces cibles pour un certificat wildcard
# Les certificats wildcard doivent être copiés dans tous les namespaces qui les utilisent
get_wildcard_target_namespaces() {
local cert_name=$1
local env=""
# Extraire l'environnement depuis le nom du certificat
if [[ $cert_name =~ wildcard-dev-tls ]]; then
env="dev"
elif [[ $cert_name =~ wildcard-rct-tls ]]; then
env="rct"
elif [[ $cert_name =~ wildcard-prd-tls ]]; then
env="prd"
else
return 1
fi
# Liste des namespaces qui utilisent le certificat wildcard pour cet environnement
# Ajoutez ici tous les namespaces qui référencent ce secret dans leurs Ingress
case "$env" in
dev)
WILDCARD_TARGET_NAMESPACES=("headlamp-dev" "homarr-dev" "longhorn-dev")
;;
rct)
WILDCARD_TARGET_NAMESPACES=("headlamp-rct" "homarr-rct" "longhorn-rct")
;;
prd)
WILDCARD_TARGET_NAMESPACES=("headlamp-prd" "homarr-prd" "longhorn-prd")
;;
*)
return 1
;;
esac
return 0
}
# Fonction pour déterminer le cluster et namespace cible à partir du nom du certificat
# Format attendu: <app>-<env>-tls
# Exemple: homarr-dev-tls -> cluster-dev, namespace homarr-dev
# Pour les certificats wildcard, cette fonction détermine seulement le cluster
determine_target() {
local cert_name=$1
# Extraire l'environnement (dev, rct, prd)
if [[ $cert_name =~ -dev-tls$ ]]; then
TARGET_CLUSTER="cluster-dev"
TARGET_NS="${cert_name%-tls}" # Garde le suffixe -dev
elif [[ $cert_name =~ -rct-tls$ ]]; then
TARGET_CLUSTER="cluster-rct"
TARGET_NS="${cert_name%-tls}" # Garde le suffixe -rct
elif [[ $cert_name =~ -prd-tls$ ]]; then
TARGET_CLUSTER="cluster-prd"
TARGET_NS="${cert_name%-tls}" # Garde le suffixe -prd
else
# Par défaut, essayer de deviner depuis le nom
# Si le nom contient "dev", utiliser cluster-dev
if [[ $cert_name == *"dev"* ]]; then
# Détecter les certificats wildcard
if [[ $cert_name =~ ^wildcard- ]]; then
# Pour les certificats wildcard, on détermine seulement le cluster
# Les namespaces seront déterminés par get_wildcard_target_namespaces
if [[ $cert_name =~ -dev-tls$ ]]; then
TARGET_CLUSTER="cluster-dev"
TARGET_NS="${cert_name%-tls}"
TARGET_NS="" # Sera rempli par get_wildcard_target_namespaces
IS_WILDCARD=true
elif [[ $cert_name =~ -rct-tls$ ]]; then
TARGET_CLUSTER="cluster-rct"
TARGET_NS=""
IS_WILDCARD=true
elif [[ $cert_name =~ -prd-tls$ ]]; then
TARGET_CLUSTER="cluster-prd"
TARGET_NS=""
IS_WILDCARD=true
else
echo "⚠️ Impossible de déterminer le cluster cible pour $cert_name"
return 1
fi
else
# Certificats normaux (non-wildcard)
IS_WILDCARD=false
# Extraire l'environnement (dev, rct, prd)
if [[ $cert_name =~ -dev-tls$ ]]; then
TARGET_CLUSTER="cluster-dev"
TARGET_NS="${cert_name%-tls}" # Garde le suffixe -dev
elif [[ $cert_name =~ -rct-tls$ ]]; then
TARGET_CLUSTER="cluster-rct"
TARGET_NS="${cert_name%-tls}" # Garde le suffixe -rct
elif [[ $cert_name =~ -prd-tls$ ]]; then
TARGET_CLUSTER="cluster-prd"
TARGET_NS="${cert_name%-tls}" # Garde le suffixe -prd
else
# Par défaut, essayer de deviner depuis le nom
# Si le nom contient "dev", utiliser cluster-dev
if [[ $cert_name == *"dev"* ]]; then
TARGET_CLUSTER="cluster-dev"
TARGET_NS="${cert_name%-tls}"
else
echo "⚠️ Impossible de déterminer le cluster cible pour $cert_name"
return 1
fi
fi
fi
return 0
@@ -167,6 +229,7 @@ for i in $(seq 0 $((CERT_COUNT - 1))); do
CERT_NAME=$(echo "$CERTIFICATES" | jq -r ".items[$i].metadata.name")
SECRET_NAME=$(echo "$CERTIFICATES" | jq -r ".items[$i].spec.secretName")
CERT_NAMESPACE=$(echo "$CERTIFICATES" | jq -r ".items[$i].metadata.namespace")
IS_WILDCARD=false # Initialisation par défaut
# Si secretName n'est pas défini, utiliser le nom du certificat
if [ "$SECRET_NAME" == "null" ] || [ -z "$SECRET_NAME" ]; then
@@ -184,8 +247,6 @@ for i in $(seq 0 $((CERT_COUNT - 1))); do
continue
fi
echo " Destination: $TARGET_CLUSTER (namespace: $TARGET_NS)"
# Vérifier que le secret existe dans la source
if [ -z "$SOURCE_CLUSTER" ]; then
SECRET_CHECK_CMD="kubectl get secret \"$SECRET_NAME\" -n \"$SOURCE_NS\""
@@ -200,10 +261,7 @@ for i in $(seq 0 $((CERT_COUNT - 1))); do
continue
fi
# Synchroniser le secret
echo " Synchronisation en cours..."
# Récupérer le secret
# Récupérer le secret une seule fois
TEMP_FILE=$(mktemp)
if [ -z "$SOURCE_CLUSTER" ]; then
SECRET_GET_CMD="kubectl get secret \"$SECRET_NAME\" -n \"$SOURCE_NS\" -o yaml"
@@ -219,30 +277,88 @@ for i in $(seq 0 $((CERT_COUNT - 1))); do
continue
fi
# Modifier les métadonnées
sed -i.bak '/^ uid:/d' "$TEMP_FILE" 2>/dev/null || true
sed -i.bak '/^ resourceVersion:/d' "$TEMP_FILE" 2>/dev/null || true
sed -i.bak '/^ selfLink:/d' "$TEMP_FILE" 2>/dev/null || true
sed -i.bak '/^ creationTimestamp:/d' "$TEMP_FILE" 2>/dev/null || true
sed -i.bak "s/namespace: $SOURCE_NS/namespace: $TARGET_NS/" "$TEMP_FILE" 2>/dev/null || true
rm -f "${TEMP_FILE}.bak" 2>/dev/null || true
# Créer le namespace s'il n'existe pas
kubectl create namespace "$TARGET_NS" --context="$TARGET_CLUSTER" --dry-run=client -o yaml | kubectl apply --context="$TARGET_CLUSTER" -f - >/dev/null 2>&1 || true
# Appliquer le secret
if kubectl apply -f "$TEMP_FILE" --context="$TARGET_CLUSTER" >/dev/null 2>&1; then
# Vérifier que le secret existe maintenant
if kubectl get secret "$SECRET_NAME" -n "$TARGET_NS" --context="$TARGET_CLUSTER" >/dev/null 2>&1; then
echo " ✅ Synchronisé avec succès"
SUCCESS_COUNT=$((SUCCESS_COUNT + 1))
# Traitement spécial pour les certificats wildcard
if [ "$IS_WILDCARD" = true ]; then
echo " 🌐 Certificat wildcard détecté"
# Obtenir la liste des namespaces cibles
if ! get_wildcard_target_namespaces "$CERT_NAME"; then
echo " ⚠️ Impossible de déterminer les namespaces cibles pour le wildcard"
rm -f "$TEMP_FILE"
SKIP_COUNT=$((SKIP_COUNT + 1))
echo ""
continue
fi
echo " Destination: $TARGET_CLUSTER (namespaces: ${WILDCARD_TARGET_NAMESPACES[*]})"
# Copier le secret dans chaque namespace cible
for TARGET_NS in "${WILDCARD_TARGET_NAMESPACES[@]}"; do
echo " → Synchronisation vers namespace: $TARGET_NS"
# Créer une copie temporaire du fichier pour ce namespace
TEMP_FILE_NS=$(mktemp)
cp "$TEMP_FILE" "$TEMP_FILE_NS"
# Modifier les métadonnées pour ce namespace
sed -i.bak '/^ uid:/d' "$TEMP_FILE_NS" 2>/dev/null || true
sed -i.bak '/^ resourceVersion:/d' "$TEMP_FILE_NS" 2>/dev/null || true
sed -i.bak '/^ selfLink:/d' "$TEMP_FILE_NS" 2>/dev/null || true
sed -i.bak '/^ creationTimestamp:/d' "$TEMP_FILE_NS" 2>/dev/null || true
sed -i.bak "s/namespace: $SOURCE_NS/namespace: $TARGET_NS/" "$TEMP_FILE_NS" 2>/dev/null || true
rm -f "${TEMP_FILE_NS}.bak" 2>/dev/null || true
# Créer le namespace s'il n'existe pas
kubectl create namespace "$TARGET_NS" --context="$TARGET_CLUSTER" --dry-run=client -o yaml | kubectl apply --context="$TARGET_CLUSTER" -f - >/dev/null 2>&1 || true
# Appliquer le secret
if kubectl apply -f "$TEMP_FILE_NS" --context="$TARGET_CLUSTER" >/dev/null 2>&1; then
# Vérifier que le secret existe maintenant
if kubectl get secret "$SECRET_NAME" -n "$TARGET_NS" --context="$TARGET_CLUSTER" >/dev/null 2>&1; then
echo " ✅ Synchronisé avec succès dans $TARGET_NS"
SUCCESS_COUNT=$((SUCCESS_COUNT + 1))
else
echo " ❌ Erreur: Le secret n'a pas été créé dans $TARGET_NS"
ERROR_COUNT=$((ERROR_COUNT + 1))
fi
else
echo " ❌ Erreur lors de l'application du secret dans $TARGET_NS"
ERROR_COUNT=$((ERROR_COUNT + 1))
fi
# Nettoyage
rm -f "$TEMP_FILE_NS" "${TEMP_FILE_NS}.bak" 2>/dev/null || true
done
else
# Certificats normaux (non-wildcard) - comportement original
echo " Destination: $TARGET_CLUSTER (namespace: $TARGET_NS)"
echo " Synchronisation en cours..."
# Modifier les métadonnées
sed -i.bak '/^ uid:/d' "$TEMP_FILE" 2>/dev/null || true
sed -i.bak '/^ resourceVersion:/d' "$TEMP_FILE" 2>/dev/null || true
sed -i.bak '/^ selfLink:/d' "$TEMP_FILE" 2>/dev/null || true
sed -i.bak '/^ creationTimestamp:/d' "$TEMP_FILE" 2>/dev/null || true
sed -i.bak "s/namespace: $SOURCE_NS/namespace: $TARGET_NS/" "$TEMP_FILE" 2>/dev/null || true
rm -f "${TEMP_FILE}.bak" 2>/dev/null || true
# Créer le namespace s'il n'existe pas
kubectl create namespace "$TARGET_NS" --context="$TARGET_CLUSTER" --dry-run=client -o yaml | kubectl apply --context="$TARGET_CLUSTER" -f - >/dev/null 2>&1 || true
# Appliquer le secret
if kubectl apply -f "$TEMP_FILE" --context="$TARGET_CLUSTER" >/dev/null 2>&1; then
# Vérifier que le secret existe maintenant
if kubectl get secret "$SECRET_NAME" -n "$TARGET_NS" --context="$TARGET_CLUSTER" >/dev/null 2>&1; then
echo " ✅ Synchronisé avec succès"
SUCCESS_COUNT=$((SUCCESS_COUNT + 1))
else
echo " ❌ Erreur: Le secret n'a pas été créé"
ERROR_COUNT=$((ERROR_COUNT + 1))
fi
else
echo " ❌ Erreur: Le secret n'a pas été créé"
echo " ❌ Erreur lors de l'application du secret"
ERROR_COUNT=$((ERROR_COUNT + 1))
fi
else
echo " ❌ Erreur lors de l'application du secret"
ERROR_COUNT=$((ERROR_COUNT + 1))
fi
# Nettoyage