diff --git a/pkg/kubectl/cmd/top.go b/pkg/kubectl/cmd/top.go index 451e5bab7d4..63ca2bcc9dc 100644 --- a/pkg/kubectl/cmd/top.go +++ b/pkg/kubectl/cmd/top.go @@ -52,9 +52,8 @@ func NewCmdTop(f cmdutil.Factory, out, errOut io.Writer) *cobra.Command { } // create subcommands - topPod, _ := NewCmdTopPod(f, nil, out) cmd.AddCommand(NewCmdTopNode(f, nil, out)) - cmd.AddCommand(topPod) + cmd.AddCommand(NewCmdTopPod(f, nil, out)) return cmd } diff --git a/pkg/kubectl/cmd/top_pod.go b/pkg/kubectl/cmd/top_pod.go index 007dc1484b0..75f7906dfe9 100644 --- a/pkg/kubectl/cmd/top_pod.go +++ b/pkg/kubectl/cmd/top_pod.go @@ -78,7 +78,7 @@ var ( kubectl top pod -l name=myLabel`)) ) -func NewCmdTopPod(f cmdutil.Factory, options *TopPodOptions, out io.Writer) (*cobra.Command, *TopPodOptions) { +func NewCmdTopPod(f cmdutil.Factory, options *TopPodOptions, out io.Writer) *cobra.Command { if options == nil { options = &TopPodOptions{} } @@ -106,7 +106,7 @@ func NewCmdTopPod(f cmdutil.Factory, options *TopPodOptions, out io.Writer) (*co cmd.Flags().BoolVar(&options.PrintContainers, "containers", false, "If present, print usage of containers within a pod.") cmd.Flags().BoolVar(&options.AllNamespaces, "all-namespaces", false, "If present, list the requested object(s) across all namespaces. Namespace in current context is ignored even if specified with --namespace.") options.HeapsterOptions.Bind(cmd.Flags()) - return cmd, options + return cmd } func (o *TopPodOptions) Complete(f cmdutil.Factory, cmd *cobra.Command, args []string, out io.Writer) error { diff --git a/pkg/kubectl/cmd/top_pod_test.go b/pkg/kubectl/cmd/top_pod_test.go index e23371ae595..5edb9d52ddc 100644 --- a/pkg/kubectl/cmd/top_pod_test.go +++ b/pkg/kubectl/cmd/top_pod_test.go @@ -190,7 +190,7 @@ func TestTopPod(t *testing.T) { tf.ClientConfig = defaultClientConfig() buf := bytes.NewBuffer([]byte{}) - cmd, _ := NewCmdTopPod(f, nil, buf) + cmd := NewCmdTopPod(f, nil, buf) for name, value := range testCase.flags { cmd.Flags().Set(name, value) } @@ -226,7 +226,7 @@ func TestTopPodWithMetricsServer(t *testing.T) { testCases := []struct { name string namespace string - flags map[string]string + options *TopPodOptions args []string expectedPath string expectedQuery string @@ -236,7 +236,7 @@ func TestTopPodWithMetricsServer(t *testing.T) { }{ { name: "all namespaces", - flags: map[string]string{"all-namespaces": "true"}, + options: &TopPodOptions{AllNamespaces: true}, expectedPath: topMetricsAPIPathPrefix + "/pods", namespaces: []string{testNS, "secondtestns", "thirdtestns"}, listsNamespaces: true, @@ -254,14 +254,14 @@ func TestTopPodWithMetricsServer(t *testing.T) { }, { name: "pod with label selector", - flags: map[string]string{"selector": "key=value"}, + options: &TopPodOptions{Selector: "key=value"}, expectedPath: topMetricsAPIPathPrefix + "/namespaces/" + testNS + "/pods", expectedQuery: "labelSelector=" + url.QueryEscape("key=value"), namespaces: []string{testNS, testNS}, }, { name: "pod with container metrics", - flags: map[string]string{"containers": "true"}, + options: &TopPodOptions{PrintContainers: true}, args: []string{"pod1"}, expectedPath: topMetricsAPIPathPrefix + "/namespaces/" + testNS + "/pods/pod1", namespaces: []string{testNS}, @@ -326,9 +326,12 @@ func TestTopPodWithMetricsServer(t *testing.T) { tf.ClientConfig = defaultClientConfig() buf := bytes.NewBuffer([]byte{}) - cmd, cmdOptions := NewCmdTopPod(f, nil, buf) - for name, value := range testCase.flags { - cmd.Flags().Set(name, value) + cmd := NewCmdTopPod(f, nil, buf) + var cmdOptions *TopPodOptions + if testCase.options != nil { + cmdOptions = testCase.options + } else { + cmdOptions = &TopPodOptions{} } // TODO in the long run, we want to test most of our commands like this. Wire the options struct with specific mocks @@ -532,7 +535,7 @@ func TestTopPodCustomDefaults(t *testing.T) { }, DiscoveryClient: &fakeDiscovery{}, } - cmd, _ := NewCmdTopPod(f, opts, buf) + cmd := NewCmdTopPod(f, opts, buf) for name, value := range testCase.flags { cmd.Flags().Set(name, value) }