Skip to content

Commit

Permalink
ctp-list: adjust logic
Browse files Browse the repository at this point in the history
Signed-off-by: Piotr Zaniewski <piotr@upbound.io>
  • Loading branch information
Piotr1215 committed Apr 4, 2024
1 parent da4642b commit c8233a3
Showing 1 changed file with 29 additions and 0 deletions.
29 changes: 29 additions & 0 deletions cmd/up/controlplane/list.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,10 @@ import (
"github.com/upbound/up/internal/controlplane"
"github.com/upbound/up/internal/controlplane/cloud"
"github.com/upbound/up/internal/controlplane/space"
"github.com/upbound/up/internal/profile"
"github.com/upbound/up/internal/upbound"
"github.com/upbound/up/internal/upterm"
"k8s.io/client-go/tools/clientcmd"
)

type ctpLister interface {
Expand Down Expand Up @@ -75,7 +77,28 @@ func (c *listCmd) AfterApply(kongCtx *kong.Context, upCtx *upbound.Context) erro

// Run executes the list command.
func (c *listCmd) Run(ctx context.Context, printer upterm.ObjectPrinter, p pterm.TextPrinter, upCtx *upbound.Context) error {
// find profile and derive controlplane from kubeconfig
profiles, err := upCtx.Cfg.GetUpboundProfiles()
if err != nil {
return err
}
po := clientcmd.NewDefaultPathOptions()
conf, err := po.GetStartingConfig()
if err != nil {
return err
}

_, _, ctp, err := profile.FromKubeconfig(ctx, profiles, conf)

if err != nil {
return err
}
if p == nil {
return nil
}

l, err := c.client.List(ctx, c.deriveGroup())

if controlplane.IsNotFound(err) {
p.Printfln("No Control planes found in %s group", c.deriveGroup())
return nil
Expand All @@ -89,6 +112,12 @@ func (c *listCmd) Run(ctx context.Context, printer upterm.ObjectPrinter, p pterm
return nil
}

for _, resp := range l {
if ctp.Name == resp.Name {
p.Printfln("Cannot list control from inside a controlplane, use `up ctx ..` to switch to a goup level")
return nil
}
}
return tabularPrint(l, printer, upCtx)
}

Expand Down

0 comments on commit c8233a3

Please sign in to comment.