allow default option values - kube top node|pod

This commit is contained in:
juanvallejo 2017-08-15 14:59:56 -04:00
parent 5e0a539cf1
commit 5b8b1eb2eb
No known key found for this signature in database
GPG Key ID: 7D2C958002D6448D
5 changed files with 31 additions and 14 deletions

View File

@ -47,7 +47,7 @@ func NewCmdTop(f cmdutil.Factory, out, errOut io.Writer) *cobra.Command {
}
// create subcommands
cmd.AddCommand(NewCmdTopNode(f, out))
cmd.AddCommand(NewCmdTopPod(f, out))
cmd.AddCommand(NewCmdTopNode(f, nil, out))
cmd.AddCommand(NewCmdTopPod(f, nil, out))
return cmd
}

View File

@ -50,10 +50,23 @@ type HeapsterTopOptions struct {
}
func (o *HeapsterTopOptions) Bind(flags *pflag.FlagSet) {
flags.StringVar(&o.Namespace, "heapster-namespace", metricsutil.DefaultHeapsterNamespace, "Namespace Heapster service is located in")
flags.StringVar(&o.Service, "heapster-service", metricsutil.DefaultHeapsterService, "Name of Heapster service")
flags.StringVar(&o.Scheme, "heapster-scheme", metricsutil.DefaultHeapsterScheme, "Scheme (http or https) to connect to Heapster as")
flags.StringVar(&o.Port, "heapster-port", metricsutil.DefaultHeapsterPort, "Port name in service to use")
if len(o.Namespace) == 0 {
o.Namespace = metricsutil.DefaultHeapsterNamespace
}
if len(o.Service) == 0 {
o.Service = metricsutil.DefaultHeapsterService
}
if len(o.Scheme) == 0 {
o.Scheme = metricsutil.DefaultHeapsterScheme
}
if len(o.Port) == 0 {
o.Port = metricsutil.DefaultHeapsterPort
}
flags.StringVar(&o.Namespace, "heapster-namespace", o.Namespace, "Namespace Heapster service is located in")
flags.StringVar(&o.Service, "heapster-service", o.Service, "Name of Heapster service")
flags.StringVar(&o.Scheme, "heapster-scheme", o.Scheme, "Scheme (http or https) to connect to Heapster as")
flags.StringVar(&o.Port, "heapster-port", o.Port, "Port name in service to use")
}
var (
@ -70,8 +83,10 @@ var (
kubectl top node NODE_NAME`))
)
func NewCmdTopNode(f cmdutil.Factory, out io.Writer) *cobra.Command {
options := &TopNodeOptions{}
func NewCmdTopNode(f cmdutil.Factory, options *TopNodeOptions, out io.Writer) *cobra.Command {
if options == nil {
options = &TopNodeOptions{}
}
cmd := &cobra.Command{
Use: "node [NAME | -l label]",

View File

@ -67,7 +67,7 @@ func TestTopNodeAllMetrics(t *testing.T) {
tf.ClientConfig = defaultClientConfig()
buf := bytes.NewBuffer([]byte{})
cmd := NewCmdTopNode(f, buf)
cmd := NewCmdTopNode(f, nil, buf)
cmd.Run(cmd, []string{})
// Check the presence of node names in the output.
@ -116,7 +116,7 @@ func TestTopNodeWithNameMetrics(t *testing.T) {
tf.ClientConfig = defaultClientConfig()
buf := bytes.NewBuffer([]byte{})
cmd := NewCmdTopNode(f, buf)
cmd := NewCmdTopNode(f, nil, buf)
cmd.Run(cmd, []string{expectedMetrics.Name})
// Check the presence of node names in the output.
@ -176,7 +176,7 @@ func TestTopNodeWithLabelSelectorMetrics(t *testing.T) {
tf.ClientConfig = defaultClientConfig()
buf := bytes.NewBuffer([]byte{})
cmd := NewCmdTopNode(f, buf)
cmd := NewCmdTopNode(f, nil, buf)
cmd.Flags().Set("selector", label)
cmd.Run(cmd, []string{})

View File

@ -72,8 +72,10 @@ var (
kubectl top pod -l name=myLabel`))
)
func NewCmdTopPod(f cmdutil.Factory, out io.Writer) *cobra.Command {
options := &TopPodOptions{}
func NewCmdTopPod(f cmdutil.Factory, options *TopPodOptions, out io.Writer) *cobra.Command {
if options == nil {
options = &TopPodOptions{}
}
cmd := &cobra.Command{
Use: "pod [NAME | -l label]",

View File

@ -139,7 +139,7 @@ func TestTopPod(t *testing.T) {
tf.ClientConfig = defaultClientConfig()
buf := bytes.NewBuffer([]byte{})
cmd := NewCmdTopPod(f, buf)
cmd := NewCmdTopPod(f, nil, buf)
for name, value := range testCase.flags {
cmd.Flags().Set(name, value)
}