Skip to content

Commit

Permalink
Get clusters (#21)
Browse files Browse the repository at this point in the history
* Integrate get/config

Signed-off-by: Dominique Vernier <[email protected]>

* Add configoptions

Signed-off-by: Dominique Vernier <[email protected]>

* update go.mod

Signed-off-by: Dominique Vernier <[email protected]>
  • Loading branch information
itdove authored May 19, 2021
1 parent 5c5f408 commit be3bb81
Show file tree
Hide file tree
Showing 11 changed files with 201 additions and 38 deletions.
51 changes: 40 additions & 11 deletions cmd/cm.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,34 +6,63 @@ import (
"os"

"github.com/spf13/cobra"
"github.com/spf13/pflag"

"github.com/open-cluster-management/cm-cli/pkg/cmd/verbs"
"k8s.io/cli-runtime/pkg/genericclioptions"
"k8s.io/client-go/tools/clientcmd"
cliflag "k8s.io/component-base/cli/flag"
cmdconfig "k8s.io/kubectl/pkg/cmd/config"
"k8s.io/kubectl/pkg/cmd/options"
cmdutil "k8s.io/kubectl/pkg/cmd/util"
)

func main() {
flags := pflag.NewFlagSet("cm", pflag.ExitOnError)
pflag.CommandLine = flags
streams := genericclioptions.IOStreams{In: os.Stdin, Out: os.Stdout, ErrOut: os.Stderr}

// flags := pflag.NewFlagSet("cm", pflag.ExitOnError)
// pflag.CommandLine = flags

configFlags := genericclioptions.NewConfigFlags(true).WithDeprecatedPasswordFlag()
matchVersionKubeConfigFlags := cmdutil.NewMatchVersionFlags(configFlags)
f := cmdutil.NewFactory(matchVersionKubeConfigFlags)

root := newCmdCMVerbs(f, streams)

flags := root.PersistentFlags()
matchVersionKubeConfigFlags.AddFlags(flags)
flags.SetNormalizeFunc(cliflag.WarnWordSepNormalizeFunc) // Warn for "_" flags

// Normalize all flags that are coming from other packages or pre-configurations
// a.k.a. change all "_" to "-". e.g. glog package
flags.SetNormalizeFunc(cliflag.WordSepNormalizeFunc)
// From this point and forward we get warnings on flags that contain "_" separators
root.SetGlobalNormalizationFunc(cliflag.WarnWordSepNormalizeFunc)

configFlags.AddFlags(flags)
root.AddCommand(cmdconfig.NewCmdConfig(f, clientcmd.NewDefaultPathOptions(), streams))
// root.AddCommand(plugin.NewCmdPlugin(f, streams))
// root.AddCommand(version.NewCmdVersion(f, streams))
// root.AddCommand(apiresources.NewCmdAPIVersions(f, streams))
// root.AddCommand(apiresources.NewCmdAPIResources(f, streams))
root.AddCommand(options.NewCmdOptions(streams.Out))

root := newCmdCMVerbs(genericclioptions.IOStreams{In: os.Stdin, Out: os.Stdout, ErrOut: os.Stderr})
if err := root.Execute(); err != nil {
os.Exit(1)
}
}

// NewCmdNamespace provides a cobra command wrapping NamespaceOptions
func newCmdCMVerbs(streams genericclioptions.IOStreams) *cobra.Command {
func newCmdCMVerbs(f cmdutil.Factory, streams genericclioptions.IOStreams) *cobra.Command {
cmd := &cobra.Command{Use: "cm"}
cmd.AddCommand(
verbs.NewVerb("create", streams),
// verbs.NewVerb("get", streams),
verbs.NewVerb("create", f, streams),
verbs.NewVerb("get", f, streams),
// verbs.NewVerb("update", streams),
verbs.NewVerb("delete", streams),
verbs.NewVerb("delete", f, streams),
// verbs.NewVerb("list", streams),
verbs.NewVerb("applier", streams),
verbs.NewVerb("attach", streams),
verbs.NewVerb("detach", streams),
verbs.NewVerb("applier", f, streams),
verbs.NewVerb("attach", f, streams),
verbs.NewVerb("detach", f, streams),
)

return cmd
Expand Down
2 changes: 2 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -17,5 +17,7 @@ require (
k8s.io/apimachinery v0.20.5
k8s.io/cli-runtime v0.20.5
k8s.io/client-go v1.5.2 // indirect
k8s.io/component-base v0.20.1 // indirect
k8s.io/kubectl v0.20.1
sigs.k8s.io/controller-runtime v0.6.2
)
61 changes: 61 additions & 0 deletions go.sum

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion pkg/cmd/create/cluster/cmd.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
// Copyright Contributors to the Open Cluster Management project
package create
package cluster

import (
"fmt"
Expand Down
2 changes: 1 addition & 1 deletion pkg/cmd/create/cluster/exec.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
// Copyright Contributors to the Open Cluster Management project
package create
package cluster

import (
"context"
Expand Down
2 changes: 1 addition & 1 deletion pkg/cmd/create/cluster/exec_test.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
// Copyright Contributors to the Open Cluster Management project
package create
package cluster

import (
"path/filepath"
Expand Down
2 changes: 1 addition & 1 deletion pkg/cmd/create/cluster/options.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
// Copyright Contributors to the Open Cluster Management project
package create
package cluster

import (
"github.com/open-cluster-management/cm-cli/pkg/cmd/applierscenarios"
Expand Down
2 changes: 1 addition & 1 deletion pkg/cmd/create/cluster/options_test.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
// Copyright Contributors to the Open Cluster Management project
package create
package cluster

import (
"reflect"
Expand Down
67 changes: 67 additions & 0 deletions pkg/cmd/get/clusters/cmd.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
// Copyright Contributors to the Open Cluster Management project
package clusters

import (
"fmt"

"k8s.io/kubectl/pkg/cmd/get"
cmdutil "k8s.io/kubectl/pkg/cmd/util"

"github.com/open-cluster-management/cm-cli/pkg/helpers"
"github.com/spf13/cobra"
"k8s.io/cli-runtime/pkg/genericclioptions"
)

const (
example = `
# get a cluster
%[1]s clusters <cluster-name> -oyaml`
)

// NewCmd ...
func NewCmd(f cmdutil.Factory, streams genericclioptions.IOStreams) *cobra.Command {
o := get.NewGetOptions("cm", streams)
cmd := &cobra.Command{
Use: "clusters [(-o|--output=)json|yaml|wide|custom-columns=...|custom-columns-file=...|go-template=...|go-template-file=...|jsonpath=...|jsonpath-file=...] (TYPE[.VERSION][.GROUP] [NAME | -l label] | TYPE[.VERSION][.GROUP]/NAME ...) [flags]",
Aliases: []string{"cluster"},
DisableFlagsInUseLine: true,
Short: "Display one or many resources",
Example: fmt.Sprintf(example, helpers.GetExampleHeader()),
Run: func(cmd *cobra.Command, args []string) {
args = append([]string{"managedcluster"}, args...)
cmdutil.CheckErr(o.Complete(f, cmd, args))
cmdutil.CheckErr(o.Validate(cmd))
cmdutil.CheckErr(o.Run(f, cmd, args))
},
SuggestFor: []string{"list", "ps"},
}

o.PrintFlags.AddFlags(cmd)

cmd.Flags().BoolVarP(&o.Watch, "watch", "w", o.Watch, "After listing/getting the requested object, watch for changes. Uninitialized objects are excluded if no object name is provided.")
cmd.Flags().BoolVar(&o.WatchOnly, "watch-only", o.WatchOnly, "Watch for changes to the requested object(s), without listing/getting first.")
cmd.Flags().BoolVar(&o.OutputWatchEvents, "output-watch-events", o.OutputWatchEvents, "Output watch event objects when --watch or --watch-only is used. Existing objects are output as initial ADDED events.")
cmd.Flags().Int64Var(&o.ChunkSize, "chunk-size", o.ChunkSize, "Return large lists in chunks rather than all at once. Pass 0 to disable. This flag is beta and may change in the future.")
cmd.Flags().BoolVar(&o.IgnoreNotFound, "ignore-not-found", o.IgnoreNotFound, "If the requested object does not exist the command will return exit code 0.")
cmd.Flags().StringVarP(&o.LabelSelector, "selector", "l", o.LabelSelector, "Selector (label query) to filter on, supports '=', '==', and '!='.(e.g. -l key1=value1,key2=value2)")
cmd.Flags().StringVar(&o.FieldSelector, "field-selector", o.FieldSelector, "Selector (field query) to filter on, supports '=', '==', and '!='.(e.g. --field-selector key1=value1,key2=value2). The server only supports a limited number of field queries per type.")
cmd.Flags().BoolVarP(&o.AllNamespaces, "all-namespaces", "A", o.AllNamespaces, "If present, list the requested object(s) across all namespaces. Namespace in current context is ignored even if specified with --namespace.")
addOpenAPIPrintColumnFlags(cmd, o)
addServerPrintColumnFlags(cmd, o)
cmdutil.AddFilenameOptionFlags(cmd, &o.FilenameOptions, "identifying the resource to get from a server.")
return cmd
}

const (
useOpenAPIPrintColumnFlagLabel = "use-openapi-print-columns"
useServerPrintColumns = "server-print"
)

func addOpenAPIPrintColumnFlags(cmd *cobra.Command, opt *get.GetOptions) {
cmd.Flags().BoolVar(&opt.PrintWithOpenAPICols, useOpenAPIPrintColumnFlagLabel, opt.PrintWithOpenAPICols, "If true, use x-kubernetes-print-column metadata (if present) from the OpenAPI schema for displaying a resource.")
cmd.Flags().MarkDeprecated(useOpenAPIPrintColumnFlagLabel, "deprecated in favor of server-side printing")
}

func addServerPrintColumnFlags(cmd *cobra.Command, opt *get.GetOptions) {
cmd.Flags().BoolVar(&opt.ServerPrint, useServerPrintColumns, opt.ServerPrint, "If true, have the server return the appropriate table output. Supports extension APIs and CRDs.")
}
47 changes: 25 additions & 22 deletions pkg/cmd/verbs/verbs.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,35 +9,39 @@ import (
createcluster "github.com/open-cluster-management/cm-cli/pkg/cmd/create/cluster"
deletecluster "github.com/open-cluster-management/cm-cli/pkg/cmd/delete/cluster"
detachcluster "github.com/open-cluster-management/cm-cli/pkg/cmd/detach/cluster"
getclusters "github.com/open-cluster-management/cm-cli/pkg/cmd/get/clusters"
"github.com/spf13/cobra"

"k8s.io/cli-runtime/pkg/genericclioptions"

"k8s.io/kubectl/pkg/cmd/get"
cmdutil "k8s.io/kubectl/pkg/cmd/util"
)

//NewVerb creates a new verb
func NewVerb(verb string, streams genericclioptions.IOStreams) *cobra.Command {
func NewVerb(verb string, f cmdutil.Factory, streams genericclioptions.IOStreams) *cobra.Command {
switch verb {
case "create":
return newVerbCreate(verb, streams)
return newVerbCreate(verb, f, streams)
case "get":
return newVerbGet(verb, streams)
return newVerbGet(verb, f, streams)
case "update":
return newVerbUpdate(verb, streams)
return newVerbUpdate(verb, f, streams)
case "delete":
return newVerbDelete(verb, streams)
return newVerbDelete(verb, f, streams)
case "list":
return newVerbList(verb, streams)
return newVerbList(verb, f, streams)
case "attach":
return newVerbAttach(verb, streams)
return newVerbAttach(verb, f, streams)
case "applier":
return newVerbApplier(verb, streams)
return newVerbApplier(verb, f, streams)
case "detach":
return newVerbDetach(verb, streams)
return newVerbDetach(verb, f, streams)
}
panic(fmt.Sprintf("Unknow verb: %s", verb))
}

func newVerbCreate(verb string, streams genericclioptions.IOStreams) *cobra.Command {
func newVerbCreate(verb string, f cmdutil.Factory, streams genericclioptions.IOStreams) *cobra.Command {
cmd := &cobra.Command{
Use: verb,
}
Expand All @@ -48,16 +52,15 @@ func newVerbCreate(verb string, streams genericclioptions.IOStreams) *cobra.Comm
return cmd
}

func newVerbGet(verb string, streams genericclioptions.IOStreams) *cobra.Command {
cmd := &cobra.Command{
Use: verb,
Short: "Not yet implemented",
}

func newVerbGet(verb string, f cmdutil.Factory, streams genericclioptions.IOStreams) *cobra.Command {
cmd := get.NewCmdGet("cm", f, streams)
cmd.AddCommand(
getclusters.NewCmd(f, streams),
)
return cmd
}

func newVerbUpdate(verb string, streams genericclioptions.IOStreams) *cobra.Command {
func newVerbUpdate(verb string, f cmdutil.Factory, streams genericclioptions.IOStreams) *cobra.Command {
cmd := &cobra.Command{
Use: verb,
Short: "Not yet implemented",
Expand All @@ -66,7 +69,7 @@ func newVerbUpdate(verb string, streams genericclioptions.IOStreams) *cobra.Comm
return cmd
}

func newVerbDelete(verb string, streams genericclioptions.IOStreams) *cobra.Command {
func newVerbDelete(verb string, f cmdutil.Factory, streams genericclioptions.IOStreams) *cobra.Command {
cmd := &cobra.Command{
Use: verb,
}
Expand All @@ -77,7 +80,7 @@ func newVerbDelete(verb string, streams genericclioptions.IOStreams) *cobra.Comm
return cmd
}

func newVerbList(verb string, streams genericclioptions.IOStreams) *cobra.Command {
func newVerbList(verb string, f cmdutil.Factory, streams genericclioptions.IOStreams) *cobra.Command {
cmd := &cobra.Command{
Use: verb,
Short: "Not yet implemented",
Expand All @@ -86,13 +89,13 @@ func newVerbList(verb string, streams genericclioptions.IOStreams) *cobra.Comman
return cmd
}

func newVerbApplier(verb string, streams genericclioptions.IOStreams) *cobra.Command {
func newVerbApplier(verb string, f cmdutil.Factory, streams genericclioptions.IOStreams) *cobra.Command {
cmd := appliercmd.NewCmd(streams)

return cmd
}

func newVerbAttach(verb string, streams genericclioptions.IOStreams) *cobra.Command {
func newVerbAttach(verb string, f cmdutil.Factory, streams genericclioptions.IOStreams) *cobra.Command {
cmd := &cobra.Command{
Use: verb,
Short: "Attach cluster to hub",
Expand All @@ -103,7 +106,7 @@ func newVerbAttach(verb string, streams genericclioptions.IOStreams) *cobra.Comm
return cmd
}

func newVerbDetach(verb string, streams genericclioptions.IOStreams) *cobra.Command {
func newVerbDetach(verb string, f cmdutil.Factory, streams genericclioptions.IOStreams) *cobra.Command {
cmd := &cobra.Command{
Use: verb,
Short: "Detatch a cluster from the hub",
Expand Down
1 change: 1 addition & 0 deletions pkg/helpers/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,5 +13,6 @@ func GetControllerRuntimeClientFromFlags(configFlags *genericclioptions.ConfigFl
if err != nil {
return nil, err
}
config.QPS = 20
return crclient.New(config, crclient.Options{})
}

0 comments on commit be3bb81

Please sign in to comment.