@@ -13,20 +13,67 @@ import (
13
13
func newOpenshiftCluster (t * testing.T , cfg * config.TestConfig , secure , namespaceMirroring bool ) {
14
14
cmd := exec .Command ("helm" , "repo" , "add" , "hashicorp" , "https://helm.releases.hashicorp.com" )
15
15
output , err := cmd .CombinedOutput ()
16
- require .NoErrorf (t , err , "failed to add hashicorp helm repo: %s" , string (output ))
16
+ require .NoErrorf (t , err , "failed to add hashicorp helm repo : %s" , string (output ))
17
+ // Add diagnostic logging before attempting cleanup
18
+ logCmd := exec .Command ("kubectl" , "get" , "all" , "-n" , "consul" )
19
+ logOutput , _ := logCmd .CombinedOutput ()
20
+ t .Logf ("Resources in consul namespace before cleanup:\n %s" , string (logOutput ))
21
+
22
+ // Check if namespace exists and its status
23
+ nsCheckCmd := exec .Command ("kubectl" , "get" , "namespace" , "consul" , "-o" , "json" )
24
+ nsOutput , _ := nsCheckCmd .CombinedOutput ()
25
+ t .Logf ("Consul namespace status before cleanup:\n %s" , string (nsOutput ))
17
26
18
27
// FUTURE for some reason NewHelmCluster creates a consul server pod that runs as root which
19
28
// isn't allowed in OpenShift. In order to test OpenShift properly, we have to call helm and k8s
20
29
// directly to bypass. Ideally we would just fix the framework that is running the pod as root.
30
+ // First, try to delete the namespace if it exists (cleanup from previous runs)
31
+ t .Log ("Attempting to delete consul namespace if it exists..." )
32
+ cleanupCmd := exec .Command ("kubectl" , "delete" , "namespace" , "consul" , "--ignore-not-found=true" )
33
+ cleanupOutput , cleanupErr := cleanupCmd .CombinedOutput ()
34
+ // We don't check error here since it's just precautionary cleanup
35
+ t .Logf ("Namespace deletion attempt result: %v\n Output: %s" , cleanupErr , string (cleanupOutput ))
36
+
37
+ // Wait for namespace to be fully deleted before proceeding
38
+ t .Log ("Waiting for consul namespace to be fully deleted..." )
39
+ waitCmd := exec .Command ("kubectl" , "wait" , "--for=delete" , "namespace/consul" , "--timeout=120s" )
40
+ waitOutput , waitErr := waitCmd .CombinedOutput () // Ignore errors, as this will error if the namespace doesn't exist at all
41
+ t .Logf ("Wait result: %v\n Output: %s" , waitErr , string (waitOutput ))
42
+
43
+ // Force cleanup of any stuck resources in the namespace (if it still exists)
44
+ t .Log ("Checking for any stuck resources..." )
45
+ forceCleanupCmd := exec .Command ("bash" , "-c" , `
46
+ # Try to find finalizers on the namespace
47
+ FINALIZERS=$(kubectl get namespace consul -o json 2>/dev/null | jq '.spec.finalizers' 2>/dev/null)
48
+ if [ ! -z "$FINALIZERS" ] && [ "$FINALIZERS" != "null" ]; then
49
+ echo "Found finalizers on namespace, attempting to remove them..."
50
+ # Remove finalizers from namespace to force deletion
51
+ kubectl get namespace consul -o json | jq '.spec.finalizers = []' | kubectl replace --raw "/api/v1/namespaces/consul/finalize" -f -
52
+ fi
53
+ ` )
54
+ forceOutput , _ := forceCleanupCmd .CombinedOutput ()
55
+ t .Logf ("Force cleanup result:\n %s" , string (forceOutput ))
56
+
57
+ // Now create the namespace
21
58
cmd = exec .Command ("kubectl" , "create" , "namespace" , "consul" )
22
59
output , err = cmd .CombinedOutput ()
60
+
23
61
helpers .Cleanup (t , cfg .NoCleanupOnFailure , cfg .NoCleanup , func () {
62
+ if t .Failed () {
63
+ t .Log ("Test already failed, skipping cleanup assertions" )
64
+ cmd = exec .Command ("kubectl" , "delete" , "namespace" , "consul" )
65
+ if err := cmd .Run (); err != nil {
66
+ t .Logf ("Failed to delete namespace: %v" , err )
67
+ }
68
+ return
69
+ }
70
+
24
71
cmd = exec .Command ("kubectl" , "delete" , "namespace" , "consul" )
25
72
output , err = cmd .CombinedOutput ()
26
73
assert .NoErrorf (t , err , "failed to delete namespace: %s" , string (output ))
27
74
})
28
75
29
- require .NoErrorf (t , err , "failed to add hashicorp helm repo : %s" , string (output ))
76
+ require .NoErrorf (t , err , "failed to create namespace : %s" , string (output ))
30
77
31
78
cmd = exec .Command ("kubectl" , "create" , "secret" , "generic" ,
32
79
"consul-ent-license" ,
0 commit comments