Skip to content

Commit

Permalink
platform/brightbox: remove cloud ips from ore
Browse files Browse the repository at this point in the history
Garbage collection can't handle yet the removal of cloud IPs - we delete
unmapped cloud IPs from ore.

Signed-off-by: Mathieu Tortuyaux <mtortuyaux@microsoft.com>
  • Loading branch information
tormath1 committed Nov 20, 2023
1 parent fd7dd41 commit 1928222
Show file tree
Hide file tree
Showing 2 changed files with 55 additions and 0 deletions.
30 changes: 30 additions & 0 deletions cmd/ore/brightbox/remove-cloudips.go
Original file line number Diff line number Diff line change
@@ -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
}
25 changes: 25 additions & 0 deletions platform/api/brightbox/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"

Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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
}

0 comments on commit 1928222

Please sign in to comment.