@@ -11,6 +11,7 @@ import (
1111 "net/http"
1212 "os"
1313 "strconv"
14+ "strings"
1415 "testing"
1516 "time"
1617
@@ -23,6 +24,7 @@ import (
2324 "k8s.io/client-go/kubernetes"
2425 "k8s.io/client-go/tools/clientcmd"
2526
27+ "github.com/hetznercloud/hcloud-cloud-controller-manager/internal/hcops"
2628 "github.com/hetznercloud/hcloud-cloud-controller-manager/internal/testsupport"
2729 "github.com/hetznercloud/hcloud-cloud-controller-manager/internal/utils"
2830 "github.com/hetznercloud/hcloud-go/v2/hcloud"
@@ -52,7 +54,7 @@ type TestCluster struct {
5254 certDomain string
5355
5456 certificates * utils.SyncSet [int64 ]
55- loadBalancers * utils.SyncSet [int64 ]
57+ loadBalancers * utils.SyncSet [string ]
5658}
5759
5860func (tc * TestCluster ) Start () error {
@@ -103,7 +105,7 @@ func (tc *TestCluster) Start() error {
103105 tc .certDomain = os .Getenv ("CERT_DOMAIN" )
104106
105107 tc .certificates = utils .NewSyncSet [int64 ]()
106- tc .loadBalancers = utils .NewSyncSet [int64 ]()
108+ tc .loadBalancers = utils .NewSyncSet [string ]()
107109
108110 return nil
109111}
@@ -112,10 +114,20 @@ func (tc *TestCluster) Stop() error {
112114 errs := make ([]error , 0 , tc .loadBalancers .Size ()+ tc .certificates .Size ())
113115 ctx := context .Background ()
114116
115- for _ , item := range tc .loadBalancers .All () {
116- fmt .Printf ("deleting load balancer %d\n " , item )
117- if _ , err := tc .hcloud .LoadBalancer .Delete (ctx , & hcloud.LoadBalancer {ID : item }); err != nil {
118- errs = append (errs , fmt .Errorf ("delete load balancer %d failed: %w" , item , err ))
117+ uids := tc .loadBalancers .All ()
118+ selector := fmt .Sprintf ("%s in (%s)" , hcops .LabelServiceUID , strings .Join (uids , "," ))
119+ lbs , err := tc .hcloud .LoadBalancer .AllWithOpts (ctx , hcloud.LoadBalancerListOpts {
120+ ListOpts : hcloud.ListOpts {
121+ LabelSelector : selector ,
122+ },
123+ })
124+ if err != nil {
125+ return fmt .Errorf ("fetching load balancers via selector %s: %w" , selector , err )
126+ }
127+ for _ , lb := range lbs {
128+ fmt .Printf ("force-deleting leaked load balancer %d (%s)\n " , lb .ID , lb .Name )
129+ if _ , err := tc .hcloud .LoadBalancer .Delete (ctx , lb ); err != nil {
130+ errs = append (errs , fmt .Errorf ("delete leaked load balancer %d failed: %w" , lb .ID , err ))
119131 }
120132 }
121133
@@ -288,6 +300,8 @@ func (l *lbTestHelper) CreateService(lbSvc *corev1.Service) (*corev1.Service, er
288300 return nil , fmt .Errorf ("could not create service: %w" , err )
289301 }
290302
303+ testCluster .loadBalancers .Add (string (lbSvc .UID ))
304+
291305 ctx , cancel := context .WithTimeout (l .t .Context (), 8 * time .Minute )
292306 defer cancel ()
293307
0 commit comments