Skip to content

Commit

Permalink
Merge pull request #16231 from hakman/cluster-deletion-configurable
Browse files Browse the repository at this point in the history
Make cluster deletion configurable
  • Loading branch information
k8s-ci-robot authored Jan 7, 2024
2 parents 0162f39 + 03c109b commit dc82968
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 10 deletions.
16 changes: 15 additions & 1 deletion cmd/kops/delete_cluster.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import (
"context"
"fmt"
"io"
"time"

"github.com/spf13/cobra"
"k8s.io/client-go/tools/clientcmd"
Expand All @@ -44,6 +45,14 @@ type DeleteClusterOptions struct {
External bool
Unregister bool
ClusterName string
wait time.Duration
count int
interval time.Duration
}

func (o *DeleteClusterOptions) InitDefaults() {
o.count = 42
o.interval = 10 * time.Second
}

var (
Expand All @@ -64,6 +73,7 @@ var (

func NewCmdDeleteCluster(f *util.Factory, out io.Writer) *cobra.Command {
options := &DeleteClusterOptions{}
options.InitDefaults()

cmd := &cobra.Command{
Use: "cluster [CLUSTER]",
Expand All @@ -84,6 +94,10 @@ func NewCmdDeleteCluster(f *util.Factory, out io.Writer) *cobra.Command {
cmd.Flags().StringVar(&options.Region, "region", options.Region, "External cluster's cloud region")
cmd.RegisterFlagCompletionFunc("region", completeRegion)

cmd.Flags().DurationVar(&options.wait, "wait", options.wait, "Amount of time to wait for the cluster resources to de deleted")
cmd.Flags().IntVar(&options.count, "count", options.count, "Number of consecutive failures to make progress deleting the cluster resources")
cmd.Flags().DurationVar(&options.interval, "interval", options.interval, "Time in duration to wait between deletion attempts")

return cmd
}

Expand Down Expand Up @@ -171,7 +185,7 @@ func RunDeleteCluster(ctx context.Context, f *util.Factory, out io.Writer, optio

fmt.Fprintf(out, "\n")

err = resourceops.DeleteResources(cloud, clusterResources)
err = resourceops.DeleteResources(cloud, clusterResources, options.count, options.interval, options.wait)
if err != nil {
return err
}
Expand Down
13 changes: 8 additions & 5 deletions docs/cli/kops_delete_cluster.md

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

11 changes: 7 additions & 4 deletions pkg/resources/ops/delete.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ import (
)

// DeleteResources deletes the resources, as previously collected by ListResources
func DeleteResources(cloud fi.Cloud, resourceMap map[string]*resources.Resource) error {
func DeleteResources(cloud fi.Cloud, resourceMap map[string]*resources.Resource, count int, interval, wait time.Duration) error {
depMap := make(map[string][]string)

done := make(map[string]*resources.Resource)
Expand All @@ -52,9 +52,12 @@ func DeleteResources(cloud fi.Cloud, resourceMap map[string]*resources.Resource)
klog.V(2).Infof("\t%s\t%v", k, v)
}

timeout := time.Now().Add(wait)
iterationsWithNoProgress := 0
for {
// TODO: Some form of default ordering based on types?
if wait > 0 && time.Now().After(timeout) {
return fmt.Errorf("wait time exceeded during resources deletion")
}

failed := make(map[string]*resources.Resource)

Expand Down Expand Up @@ -167,10 +170,10 @@ func DeleteResources(cloud fi.Cloud, resourceMap map[string]*resources.Resource)
}

iterationsWithNoProgress++
if iterationsWithNoProgress > 42 {
if iterationsWithNoProgress > count && count != 0 {
return fmt.Errorf("not making progress deleting resources; giving up")
}

time.Sleep(10 * time.Second)
time.Sleep(interval)
}
}

0 comments on commit dc82968

Please sign in to comment.