From 3d5a348d343255a707c8833c21398599a6c2bf2e Mon Sep 17 00:00:00 2001 From: Dmitriy Kalinin Date: Tue, 25 Jun 2019 11:17:59 -0700 Subject: [PATCH] add --all-namespaces flag to kapp ls --- pkg/kapp/app/apps.go | 6 +++++- pkg/kapp/app/interfaces.go | 1 + pkg/kapp/app/labeled_app.go | 2 ++ pkg/kapp/app/recorded_app.go | 3 ++- pkg/kapp/cmd/app/list.go | 20 ++++++++++++++++++-- 5 files changed, 28 insertions(+), 4 deletions(-) diff --git a/pkg/kapp/app/apps.go b/pkg/kapp/app/apps.go index 2073e123d..356d3d650 100644 --- a/pkg/kapp/app/apps.go +++ b/pkg/kapp/app/apps.go @@ -42,6 +42,10 @@ func (a Apps) Find(name string) (App, error) { return &LabeledApp{sel, a.coreClient, a.dynamicClient}, nil } + if len(a.nsName) == 0 { + return nil, fmt.Errorf("Expected non-empty namespace") + } + return &RecordedApp{name, a.nsName, a.coreClient, a.dynamicClient, nil}, nil } @@ -67,7 +71,7 @@ func (a Apps) List(additionalLabels map[string]string) ([]App, error) { for _, app := range apps.Items { meta := NewAppMetaFromData(app.Data) - result = append(result, &RecordedApp{app.Name, a.nsName, a.coreClient, a.dynamicClient, &meta}) + result = append(result, &RecordedApp{app.Name, app.Namespace, a.coreClient, a.dynamicClient, &meta}) } return result, nil diff --git a/pkg/kapp/app/interfaces.go b/pkg/kapp/app/interfaces.go index 8f030cb84..08aa24349 100644 --- a/pkg/kapp/app/interfaces.go +++ b/pkg/kapp/app/interfaces.go @@ -6,6 +6,7 @@ import ( type App interface { Name() string + Namespace() string Meta() (AppMeta, error) LabelSelector() (labels.Selector, error) diff --git a/pkg/kapp/app/labeled_app.go b/pkg/kapp/app/labeled_app.go index 983dd50ed..556ef699d 100644 --- a/pkg/kapp/app/labeled_app.go +++ b/pkg/kapp/app/labeled_app.go @@ -27,6 +27,8 @@ func (a *LabeledApp) Name() string { return str } +func (a *LabeledApp) Namespace() string { return "" } + func (a *LabeledApp) LabelSelector() (labels.Selector, error) { return a.labelSelector, nil } diff --git a/pkg/kapp/app/recorded_app.go b/pkg/kapp/app/recorded_app.go index 971bd764c..522d8574e 100644 --- a/pkg/kapp/app/recorded_app.go +++ b/pkg/kapp/app/recorded_app.go @@ -28,7 +28,8 @@ type RecordedApp struct { var _ App = &RecordedApp{} -func (a *RecordedApp) Name() string { return a.name } +func (a *RecordedApp) Name() string { return a.name } +func (a *RecordedApp) Namespace() string { return a.nsName } func (a *RecordedApp) LabelSelector() (labels.Selector, error) { app, err := a.labeledApp() diff --git a/pkg/kapp/cmd/app/list.go b/pkg/kapp/cmd/app/list.go index dfe7fc18b..dc585f563 100644 --- a/pkg/kapp/cmd/app/list.go +++ b/pkg/kapp/cmd/app/list.go @@ -17,6 +17,7 @@ type ListOptions struct { depsFactory cmdcore.DepsFactory NamespaceFlags cmdcore.NamespaceFlags + AllNamespaces bool } func NewListOptions(ui ui.UI, depsFactory cmdcore.DepsFactory) *ListOptions { @@ -31,6 +32,7 @@ func NewListCmd(o *ListOptions, flagsFactory cmdcore.FlagsFactory) *cobra.Comman RunE: func(_ *cobra.Command, _ []string) error { return o.Run() }, } o.NamespaceFlags.Set(cmd, flagsFactory) + cmd.Flags().BoolVar(&o.AllNamespaces, "all-namespaces", false, "List apps in all namespaces") return cmd } @@ -45,7 +47,18 @@ func (o *ListOptions) Run() error { return err } - apps := ctlapp.NewApps(o.NamespaceFlags.Name, coreClient, dynamicClient) + nsName := o.NamespaceFlags.Name + tableTitle := fmt.Sprintf("Apps in namespace '%s'", o.NamespaceFlags.Name) + nsHeader := uitable.NewHeader("Namespace") + nsHeader.Hidden = true + + if o.AllNamespaces { + nsName = "" + tableTitle = "Apps in all namespaces" + nsHeader.Hidden = false + } + + apps := ctlapp.NewApps(nsName, coreClient, dynamicClient) items, err := apps.List(nil) if err != nil { @@ -53,10 +66,11 @@ func (o *ListOptions) Run() error { } table := uitable.Table{ - Title: fmt.Sprintf("Apps in namespace '%s'", o.NamespaceFlags.Name), + Title: tableTitle, Content: "apps", Header: []uitable.Header{ + nsHeader, uitable.NewHeader("Name"), uitable.NewHeader("Label"), uitable.NewHeader("Namespaces"), @@ -66,6 +80,7 @@ func (o *ListOptions) Run() error { SortBy: []uitable.ColumnSort{ {Column: 0, Asc: true}, + {Column: 1, Asc: true}, }, } @@ -76,6 +91,7 @@ func (o *ListOptions) Run() error { } row := []uitable.Value{ + uitable.NewValueString(item.Namespace()), uitable.NewValueString(item.Name()), uitable.NewValueString(sel.String()), }