Merge pull request #16231 from hakman/cluster-deletion-configurable

Make cluster deletion configurable
This commit is contained in:
Kubernetes Prow Robot 2024-01-07 09:10:52 +01:00 committed by GitHub
commit dc829682bd
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 30 additions and 10 deletions

View File

@ -20,6 +20,7 @@ import (
"context"
"fmt"
"io"
"time"
"github.com/spf13/cobra"
"k8s.io/client-go/tools/clientcmd"
@ -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 (
@ -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]",
@ -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
}
@ -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
}

View File

@ -24,10 +24,13 @@ kops delete cluster [CLUSTER] [flags]
### Options
```
--count int Number of consecutive failures to make progress deleting the cluster resources (default 42)
--external Delete an external cluster
-h, --help help for cluster
--interval duration Time in duration to wait between deletion attempts (default 10s)
--region string External cluster's cloud region
--unregister Don't delete cloud resources, just unregister the cluster
--wait duration Amount of time to wait for the cluster resources to de deleted
-y, --yes Specify --yes to delete the cluster
```

View File

@ -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)
@ -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)
@ -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)
}
}