Skip to content

Commit

Permalink
feat: add command delete clusterset
Browse files Browse the repository at this point in the history
Signed-off-by: ycyaoxdu <yaoyuchen0626@163.com>
  • Loading branch information
ycyaoxdu committed Mar 15, 2022
1 parent 7d3591c commit dd949b6
Show file tree
Hide file tree
Showing 4 changed files with 171 additions and 0 deletions.
49 changes: 49 additions & 0 deletions pkg/cmd/delete/clusterset/cmd.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
// Copyright Contributors to the Open Cluster Management project
package clusterset

import (
"fmt"

genericclioptionsclusteradm "open-cluster-management.io/clusteradm/pkg/genericclioptions"
clusteradmhelpers "open-cluster-management.io/clusteradm/pkg/helpers"

"github.com/spf13/cobra"
"k8s.io/cli-runtime/pkg/genericclioptions"
)

var example = `
# delete a clusterset.
%[1]s delete clusterset clusterset1
`

// NewCmd...
func NewCmd(clusteradmFlags *genericclioptionsclusteradm.ClusteradmFlags, streams genericclioptions.IOStreams) *cobra.Command {
o := newOptions(clusteradmFlags, streams)

cmd := &cobra.Command{
Use: "clusterset",
Short: "delete a clusterset",
Example: fmt.Sprintf(example, clusteradmhelpers.GetExampleHeader()),
SilenceUsage: true,
PreRunE: func(c *cobra.Command, args []string) error {
clusteradmhelpers.DryRunMessage(clusteradmFlags.DryRun)

return nil
},
RunE: func(c *cobra.Command, args []string) error {
if err := o.complete(c, args); err != nil {
return err
}
if err := o.validate(); err != nil {
return err
}
if err := o.run(); err != nil {
return err
}

return nil
},
}

return cmd
}
96 changes: 96 additions & 0 deletions pkg/cmd/delete/clusterset/exec.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
// Copyright Contributors to the Open Cluster Management project
package clusterset

import (
"context"
"fmt"

"github.com/spf13/cobra"
"k8s.io/apimachinery/pkg/api/errors"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/watch"
clusterclientset "open-cluster-management.io/api/client/cluster/clientset/versioned"
"open-cluster-management.io/clusteradm/pkg/helpers"
)

func (o *Options) complete(cmd *cobra.Command, args []string) (err error) {

o.Clustersets = args

return nil
}

func (o *Options) validate() (err error) {
if len(o.Clustersets) == 0 {
return fmt.Errorf("the name of the clusterset must be specified")
}
if len(o.Clustersets) > 1 {
return fmt.Errorf("only one clusterset can be deleted")
}
return nil
}

func (o *Options) run() (err error) {
restConfig, err := o.ClusteradmFlags.KubectlFactory.ToRESTConfig()
if err != nil {
return err
}
clusterClient, err := clusterclientset.NewForConfig(restConfig)
if err != nil {
return err
}

clusterSetName := o.Clustersets[0]

return o.runWithClient(clusterClient, o.ClusteradmFlags.DryRun, clusterSetName)
}

// check unband first

func (o *Options) runWithClient(clusterClient clusterclientset.Interface,
dryRun bool,
clusterset string) error {

// check existing
_, err := clusterClient.ClusterV1beta1().ManagedClusterSets().Get(context.TODO(), clusterset, metav1.GetOptions{})
if err != nil {
if errors.IsNotFound(err) {
fmt.Fprintf(o.Streams.Out, "Clusterset %s is already deleted\n", clusterset)
return nil
}
return err
}

// check binding
list, err := clusterClient.ClusterV1beta1().ManagedClusterSetBindings(metav1.NamespaceAll).List(context.TODO(), metav1.ListOptions{})
// if exist, return
if err == nil && len(list.Items) != 0 {
fmt.Fprintf(o.Streams.Out, "Clusterset %s still bind to a namespace! Please unbind before deleted.\n", clusterset)
return nil
}
if err != nil && !errors.IsNotFound(err) {
return err
}

// delete
err = clusterClient.ClusterV1beta1().ManagedClusterSets().Delete(context.TODO(), clusterset, metav1.DeleteOptions{})
if err != nil {
return err
}

// watch until clusterset is removed
err = helpers.WatchUntil(
func() (watch.Interface, error) {
return clusterClient.ClusterV1beta1().ManagedClusterSets().Watch(context.TODO(), metav1.ListOptions{})
},
func(event watch.Event) bool {
return event.Type == watch.Deleted
},
)
if err != nil {
return err
}

fmt.Fprintf(o.Streams.Out, "Clusterset %s is deleted\n", clusterset)
return nil
}
24 changes: 24 additions & 0 deletions pkg/cmd/delete/clusterset/options.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
// Copyright Contributors to the Open Cluster Management project
package clusterset

import (
"k8s.io/cli-runtime/pkg/genericclioptions"
genericclioptionsclusteradm "open-cluster-management.io/clusteradm/pkg/genericclioptions"
)

type Options struct {
//ClusteradmFlags: The generic optiosn from the clusteradm cli-runtime.
ClusteradmFlags *genericclioptionsclusteradm.ClusteradmFlags

Streams genericclioptions.IOStreams

Clustersets []string
}

func newOptions(clusteradmFlags *genericclioptionsclusteradm.ClusteradmFlags, streams genericclioptions.IOStreams) *Options {
return &Options{
ClusteradmFlags: clusteradmFlags,
Streams: streams,
Clustersets: []string{},
}
}
2 changes: 2 additions & 0 deletions pkg/cmd/delete/cmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ package get
import (
"github.com/spf13/cobra"
"k8s.io/cli-runtime/pkg/genericclioptions"
"open-cluster-management.io/clusteradm/pkg/cmd/delete/clusterset"
"open-cluster-management.io/clusteradm/pkg/cmd/delete/token"
genericclioptionsclusteradm "open-cluster-management.io/clusteradm/pkg/genericclioptions"
)
Expand All @@ -16,6 +17,7 @@ func NewCmd(clusteradmFlags *genericclioptionsclusteradm.ClusteradmFlags, stream
}

cmd.AddCommand(token.NewCmd(clusteradmFlags, streams))
cmd.AddCommand(clusterset.NewCmd(clusteradmFlags, streams))

return cmd
}

0 comments on commit dd949b6

Please sign in to comment.