|
9 | 9 | # Orphaned resources are those that: |
10 | 10 | # - Have the label 'managed-by=github-action' |
11 | 11 | # - Do NOT have corresponding services in docker-compose.yml |
| 12 | +# - Are NOT in the exclusion list (special deployments managed separately) |
| 13 | +# |
| 14 | +# Excluded deployments: |
| 15 | +# - uni-resolver-frontend: Web UI deployed separately, not in docker-compose.yml |
12 | 16 | # |
13 | 17 | # The script removes: |
14 | | -# - Orphaned Deployments |
15 | | -# - Orphaned Services |
| 18 | +# - Orphaned Deployments (not in docker-compose.yml and not excluded) |
| 19 | +# - Orphaned Services (not in docker-compose.yml AND no matching deployment) |
16 | 20 | # - Service-specific ConfigMaps (not the global app-config) |
17 | 21 | # |
18 | 22 | # Prerequisites: |
@@ -41,6 +45,10 @@ echo "====================================================================" |
41 | 45 | echo "Checking for orphaned deployments in namespace: $NAMESPACE" |
42 | 46 | echo "====================================================================" |
43 | 47 |
|
| 48 | +# Deployments that are not in docker-compose.yml but should be kept |
| 49 | +# These are special deployments managed separately |
| 50 | +EXCLUDED_DEPLOYMENTS="uni-resolver-frontend" |
| 51 | + |
44 | 52 | # Get all deployments managed by this GitHub Action |
45 | 53 | kubectl get deployments -n "$NAMESPACE" -l managed-by=github-action -o json 2>/dev/null | \ |
46 | 54 | jq -r '.items[].metadata.name' > managed_deployments.txt || touch managed_deployments.txt |
@@ -69,6 +77,13 @@ orphans_found=0 |
69 | 77 |
|
70 | 78 | # Find and remove orphaned deployments |
71 | 79 | while read -r deployment; do |
| 80 | + # Check if this deployment is in the exclusion list |
| 81 | + if echo "$EXCLUDED_DEPLOYMENTS" | grep -qw "$deployment"; then |
| 82 | + echo "" |
| 83 | + echo "ℹ Skipping deployment: $deployment (excluded from cleanup)" |
| 84 | + continue |
| 85 | + fi |
| 86 | + |
72 | 87 | # Check if this deployment exists in docker-compose.yml |
73 | 88 | if ! grep -q "^${deployment}$" compose_services.txt; then |
74 | 89 | echo "" |
@@ -113,3 +128,80 @@ else |
113 | 128 | echo "✓ Cleanup completed: $orphans_found orphaned deployment(s) removed" |
114 | 129 | fi |
115 | 130 | echo "====================================================================" |
| 131 | + |
| 132 | +echo "" |
| 133 | +echo "====================================================================" |
| 134 | +echo "Checking for orphaned services in namespace: $NAMESPACE" |
| 135 | +echo "====================================================================" |
| 136 | + |
| 137 | +# Get all services managed by this GitHub Action |
| 138 | +kubectl get services -n "$NAMESPACE" -l managed-by=github-action -o json 2>/dev/null | \ |
| 139 | + jq -r '.items[].metadata.name' > managed_services.txt || touch managed_services.txt |
| 140 | + |
| 141 | +# Check if there are any managed services |
| 142 | +if [ ! -s managed_services.txt ]; then |
| 143 | + echo "No managed services found in namespace" |
| 144 | + echo "====================================================================" |
| 145 | + exit 0 |
| 146 | +fi |
| 147 | + |
| 148 | +echo "Managed services in cluster:" |
| 149 | +cat managed_services.txt |
| 150 | + |
| 151 | +echo "" |
| 152 | +echo "Services in docker-compose.yml:" |
| 153 | +cat compose_services.txt |
| 154 | + |
| 155 | +echo "" |
| 156 | +echo "Comparing services with docker-compose.yml and deployments..." |
| 157 | + |
| 158 | +# Track orphaned services |
| 159 | +orphaned_services=0 |
| 160 | + |
| 161 | +# Find and remove orphaned services |
| 162 | +while read -r service; do |
| 163 | + # Check if this service is in the exclusion list |
| 164 | + if echo "$EXCLUDED_DEPLOYMENTS" | grep -qw "$service"; then |
| 165 | + echo "" |
| 166 | + echo "ℹ Skipping service: $service (excluded from cleanup)" |
| 167 | + continue |
| 168 | + fi |
| 169 | + |
| 170 | + # Check if service exists in docker-compose.yml OR has a matching deployment |
| 171 | + service_in_compose=false |
| 172 | + deployment_exists=false |
| 173 | + |
| 174 | + if grep -q "^${service}$" compose_services.txt; then |
| 175 | + service_in_compose=true |
| 176 | + fi |
| 177 | + |
| 178 | + if kubectl get deployment "$service" -n "$NAMESPACE" &>/dev/null; then |
| 179 | + deployment_exists=true |
| 180 | + fi |
| 181 | + |
| 182 | + # Service is orphaned if it's not in compose AND has no matching deployment |
| 183 | + if [ "$service_in_compose" = false ] && [ "$deployment_exists" = false ]; then |
| 184 | + echo "" |
| 185 | + echo "⚠ Found orphaned service: $service" |
| 186 | + echo " This service is not defined in docker-compose.yml and has no matching deployment" |
| 187 | + echo " Removing service..." |
| 188 | + |
| 189 | + orphaned_services=$((orphaned_services + 1)) |
| 190 | + |
| 191 | + if kubectl delete service "$service" -n "$NAMESPACE" --ignore-not-found=true; then |
| 192 | + echo " ✓ Service '$service' deleted" |
| 193 | + else |
| 194 | + echo " ✗ Failed to delete service '$service'" |
| 195 | + fi |
| 196 | + fi |
| 197 | +done < managed_services.txt |
| 198 | + |
| 199 | +echo "" |
| 200 | +echo "====================================================================" |
| 201 | +if [ $orphaned_services -eq 0 ]; then |
| 202 | + echo "✓ No orphaned services found" |
| 203 | + echo " All managed services match docker-compose.yml or have deployments" |
| 204 | +else |
| 205 | + echo "✓ Cleanup completed: $orphaned_services orphaned service(s) removed" |
| 206 | +fi |
| 207 | +echo "====================================================================" |
0 commit comments