@@ -5,15 +5,148 @@ import (
5
5
"github.com/hashicorp/consul-k8s/acceptance/framework/helpers"
6
6
"github.com/stretchr/testify/assert"
7
7
"github.com/stretchr/testify/require"
8
+ "os"
8
9
"os/exec"
9
10
"strconv"
11
+ "strings"
12
+ "syscall"
10
13
"testing"
11
14
)
12
15
16
+ func interruptProcess (t * testing.T ) {
17
+ p , err := os .FindProcess (os .Getpid ())
18
+ if err != nil {
19
+ t .Logf ("Failed to find process: %v" , err )
20
+ return
21
+ }
22
+ err = p .Signal (syscall .SIGINT )
23
+ if err != nil {
24
+ t .Logf ("Failed to send interrupt signal: %v" , err )
25
+ }
26
+ }
27
+ func removeNamespaceFinalizer (t * testing.T , namespace string ) {
28
+ cmd := exec .Command ("kubectl" , "patch" , "namespace" , namespace ,
29
+ "--type=json" , "-p" , `[{"op": "remove", "path": "/spec/finalizers"}]` )
30
+
31
+ if output , err := cmd .CombinedOutput (); err != nil {
32
+ t .Logf ("Error removing namespace finalizer: %v\n Output: %s\n " , err , output )
33
+ }
34
+ verifyNamespaceDeletion (t , namespace )
35
+ }
36
+
37
+ func verifyNamespaceDeletion (t * testing.T , namespace string ) {
38
+ checkCmd := exec .Command ("kubectl" , "get" , "ns" , namespace )
39
+ if checkErr := checkCmd .Run (); checkErr != nil {
40
+ t .Logf ("Namespace %s deleted successfully\n " , namespace )
41
+ return
42
+ }
43
+
44
+ t .Logf ("Namespace still exists. Additional cleanup might be required" )
45
+ interruptProcess (t )
46
+ }
47
+
48
+ func gatewayControllerDiagnostic (t * testing.T , namespace string ) {
49
+ // Add diagnostic logging for pods for controller identification
50
+ lsNamespaceCmd := exec .Command ("oc" , "get" , "namespaces" , "-o" , "wide" )
51
+ lsNamespaceOutput , _ := lsNamespaceCmd .CombinedOutput ()
52
+ t .Logf ("Namespaces in cluster:\n %s" , string (lsNamespaceOutput ))
53
+
54
+ podInfoCmd := exec .Command ("kubectl" , "get" , "pods" , "-n" , namespace )
55
+ podInfoOutput , _ := podInfoCmd .CombinedOutput ()
56
+ t .Logf ("Pod status in namespace %s before cleanup:\n %s" , namespace , string (podInfoOutput ))
57
+
58
+ podInfoCmd = exec .Command ("kubectl" , "get" , "pods" , "-n" , "kube-system" )
59
+ podInfoOutput , _ = podInfoCmd .CombinedOutput ()
60
+ t .Logf ("Pod status in namespace %s before cleanup:\n %s" , "kube-system" , string (podInfoOutput ))
61
+ }
62
+
63
+ func checkAndDeleteNamespace (t * testing.T ) {
64
+ // There are some commands in this function
65
+ //which are not being replaced with this variables and need manual replacement of namespace
66
+ namespace := "consul"
67
+ // Check if namespace exists and its status
68
+ nsCheckCmd := exec .Command ("kubectl" , "get" , "namespace" , namespace , "-o" , "json" )
69
+ nsOutput , _ := nsCheckCmd .CombinedOutput ()
70
+ t .Logf ("Consul namespace status before cleanup:\n %s" , string (nsOutput ))
71
+
72
+ // Add diagnostic logging before attempting cleanup
73
+ logCmd := exec .Command ("kubectl" , "get" , "all" , "-n" , namespace )
74
+ logOutput , _ := logCmd .CombinedOutput ()
75
+ t .Logf ("Resources in consul namespace before cleanup:\n %s" , string (logOutput ))
76
+
77
+ // find gateway controller information and logs
78
+ gatewayControllerDiagnostic (t , namespace )
79
+ // Force cleanup of any stuck resources in the namespace (if it still exists)
80
+ t .Log ("Checking for any stuck resources..." )
81
+ forceCleanupCmd := exec .Command ("bash" , "-c" , `
82
+ # Try to find finalizers on the namespace
83
+ FINALIZERS=$(kubectl get namespace consul -o json 2>/dev/null | jq '.spec.finalizers' 2>/dev/null)
84
+ if [ ! -z "$FINALIZERS" ] && [ "$FINALIZERS" != "null" ]; then
85
+ echo "Found finalizers on namespace consul"
86
+ echo $FINALIZERS
87
+ # Remove finalizers from namespace to force deletion
88
+ # kubectl get namespace consul -o json | jq '.spec.finalizers = []' | kubectl replace --raw "/api/v1/namespaces/consul/finalize" -f -
89
+ fi
90
+ if kubectl get namespace consul > /dev/null 2>&1; then
91
+ # Check for gateway resources
92
+ GATEWAYS=$(kubectl get gateways.gateway.networking.k8s.io -n consul -o json 2>/dev/null || echo "")
93
+ echo $GATEWAYS
94
+ fi
95
+ ` )
96
+ forceOutput , _ := forceCleanupCmd .CombinedOutput ()
97
+ t .Logf ("Force cleanup result:\n %s" , string (forceOutput ))
98
+
99
+ // Get remaining Gateways
100
+ getCmd := exec .Command ("kubectl" , "get" , "gateways.gateway.networking.k8s.io" , "-n" , namespace ,
101
+ "-o=jsonpath={.items[*].metadata.name}" )
102
+ output , err := getCmd .CombinedOutput ()
103
+ t .Logf ("Gateway resource check result:\n %s" , string (output ))
104
+
105
+ if err != nil {
106
+ t .Logf ("Error getting gateways: %v\n " , err )
107
+ return
108
+ }
109
+ cleanedOutput := strings .TrimSpace (string (output ))
110
+ if cleanedOutput == "" {
111
+ t .Logf ("No gateways found, removing namespace finalizer" )
112
+ removeNamespaceFinalizer (t , namespace )
113
+ return
114
+ }
115
+ if len (cleanedOutput ) > 0 {
116
+ // Remove finalizers from each gateway
117
+ patchCmd := exec .Command ("kubectl" , "patch" , "gateways.gateway.networking.k8s.io" , string (output ), "-n" , namespace ,
118
+ "--type=json" , "-p" , `[{"op": "remove", "path": "/metadata/finalizers"}]` )
119
+ patchOutput , patchErr := patchCmd .CombinedOutput ()
120
+ if patchErr != nil {
121
+ t .Logf ("Error patching gateway: %v\n Output: %s\n " , patchErr , patchOutput )
122
+ return
123
+ }
124
+ t .Logf ("Finalizers removed successfully" )
125
+ removeNamespaceFinalizer (t , namespace )
126
+ }
127
+ t .Log ("Attempting to delete consul namespace if it exists..." )
128
+ cleanupCmd := exec .Command ("kubectl" , "delete" , "namespace" , "consul" , "--ignore-not-found=true" )
129
+ cleanupOutput , cleanupErr := cleanupCmd .CombinedOutput ()
130
+ // We don't check error here since it's just precautionary cleanup
131
+ t .Logf ("Namespace deletion attempt result: %v\n Output: %s" , cleanupErr , string (cleanupOutput ))
132
+
133
+ // Wait for namespace to be fully deleted before proceeding
134
+ t .Log ("Waiting for consul namespace to be fully deleted..." )
135
+ waitCmd := exec .Command ("kubectl" , "wait" , "--for=delete" , "namespace/consul" , "--timeout=30s" )
136
+ waitOutput , waitErr := waitCmd .CombinedOutput () // Ignore errors, as this will error if the namespace doesn't exist at all
137
+ t .Logf ("Wait result: %v\n Output: %s" , waitErr , string (waitOutput ))
138
+
139
+ // Verify namespace deletion
140
+ verifyNamespaceDeletion (t , namespace )
141
+ }
142
+
13
143
func newOpenshiftCluster (t * testing.T , cfg * config.TestConfig , secure , namespaceMirroring bool ) {
14
144
cmd := exec .Command ("helm" , "repo" , "add" , "hashicorp" , "https://helm.releases.hashicorp.com" )
15
145
output , err := cmd .CombinedOutput ()
16
- require .NoErrorf (t , err , "failed to add hashicorp helm repo: %s" , string (output ))
146
+ require .NoErrorf (t , err , "failed to add hashicorp helm repo : %s" , string (output ))
147
+
148
+ // Check for any stuck resources in the namespace and force cleanup if necessary
149
+ checkAndDeleteNamespace (t )
17
150
18
151
// FUTURE for some reason NewHelmCluster creates a consul server pod that runs as root which
19
152
// isn't allowed in OpenShift. In order to test OpenShift properly, we have to call helm and k8s
@@ -26,7 +159,7 @@ func newOpenshiftCluster(t *testing.T, cfg *config.TestConfig, secure, namespace
26
159
assert .NoErrorf (t , err , "failed to delete namespace: %s" , string (output ))
27
160
})
28
161
29
- require .NoErrorf (t , err , "failed to add hashicorp helm repo : %s" , string (output ))
162
+ require .NoErrorf (t , err , "failed to create namespace : %s" , string (output ))
30
163
31
164
cmd = exec .Command ("kubectl" , "create" , "secret" , "generic" ,
32
165
"consul-ent-license" ,
@@ -62,6 +195,7 @@ func newOpenshiftCluster(t *testing.T, cfg *config.TestConfig, secure, namespace
62
195
)
63
196
64
197
output , err = cmd .CombinedOutput ()
198
+ t .Logf ("Output of the helm install command: %s" , string (output ))
65
199
helpers .Cleanup (t , cfg .NoCleanupOnFailure , cfg .NoCleanup , func () {
66
200
cmd := exec .Command ("helm" , "uninstall" , "consul" , "--namespace" , "consul" )
67
201
output , err := cmd .CombinedOutput ()
0 commit comments