@@ -20,6 +20,7 @@ import (
2020 "context"
2121 "errors"
2222 "fmt"
23+ "net/http"
2324
2425 ctrl "sigs.k8s.io/controller-runtime"
2526 "sigs.k8s.io/controller-runtime/pkg/client"
@@ -236,13 +237,22 @@ func (r *clusterReconciler) ensureControlPlaneIP() (string, error) {
236237}
237238
238239func (r * clusterReconciler ) deleteControlPlaneIP () error {
239- ip , err := r .findControlPlaneIP ()
240- if err != nil && errors .Is (err , errProviderIPNotFound ) {
240+ if r .infraCluster .Spec .ControlPlaneIP == nil {
241241 return nil
242242 }
243+
244+ resp , err := r .metalClient .IP ().FindIP (ipmodels .NewFindIPParams ().WithID (* r .infraCluster .Spec .ControlPlaneIP ).WithContext (r .ctx ), nil )
243245 if err != nil {
244- return fmt .Errorf ("unable to delete control plane ip: %w" , err )
246+ var r * ipmodels.FindIPDefault
247+ if errors .As (err , & r ) && r .Code () == http .StatusNotFound {
248+ return nil
249+ }
250+
251+ return err
245252 }
253+
254+ ip := resp .Payload
255+
246256 if ip .Type != nil && * ip .Type == models .V1IPBaseTypeStatic {
247257 r .log .Info ("skip deletion of static control plane ip" )
248258 return nil
@@ -252,46 +262,12 @@ func (r *clusterReconciler) deleteControlPlaneIP() error {
252262 return fmt .Errorf ("control plane ip address not set" )
253263 }
254264
255- if ip .Type != nil && * ip .Type == models .V1IPAllocateRequestTypeStatic {
256- r .log .Info ("skipping deletion of static control plane ip" , "ip" , * ip .Ipaddress )
257- return nil
258- }
259265 _ , err = r .metalClient .IP ().FreeIP (ipmodels .NewFreeIPParams ().WithID (* ip .Ipaddress ).WithContext (r .ctx ), nil )
260266 if err != nil {
261267 return err
262268 }
269+
263270 r .log .Info ("deleted control plane ip" , "address" , * ip .Ipaddress )
264271
265272 return nil
266273}
267-
268- func (r * clusterReconciler ) findControlPlaneIP () (* models.V1IPResponse , error ) {
269- if r .infraCluster .Spec .ControlPlaneIP != nil {
270- resp , err := r .metalClient .IP ().FindIP (ipmodels .NewFindIPParams ().WithID (* r .infraCluster .Spec .ControlPlaneIP ).WithContext (r .ctx ), nil )
271- if err != nil {
272- return nil , err
273- }
274-
275- return resp .Payload , nil
276- }
277-
278- resp , err := r .metalClient .IP ().FindIPs (ipmodels .NewFindIPsParams ().WithBody (& models.V1IPFindRequest {
279- Projectid : r .infraCluster .Spec .ProjectID ,
280- Tags : []string {
281- tag .New (tag .ClusterID , string (r .infraCluster .GetUID ())),
282- v1alpha1 .TagControlPlanePurpose ,
283- },
284- }).WithContext (r .ctx ), nil )
285- if err != nil {
286- return nil , err
287- }
288-
289- switch len (resp .Payload ) {
290- case 0 :
291- return nil , errProviderIPNotFound
292- case 1 :
293- return resp .Payload [0 ], nil
294- default :
295- return nil , errProviderIPTooManyFound
296- }
297- }
0 commit comments