From 3ef0e542381fa55922a056e19f180725ce0aa5a9 Mon Sep 17 00:00:00 2001 From: kqzh <35095889+kqzh@users.noreply.github.com> Date: Tue, 6 Jul 2021 11:00:39 +0800 Subject: [PATCH 01/12] fix doc and ngctl command --- doc/user/ngctl_guide.md | 89 ++++++++++++++ pkg/ngctl/cmd/check/check.go | 191 +++++++++++++++++++++++++++++++ pkg/ngctl/cmd/cmd.go | 2 + pkg/ngctl/cmd/console/console.go | 6 +- pkg/ngctl/cmd/info/info.go | 6 +- pkg/ngctl/cmd/list/list.go | 139 +++++++++++++--------- pkg/ngctl/cmd/use/use.go | 2 +- pkg/ngctl/cmd/util/factory.go | 6 +- pkg/ngctl/cmd/version/version.go | 1 + 9 files changed, 378 insertions(+), 64 deletions(-) create mode 100644 doc/user/ngctl_guide.md create mode 100644 pkg/ngctl/cmd/check/check.go diff --git a/doc/user/ngctl_guide.md b/doc/user/ngctl_guide.md new file mode 100644 index 00000000..e631df02 --- /dev/null +++ b/doc/user/ngctl_guide.md @@ -0,0 +1,89 @@ + +# Overview +ngctl is a terminal cmd tool for nebula-operator, it has the following commands: +- [ngctl use](#ngctl-use) +- [ngctl console](#ngctl-console) +- [ngctl list](#ngctl-list) +- [ngctl check](#ngctl-check) +- [ngctl info](#ngctl-info) + +## ngctl use +`ngctl use` specify a nebula cluster to use. By using a certain cluster, you may omit --nebulacluster option in many control commands. + +``` +Examples: + # specify a nebula cluster to use + ngctl use demo-cluster + + # specify kubernetes context and namespace + ngctl use demo-cluster -n test-system +``` +## ngctl console +`ngctl console` create a nebula-console pod and connect to the specified nebula cluster. + +``` +Examples: + # open console to the current nebula cluster, which is set by 'ngctl use' command + ngctl console + + # Open console to the specified nebula cluster + ngctl console --nebulacluster=nebula +``` +## ngctl list +`ngctl list` list nebula clusters or there sub resources. Its usage is the same as `kubectl get`, but only resources related to nbuela cluster are displayed. +``` +Examples: + # List all nebula clusters. + ngctl list + + # List all nebula clusters in all namespaces. + ngctl list -A + + # List all nebula clusters with json format. + ngctl list -o json + + # List nebula cluster sub resources with specified cluster name. + ngctl list pod --nebulacluster=nebula + + # You can also use 'use' command to specify a nebula cluster. + use nebula + ngctl list pod + + # Return only the metad's phase value of the specified pod. + ngctl list -o template --template="{{.status.graphd.phase}}" NAME + + # List image information in custom columns. + ngctl list -o custom-columns=NAMESPACE:.metadata.namespace,NAME:.metadata.name,IMAGE:.spec.graphd.image +``` + +## ngctl check +`ngctl check` check whether the specified nebula cluster resources are ready. Command will print the error message from nebula cluster resource conditions, help you locate the reason quickly. + +``` +Examples: + # check whether the specified nebula cluster is ready + ngctl check + + # check specified nebula cluster pods + ngctl check pods --nebulacluster=nebula +``` + +## ngctl info +`ngctl info` get current nebula cluster information, the cluster is set by 'ngctl use' command or use `--nebulacluster` flag. + +```Examples: + # get current nebula cluster information, which is set by 'ngctl use' command + ngctl info + + # get current nebula cluster information, which is set by '--nebulacluster' flag + ngctl info --nebulacluster=nebula +``` +## ngctl version +`nfgctl version` print the cli and nebula operator version + +```bash +Examples: + # Print the cli and nebula operator version + ngctl version +``` + diff --git a/pkg/ngctl/cmd/check/check.go b/pkg/ngctl/cmd/check/check.go new file mode 100644 index 00000000..c3152817 --- /dev/null +++ b/pkg/ngctl/cmd/check/check.go @@ -0,0 +1,191 @@ +/* +Copyright 2021 Vesoft Inc. + +Licensed under the Apache License, Version 2.0 (the "License"); +you may not use this file except in compliance with the License. +You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + +Unless required by applicable law or agreed to in writing, software +distributed under the License is distributed on an "AS IS" BASIS, +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +See the License for the specific language governing permissions and +limitations under the License. +*/ + +package check + +import ( + "bytes" + "context" + "text/tabwriter" + + "github.com/spf13/cobra" + appsv1alpha1 "github.com/vesoft-inc/nebula-operator/apis/apps/v1alpha1" + corev1 "k8s.io/api/core/v1" + "k8s.io/cli-runtime/pkg/genericclioptions" + "k8s.io/kubectl/pkg/util/templates" + "sigs.k8s.io/controller-runtime/pkg/client" + + "github.com/vesoft-inc/nebula-operator/apis/apps/v1alpha1" + "github.com/vesoft-inc/nebula-operator/pkg/label" + cmdutil "github.com/vesoft-inc/nebula-operator/pkg/ngctl/cmd/util" + "github.com/vesoft-inc/nebula-operator/pkg/ngctl/cmd/util/ignore" +) + +var ( + checkLong = templates.LongDesc(` + Check whether the specified nebula cluster resources are ready.`) + + checkExample = templates.Examples(` + # check whether the specified nebula cluster is ready + ngctl check + + # check specified nebula cluster pods + ngctl check pods --nebulacluster=nebula`) +) + +type CheckOptions struct { + Namespace string + NebulaClusterName string + ResourceType string + AllNamespaces bool + + runtimeCli client.Client + genericclioptions.IOStreams +} + +func NewCheckOptions(streams genericclioptions.IOStreams) *CheckOptions { + return &CheckOptions{ + IOStreams: streams, + } +} + +// NewCmdCheck returns a cobra command for check whether nebula cluster resources are ready +func NewCmdCheck(f cmdutil.Factory, ioStreams genericclioptions.IOStreams) *cobra.Command { + o := NewCheckOptions(ioStreams) + + cmd := &cobra.Command{ + Use: "check", + Short: "check whether nebula cluster resources are ready", + Long: checkLong, + Example: checkExample, + Run: func(cmd *cobra.Command, args []string) { + cmdutil.CheckErr(o.Complete(f, args)) + cmdutil.CheckErr(o.Validate(cmd)) + cmdutil.CheckErr(o.RunCheck()) + }, + } + + f.AddFlags(cmd) + return cmd +} + +// Complete completes all the required options +func (o *CheckOptions) Complete(f cmdutil.Factory, args []string) error { + var err error + + if o.NebulaClusterName, err = f.GetNebulaClusterName(); err != nil && !cmdutil.IsErNotSpecified(err) { + return err + } + + if o.Namespace, err = f.GetNamespace(); err != nil { + return err + } + + if len(args) > 0 { + o.ResourceType = args[0] + } + + o.runtimeCli, err = f.ToRuntimeClient() + if err != nil { + return err + } + + return nil +} + +// Validate validates the provided options +func (o *CheckOptions) Validate(cmd *cobra.Command) error { + if o.NebulaClusterName == "" { + return cmdutil.UsageErrorf(cmd, "expected specify nebula cluster like 'ngctl check resource --nebulacluster=CLUSTER_NAME' for the check command, or using 'ngctl use' \nto set nebula cluster first.") + } + + if o.ResourceType == "" { + o.ResourceType = "nebulacluster" + } + + return nil +} + +// Run executes debug command +func (o *CheckOptions) RunCheck() error { + switch o.ResourceType { + case "nebulaclusters", "nebulacluster", "nc": + return o.CheckNebulaCluster() + case "pod", "pods": + return o.CheckPods() + } + + return nil +} + +func (o *CheckOptions) CheckNebulaCluster() error { + var nc appsv1alpha1.NebulaCluster + key := client.ObjectKey{Namespace: o.Namespace, Name: o.NebulaClusterName} + if err := o.runtimeCli.Get(context.TODO(), key, &nc); err != nil { + return err + } + for _, cond := range nc.Status.Conditions { + if cond.Type == v1alpha1.NebulaClusterReady { + ignore.Fprintf(o.Out, "%s\n", cond.Message) + } + } + return nil +} + +func (o *CheckOptions) CheckPods() error { + selector, err := label.New().Cluster(o.NebulaClusterName).Selector() + if err != nil { + return err + } + + var pods corev1.PodList + listOptions := client.ListOptions{ + LabelSelector: selector, + Namespace: o.Namespace, + } + if err := o.runtimeCli.List(context.TODO(), &pods, &listOptions); err != nil { + return err + } + + allWork := true + tw := new(tabwriter.Writer) + buf := &bytes.Buffer{} + tw.Init(buf, 0, 8, 4, ' ', 0) + + ignore.Fprintf(tw, "Trouble Pods:\n") + ignore.Fprintf(tw, "\tPodName\tPhase\tConditionType\tMessage\n") + ignore.Fprintf(tw, "\t-------\t------\t-------------\t-------\n") + + for _, pod := range pods.Items { + if pod.Status.Phase != corev1.PodRunning { + allWork = false + for _, cond := range pod.Status.Conditions { + ignore.Fprintf(tw, "\t%s", pod.Name) + ignore.Fprintf(tw, "\t%s\t%s\t%s", pod.Status.Phase, cond.Type, cond.Message) + } + } + } + + _ = tw.Flush() + + if allWork { + ignore.Fprintf(o.Out, "%s", "All pods are running") + } else { + ignore.Fprintf(o.Out, "%s\n", buf.String()) + } + + return nil +} diff --git a/pkg/ngctl/cmd/cmd.go b/pkg/ngctl/cmd/cmd.go index 307584ed..ae903099 100644 --- a/pkg/ngctl/cmd/cmd.go +++ b/pkg/ngctl/cmd/cmd.go @@ -18,6 +18,7 @@ package cmd import ( "flag" + "github.com/vesoft-inc/nebula-operator/pkg/ngctl/cmd/check" "io" "github.com/spf13/cobra" @@ -64,6 +65,7 @@ func NewNgctlCmd(in io.Reader, out, err io.Writer) *cobra.Command { { Message: "Cluster Management Commands:", Commands: []*cobra.Command{ + check.NewCmdCheck(f, ioStreams), console.NewCmdConsole(f, ioStreams), info.NewCmdInfo(f, ioStreams), list.NewCmdList(f, ioStreams), diff --git a/pkg/ngctl/cmd/console/console.go b/pkg/ngctl/cmd/console/console.go index 3874a226..b1bda3bd 100644 --- a/pkg/ngctl/cmd/console/console.go +++ b/pkg/ngctl/cmd/console/console.go @@ -53,7 +53,7 @@ to set nebula cluster first. ) type ( - // Options is a struct to support version command + // Options is a struct to support console command Options struct { Namespace string NebulaClusterName string @@ -79,7 +79,7 @@ func NewOptions(ioStreams genericclioptions.IOStreams) *Options { } } -// NewCmdConsole returns a cobra command for specify a nebula cluster to use +// NewCmdConsole returns a cobra command for open console to the specified nebula cluster func NewCmdConsole(f cmdutil.Factory, ioStreams genericclioptions.IOStreams) *cobra.Command { o := NewOptions(ioStreams) cmd := &cobra.Command{ @@ -159,7 +159,7 @@ func (o *Options) Validate(cmd *cobra.Command) error { return nil } -// Run executes use command +// Run executes console command func (o *Options) Run() error { pod, err := o.generateConsolePod() if err != nil { diff --git a/pkg/ngctl/cmd/info/info.go b/pkg/ngctl/cmd/info/info.go index 5dbbfb78..db23e31c 100644 --- a/pkg/ngctl/cmd/info/info.go +++ b/pkg/ngctl/cmd/info/info.go @@ -43,7 +43,7 @@ to set nebula cluster first. ) type ( - // Options is a struct to support version command + // Options is a struct to support info command Options struct { Namespace string NebulaClusterName string @@ -60,7 +60,7 @@ func NewOptions(streams genericclioptions.IOStreams) *Options { } } -// NewCmdInfo returns a cobra command for specify a nebula cluster to use +// NewCmdInfo returns a cobra command for get specified nebula cluster information func NewCmdInfo(f cmdutil.Factory, ioStreams genericclioptions.IOStreams) *cobra.Command { o := NewOptions(ioStreams) cmd := &cobra.Command{ @@ -104,7 +104,7 @@ func (o *Options) Validate(cmd *cobra.Command) error { return nil } -// Run executes use command +// Run executes info command func (o *Options) Run() error { nci, err := NewNebulaClusterInfo(o.NebulaClusterName, o.Namespace, o.runtimeCli) if err != nil { diff --git a/pkg/ngctl/cmd/list/list.go b/pkg/ngctl/cmd/list/list.go index 508f1e3d..15feb188 100644 --- a/pkg/ngctl/cmd/list/list.go +++ b/pkg/ngctl/cmd/list/list.go @@ -19,6 +19,8 @@ package list import ( "encoding/json" "fmt" + "github.com/vesoft-inc/nebula-operator/pkg/label" + "k8s.io/kubectl/pkg/util/templates" "strings" "github.com/spf13/cobra" @@ -39,18 +41,14 @@ import ( cmdutil "github.com/vesoft-inc/nebula-operator/pkg/ngctl/cmd/util" ) -const ( - listLong = ` - List nebula clusters. +var ( + listLong = templates.LongDesc(` + List nebula clusters or there sub resources. - Prints a table of the most important information about the nebula clusters. You can - filter the list using a label selector and the --selector flag. You will only see - results in your current namespace unless you pass --all-namespaces. - - By specifying the output as 'template' and providing a Go template as the value of - the --template flag, you can filter the attributes of the nebula clusters. -` - listExample = ` + Prints a table of the most important information about the nebula cluster resources. + You can use many of the same flags as kubectl get`) + + listExample = templates.Examples(` # List all nebula clusters. ngctl list @@ -60,52 +58,58 @@ const ( # List all nebula clusters with json format. ngctl list -o json - # List a single nebula clusters with specified NAME in ps output format. - ngctl list NAME + # List nebula cluster sub resources with specified cluster name. + ngctl list pod --nebulacluster=nebula + + # You can also use 'use' command to specify a nebula cluster. + use nebula + ngctl list pod # Return only the metad's phase value of the specified pod. ngctl list -o template --template="{{.status.graphd.phase}}" NAME # List image information in custom columns. - ngctl list -o custom-columns=NAMESPACE:.metadata.namespace,NAME:.metadata.name,IMAGE:.spec.graphd.image -` - OutputFormatWide = "wide" + ngctl list -o custom-columns=NAMESPACE:.metadata.namespace,NAME:.metadata.name,IMAGE:.spec.graphd.image`) ) -type ( - // Options is a struct to support version command - Options struct { - LabelSelector string - FieldSelector string - AllNamespaces bool - Namespace string - NebulaClusterNames []string - Sort bool - SortBy string - IgnoreNotFound bool - IsHumanReadablePrinter bool - ServerPrint bool - - PrintFlags *PrintFlags - ToPrinter func(mapping *meta.RESTMapping, withNamespace bool) (printers.ResourcePrinterFunc, error) - - builder *resource.Builder - genericclioptions.IOStreams - } -) +const OutputFormatWide = "wide" + +// ListOptions is a struct to support list command +type ListOptions struct { + Namespace string + NebulaClusterName string + NebulaClusterLabel string + ResourceType string + + LabelSelector string + FieldSelector string + AllNamespaces bool + Sort bool + SortBy string + IgnoreNotFound bool -// NewOptions returns initialized Options -func NewOptions(streams genericclioptions.IOStreams) *Options { - return &Options{ + ServerPrint bool + + PrintFlags *PrintFlags + ToPrinter func(mapping *meta.RESTMapping, withNamespace bool) (printers.ResourcePrinterFunc, error) + IsHumanReadablePrinter bool + + builder *resource.Builder + genericclioptions.IOStreams +} + +// NewListOptions returns initialized Options +func NewListOptions(streams genericclioptions.IOStreams) *ListOptions { + return &ListOptions{ PrintFlags: NewGetPrintFlags(), IOStreams: streams, ServerPrint: true, } } -// NewCmdList returns a cobra command for list nebula clusters +// NewCmdList returns a cobra command for list nebula clusters or there sub resources. func NewCmdList(f cmdutil.Factory, ioStreams genericclioptions.IOStreams) *cobra.Command { - o := NewOptions(ioStreams) + o := NewListOptions(ioStreams) cmd := &cobra.Command{ Use: "list", Short: "list all nebula clusters", @@ -113,18 +117,20 @@ func NewCmdList(f cmdutil.Factory, ioStreams genericclioptions.IOStreams) *cobra Example: listExample, Run: func(cmd *cobra.Command, args []string) { cmdutil.CheckErr(o.Complete(f, cmd, args)) + cmdutil.CheckErr(o.Validate(cmd)) cmdutil.CheckErr(o.Run()) }, } o.PrintFlags.AddFlags(cmd) + f.AddFlags(cmd) o.AddFlags(cmd) return cmd } -// AddFlags add all the flags. -func (o *Options) AddFlags(cmd *cobra.Command) { +// AddFlags add extra list options flags. +func (o *ListOptions) AddFlags(cmd *cobra.Command) { 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, @@ -139,13 +145,23 @@ func (o *Options) AddFlags(cmd *cobra.Command) { } // Complete completes all the required options -func (o *Options) Complete(f cmdutil.Factory, cmd *cobra.Command, args []string) error { +func (o *ListOptions) Complete(f cmdutil.Factory, cmd *cobra.Command, args []string) error { var err error - o.NebulaClusterNames, o.Namespace, err = f.GetNebulaClusterNamesAndNamespace(false, args) - if err != nil && !cmdutil.IsErNotSpecified(err) { + + if o.NebulaClusterName, err = f.GetNebulaClusterName(); err != nil && !cmdutil.IsErNotSpecified(err) { return err } + o.NebulaClusterLabel = label.ClusterLabelKey + "=" + o.NebulaClusterName + + if o.Namespace, err = f.GetNamespace(); err != nil { + return err + } + + if len(args) > 0 { + o.ResourceType = args[0] + } + o.SortBy, err = cmd.Flags().GetString("sort-by") if err != nil { return err @@ -203,7 +219,7 @@ func (o *Options) Complete(f cmdutil.Factory, cmd *cobra.Command, args []string) return nil } -func (o *Options) Validate(cmd *cobra.Command) error { +func (o *ListOptions) Validate(cmd *cobra.Command) error { if showLabels, err := cmd.Flags().GetBool("show-labels"); err != nil { return err } else if showLabels { @@ -213,10 +229,19 @@ func (o *Options) Validate(cmd *cobra.Command) error { } } + if o.NebulaClusterName == "" && o.ResourceType != "" { + return cmdutil.UsageErrorf(cmd, "expected specify nebula cluster like 'ngctl list resource --nebulacluster=CLUSTER_NAME' for the list command, or using 'ngctl use' \nto set nebula cluster first.") + } + + if o.ResourceType == "" { + o.ResourceType = "nebulacluster" + o.NebulaClusterLabel = "" + } + return nil } -func (o *Options) transformRequests(req *rest.Request) { +func (o *ListOptions) transformRequests(req *rest.Request) { if !o.ServerPrint || !o.IsHumanReadablePrinter { return } @@ -232,14 +257,21 @@ func (o *Options) transformRequests(req *rest.Request) { } } -// Run executes use command -func (o *Options) Run() error { +// Run executes list command +func (o *ListOptions) Run() error { + + if o.LabelSelector == "" { + o.LabelSelector = o.NebulaClusterLabel + } else { + o.LabelSelector = o.LabelSelector + "," + o.NebulaClusterLabel + } + r := o.builder. Unstructured(). NamespaceParam(o.Namespace).DefaultNamespace().AllNamespaces(o.AllNamespaces). LabelSelectorParam(o.LabelSelector). FieldSelectorParam(o.FieldSelector). - ResourceTypeOrNameArgs(true, append([]string{"nebulacluster"}, o.NebulaClusterNames...)...). + ResourceTypeOrNameArgs(true, o.ResourceType). ContinueOnError(). Latest(). Flatten(). @@ -261,7 +293,6 @@ func (o *Options) Run() error { if err != nil { return err } - objs := make([]runtime.Object, len(infos)) for ix := range infos { objs[ix] = infos[ix].Object @@ -314,7 +345,7 @@ func (o *Options) Run() error { return nil } -func (o *Options) printGeneric(r *resource.Result) error { +func (o *ListOptions) printGeneric(r *resource.Result) error { var errs []error singleItemImplied := false infos, err := r.IntoSingleItemImplied(&singleItemImplied).Infos() diff --git a/pkg/ngctl/cmd/use/use.go b/pkg/ngctl/cmd/use/use.go index a3ae9a4b..c89ea434 100644 --- a/pkg/ngctl/cmd/use/use.go +++ b/pkg/ngctl/cmd/use/use.go @@ -46,7 +46,7 @@ const ( ) type ( - // Options is a struct to support version command + // Options is a struct to support use command Options struct { Namespace string NebulaClusterName string diff --git a/pkg/ngctl/cmd/util/factory.go b/pkg/ngctl/cmd/util/factory.go index 7a173ed5..584361d3 100644 --- a/pkg/ngctl/cmd/util/factory.go +++ b/pkg/ngctl/cmd/util/factory.go @@ -36,9 +36,9 @@ type ( ToRuntimeClient() (client.Client, error) GetNebulaClusterNameAndNamespace(withUseConfig bool, args []string) (string, string, error) GetNebulaClusterNamesAndNamespace(withUseConfig bool, args []string) ([]string, string, error) - // GetNebulaClusterName() (string, error) + GetNebulaClusterName() (string, error) // GetNebulaClusterNameWithoutConfig() string - // GetNamespace() (string, error) + GetNamespace() (string, error) GetNebulaClusterConfigFile() (string, error) } factoryImpl struct { @@ -152,7 +152,7 @@ func (f *factoryImpl) getNebulaClusterName(withConfig bool) (string, error) { c, err := f.getNebulaClusterConfig() if err != nil { - return "", err + return "", errNotSpecified } return c.ClusterName, nil diff --git a/pkg/ngctl/cmd/version/version.go b/pkg/ngctl/cmd/version/version.go index 166d9329..77df682e 100644 --- a/pkg/ngctl/cmd/version/version.go +++ b/pkg/ngctl/cmd/version/version.go @@ -62,6 +62,7 @@ func NewCmdVersion(f cmdutil.Factory, ioStreams genericclioptions.IOStreams) *co cmd := &cobra.Command{ Use: "version", Short: "Print the cli and nebula operator version", + Long: "Print the cli and nebula operator version", Example: versionExample, Run: func(cmd *cobra.Command, args []string) { cmdutil.CheckErr(o.Complete(f)) From 74fe06e22d4fb1fe343518aa375b232520334204 Mon Sep 17 00:00:00 2001 From: kqzh <35095889+kqzh@users.noreply.github.com> Date: Tue, 6 Jul 2021 11:06:19 +0800 Subject: [PATCH 02/12] fix fmt --- pkg/ngctl/cmd/cmd.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pkg/ngctl/cmd/cmd.go b/pkg/ngctl/cmd/cmd.go index ae903099..341e271b 100644 --- a/pkg/ngctl/cmd/cmd.go +++ b/pkg/ngctl/cmd/cmd.go @@ -18,13 +18,13 @@ package cmd import ( "flag" - "github.com/vesoft-inc/nebula-operator/pkg/ngctl/cmd/check" "io" "github.com/spf13/cobra" "k8s.io/cli-runtime/pkg/genericclioptions" "k8s.io/kubectl/pkg/util/templates" + "github.com/vesoft-inc/nebula-operator/pkg/ngctl/cmd/check" "github.com/vesoft-inc/nebula-operator/pkg/ngctl/cmd/console" "github.com/vesoft-inc/nebula-operator/pkg/ngctl/cmd/info" "github.com/vesoft-inc/nebula-operator/pkg/ngctl/cmd/list" From d8d5bbf9806438214062c890580c0b162ec77b18 Mon Sep 17 00:00:00 2001 From: kqzh <35095889+kqzh@users.noreply.github.com> Date: Tue, 6 Jul 2021 16:57:38 +0800 Subject: [PATCH 03/12] add unit test --- pkg/ngctl/cmd/check/check_test.go | 1 + pkg/ngctl/cmd/console/console_test.go | 71 +++++++++++++++++++++++++++ 2 files changed, 72 insertions(+) create mode 100644 pkg/ngctl/cmd/check/check_test.go create mode 100644 pkg/ngctl/cmd/console/console_test.go diff --git a/pkg/ngctl/cmd/check/check_test.go b/pkg/ngctl/cmd/check/check_test.go new file mode 100644 index 00000000..a116660f --- /dev/null +++ b/pkg/ngctl/cmd/check/check_test.go @@ -0,0 +1 @@ +package check diff --git a/pkg/ngctl/cmd/console/console_test.go b/pkg/ngctl/cmd/console/console_test.go new file mode 100644 index 00000000..9301709b --- /dev/null +++ b/pkg/ngctl/cmd/console/console_test.go @@ -0,0 +1,71 @@ +/* +Copyright 2021 Vesoft Inc. + +Licensed under the Apache License, Version 2.0 (the "License"); +you may not use this file except in compliance with the License. +You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + +Unless required by applicable law or agreed to in writing, software +distributed under the License is distributed on an "AS IS" BASIS, +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +See the License for the specific language governing permissions and +limitations under the License. +*/ + +package console + +import ( + "context" + metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" + "sigs.k8s.io/controller-runtime/pkg/client" + "testing" + + "k8s.io/apimachinery/pkg/runtime" + clientgoscheme "k8s.io/client-go/kubernetes/scheme" + "sigs.k8s.io/controller-runtime/pkg/client/fake" + + "github.com/vesoft-inc/nebula-operator/apis/apps/v1alpha1" +) + +var scheme = runtime.NewScheme() + +func init() { + _ = clientgoscheme.AddToScheme(scheme) + _ = v1alpha1.AddToScheme(scheme) +} + +func TestGenerateConsolePod(t *testing.T) { + + fakeClient := fake.NewClientBuilder().WithScheme(scheme).Build() + + fakeOptions := Options{ + Namespace: "nebula", + NebulaClusterName: "nc-test1", + Image: consoleDefaultImage, + User: "root", + Password: "*", + runtimeCli: fakeClient, + restClientGetter: nil, + } + + fakeNebulaCluster := &v1alpha1.NebulaCluster{ + ObjectMeta: metav1.ObjectMeta{ + Name: fakeOptions.NebulaClusterName, + Namespace: fakeOptions.Namespace, + }, + } + _ = fakeClient.Create(context.Background(), fakeNebulaCluster, &client.CreateOptions{}) + + pod, err := fakeOptions.generateConsolePod() + if err != nil { + t.Error(err) + } + + err = fakeOptions.runtimeCli.Create(context.Background(), pod, &client.CreateOptions{}) + if err != nil { + t.Error(err) + } + +} From 3308213388072d6fabf02e66450edef8f3740168 Mon Sep 17 00:00:00 2001 From: kqzh <35095889+kqzh@users.noreply.github.com> Date: Tue, 6 Jul 2021 16:59:31 +0800 Subject: [PATCH 04/12] add check unit-test --- pkg/ngctl/cmd/check/check.go | 45 ++++--- pkg/ngctl/cmd/check/check_test.go | 207 ++++++++++++++++++++++++++++++ 2 files changed, 236 insertions(+), 16 deletions(-) diff --git a/pkg/ngctl/cmd/check/check.go b/pkg/ngctl/cmd/check/check.go index c3152817..78e1d964 100644 --- a/pkg/ngctl/cmd/check/check.go +++ b/pkg/ngctl/cmd/check/check.go @@ -119,36 +119,49 @@ func (o *CheckOptions) Validate(cmd *cobra.Command) error { return nil } -// Run executes debug command +// Run executes check command func (o *CheckOptions) RunCheck() error { switch o.ResourceType { case "nebulaclusters", "nebulacluster", "nc": - return o.CheckNebulaCluster() + { + str, err := o.CheckNebulaCluster() + if err != nil { + return err + } + ignore.Fprintf(o.Out, "%s\n", str) + } case "pod", "pods": - return o.CheckPods() + { + str, err := o.CheckPods() + if err != nil { + return err + } + ignore.Fprintf(o.Out, "%s\n", str) + + } } return nil } -func (o *CheckOptions) CheckNebulaCluster() error { +func (o *CheckOptions) CheckNebulaCluster() (string, error) { var nc appsv1alpha1.NebulaCluster key := client.ObjectKey{Namespace: o.Namespace, Name: o.NebulaClusterName} if err := o.runtimeCli.Get(context.TODO(), key, &nc); err != nil { - return err + return "", err } for _, cond := range nc.Status.Conditions { if cond.Type == v1alpha1.NebulaClusterReady { - ignore.Fprintf(o.Out, "%s\n", cond.Message) + return cond.Message, nil } } - return nil + return "", nil } -func (o *CheckOptions) CheckPods() error { +func (o *CheckOptions) CheckPods() (string, error) { selector, err := label.New().Cluster(o.NebulaClusterName).Selector() if err != nil { - return err + return "", err } var pods corev1.PodList @@ -157,7 +170,7 @@ func (o *CheckOptions) CheckPods() error { Namespace: o.Namespace, } if err := o.runtimeCli.List(context.TODO(), &pods, &listOptions); err != nil { - return err + return "", err } allWork := true @@ -173,8 +186,10 @@ func (o *CheckOptions) CheckPods() error { if pod.Status.Phase != corev1.PodRunning { allWork = false for _, cond := range pod.Status.Conditions { - ignore.Fprintf(tw, "\t%s", pod.Name) - ignore.Fprintf(tw, "\t%s\t%s\t%s", pod.Status.Phase, cond.Type, cond.Message) + if cond.Status != corev1.ConditionTrue { + ignore.Fprintf(tw, "\t%s", pod.Name) + ignore.Fprintf(tw, "\t%s\t%s\t%s\n", pod.Status.Phase, cond.Type, cond.Message) + } } } } @@ -182,10 +197,8 @@ func (o *CheckOptions) CheckPods() error { _ = tw.Flush() if allWork { - ignore.Fprintf(o.Out, "%s", "All pods are running") + return "All pods are running", nil } else { - ignore.Fprintf(o.Out, "%s\n", buf.String()) + return buf.String(), nil } - - return nil } diff --git a/pkg/ngctl/cmd/check/check_test.go b/pkg/ngctl/cmd/check/check_test.go index a116660f..a39753da 100644 --- a/pkg/ngctl/cmd/check/check_test.go +++ b/pkg/ngctl/cmd/check/check_test.go @@ -1 +1,208 @@ +/* +Copyright 2021 Vesoft Inc. + +Licensed under the Apache License, Version 2.0 (the "License"); +you may not use this file except in compliance with the License. +You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + +Unless required by applicable law or agreed to in writing, software +distributed under the License is distributed on an "AS IS" BASIS, +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +See the License for the specific language governing permissions and +limitations under the License. +*/ + package check + +import ( + "context" + "testing" + + corev1 "k8s.io/api/core/v1" + metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" + "k8s.io/apimachinery/pkg/runtime" + clientgoscheme "k8s.io/client-go/kubernetes/scheme" + "sigs.k8s.io/controller-runtime/pkg/client" + "sigs.k8s.io/controller-runtime/pkg/client/fake" + + "github.com/vesoft-inc/nebula-operator/apis/apps/v1alpha1" +) + +var scheme = runtime.NewScheme() + +func init() { + _ = clientgoscheme.AddToScheme(scheme) + _ = v1alpha1.AddToScheme(scheme) +} + +func TestCheckNebulaCluster(t *testing.T) { + + fakeClient := fake.NewClientBuilder().WithScheme(scheme).Build() + + testcases := []struct { + desc string + options CheckOptions + condType v1alpha1.NebulaClusterConditionType + message string + expected string + }{ + { + desc: "", + options: CheckOptions{ + Namespace: "", + NebulaClusterName: "nc-test1", + runtimeCli: fakeClient, + }, + condType: v1alpha1.NebulaClusterReady, + message: "Nebula cluster is running", + expected: "Nebula cluster is running", + }, + { + desc: "", + options: CheckOptions{ + Namespace: "nebula", + NebulaClusterName: "nc-test2", + runtimeCli: fakeClient, + }, + condType: v1alpha1.NebulaClusterReady, + message: "Nebula cluster is running", + expected: "Nebula cluster is running", + }, + { + desc: "", + options: CheckOptions{ + Namespace: "nebula2", + NebulaClusterName: "nc-test3", + runtimeCli: fakeClient, + }, + expected: "", + }, + } + + for _, tc := range testcases { + fakeNebulaCluster := &v1alpha1.NebulaCluster{ + ObjectMeta: metav1.ObjectMeta{ + Name: tc.options.NebulaClusterName, + Namespace: tc.options.Namespace, + }, + Status: v1alpha1.NebulaClusterStatus{ + Conditions: []v1alpha1.NebulaClusterCondition{ + { + Type: tc.condType, + Message: tc.message, + }, + }, + }, + } + _ = fakeClient.Create(context.Background(), fakeNebulaCluster, &client.CreateOptions{}) + } + + for i, tc := range testcases { + str, err := tc.options.CheckNebulaCluster() + if err != nil { + t.Error(err) + } + if tc.expected != str { + t.Errorf("%d: Expected: \n%#v\n but actual: \n%#v\n", i, tc.expected, str) + } + } +} + +func TestCheckPods(t *testing.T) { + + fakeClient := fake.NewClientBuilder().WithScheme(scheme).Build() + + testcases := []struct { + desc string + options CheckOptions + phase corev1.PodPhase + conditions []corev1.PodCondition + expected string + }{ + { + options: CheckOptions{ + Namespace: "", + NebulaClusterName: "nc-test1", + runtimeCli: fakeClient, + }, + phase: corev1.PodPending, + conditions: []corev1.PodCondition{ + { + Type: corev1.PodScheduled, + Status: corev1.ConditionFalse, + Message: "0/5 nodes are available: 5 pod has unbound immediate PersistentVolumeClaims.", + }, + }, + expected: `Trouble Pods: + PodName Phase ConditionType Message + ------- ------ ------------- ------- + nc-test1 Pending PodScheduled 0/5 nodes are available: 5 pod has unbound immediate PersistentVolumeClaims. +`, + }, { + options: CheckOptions{ + Namespace: "nebula", + NebulaClusterName: "nc-test2", + runtimeCli: fakeClient, + }, + phase: corev1.PodRunning, + conditions: []corev1.PodCondition{}, + expected: "All pods are running", + }, { + options: CheckOptions{ + Namespace: "nebula2", + NebulaClusterName: "nc-test3", + runtimeCli: fakeClient, + }, + phase: corev1.PodFailed, + conditions: []corev1.PodCondition{ + { + Type: corev1.PodScheduled, + Status: corev1.ConditionFalse, + Message: "0/5 nodes are available: 5 pod has unbound immediate PersistentVolumeClaims.", + }, + { + Type: corev1.PodInitialized, + Status: corev1.ConditionFalse, + Message: "0/5 nodes are available: 5 pod has unbound immediate PersistentVolumeClaims.", + }, + }, + expected: `Trouble Pods: + PodName Phase ConditionType Message + ------- ------ ------------- ------- + nc-test3 Failed PodScheduled 0/5 nodes are available: 5 pod has unbound immediate PersistentVolumeClaims. + nc-test3 Failed Initialized 0/5 nodes are available: 5 pod has unbound immediate PersistentVolumeClaims. +`, + }, + } + + for _, tc := range testcases { + fakePod := &corev1.Pod{ + ObjectMeta: metav1.ObjectMeta{ + Name: tc.options.NebulaClusterName, + Namespace: tc.options.Namespace, + Labels: map[string]string{ + "app.kubernetes.io/cluster": tc.options.NebulaClusterName, + "app.kubernetes.io/managed-by": "nebula-operator", + "app.kubernetes.io/name": "nebula-graph", + }, + }, + Status: corev1.PodStatus{ + Phase: tc.phase, + Conditions: tc.conditions, + }, + } + _ = fakeClient.Create(context.Background(), fakePod, &client.CreateOptions{}) + } + + for i, tc := range testcases { + str, err := tc.options.CheckPods() + if err != nil { + t.Error(err) + } + if tc.expected != str { + t.Errorf("%d: Expected: \n%#v\n but actual: \n%#v\n", i, tc.expected, str) + } + } +} From 9bde900a954c7acbed7de9921f0b85b6ba461b9f Mon Sep 17 00:00:00 2001 From: kqzh <35095889+kqzh@users.noreply.github.com> Date: Tue, 6 Jul 2021 17:55:56 +0800 Subject: [PATCH 05/12] fix help format --- pkg/ngctl/cmd/info/info.go | 15 ++++++++------- pkg/ngctl/cmd/use/use.go | 16 +++++++++------- pkg/ngctl/cmd/version/version.go | 8 ++++---- 3 files changed, 21 insertions(+), 18 deletions(-) diff --git a/pkg/ngctl/cmd/info/info.go b/pkg/ngctl/cmd/info/info.go index db23e31c..91e47759 100644 --- a/pkg/ngctl/cmd/info/info.go +++ b/pkg/ngctl/cmd/info/info.go @@ -18,6 +18,7 @@ package info import ( "fmt" + "k8s.io/kubectl/pkg/util/templates" "github.com/spf13/cobra" "k8s.io/cli-runtime/pkg/genericclioptions" @@ -26,17 +27,17 @@ import ( cmdutil "github.com/vesoft-inc/nebula-operator/pkg/ngctl/cmd/util" ) -const ( - infoLong = ` - Get specified nebula cluster information. -` - infoExample = ` +var ( + infoLong = templates.LongDesc(` + Get specified nebula cluster information.`) + + infoExample = templates.Examples(` # get current nebula cluster information, which is set by 'ngctl use' command ngctl info # get specified nebula cluster information - ngctl info CLUSTER_NAME -` + ngctl info CLUSTER_NAME`) + infoUsage = `expected 'info CLUSTER_NAME' for the info command, or using 'ngctl use' to set nebula cluster first. ` diff --git a/pkg/ngctl/cmd/use/use.go b/pkg/ngctl/cmd/use/use.go index c89ea434..7aac8f32 100644 --- a/pkg/ngctl/cmd/use/use.go +++ b/pkg/ngctl/cmd/use/use.go @@ -19,6 +19,7 @@ package use import ( "context" "fmt" + "k8s.io/kubectl/pkg/util/templates" "github.com/spf13/cobra" "k8s.io/cli-runtime/pkg/genericclioptions" @@ -29,19 +30,20 @@ import ( "github.com/vesoft-inc/nebula-operator/pkg/ngctl/config" ) -const ( - useLong = ` +var ( + useLong = templates.LongDesc(` Specify a nebula cluster to use. By using a certain cluster, you may omit --nebulacluster option - in many control commands. -` - useExample = ` + in many control commands.`) + + useExample = templates.Examples(` # specify a nebula cluster to use ngctl use demo-cluster + # specify kubernetes context and namespace - ngctl use --namespace=demo-ns demo-cluster -` + ngctl use --namespace=demo-ns demo-cluster`) + useUsage = "expected 'use CLUSTER_NAME' for the use command" ) diff --git a/pkg/ngctl/cmd/version/version.go b/pkg/ngctl/cmd/version/version.go index 77df682e..6a14142a 100644 --- a/pkg/ngctl/cmd/version/version.go +++ b/pkg/ngctl/cmd/version/version.go @@ -19,6 +19,7 @@ package version import ( "context" "fmt" + "k8s.io/kubectl/pkg/util/templates" "github.com/spf13/cobra" v1 "k8s.io/apimachinery/pkg/apis/meta/v1" @@ -32,11 +33,10 @@ import ( operatorversion "github.com/vesoft-inc/nebula-operator/pkg/version" ) -const ( - versionExample = ` +var ( + versionExample = templates.Examples(` # Print the cli and nebula operator version - ngctl version -` + ngctl version`) ) type ( From f1472192142134249d582507d1fedbebb8885293 Mon Sep 17 00:00:00 2001 From: kqzh <35095889+kqzh@users.noreply.github.com> Date: Tue, 6 Jul 2021 17:58:00 +0800 Subject: [PATCH 06/12] fix format --- pkg/ngctl/cmd/info/info.go | 2 +- pkg/ngctl/cmd/use/use.go | 2 +- pkg/ngctl/cmd/version/version.go | 2 +- 3 files changed, 3 insertions(+), 3 deletions(-) diff --git a/pkg/ngctl/cmd/info/info.go b/pkg/ngctl/cmd/info/info.go index 91e47759..d5fcd93c 100644 --- a/pkg/ngctl/cmd/info/info.go +++ b/pkg/ngctl/cmd/info/info.go @@ -18,10 +18,10 @@ package info import ( "fmt" - "k8s.io/kubectl/pkg/util/templates" "github.com/spf13/cobra" "k8s.io/cli-runtime/pkg/genericclioptions" + "k8s.io/kubectl/pkg/util/templates" "sigs.k8s.io/controller-runtime/pkg/client" cmdutil "github.com/vesoft-inc/nebula-operator/pkg/ngctl/cmd/util" diff --git a/pkg/ngctl/cmd/use/use.go b/pkg/ngctl/cmd/use/use.go index 7aac8f32..42a4f010 100644 --- a/pkg/ngctl/cmd/use/use.go +++ b/pkg/ngctl/cmd/use/use.go @@ -19,10 +19,10 @@ package use import ( "context" "fmt" - "k8s.io/kubectl/pkg/util/templates" "github.com/spf13/cobra" "k8s.io/cli-runtime/pkg/genericclioptions" + "k8s.io/kubectl/pkg/util/templates" "sigs.k8s.io/controller-runtime/pkg/client" appsv1alpha1 "github.com/vesoft-inc/nebula-operator/apis/apps/v1alpha1" diff --git a/pkg/ngctl/cmd/version/version.go b/pkg/ngctl/cmd/version/version.go index 6a14142a..36998541 100644 --- a/pkg/ngctl/cmd/version/version.go +++ b/pkg/ngctl/cmd/version/version.go @@ -19,12 +19,12 @@ package version import ( "context" "fmt" - "k8s.io/kubectl/pkg/util/templates" "github.com/spf13/cobra" v1 "k8s.io/apimachinery/pkg/apis/meta/v1" "k8s.io/cli-runtime/pkg/genericclioptions" "k8s.io/client-go/kubernetes" + "k8s.io/kubectl/pkg/util/templates" "k8s.io/kubernetes/pkg/apis/core" "github.com/vesoft-inc/nebula-operator/pkg/label" From e211102272eda09926fa719b1f829c25f0efab3f Mon Sep 17 00:00:00 2001 From: kqzh <35095889+kqzh@users.noreply.github.com> Date: Tue, 6 Jul 2021 20:10:20 +0800 Subject: [PATCH 07/12] add unit-test --- pkg/ngctl/cmd/util/factory_test.go | 70 +++++++++++++++++++++++++++++ pkg/ngctl/config/config_test.go | 71 ++++++++++++++++++++++++++++++ 2 files changed, 141 insertions(+) create mode 100644 pkg/ngctl/cmd/util/factory_test.go create mode 100644 pkg/ngctl/config/config_test.go diff --git a/pkg/ngctl/cmd/util/factory_test.go b/pkg/ngctl/cmd/util/factory_test.go new file mode 100644 index 00000000..b1042e2f --- /dev/null +++ b/pkg/ngctl/cmd/util/factory_test.go @@ -0,0 +1,70 @@ +/* +Copyright 2021 Vesoft Inc. + +Licensed under the Apache License, Version 2.0 (the "License"); +you may not use this file except in compliance with the License. +You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + +Unless required by applicable law or agreed to in writing, software +distributed under the License is distributed on an "AS IS" BASIS, +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +See the License for the specific language governing permissions and +limitations under the License. +*/ + +package util + +import ( + "sync" + "testing" + + "github.com/vesoft-inc/nebula-operator/pkg/ngctl/config" +) + +func TestGetNebulaClusterConfig(t *testing.T) { + testcases := []struct { + desc string + config *config.NebulaClusterConfig + factory *factoryImpl + expectedErr bool + }{ + { + config: &config.NebulaClusterConfig{ + Namespace: "nebula-system", + ClusterName: "nebula", + }, + factory: &factoryImpl{ + nebulaClusterName: "", + nebulaClusterConfigFile: "~/.kube/config1", + loadingLock: sync.Mutex{}, + nebulaClusterConfig: nil, + }, + expectedErr: true, + }, + { + factory: &factoryImpl{ + nebulaClusterName: "", + nebulaClusterConfigFile: "~/.kube/config2", + loadingLock: sync.Mutex{}, + nebulaClusterConfig: nil, + }, + expectedErr: false, + }, + } + + for i, tc := range testcases { + if tc.config != nil { + err := tc.config.SaveToFile(tc.factory.nebulaClusterConfigFile) + if err != nil { + t.Error(err) + } + } + + _, err := tc.factory.getNebulaClusterConfig() + if (err != nil) == tc.expectedErr { + t.Errorf("%d: Expected: \n%#v\n but actual: \n%#v\n", i, tc.expectedErr, err != nil) + } + } +} diff --git a/pkg/ngctl/config/config_test.go b/pkg/ngctl/config/config_test.go new file mode 100644 index 00000000..30ff3344 --- /dev/null +++ b/pkg/ngctl/config/config_test.go @@ -0,0 +1,71 @@ +/* +Copyright 2021 Vesoft Inc. + +Licensed under the Apache License, Version 2.0 (the "License"); +you may not use this file except in compliance with the License. +You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + +Unless required by applicable law or agreed to in writing, software +distributed under the License is distributed on an "AS IS" BASIS, +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +See the License for the specific language governing permissions and +limitations under the License. +*/ + +package config + +import "testing" + +func TestLoadFromFileAndSaveToFile(t *testing.T) { + testcases := []struct { + desc string + filename string + namespace string + nebulaCluster string + config NebulaClusterConfig + expected NebulaClusterConfig + }{ + { + desc: "", + filename: "~/.kube/config1", + config: NebulaClusterConfig{ + ClusterName: "nebula", + Namespace: "nebula-system", + }, + expected: NebulaClusterConfig{ + ClusterName: "nebula", + Namespace: "nebula-system", + }, + }, + { + desc: "", + filename: "~/.kube/config2", + config: NebulaClusterConfig{ + ClusterName: "nebula", + Namespace: "", + }, + expected: NebulaClusterConfig{ + ClusterName: "nebula", + Namespace: "", + }, + }, + } + + for i, tc := range testcases { + if err := tc.config.SaveToFile(tc.filename); err != nil { + t.Error(err) + } + + tc.config = NebulaClusterConfig{} + + if err := tc.config.LoadFromFile(tc.filename); err != nil { + t.Error(err) + } + if tc.config != tc.expected { + t.Errorf("%d: Expected: \n%#v\n but actual: \n%#v\n", i, tc.expected, tc.config) + } + } + +} From 6ee1983a430230e57524a83dfe30e222f3e434d4 Mon Sep 17 00:00:00 2001 From: kqzh <35095889+kqzh@users.noreply.github.com> Date: Wed, 7 Jul 2021 10:58:23 +0800 Subject: [PATCH 08/12] fix import format --- pkg/ngctl/cmd/check/check.go | 2 +- pkg/ngctl/cmd/console/console_test.go | 4 ++-- pkg/ngctl/cmd/list/list.go | 4 ++-- 3 files changed, 5 insertions(+), 5 deletions(-) diff --git a/pkg/ngctl/cmd/check/check.go b/pkg/ngctl/cmd/check/check.go index 78e1d964..4d0ea479 100644 --- a/pkg/ngctl/cmd/check/check.go +++ b/pkg/ngctl/cmd/check/check.go @@ -22,13 +22,13 @@ import ( "text/tabwriter" "github.com/spf13/cobra" - appsv1alpha1 "github.com/vesoft-inc/nebula-operator/apis/apps/v1alpha1" corev1 "k8s.io/api/core/v1" "k8s.io/cli-runtime/pkg/genericclioptions" "k8s.io/kubectl/pkg/util/templates" "sigs.k8s.io/controller-runtime/pkg/client" "github.com/vesoft-inc/nebula-operator/apis/apps/v1alpha1" + appsv1alpha1 "github.com/vesoft-inc/nebula-operator/apis/apps/v1alpha1" "github.com/vesoft-inc/nebula-operator/pkg/label" cmdutil "github.com/vesoft-inc/nebula-operator/pkg/ngctl/cmd/util" "github.com/vesoft-inc/nebula-operator/pkg/ngctl/cmd/util/ignore" diff --git a/pkg/ngctl/cmd/console/console_test.go b/pkg/ngctl/cmd/console/console_test.go index 9301709b..f2456ede 100644 --- a/pkg/ngctl/cmd/console/console_test.go +++ b/pkg/ngctl/cmd/console/console_test.go @@ -18,12 +18,12 @@ package console import ( "context" - metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" - "sigs.k8s.io/controller-runtime/pkg/client" "testing" + metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" "k8s.io/apimachinery/pkg/runtime" clientgoscheme "k8s.io/client-go/kubernetes/scheme" + "sigs.k8s.io/controller-runtime/pkg/client" "sigs.k8s.io/controller-runtime/pkg/client/fake" "github.com/vesoft-inc/nebula-operator/apis/apps/v1alpha1" diff --git a/pkg/ngctl/cmd/list/list.go b/pkg/ngctl/cmd/list/list.go index 15feb188..867e6094 100644 --- a/pkg/ngctl/cmd/list/list.go +++ b/pkg/ngctl/cmd/list/list.go @@ -19,8 +19,6 @@ package list import ( "encoding/json" "fmt" - "github.com/vesoft-inc/nebula-operator/pkg/label" - "k8s.io/kubectl/pkg/util/templates" "strings" "github.com/spf13/cobra" @@ -37,7 +35,9 @@ import ( "k8s.io/cli-runtime/pkg/resource" "k8s.io/client-go/kubernetes/scheme" "k8s.io/client-go/rest" + "k8s.io/kubectl/pkg/util/templates" + "github.com/vesoft-inc/nebula-operator/pkg/label" cmdutil "github.com/vesoft-inc/nebula-operator/pkg/ngctl/cmd/util" ) From e9a77b22b8cc13869bb8fe297b71be5dcbf485e4 Mon Sep 17 00:00:00 2001 From: kqzh <35095889+kqzh@users.noreply.github.com> Date: Wed, 7 Jul 2021 11:42:12 +0800 Subject: [PATCH 09/12] fix golang-lint --- pkg/ngctl/cmd/check/check.go | 27 +++++++++++++-------------- pkg/ngctl/cmd/check/check_test.go | 2 -- pkg/ngctl/cmd/console/console_test.go | 2 -- pkg/ngctl/cmd/list/list.go | 7 ++++--- pkg/ngctl/cmd/version/version.go | 6 +----- pkg/ngctl/config/config_test.go | 1 - 6 files changed, 18 insertions(+), 27 deletions(-) diff --git a/pkg/ngctl/cmd/check/check.go b/pkg/ngctl/cmd/check/check.go index 4d0ea479..6b1f2c74 100644 --- a/pkg/ngctl/cmd/check/check.go +++ b/pkg/ngctl/cmd/check/check.go @@ -27,7 +27,6 @@ import ( "k8s.io/kubectl/pkg/util/templates" "sigs.k8s.io/controller-runtime/pkg/client" - "github.com/vesoft-inc/nebula-operator/apis/apps/v1alpha1" appsv1alpha1 "github.com/vesoft-inc/nebula-operator/apis/apps/v1alpha1" "github.com/vesoft-inc/nebula-operator/pkg/label" cmdutil "github.com/vesoft-inc/nebula-operator/pkg/ngctl/cmd/util" @@ -46,6 +45,8 @@ var ( ngctl check pods --nebulacluster=nebula`) ) +const resourceType = "nebulacluster" + type CheckOptions struct { Namespace string NebulaClusterName string @@ -109,20 +110,20 @@ func (o *CheckOptions) Complete(f cmdutil.Factory, args []string) error { // Validate validates the provided options func (o *CheckOptions) Validate(cmd *cobra.Command) error { if o.NebulaClusterName == "" { - return cmdutil.UsageErrorf(cmd, "expected specify nebula cluster like 'ngctl check resource --nebulacluster=CLUSTER_NAME' for the check command, or using 'ngctl use' \nto set nebula cluster first.") + return cmdutil.UsageErrorf(cmd, "using 'ngctl use' or '--nebulacluster' to set nebula cluster first.") } if o.ResourceType == "" { - o.ResourceType = "nebulacluster" + o.ResourceType = resourceType } return nil } -// Run executes check command +// RunCheck executes check command func (o *CheckOptions) RunCheck() error { switch o.ResourceType { - case "nebulaclusters", "nebulacluster", "nc": + case resourceType, "nebulaclusters", "nc": { str, err := o.CheckNebulaCluster() if err != nil { @@ -137,7 +138,6 @@ func (o *CheckOptions) RunCheck() error { return err } ignore.Fprintf(o.Out, "%s\n", str) - } } @@ -151,7 +151,7 @@ func (o *CheckOptions) CheckNebulaCluster() (string, error) { return "", err } for _, cond := range nc.Status.Conditions { - if cond.Type == v1alpha1.NebulaClusterReady { + if cond.Type == appsv1alpha1.NebulaClusterReady { return cond.Message, nil } } @@ -182,13 +182,13 @@ func (o *CheckOptions) CheckPods() (string, error) { ignore.Fprintf(tw, "\tPodName\tPhase\tConditionType\tMessage\n") ignore.Fprintf(tw, "\t-------\t------\t-------------\t-------\n") - for _, pod := range pods.Items { - if pod.Status.Phase != corev1.PodRunning { + for i := range pods.Items { + if pods.Items[i].Status.Phase != corev1.PodRunning { allWork = false - for _, cond := range pod.Status.Conditions { + for _, cond := range pods.Items[i].Status.Conditions { if cond.Status != corev1.ConditionTrue { - ignore.Fprintf(tw, "\t%s", pod.Name) - ignore.Fprintf(tw, "\t%s\t%s\t%s\n", pod.Status.Phase, cond.Type, cond.Message) + ignore.Fprintf(tw, "\t%s", pods.Items[i].Name) + ignore.Fprintf(tw, "\t%s\t%s\t%s\n", pods.Items[i].Status.Phase, cond.Type, cond.Message) } } } @@ -198,7 +198,6 @@ func (o *CheckOptions) CheckPods() (string, error) { if allWork { return "All pods are running", nil - } else { - return buf.String(), nil } + return buf.String(), nil } diff --git a/pkg/ngctl/cmd/check/check_test.go b/pkg/ngctl/cmd/check/check_test.go index a39753da..d1383b74 100644 --- a/pkg/ngctl/cmd/check/check_test.go +++ b/pkg/ngctl/cmd/check/check_test.go @@ -38,7 +38,6 @@ func init() { } func TestCheckNebulaCluster(t *testing.T) { - fakeClient := fake.NewClientBuilder().WithScheme(scheme).Build() testcases := []struct { @@ -111,7 +110,6 @@ func TestCheckNebulaCluster(t *testing.T) { } func TestCheckPods(t *testing.T) { - fakeClient := fake.NewClientBuilder().WithScheme(scheme).Build() testcases := []struct { diff --git a/pkg/ngctl/cmd/console/console_test.go b/pkg/ngctl/cmd/console/console_test.go index f2456ede..09dd3c4f 100644 --- a/pkg/ngctl/cmd/console/console_test.go +++ b/pkg/ngctl/cmd/console/console_test.go @@ -37,7 +37,6 @@ func init() { } func TestGenerateConsolePod(t *testing.T) { - fakeClient := fake.NewClientBuilder().WithScheme(scheme).Build() fakeOptions := Options{ @@ -67,5 +66,4 @@ func TestGenerateConsolePod(t *testing.T) { if err != nil { t.Error(err) } - } diff --git a/pkg/ngctl/cmd/list/list.go b/pkg/ngctl/cmd/list/list.go index 867e6094..499d225a 100644 --- a/pkg/ngctl/cmd/list/list.go +++ b/pkg/ngctl/cmd/list/list.go @@ -72,7 +72,9 @@ var ( ngctl list -o custom-columns=NAMESPACE:.metadata.namespace,NAME:.metadata.name,IMAGE:.spec.graphd.image`) ) -const OutputFormatWide = "wide" +const ( + OutputFormatWide = "wide" +) // ListOptions is a struct to support list command type ListOptions struct { @@ -230,7 +232,7 @@ func (o *ListOptions) Validate(cmd *cobra.Command) error { } if o.NebulaClusterName == "" && o.ResourceType != "" { - return cmdutil.UsageErrorf(cmd, "expected specify nebula cluster like 'ngctl list resource --nebulacluster=CLUSTER_NAME' for the list command, or using 'ngctl use' \nto set nebula cluster first.") + return cmdutil.UsageErrorf(cmd, "using 'ngctl use' or '--nebulacluster' to set nebula cluster first.") } if o.ResourceType == "" { @@ -259,7 +261,6 @@ func (o *ListOptions) transformRequests(req *rest.Request) { // Run executes list command func (o *ListOptions) Run() error { - if o.LabelSelector == "" { o.LabelSelector = o.NebulaClusterLabel } else { diff --git a/pkg/ngctl/cmd/version/version.go b/pkg/ngctl/cmd/version/version.go index 36998541..39a8c765 100644 --- a/pkg/ngctl/cmd/version/version.go +++ b/pkg/ngctl/cmd/version/version.go @@ -33,11 +33,7 @@ import ( operatorversion "github.com/vesoft-inc/nebula-operator/pkg/version" ) -var ( - versionExample = templates.Examples(` - # Print the cli and nebula operator version - ngctl version`) -) +var versionExample = templates.Examples(`# Print the cli and nebula operator version、ngctl version`) type ( // Options is a struct to support version command diff --git a/pkg/ngctl/config/config_test.go b/pkg/ngctl/config/config_test.go index 30ff3344..bca17cab 100644 --- a/pkg/ngctl/config/config_test.go +++ b/pkg/ngctl/config/config_test.go @@ -67,5 +67,4 @@ func TestLoadFromFileAndSaveToFile(t *testing.T) { t.Errorf("%d: Expected: \n%#v\n but actual: \n%#v\n", i, tc.expected, tc.config) } } - } From b720642b980f1893752fbe306425d96c99f3fff1 Mon Sep 17 00:00:00 2001 From: kqzh <35095889+kqzh@users.noreply.github.com> Date: Tue, 13 Jul 2021 15:10:13 +0800 Subject: [PATCH 10/12] fix bugs --- doc/user/ngctl_guide.md | 4 ++-- pkg/ngctl/cmd/check/check.go | 11 +++-------- pkg/ngctl/cmd/list/list.go | 8 +++----- pkg/ngctl/cmd/use/use.go | 2 +- pkg/ngctl/cmd/util/factory.go | 3 ++- pkg/ngctl/cmd/util/factory_test.go | 21 +++++++++++++++++---- pkg/ngctl/config/config_test.go | 22 +++++++++++++++++++--- 7 files changed, 47 insertions(+), 24 deletions(-) diff --git a/doc/user/ngctl_guide.md b/doc/user/ngctl_guide.md index e631df02..a51daaa0 100644 --- a/doc/user/ngctl_guide.md +++ b/doc/user/ngctl_guide.md @@ -1,6 +1,6 @@ # Overview -ngctl is a terminal cmd tool for nebula-operator, it has the following commands: +ngctl is a terminal cmd tool for Nebula Graph managed by nebula-operator, it has the following commands: - [ngctl use](#ngctl-use) - [ngctl console](#ngctl-console) - [ngctl list](#ngctl-list) @@ -15,7 +15,7 @@ Examples: # specify a nebula cluster to use ngctl use demo-cluster - # specify kubernetes context and namespace + # specify the cluster name and namespace ngctl use demo-cluster -n test-system ``` ## ngctl console diff --git a/pkg/ngctl/cmd/check/check.go b/pkg/ngctl/cmd/check/check.go index 6b1f2c74..db84bada 100644 --- a/pkg/ngctl/cmd/check/check.go +++ b/pkg/ngctl/cmd/check/check.go @@ -45,13 +45,10 @@ var ( ngctl check pods --nebulacluster=nebula`) ) -const resourceType = "nebulacluster" - type CheckOptions struct { Namespace string NebulaClusterName string ResourceType string - AllNamespaces bool runtimeCli client.Client genericclioptions.IOStreams @@ -97,6 +94,8 @@ func (o *CheckOptions) Complete(f cmdutil.Factory, args []string) error { if len(args) > 0 { o.ResourceType = args[0] + } else { + o.ResourceType = cmdutil.NebulaClusterResourceType } o.runtimeCli, err = f.ToRuntimeClient() @@ -113,17 +112,13 @@ func (o *CheckOptions) Validate(cmd *cobra.Command) error { return cmdutil.UsageErrorf(cmd, "using 'ngctl use' or '--nebulacluster' to set nebula cluster first.") } - if o.ResourceType == "" { - o.ResourceType = resourceType - } - return nil } // RunCheck executes check command func (o *CheckOptions) RunCheck() error { switch o.ResourceType { - case resourceType, "nebulaclusters", "nc": + case cmdutil.NebulaClusterResourceType, "nebulaclusters", "nc": { str, err := o.CheckNebulaCluster() if err != nil { diff --git a/pkg/ngctl/cmd/list/list.go b/pkg/ngctl/cmd/list/list.go index 499d225a..b932ed9b 100644 --- a/pkg/ngctl/cmd/list/list.go +++ b/pkg/ngctl/cmd/list/list.go @@ -162,6 +162,9 @@ func (o *ListOptions) Complete(f cmdutil.Factory, cmd *cobra.Command, args []str if len(args) > 0 { o.ResourceType = args[0] + } else { + o.ResourceType = cmdutil.NebulaClusterResourceType + o.NebulaClusterLabel = "" } o.SortBy, err = cmd.Flags().GetString("sort-by") @@ -235,11 +238,6 @@ func (o *ListOptions) Validate(cmd *cobra.Command) error { return cmdutil.UsageErrorf(cmd, "using 'ngctl use' or '--nebulacluster' to set nebula cluster first.") } - if o.ResourceType == "" { - o.ResourceType = "nebulacluster" - o.NebulaClusterLabel = "" - } - return nil } diff --git a/pkg/ngctl/cmd/use/use.go b/pkg/ngctl/cmd/use/use.go index 42a4f010..ade31b5c 100644 --- a/pkg/ngctl/cmd/use/use.go +++ b/pkg/ngctl/cmd/use/use.go @@ -41,7 +41,7 @@ var ( # specify a nebula cluster to use ngctl use demo-cluster - # specify kubernetes context and namespace + # specify the cluster name and namespace ngctl use --namespace=demo-ns demo-cluster`) useUsage = "expected 'use CLUSTER_NAME' for the use command" diff --git a/pkg/ngctl/cmd/util/factory.go b/pkg/ngctl/cmd/util/factory.go index 584361d3..53c27fe5 100644 --- a/pkg/ngctl/cmd/util/factory.go +++ b/pkg/ngctl/cmd/util/factory.go @@ -52,6 +52,8 @@ type ( } ) +const NebulaClusterResourceType = "nebulacluster" + var ( _ Factory = (*factoryImpl)(nil) errNotSpecified = errors.New("Not Specified") @@ -119,7 +121,6 @@ func (f *factoryImpl) GetNebulaClusterNamesAndNamespace(withUseConfig bool, args if f.nebulaClusterName != "" { return []string{f.nebulaClusterName}, namespace, nil } - if len(args) > 0 { return args, namespace, nil } diff --git a/pkg/ngctl/cmd/util/factory_test.go b/pkg/ngctl/cmd/util/factory_test.go index b1042e2f..72dedabd 100644 --- a/pkg/ngctl/cmd/util/factory_test.go +++ b/pkg/ngctl/cmd/util/factory_test.go @@ -17,6 +17,7 @@ limitations under the License. package util import ( + "os" "sync" "testing" @@ -37,7 +38,7 @@ func TestGetNebulaClusterConfig(t *testing.T) { }, factory: &factoryImpl{ nebulaClusterName: "", - nebulaClusterConfigFile: "~/.kube/config1", + nebulaClusterConfigFile: "", loadingLock: sync.Mutex{}, nebulaClusterConfig: nil, }, @@ -46,15 +47,27 @@ func TestGetNebulaClusterConfig(t *testing.T) { { factory: &factoryImpl{ nebulaClusterName: "", - nebulaClusterConfigFile: "~/.kube/config2", + nebulaClusterConfigFile: "", loadingLock: sync.Mutex{}, nebulaClusterConfig: nil, }, - expectedErr: false, + expectedErr: true, }, } + defer func() { + for _, tc := range testcases { + _ = os.Remove(tc.factory.nebulaClusterConfigFile) + } + }() + for i, tc := range testcases { + configFile, err := os.CreateTemp("", "") + if err != nil { + t.Error(err) + } + tc.factory.nebulaClusterConfigFile = configFile.Name() + if tc.config != nil { err := tc.config.SaveToFile(tc.factory.nebulaClusterConfigFile) if err != nil { @@ -62,7 +75,7 @@ func TestGetNebulaClusterConfig(t *testing.T) { } } - _, err := tc.factory.getNebulaClusterConfig() + _, err = tc.factory.getNebulaClusterConfig() if (err != nil) == tc.expectedErr { t.Errorf("%d: Expected: \n%#v\n but actual: \n%#v\n", i, tc.expectedErr, err != nil) } diff --git a/pkg/ngctl/config/config_test.go b/pkg/ngctl/config/config_test.go index bca17cab..70ad63a3 100644 --- a/pkg/ngctl/config/config_test.go +++ b/pkg/ngctl/config/config_test.go @@ -16,7 +16,10 @@ limitations under the License. package config -import "testing" +import ( + "os" + "testing" +) func TestLoadFromFileAndSaveToFile(t *testing.T) { testcases := []struct { @@ -29,7 +32,7 @@ func TestLoadFromFileAndSaveToFile(t *testing.T) { }{ { desc: "", - filename: "~/.kube/config1", + filename: "", config: NebulaClusterConfig{ ClusterName: "nebula", Namespace: "nebula-system", @@ -41,7 +44,7 @@ func TestLoadFromFileAndSaveToFile(t *testing.T) { }, { desc: "", - filename: "~/.kube/config2", + filename: "", config: NebulaClusterConfig{ ClusterName: "nebula", Namespace: "", @@ -53,7 +56,20 @@ func TestLoadFromFileAndSaveToFile(t *testing.T) { }, } + defer func() { + for _, tc := range testcases { + _ = os.Remove(tc.filename) + } + }() + for i, tc := range testcases { + configFile, err := os.CreateTemp("", "") + if err != nil { + t.Error(err) + } + + tc.filename = configFile.Name() + if err := tc.config.SaveToFile(tc.filename); err != nil { t.Error(err) } From de67c340453e932a3ea5af6e8293fa7ea1ed88c2 Mon Sep 17 00:00:00 2001 From: kqzh <35095889+kqzh@users.noreply.github.com> Date: Wed, 14 Jul 2021 11:29:28 +0800 Subject: [PATCH 11/12] adjust list command --- doc/user/ngctl_guide.md | 4 ---- pkg/ngctl/cmd/check/check.go | 7 ++----- pkg/ngctl/cmd/list/list.go | 15 ++++----------- pkg/ngctl/cmd/use/use.go | 2 +- pkg/ngctl/cmd/util/factory.go | 31 ------------------------------- 5 files changed, 7 insertions(+), 52 deletions(-) diff --git a/doc/user/ngctl_guide.md b/doc/user/ngctl_guide.md index a51daaa0..1dd61fe9 100644 --- a/doc/user/ngctl_guide.md +++ b/doc/user/ngctl_guide.md @@ -45,10 +45,6 @@ Examples: # List nebula cluster sub resources with specified cluster name. ngctl list pod --nebulacluster=nebula - # You can also use 'use' command to specify a nebula cluster. - use nebula - ngctl list pod - # Return only the metad's phase value of the specified pod. ngctl list -o template --template="{{.status.graphd.phase}}" NAME diff --git a/pkg/ngctl/cmd/check/check.go b/pkg/ngctl/cmd/check/check.go index db84bada..c18bfc90 100644 --- a/pkg/ngctl/cmd/check/check.go +++ b/pkg/ngctl/cmd/check/check.go @@ -84,11 +84,8 @@ func NewCmdCheck(f cmdutil.Factory, ioStreams genericclioptions.IOStreams) *cobr func (o *CheckOptions) Complete(f cmdutil.Factory, args []string) error { var err error - if o.NebulaClusterName, err = f.GetNebulaClusterName(); err != nil && !cmdutil.IsErNotSpecified(err) { - return err - } - - if o.Namespace, err = f.GetNamespace(); err != nil { + o.NebulaClusterName, o.Namespace, err = f.GetNebulaClusterNameAndNamespace(true, nil) + if err != nil && !cmdutil.IsErNotSpecified(err) { return err } diff --git a/pkg/ngctl/cmd/list/list.go b/pkg/ngctl/cmd/list/list.go index b932ed9b..c7d56be4 100644 --- a/pkg/ngctl/cmd/list/list.go +++ b/pkg/ngctl/cmd/list/list.go @@ -60,10 +60,6 @@ var ( # List nebula cluster sub resources with specified cluster name. ngctl list pod --nebulacluster=nebula - - # You can also use 'use' command to specify a nebula cluster. - use nebula - ngctl list pod # Return only the metad's phase value of the specified pod. ngctl list -o template --template="{{.status.graphd.phase}}" NAME @@ -150,16 +146,13 @@ func (o *ListOptions) AddFlags(cmd *cobra.Command) { func (o *ListOptions) Complete(f cmdutil.Factory, cmd *cobra.Command, args []string) error { var err error - if o.NebulaClusterName, err = f.GetNebulaClusterName(); err != nil && !cmdutil.IsErNotSpecified(err) { + o.NebulaClusterName, o.Namespace, err = f.GetNebulaClusterNameAndNamespace(false, nil) + if err != nil && !cmdutil.IsErNotSpecified(err) { return err } o.NebulaClusterLabel = label.ClusterLabelKey + "=" + o.NebulaClusterName - if o.Namespace, err = f.GetNamespace(); err != nil { - return err - } - if len(args) > 0 { o.ResourceType = args[0] } else { @@ -234,8 +227,8 @@ func (o *ListOptions) Validate(cmd *cobra.Command) error { } } - if o.NebulaClusterName == "" && o.ResourceType != "" { - return cmdutil.UsageErrorf(cmd, "using 'ngctl use' or '--nebulacluster' to set nebula cluster first.") + if o.NebulaClusterName == "" && o.ResourceType != cmdutil.NebulaClusterResourceType { + return cmdutil.UsageErrorf(cmd, "using '--nebulacluster' to set nebula cluster first.") } return nil diff --git a/pkg/ngctl/cmd/use/use.go b/pkg/ngctl/cmd/use/use.go index ade31b5c..ee6ff9ab 100644 --- a/pkg/ngctl/cmd/use/use.go +++ b/pkg/ngctl/cmd/use/use.go @@ -41,7 +41,7 @@ var ( # specify a nebula cluster to use ngctl use demo-cluster - # specify the cluster name and namespace + # specify the cluster name and namespace ngctl use --namespace=demo-ns demo-cluster`) useUsage = "expected 'use CLUSTER_NAME' for the use command" diff --git a/pkg/ngctl/cmd/util/factory.go b/pkg/ngctl/cmd/util/factory.go index 53c27fe5..222e52d5 100644 --- a/pkg/ngctl/cmd/util/factory.go +++ b/pkg/ngctl/cmd/util/factory.go @@ -36,9 +36,6 @@ type ( ToRuntimeClient() (client.Client, error) GetNebulaClusterNameAndNamespace(withUseConfig bool, args []string) (string, string, error) GetNebulaClusterNamesAndNamespace(withUseConfig bool, args []string) ([]string, string, error) - GetNebulaClusterName() (string, error) - // GetNebulaClusterNameWithoutConfig() string - GetNamespace() (string, error) GetNebulaClusterConfigFile() (string, error) } factoryImpl struct { @@ -84,24 +81,6 @@ func (f *factoryImpl) ToRuntimeClient() (client.Client, error) { return client.New(restConfig, client.Options{}) } -func (f *factoryImpl) GetNamespace() (string, error) { - namespace, enforceNamespace, err := f.ToRawKubeConfigLoader().Namespace() - if err != nil { - return "", err - } - - if enforceNamespace { - return namespace, err - } - - c, err := f.getNebulaClusterConfig() - if err != nil { - return namespace, nil - } - - return c.Namespace, err -} - func (f *factoryImpl) GetNebulaClusterNameAndNamespace(withUseConfig bool, args []string) (name, namespace string, err error) { var names []string names, namespace, err = f.GetNebulaClusterNamesAndNamespace(withUseConfig, args) @@ -117,7 +96,6 @@ func (f *factoryImpl) GetNebulaClusterNamesAndNamespace(withUseConfig bool, args if err != nil { return nil, "", err } - if f.nebulaClusterName != "" { return []string{f.nebulaClusterName}, namespace, nil } @@ -137,15 +115,6 @@ func (f *factoryImpl) GetNebulaClusterNamesAndNamespace(withUseConfig bool, args return []string{c.ClusterName}, c.Namespace, nil } -func (f *factoryImpl) GetNebulaClusterName() (string, error) { - return f.getNebulaClusterName(true) -} - -func (f *factoryImpl) GetNebulaClusterNameWithoutConfig() string { - name, _ := f.getNebulaClusterName(false) - return name -} - func (f *factoryImpl) getNebulaClusterName(withConfig bool) (string, error) { if !withConfig || f.nebulaClusterName != "" { return f.nebulaClusterName, nil From c7357b42c59a94ee69740ae2e2782a0e28662c9e Mon Sep 17 00:00:00 2001 From: Vee Zhang <14001308+veezhang@users.noreply.github.com> Date: Wed, 14 Jul 2021 12:02:01 +0800 Subject: [PATCH 12/12] fix --- pkg/ngctl/cmd/util/factory.go | 13 ------------- 1 file changed, 13 deletions(-) diff --git a/pkg/ngctl/cmd/util/factory.go b/pkg/ngctl/cmd/util/factory.go index 222e52d5..1dbb588b 100644 --- a/pkg/ngctl/cmd/util/factory.go +++ b/pkg/ngctl/cmd/util/factory.go @@ -115,19 +115,6 @@ func (f *factoryImpl) GetNebulaClusterNamesAndNamespace(withUseConfig bool, args return []string{c.ClusterName}, c.Namespace, nil } -func (f *factoryImpl) getNebulaClusterName(withConfig bool) (string, error) { - if !withConfig || f.nebulaClusterName != "" { - return f.nebulaClusterName, nil - } - - c, err := f.getNebulaClusterConfig() - if err != nil { - return "", errNotSpecified - } - - return c.ClusterName, nil -} - func (f *factoryImpl) GetNebulaClusterConfigFile() (string, error) { if f.nebulaClusterConfigFile != "" { return f.nebulaClusterConfigFile, nil