From 84ecddee477c329d5041b1b7347af6d755cd1aa7 Mon Sep 17 00:00:00 2001 From: ycyaoxdu Date: Wed, 9 Mar 2022 17:40:25 +0800 Subject: [PATCH] feat: add command delete work to help delete manifest work from specified cluster Signed-off-by: ycyaoxdu --- pkg/cmd/delete/cmd.go | 2 + pkg/cmd/delete/work/cmd.go | 49 ++++++++++++++++++++++ pkg/cmd/delete/work/exec.go | 74 ++++++++++++++++++++++++++++++++++ pkg/cmd/delete/work/options.go | 26 ++++++++++++ 4 files changed, 151 insertions(+) create mode 100644 pkg/cmd/delete/work/cmd.go create mode 100644 pkg/cmd/delete/work/exec.go create mode 100644 pkg/cmd/delete/work/options.go diff --git a/pkg/cmd/delete/cmd.go b/pkg/cmd/delete/cmd.go index 6bee6f0ba..d7166291d 100644 --- a/pkg/cmd/delete/cmd.go +++ b/pkg/cmd/delete/cmd.go @@ -5,6 +5,7 @@ import ( "github.com/spf13/cobra" "k8s.io/cli-runtime/pkg/genericclioptions" "open-cluster-management.io/clusteradm/pkg/cmd/delete/token" + "open-cluster-management.io/clusteradm/pkg/cmd/delete/work" genericclioptionsclusteradm "open-cluster-management.io/clusteradm/pkg/genericclioptions" ) @@ -16,6 +17,7 @@ func NewCmd(clusteradmFlags *genericclioptionsclusteradm.ClusteradmFlags, stream } cmd.AddCommand(token.NewCmd(clusteradmFlags, streams)) + cmd.AddCommand(work.NewCmd(clusteradmFlags, streams)) return cmd } diff --git a/pkg/cmd/delete/work/cmd.go b/pkg/cmd/delete/work/cmd.go new file mode 100644 index 000000000..f257b625d --- /dev/null +++ b/pkg/cmd/delete/work/cmd.go @@ -0,0 +1,49 @@ +// Copyright Contributors to the Open Cluster Management project +package work + +import ( + "fmt" + + "open-cluster-management.io/clusteradm/pkg/helpers" + + "github.com/spf13/cobra" + "k8s.io/cli-runtime/pkg/genericclioptions" + genericclioptionsclusteradm "open-cluster-management.io/clusteradm/pkg/genericclioptions" +) + +var example = ` +# Delete work in specified cluster +%[1]s delete work work-example --cluster cluster1 +` + +// NewCmd ... +func NewCmd(clusteradmFlags *genericclioptionsclusteradm.ClusteradmFlags, streams genericclioptions.IOStreams) *cobra.Command { + o := newOptions(clusteradmFlags, streams) + + cmd := &cobra.Command{ + Use: "work", + Short: "delete work in specified cluster", + Example: fmt.Sprintf(example, helpers.GetExampleHeader()), + SilenceUsage: true, + PreRun: func(c *cobra.Command, args []string) { + helpers.DryRunMessage(o.ClusteradmFlags.DryRun) + }, + 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 + }, + } + + cmd.Flags().StringVar(&o.Cluster, "cluster", "", "Names of the managed cluster applied work") + + return cmd +} diff --git a/pkg/cmd/delete/work/exec.go b/pkg/cmd/delete/work/exec.go new file mode 100644 index 000000000..2853c5700 --- /dev/null +++ b/pkg/cmd/delete/work/exec.go @@ -0,0 +1,74 @@ +// Copyright Contributors to the Open Cluster Management project +package work + +import ( + "context" + "fmt" + + "k8s.io/apimachinery/pkg/watch" + + "k8s.io/apimachinery/pkg/api/errors" + metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" + workclientset "open-cluster-management.io/api/client/work/clientset/versioned" + "open-cluster-management.io/clusteradm/pkg/helpers" + + "github.com/spf13/cobra" +) + +func (o *Options) complete(cmd *cobra.Command, args []string) (err error) { + if len(args) == 0 { + return fmt.Errorf("work name must be specified") + } + + if len(args) > 1 { + return fmt.Errorf("only one work name can be specified") + } + + o.Workname = args[0] + + return nil +} + +func (o *Options) validate() error { + if len(o.Cluster) == 0 { + return fmt.Errorf("the name of the cluster must be specified") + } + + return nil +} + +func (o *Options) run() error { + restConfig, err := o.ClusteradmFlags.KubectlFactory.ToRESTConfig() + if err != nil { + return err + } + workClient, err := workclientset.NewForConfig(restConfig) + if err != nil { + return err + } + + return o.deleteWork(workClient) +} + +func (o *Options) deleteWork(workClient *workclientset.Clientset) error { + err := workClient.WorkV1().ManifestWorks(o.Cluster).Delete(context.TODO(), o.Workname, metav1.DeleteOptions{}) + if err != nil && errors.IsNotFound(err) { + return err + } + + // watch until work is fully removed + err = helpers.WatchUntil( + func() (watch.Interface, error) { + return workClient.WorkV1().ManifestWorks(o.Cluster).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, "work %s in cluster %s is deleted\n", o.Workname, o.Cluster) + return nil +} diff --git a/pkg/cmd/delete/work/options.go b/pkg/cmd/delete/work/options.go new file mode 100644 index 000000000..ac24581a0 --- /dev/null +++ b/pkg/cmd/delete/work/options.go @@ -0,0 +1,26 @@ +// Copyright Contributors to the Open Cluster Management project +package work + +import ( + "k8s.io/cli-runtime/pkg/genericclioptions" + genericclioptionsclusteradm "open-cluster-management.io/clusteradm/pkg/genericclioptions" +) + +//Options: The structure holding all the command-line options +type Options struct { + //ClusteradmFlags: The generic optiosn from the clusteradm cli-runtime. + ClusteradmFlags *genericclioptionsclusteradm.ClusteradmFlags + + Streams genericclioptions.IOStreams + + Cluster string + + Workname string +} + +func newOptions(clusteradmFlags *genericclioptionsclusteradm.ClusteradmFlags, streams genericclioptions.IOStreams) *Options { + return &Options{ + ClusteradmFlags: clusteradmFlags, + Streams: streams, + } +}