|
| 1 | +// Copyright Contributors to the Open Cluster Management project |
| 2 | +package clusterset |
| 3 | + |
| 4 | +import ( |
| 5 | + "context" |
| 6 | + "fmt" |
| 7 | + |
| 8 | + "github.com/spf13/cobra" |
| 9 | + "k8s.io/apimachinery/pkg/api/errors" |
| 10 | + metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" |
| 11 | + "k8s.io/apimachinery/pkg/watch" |
| 12 | + clusterclientset "open-cluster-management.io/api/client/cluster/clientset/versioned" |
| 13 | + "open-cluster-management.io/clusteradm/pkg/helpers" |
| 14 | +) |
| 15 | + |
| 16 | +func (o *Options) complete(cmd *cobra.Command, args []string) (err error) { |
| 17 | + |
| 18 | + o.Clustersets = args |
| 19 | + |
| 20 | + return nil |
| 21 | +} |
| 22 | + |
| 23 | +func (o *Options) validate() (err error) { |
| 24 | + if len(o.Clustersets) == 0 { |
| 25 | + return fmt.Errorf("the name of the clusterset must be specified") |
| 26 | + } |
| 27 | + if len(o.Clustersets) > 1 { |
| 28 | + return fmt.Errorf("only one clusterset can be deleted") |
| 29 | + } |
| 30 | + return nil |
| 31 | +} |
| 32 | + |
| 33 | +func (o *Options) run() (err error) { |
| 34 | + restConfig, err := o.ClusteradmFlags.KubectlFactory.ToRESTConfig() |
| 35 | + if err != nil { |
| 36 | + return err |
| 37 | + } |
| 38 | + clusterClient, err := clusterclientset.NewForConfig(restConfig) |
| 39 | + if err != nil { |
| 40 | + return err |
| 41 | + } |
| 42 | + |
| 43 | + clusterSetName := o.Clustersets[0] |
| 44 | + |
| 45 | + return o.runWithClient(clusterClient, o.ClusteradmFlags.DryRun, clusterSetName) |
| 46 | +} |
| 47 | + |
| 48 | +// check unband first |
| 49 | + |
| 50 | +func (o *Options) runWithClient(clusterClient clusterclientset.Interface, |
| 51 | + dryRun bool, |
| 52 | + clusterset string) error { |
| 53 | + |
| 54 | + // check existing |
| 55 | + _, err := clusterClient.ClusterV1beta1().ManagedClusterSets().Get(context.TODO(), clusterset, metav1.GetOptions{}) |
| 56 | + if err != nil { |
| 57 | + if errors.IsNotFound(err) { |
| 58 | + fmt.Fprintf(o.Streams.Out, "Clusterset %s is already deleted\n", clusterset) |
| 59 | + return nil |
| 60 | + } |
| 61 | + return err |
| 62 | + } |
| 63 | + |
| 64 | + // check binding |
| 65 | + list, err := clusterClient.ClusterV1beta1().ManagedClusterSetBindings(metav1.NamespaceAll).List(context.TODO(), metav1.ListOptions{ |
| 66 | + FieldSelector: fmt.Sprintf("metadata.name=%s", clusterset), |
| 67 | + }) |
| 68 | + // if exist, return |
| 69 | + if err == nil && len(list.Items) != 0 { |
| 70 | + fmt.Fprintf(o.Streams.Out, "Clusterset %s still bind to a namespace! Please unbind before deleted.\n", clusterset) |
| 71 | + return nil |
| 72 | + } |
| 73 | + if err != nil && !errors.IsNotFound(err) { |
| 74 | + return err |
| 75 | + } |
| 76 | + |
| 77 | + // start a goroutine to watch the delete event |
| 78 | + errChannel := make(chan error) |
| 79 | + go func(c chan<- error) { |
| 80 | + // watch until clusterset is removed |
| 81 | + e := helpers.WatchUntil( |
| 82 | + func() (watch.Interface, error) { |
| 83 | + return clusterClient.ClusterV1beta1().ManagedClusterSets().Watch(context.TODO(), metav1.ListOptions{ |
| 84 | + FieldSelector: fmt.Sprintf("metadata.name=%s", clusterset), |
| 85 | + }) |
| 86 | + }, |
| 87 | + func(event watch.Event) bool { |
| 88 | + return event.Type == watch.Deleted |
| 89 | + }, |
| 90 | + ) |
| 91 | + c <- e |
| 92 | + |
| 93 | + }(errChannel) |
| 94 | + |
| 95 | + // delete |
| 96 | + err = clusterClient.ClusterV1beta1().ManagedClusterSets().Delete(context.TODO(), clusterset, metav1.DeleteOptions{}) |
| 97 | + if err != nil { |
| 98 | + return err |
| 99 | + } |
| 100 | + |
| 101 | + // handle the error of watch function |
| 102 | + if err = <-errChannel; err != nil { |
| 103 | + return err |
| 104 | + } |
| 105 | + |
| 106 | + fmt.Fprintf(o.Streams.Out, "Clusterset %s is deleted\n", clusterset) |
| 107 | + return nil |
| 108 | +} |
0 commit comments