#!/bin/bash # Script to add imagePullSecrets to all Kubernetes deployments, jobs, and cronjobs # This ensures all pods can pull images from Docker Hub using the dockerhub-creds secret SECRET_NAME="dockerhub-creds" BASE_DIR="/Users/urtzialfaro/Documents/bakery-ia/infrastructure/kubernetes" # ANSI color codes GREEN='\033[0;32m' BLUE='\033[0;34m' YELLOW='\033[1;33m' NC='\033[0m' # No Color echo -e "${BLUE}Adding imagePullSecrets to all Kubernetes resources...${NC}" echo "======================================================" echo "" # Counter for files processed count=0 # Function to add imagePullSecrets to a file add_image_pull_secrets() { local file="$1" # Check if file already has imagePullSecrets if grep -q "imagePullSecrets:" "$file"; then echo -e "${YELLOW} ⊘ Skipping (already has imagePullSecrets): $(basename $file)${NC}" return fi # Temporary file for processing temp_file=$(mktemp) # Process the file using awk to add imagePullSecrets after "spec:" in template or job spec awk ' /^ spec:$/ && !done { print $0 print " imagePullSecrets:" print " - name: dockerhub-creds" done = 1 next } { print } ' "$file" > "$temp_file" # Check if changes were made if ! cmp -s "$file" "$temp_file"; then mv "$temp_file" "$file" echo -e "${GREEN} ✓ Updated: $(basename $file)${NC}" ((count++)) else rm "$temp_file" echo -e "${YELLOW} ⊘ No changes needed: $(basename $file)${NC}" fi } # Process all service deployments echo -e "${BLUE}Processing service deployments...${NC}" find $BASE_DIR/base/components -name "*-service.yaml" | while read file; do if [ -f "$file" ]; then add_image_pull_secrets "$file" fi done echo "" # Process all database deployments echo -e "${BLUE}Processing database deployments...${NC}" for file in $BASE_DIR/base/components/databases/*.yaml; do if [ -f "$file" ]; then add_image_pull_secrets "$file" fi done echo "" # Process all migration jobs echo -e "${BLUE}Processing migration jobs...${NC}" for file in $BASE_DIR/base/migrations/*.yaml; do if [ -f "$file" ]; then add_image_pull_secrets "$file" fi done echo "" # Process all cronjobs echo -e "${BLUE}Processing cronjobs...${NC}" for file in $BASE_DIR/base/cronjobs/*.yaml; do if [ -f "$file" ]; then add_image_pull_secrets "$file" fi done echo "" # Process standalone jobs echo -e "${BLUE}Processing standalone jobs...${NC}" for file in $BASE_DIR/base/jobs/*.yaml; do if [ -f "$file" ]; then add_image_pull_secrets "$file" fi done echo "" # Process deployments directory echo -e "${BLUE}Processing deployments...${NC}" for file in $BASE_DIR/base/deployments/*.yaml; do if [ -f "$file" ]; then add_image_pull_secrets "$file" fi done echo "" # Process nominatim service if [ -f "$BASE_DIR/base/components/infrastructure/nominatim.yaml" ]; then echo -e "${BLUE}Processing nominatim service...${NC}" add_image_pull_secrets "$BASE_DIR/base/components/infrastructure/nominatim.yaml" echo "" fi echo "======================================================" echo -e "${GREEN}Completed! Updated $count file(s)${NC}" echo "" echo "Next steps:" echo "1. Review the changes: git diff" echo "2. Apply to cluster: kubectl apply -k infrastructure/kubernetes/overlays/dev" echo "3. Verify pods are running: kubectl get pods -n bakery-ia"