66 "io"
77 "os"
88 "path"
9+ "slices"
910 "strings"
1011
1112 . "github.com/onsi/ginkgo/v2" //nolint:staticcheck
@@ -254,6 +255,10 @@ func (ee *E2EContext) TeardownMetalStackProject(ctx context.Context) {
254255 }
255256 }
256257
258+ isE2ETestTag := func (tag string ) bool {
259+ return strings .HasPrefix (tag , capmsv1alpha1 .TagInfraClusterResource + "=" + ee .projectNamespacePrefix ())
260+ }
261+
257262 By ("Cleanup all remaining machines" )
258263 ms , err := ee .Environment .Metal .Machine ().FindMachines (machine .NewFindMachinesParamsWithContext (ctx ).WithBody (& models.V1MachineFindRequest {
259264 PartitionID : ee .Environment .partition ,
@@ -262,6 +267,9 @@ func (ee *E2EContext) TeardownMetalStackProject(ctx context.Context) {
262267 Expect (err ).ToNot (HaveOccurred (), "failed to list metal machines for project" )
263268
264269 for _ , m := range ms .Payload {
270+ if ! slices .ContainsFunc (m .Tags , isE2ETestTag ) {
271+ continue
272+ }
265273 By (fmt .Sprintf ("Freeing machine %s" , * m .ID ))
266274 _ , err := ee .Environment .Metal .Machine ().FreeMachine (machine .NewFreeMachineParamsWithContext (ctx ).WithID (* m .ID ), nil )
267275 Expect (err ).ToNot (HaveOccurred (), fmt .Sprintf ("failed to free metal machine %s" , * m .ID ))
@@ -274,6 +282,9 @@ func (ee *E2EContext) TeardownMetalStackProject(ctx context.Context) {
274282 Expect (err ).ToNot (HaveOccurred (), "failed to list metal IPs for project" )
275283
276284 for _ , addr := range ips .Payload {
285+ if ! slices .ContainsFunc (addr .Tags , isE2ETestTag ) {
286+ continue
287+ }
277288 _ , err := ee .Environment .Metal .IP ().FreeIP (ip .NewFreeIPParamsWithContext (ctx ).WithID (* addr .Ipaddress ), nil )
278289 Expect (err ).ToNot (HaveOccurred (), fmt .Sprintf ("failed to free metal IP %s" , * addr .Ipaddress ))
279290 }
@@ -285,12 +296,18 @@ func (ee *E2EContext) TeardownMetalStackProject(ctx context.Context) {
285296 Expect (err ).ToNot (HaveOccurred (), "failed to list metal networks for project" )
286297
287298 for _ , net := range nets .Payload {
299+ if label , ok := net .Labels [capmsv1alpha1 .TagInfraClusterResource ]; ! ok || ! strings .HasPrefix (label , ee .projectNamespacePrefix ()) {
300+ continue
301+ }
288302 _ , err := ee .Environment .Metal .Network ().FreeNetwork (network .NewFreeNetworkParamsWithContext (ctx ).WithID (* net .ID ), nil )
289303 Expect (err ).ToNot (HaveOccurred (), fmt .Sprintf ("failed to free metal network %s" , * net .ID ))
290304 }
291305
292306 By ("Wait for all machines to be waiting" )
293307 for _ , m := range allMachinesInUse .Payload {
308+ if ! slices .ContainsFunc (m .Tags , isE2ETestTag ) {
309+ continue
310+ }
294311 By (fmt .Sprintf ("Waiting for machine %s" , * m .ID ))
295312 Eventually (ctx , func (g Gomega ) {
296313 mr , err := ee .Environment .Metal .Machine ().FindMachine (machine .NewFindMachineParamsWithContext (ctx ).WithID (* m .ID ), nil )
0 commit comments