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 @@ -104,6 +104,25 @@ func main() {
}
}

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

capdoStr := "capdo" // using this for repeated comparisons in the loop below
for _, key := range keys {
// we only care to cleanup keys that start with "capdo"
if len(key.Name) >= len(capdoStr) && key.Name[0:len(capdoStr)] == capdoStr {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

the ssh keys we generate start with capdo- how about we do a simple check with strings.HasPrefix(key.Name, "capdo-")

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Even better! Will update shortly

log.Printf("%s contains capdo at the start of it's name 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
}