@@ -16,6 +16,7 @@ package e2e
16
16
import (
17
17
"bytes"
18
18
"context"
19
+ "fmt"
19
20
"io"
20
21
"os/exec"
21
22
"strings"
@@ -161,18 +162,40 @@ func (c *MetricsTestConfig) validate(token string) {
161
162
// cleanup removes the created resources. Uses a context with timeout to prevent hangs.
162
163
func (c * MetricsTestConfig ) cleanup () {
163
164
c .t .Log ("Cleaning up resources" )
164
- ctx , cancel := context .WithTimeout (context .Background (), 10 * time .Second )
165
+ _ = exec .Command (c .client , "delete" , "clusterrolebinding" , c .clusterBinding , "--ignore-not-found=true" ).Run ()
166
+ _ = exec .Command (c .client , "delete" , "pod" , c .curlPodName , "-n" , c .namespace , "--ignore-not-found=true" ).Run ()
167
+
168
+ // Create a context with a 60-second timeout.
169
+ ctx , cancel := context .WithTimeout (context .Background (), 60 * time .Second )
165
170
defer cancel ()
166
171
167
- cmd := exec .CommandContext (ctx , c .client , "delete" , "clusterrolebinding" , c .clusterBinding , "--ignore-not-found=true" )
168
- if output , err := cmd .CombinedOutput (); err != nil {
169
- c .t .Logf ("Error cleaning up clusterrolebinding: %s" , string (output ))
172
+ // Wait for the ClusterRoleBinding to be deleted.
173
+ if err := waitForDeletion (ctx , c .client , "clusterrolebinding" , c .clusterBinding ); err != nil {
174
+ c .t .Logf ("Error waiting for clusterrolebinding deletion: %v" , err )
175
+ } else {
176
+ c .t .Log ("ClusterRoleBinding deleted" )
170
177
}
171
178
172
- cmd = exec .CommandContext (ctx , c .client , "delete" , "pod" , c .curlPodName , "-n" , c .namespace , "--ignore-not-found=true" )
173
- if output , err := cmd .CombinedOutput (); err != nil {
174
- c .t .Logf ("Error cleaning up pod: %s" , string (output ))
179
+ // Wait for the Pod to be deleted.
180
+ if err := waitForDeletion (ctx , c .client , "pod" , c .curlPodName , "-n" , c .namespace ); err != nil {
181
+ c .t .Logf ("Error waiting for pod deletion: %v" , err )
182
+ } else {
183
+ c .t .Log ("Pod deleted" )
184
+ }
185
+ }
186
+
187
+ // waitForDeletion uses "kubectl wait" to block until the specified resource is deleted
188
+ // or until the 30-second timeout is reached.
189
+ func waitForDeletion (ctx context.Context , client , resourceType , resourceName string , extraArgs ... string ) error {
190
+ args := []string {"wait" , "--for=delete" , resourceType , resourceName }
191
+ args = append (args , extraArgs ... )
192
+ args = append (args , "--timeout=30s" )
193
+ cmd := exec .CommandContext (ctx , client , args ... )
194
+ output , err := cmd .CombinedOutput ()
195
+ if err != nil {
196
+ return fmt .Errorf ("error waiting for deletion of %s %s: %v, output: %s" , resourceType , resourceName , err , string (output ))
175
197
}
198
+ return nil
176
199
}
177
200
178
201
// getComponentNamespace returns the namespace where operator-controller or catalogd is running
0 commit comments