Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

clean up ssh keys when cleaning up e2e test resources #380

47 changes: 47 additions & 0 deletions hack/do-janitor/do-janitor.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import (
"context"
"log"
"os"
"strings"
"time"

"github.com/digitalocean/godo"
Expand Down Expand Up @@ -104,6 +105,24 @@ func main() {
}
}

keys, err := keyList(ctx, client)
if err != nil {
log.Fatalf("failed to list keys: %+v", err.Error())
}

for _, key := range keys {
if strings.HasPrefix(key.Name, "capdo-") {
log.Printf("%s contains capdo prefix so it's safe to terminate\n", key.Name)
_, err := client.Keys.DeleteByID(ctx, key.ID)
if err != nil {
log.Printf("failed to delete key %s: %+v\n", key.Name, err.Error())
continue
}

log.Printf("key %s terminated\n", key.Name)
}
}

log.Println("Completed DO Janitor")
os.Exit(0)
}
Expand Down Expand Up @@ -190,3 +209,31 @@ func volumeList(ctx context.Context, client *godo.Client) ([]godo.Volume, error)

return list, nil
}

func keyList(ctx context.Context, client *godo.Client) ([]godo.Key, error) {
list := []godo.Key{}

// create options. initially, these will be blank.
opt := &godo.ListOptions{}
for {
keys, resp, err := client.Keys.List(ctx, opt)
if err != nil {
return nil, err
}

list = append(list, keys...)

if resp.Links == nil || resp.Links.IsLastPage() {
break
}

page, err := resp.Links.CurrentPage()
if err != nil {
return nil, err
}

opt.Page = page + 1
}

return list, nil
}