add --all-namespaces flag to request across all namespaces

This commit is contained in:
Masahiro Sano 2015-05-12 20:14:31 +09:00
parent d9d12fd3f7
commit 8ce64ec69e
11 changed files with 50 additions and 30 deletions

View File

@ -229,6 +229,7 @@ _kubectl_get()
flags_with_completion=()
flags_completion=()
flags+=("--all-namespaces")
flags+=("--help")
flags+=("-h")
flags+=("--no-headers")

View File

@ -65,6 +65,6 @@ kubectl
* [kubectl update](kubectl_update.md) - Update a resource by filename or stdin.
* [kubectl version](kubectl_version.md) - Print the client and server version information.
###### Auto generated by spf13/cobra at 2015-05-15 00:05:04.556347262 +0000 UTC
###### Auto generated by spf13/cobra at 2015-05-22 14:24:30.1784975 +0000 UTC
[![Analytics](https://kubernetes-site.appspot.com/UA-36037335-10/GitHub/docs/kubectl.md?pixel)]()

View File

@ -42,6 +42,7 @@ $ kubectl get rc/web service/frontend pods/web-pod-13je7
### Options
```
--all-namespaces=false: If present, list the requested object(s) across all namespaces. Namespace in current context is ignored even if specified with --namespace.
-h, --help=false: help for get
--no-headers=false: When using the default output, don't print headers.
-o, --output="": Output format. One of: json|yaml|template|templatefile.
@ -84,6 +85,6 @@ $ kubectl get rc/web service/frontend pods/web-pod-13je7
### SEE ALSO
* [kubectl](kubectl.md) - kubectl controls the Kubernetes cluster manager
###### Auto generated by spf13/cobra at 2015-05-20 23:52:21.968486735 +0000 UTC
###### Auto generated by spf13/cobra at 2015-05-22 14:24:30.17132893 +0000 UTC
[![Analytics](https://kubernetes-site.appspot.com/UA-36037335-10/GitHub/docs/kubectl_get.md?pixel)]()

View File

@ -25,6 +25,10 @@ of the \-\-template flag, you can filter the attributes of the fetched resource(
.SH OPTIONS
.PP
\fB\-\-all\-namespaces\fP=false
If present, list the requested object(s) across all namespaces. Namespace in current context is ignored even if specified with \-\-namespace.
.PP
\fB\-h\fP, \fB\-\-help\fP=false
help for get

View File

@ -138,7 +138,7 @@ func NewTestFactory() (*cmdutil.Factory, *testFactory, runtime.Codec) {
Describer: func(*meta.RESTMapping) (kubectl.Describer, error) {
return t.Describer, t.Err
},
Printer: func(mapping *meta.RESTMapping, noHeaders bool) (kubectl.ResourcePrinter, error) {
Printer: func(mapping *meta.RESTMapping, noHeaders, withNamespace bool) (kubectl.ResourcePrinter, error) {
return t.Printer, t.Err
},
Validator: func() (validation.Schema, error) {
@ -192,7 +192,7 @@ func NewAPIFactory() (*cmdutil.Factory, *testFactory, runtime.Codec) {
Describer: func(*meta.RESTMapping) (kubectl.Describer, error) {
return t.Describer, t.Err
},
Printer: func(mapping *meta.RESTMapping, noHeaders bool) (kubectl.ResourcePrinter, error) {
Printer: func(mapping *meta.RESTMapping, noHeaders, withNamespace bool) (kubectl.ResourcePrinter, error) {
return t.Printer, t.Err
},
Validator: func() (validation.Schema, error) {
@ -239,7 +239,7 @@ func TestClientVersions(t *testing.T) {
func ExamplePrintReplicationController() {
f, tf, codec := NewAPIFactory()
tf.Printer = kubectl.NewHumanReadablePrinter(false)
tf.Printer = kubectl.NewHumanReadablePrinter(false, false)
tf.Client = &client.FakeRESTClient{
Codec: codec,
Client: nil,

View File

@ -58,7 +58,7 @@ $ kubectl get rc/web service/frontend pods/web-pod-13je7`
// NewCmdGet creates a command object for the generic "get" action, which
// retrieves one or more resources from a server.
func NewCmdGet(f *cmdutil.Factory, out io.Writer) *cobra.Command {
p := kubectl.NewHumanReadablePrinter(false)
p := kubectl.NewHumanReadablePrinter(false, false)
validArgs := p.HandledResources()
cmd := &cobra.Command{
@ -76,6 +76,7 @@ func NewCmdGet(f *cmdutil.Factory, out io.Writer) *cobra.Command {
cmd.Flags().StringP("selector", "l", "", "Selector (label query) to filter on")
cmd.Flags().BoolP("watch", "w", false, "After listing/getting the requested object, watch for changes.")
cmd.Flags().Bool("watch-only", false, "Watch for changes to the requested object(s), without listing/getting first.")
cmd.Flags().Bool("all-namespaces", false, "If present, list the requested object(s) across all namespaces. Namespace in current context is ignored even if specified with --namespace.")
return cmd
}
@ -83,6 +84,7 @@ func NewCmdGet(f *cmdutil.Factory, out io.Writer) *cobra.Command {
// TODO: convert all direct flag accessors to a struct and pass that instead of cmd
func RunGet(f *cmdutil.Factory, out io.Writer, cmd *cobra.Command, args []string) error {
selector := cmdutil.GetFlagString(cmd, "selector")
allNamespaces := cmdutil.GetFlagBool(cmd, "all-namespaces")
mapper, typer := f.Object()
cmdNamespace, err := f.DefaultNamespace()
@ -94,7 +96,7 @@ func RunGet(f *cmdutil.Factory, out io.Writer, cmd *cobra.Command, args []string
isWatch, isWatchOnly := cmdutil.GetFlagBool(cmd, "watch"), cmdutil.GetFlagBool(cmd, "watch-only")
if isWatch || isWatchOnly {
r := resource.NewBuilder(mapper, typer, f.ClientMapperForCommand()).
NamespaceParam(cmdNamespace).DefaultNamespace().
NamespaceParam(cmdNamespace).DefaultNamespace().AllNamespaces(allNamespaces).
SelectorParam(selector).
ResourceTypeOrNameArgs(true, args...).
SingleResourceType().
@ -108,7 +110,7 @@ func RunGet(f *cmdutil.Factory, out io.Writer, cmd *cobra.Command, args []string
return err
}
printer, err := f.PrinterForMapping(cmd, mapping)
printer, err := f.PrinterForMapping(cmd, mapping, allNamespaces)
if err != nil {
return err
}
@ -143,7 +145,7 @@ func RunGet(f *cmdutil.Factory, out io.Writer, cmd *cobra.Command, args []string
}
b := resource.NewBuilder(mapper, typer, f.ClientMapperForCommand()).
NamespaceParam(cmdNamespace).DefaultNamespace().
NamespaceParam(cmdNamespace).DefaultNamespace().AllNamespaces(allNamespaces).
SelectorParam(selector).
ResourceTypeOrNameArgs(true, args...).
ContinueOnError().
@ -180,7 +182,7 @@ func RunGet(f *cmdutil.Factory, out io.Writer, cmd *cobra.Command, args []string
// use the default printer for each object
return b.Do().Visit(func(r *resource.Info) error {
printer, err := f.PrinterForMapping(cmd, r.Mapping)
printer, err := f.PrinterForMapping(cmd, r.Mapping, allNamespaces)
if err != nil {
return err
}

View File

@ -220,7 +220,7 @@ func RunLabel(f *cmdutil.Factory, out io.Writer, cmd *cobra.Command, args []stri
return err
}
printer, err := f.PrinterForMapping(cmd, info.Mapping)
printer, err := f.PrinterForMapping(cmd, info.Mapping, false)
if err != nil {
return err
}

View File

@ -64,7 +64,7 @@ type Factory struct {
// Returns a Describer for displaying the specified RESTMapping type or an error.
Describer func(mapping *meta.RESTMapping) (kubectl.Describer, error)
// Returns a Printer for formatting objects of the given type or an error.
Printer func(mapping *meta.RESTMapping, noHeaders bool) (kubectl.ResourcePrinter, error)
Printer func(mapping *meta.RESTMapping, noHeaders, withNamespace bool) (kubectl.ResourcePrinter, error)
// Returns a Resizer for changing the size of the specified RESTMapping type or an error
Resizer func(mapping *meta.RESTMapping) (kubectl.Resizer, error)
// Returns a Reaper for gracefully shutting down resources.
@ -143,8 +143,8 @@ func NewFactory(optionalClientConfig clientcmd.ClientConfig) *Factory {
}
return describer, nil
},
Printer: func(mapping *meta.RESTMapping, noHeaders bool) (kubectl.ResourcePrinter, error) {
return kubectl.NewHumanReadablePrinter(noHeaders), nil
Printer: func(mapping *meta.RESTMapping, noHeaders, withNamespace bool) (kubectl.ResourcePrinter, error) {
return kubectl.NewHumanReadablePrinter(noHeaders, withNamespace), nil
},
PodSelectorForObject: func(object runtime.Object) (string, error) {
// TODO: replace with a swagger schema based approach (identify pod selector via schema introspection)
@ -343,7 +343,7 @@ func (f *Factory) PrintObject(cmd *cobra.Command, obj runtime.Object, out io.Wri
return err
}
printer, err := f.PrinterForMapping(cmd, mapping)
printer, err := f.PrinterForMapping(cmd, mapping, false)
if err != nil {
return err
}
@ -352,7 +352,7 @@ func (f *Factory) PrintObject(cmd *cobra.Command, obj runtime.Object, out io.Wri
// PrinterForMapping returns a printer suitable for displaying the provided resource type.
// Requires that printer flags have been added to cmd (see AddPrinterFlags).
func (f *Factory) PrinterForMapping(cmd *cobra.Command, mapping *meta.RESTMapping) (kubectl.ResourcePrinter, error) {
func (f *Factory) PrinterForMapping(cmd *cobra.Command, mapping *meta.RESTMapping, withNamespace bool) (kubectl.ResourcePrinter, error) {
printer, ok, err := PrinterForCommand(cmd)
if err != nil {
return nil, err
@ -373,7 +373,7 @@ func (f *Factory) PrinterForMapping(cmd *cobra.Command, mapping *meta.RESTMappin
}
printer = kubectl.NewVersionedPrinter(printer, mapping.ObjectConvertor, version, mapping.APIVersion)
} else {
printer, err = f.Printer(mapping, GetFlagBool(cmd, "no-headers"))
printer, err = f.Printer(mapping, GetFlagBool(cmd, "no-headers"), withNamespace)
if err != nil {
return nil, err
}

View File

@ -23,6 +23,7 @@ import (
"os"
"strings"
"github.com/GoogleCloudPlatform/kubernetes/pkg/api"
"github.com/GoogleCloudPlatform/kubernetes/pkg/api/meta"
"github.com/GoogleCloudPlatform/kubernetes/pkg/api/validation"
"github.com/GoogleCloudPlatform/kubernetes/pkg/labels"
@ -220,6 +221,15 @@ func (b *Builder) DefaultNamespace() *Builder {
return b
}
// AllNamespaces instructs the builder to use NamespaceAll as a namespace to request resources
// acroll all namespace. This overrides the namespace set by NamespaceParam().
func (b *Builder) AllNamespaces(allNamespace bool) *Builder {
if allNamespace {
b.namespace = api.NamespaceAll
}
return b
}
// RequireNamespace instructs the builder to set the namespace value for any object found
// to NamespaceParam() if empty, and if the value on the resource does not match
// NamespaceParam() an error will be returned.

View File

@ -177,16 +177,18 @@ type handlerEntry struct {
// will only be printed if the object type changes. This makes it useful for printing items
// recieved from watches.
type HumanReadablePrinter struct {
handlerMap map[reflect.Type]*handlerEntry
noHeaders bool
lastType reflect.Type
handlerMap map[reflect.Type]*handlerEntry
noHeaders bool
withNamespace bool
lastType reflect.Type
}
// NewHumanReadablePrinter creates a HumanReadablePrinter.
func NewHumanReadablePrinter(noHeaders bool) *HumanReadablePrinter {
func NewHumanReadablePrinter(noHeaders, withNamespace bool) *HumanReadablePrinter {
printer := &HumanReadablePrinter{
handlerMap: make(map[reflect.Type]*handlerEntry),
noHeaders: noHeaders,
handlerMap: make(map[reflect.Type]*handlerEntry),
noHeaders: noHeaders,
withNamespace: withNamespace,
}
printer.addDefaultHandlers()
return printer

View File

@ -248,7 +248,7 @@ func ErrorPrintHandler(obj *TestPrintType, w io.Writer) error {
func TestCustomTypePrinting(t *testing.T) {
columns := []string{"Data"}
printer := NewHumanReadablePrinter(false)
printer := NewHumanReadablePrinter(false, false)
printer.Handler(columns, PrintCustomType)
obj := TestPrintType{"test object"}
@ -265,7 +265,7 @@ func TestCustomTypePrinting(t *testing.T) {
func TestPrintHandlerError(t *testing.T) {
columns := []string{"Data"}
printer := NewHumanReadablePrinter(false)
printer := NewHumanReadablePrinter(false, false)
printer.Handler(columns, ErrorPrintHandler)
obj := TestPrintType{"test object"}
buffer := &bytes.Buffer{}
@ -276,7 +276,7 @@ func TestPrintHandlerError(t *testing.T) {
}
func TestUnknownTypePrinting(t *testing.T) {
printer := NewHumanReadablePrinter(false)
printer := NewHumanReadablePrinter(false, false)
buffer := &bytes.Buffer{}
err := printer.PrintObj(&TestUnknownType{}, buffer)
if err == nil {
@ -465,8 +465,8 @@ func TestPrinters(t *testing.T) {
t.Fatal(err)
}
printers := map[string]ResourcePrinter{
"humanReadable": NewHumanReadablePrinter(true),
"humanReadableHeaders": NewHumanReadablePrinter(false),
"humanReadable": NewHumanReadablePrinter(true, false),
"humanReadableHeaders": NewHumanReadablePrinter(false, false),
"json": &JSONPrinter{},
"yaml": &YAMLPrinter{},
"template": templatePrinter,
@ -503,7 +503,7 @@ func TestPrinters(t *testing.T) {
func TestPrintEventsResultSorted(t *testing.T) {
// Arrange
printer := NewHumanReadablePrinter(false /* noHeaders */)
printer := NewHumanReadablePrinter(false /* noHeaders */, false)
obj := api.EventList{
Items: []api.Event{
@ -544,7 +544,7 @@ func TestPrintEventsResultSorted(t *testing.T) {
}
func TestPrintMinionStatus(t *testing.T) {
printer := NewHumanReadablePrinter(false)
printer := NewHumanReadablePrinter(false, false)
table := []struct {
minion api.Node
status string