Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add command delete work #149

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions pkg/cmd/delete/cmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)

Expand All @@ -16,6 +17,7 @@ func NewCmd(clusteradmFlags *genericclioptionsclusteradm.ClusteradmFlags, stream
}

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

return cmd
}
49 changes: 49 additions & 0 deletions pkg/cmd/delete/work/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 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
}
74 changes: 74 additions & 0 deletions pkg/cmd/delete/work/exec.go
Original file line number Diff line number Diff line change
@@ -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
}

ycyaoxdu marked this conversation as resolved.
Show resolved Hide resolved
// 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
}
26 changes: 26 additions & 0 deletions pkg/cmd/delete/work/options.go
Original file line number Diff line number Diff line change
@@ -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,
}
}