From 1928222278cb34228a9b7b1c87503edaf505b015 Mon Sep 17 00:00:00 2001 From: Mathieu Tortuyaux Date: Mon, 20 Nov 2023 12:23:38 +0100 Subject: [PATCH] platform/brightbox: remove cloud ips from ore Garbage collection can't handle yet the removal of cloud IPs - we delete unmapped cloud IPs from ore. Signed-off-by: Mathieu Tortuyaux --- cmd/ore/brightbox/remove-cloudips.go | 30 ++++++++++++++++++++++++++++ platform/api/brightbox/api.go | 25 +++++++++++++++++++++++ 2 files changed, 55 insertions(+) create mode 100644 cmd/ore/brightbox/remove-cloudips.go diff --git a/cmd/ore/brightbox/remove-cloudips.go b/cmd/ore/brightbox/remove-cloudips.go new file mode 100644 index 000000000..04662a436 --- /dev/null +++ b/cmd/ore/brightbox/remove-cloudips.go @@ -0,0 +1,30 @@ +// Copyright The Mantle Authors. +// SPDX-License-Identifier: Apache-2.0 +package brightbox + +import ( + "fmt" + + "github.com/spf13/cobra" +) + +var ( + cmdRemoveCloudIPs = &cobra.Command{ + Use: "remove-ips", + Short: "Remove any remaining cloud IPs", + Long: `Remove left overs IP from previous garbage collection`, + RunE: removeCloudIPs, + } +) + +func init() { + Brightbox.AddCommand(cmdRemoveCloudIPs) +} + +func removeCloudIPs(cmd *cobra.Command, args []string) error { + if err := API.RemoveCloudIPs(); err != nil { + return fmt.Errorf("removing cloud IPs: %w", err) + } + + return nil +} diff --git a/platform/api/brightbox/api.go b/platform/api/brightbox/api.go index b4ee7de4b..63f79d0e2 100644 --- a/platform/api/brightbox/api.go +++ b/platform/api/brightbox/api.go @@ -11,6 +11,7 @@ import ( brightbox "github.com/brightbox/gobrightbox/v2" "github.com/brightbox/gobrightbox/v2/clientcredentials" "github.com/brightbox/gobrightbox/v2/enums/arch" + "github.com/brightbox/gobrightbox/v2/enums/cloudipstatus" "github.com/brightbox/gobrightbox/v2/enums/imagestatus" "github.com/brightbox/gobrightbox/v2/enums/serverstatus" @@ -161,6 +162,8 @@ func (a *API) GC(gracePeriod time.Duration) error { threshold := time.Now().Add(-gracePeriod) // TODO: CloudIP has no creation date for now. // We can't safely delete "old" cloud IPs. + // NOTE: Currently, cloud IPs removal is implemented as an independant + // 'ore' subcommand. servers, err := a.client.Servers(context.TODO()) if err != nil { @@ -232,3 +235,25 @@ func (a *API) UploadImage(name, URL string) (string, error) { return img.ID, nil } + +// RemoveCloudIPs remove any left overs IPs. +func (a *API) RemoveCloudIPs() error { + cloudIPs, err := a.client.CloudIPs(context.TODO()) + if err != nil { + return fmt.Errorf("getting cloud IPs: %w", err) + } + + for _, cloudIP := range cloudIPs { + // Do not remove a mapped cloud IP - otherwise, we can end up + // with a server without public IP. + if cloudIP.Status == cloudipstatus.Mapped { + continue + } + + if err := a.DeleteCloudIP(cloudIP.ID); err != nil { + return fmt.Errorf("deleting cloud IP: %w", err) + } + } + + return nil +}