Skip to content

Commit

Permalink
add --all-namespaces flag to kapp ls
Browse files Browse the repository at this point in the history
  • Loading branch information
cppforlife committed Jun 25, 2019
1 parent fa21703 commit 3d5a348
Show file tree
Hide file tree
Showing 5 changed files with 28 additions and 4 deletions.
6 changes: 5 additions & 1 deletion pkg/kapp/app/apps.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

Expand All @@ -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
Expand Down
1 change: 1 addition & 0 deletions pkg/kapp/app/interfaces.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (

type App interface {
Name() string
Namespace() string
Meta() (AppMeta, error)
LabelSelector() (labels.Selector, error)

Expand Down
2 changes: 2 additions & 0 deletions pkg/kapp/app/labeled_app.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down
3 changes: 2 additions & 1 deletion pkg/kapp/app/recorded_app.go
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down
20 changes: 18 additions & 2 deletions pkg/kapp/cmd/app/list.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ type ListOptions struct {
depsFactory cmdcore.DepsFactory

NamespaceFlags cmdcore.NamespaceFlags
AllNamespaces bool
}

func NewListOptions(ui ui.UI, depsFactory cmdcore.DepsFactory) *ListOptions {
Expand All @@ -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
}

Expand All @@ -45,18 +47,30 @@ 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 {
return err
}

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"),
Expand All @@ -66,6 +80,7 @@ func (o *ListOptions) Run() error {

SortBy: []uitable.ColumnSort{
{Column: 0, Asc: true},
{Column: 1, Asc: true},
},
}

Expand All @@ -76,6 +91,7 @@ func (o *ListOptions) Run() error {
}

row := []uitable.Value{
uitable.NewValueString(item.Namespace()),
uitable.NewValueString(item.Name()),
uitable.NewValueString(sel.String()),
}
Expand Down

0 comments on commit 3d5a348

Please sign in to comment.