Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make ctp list derive context from kubeconfig #457

Merged
merged 1 commit into from
Apr 30, 2024
Merged

Conversation

Piotr1215
Copy link

@Piotr1215 Piotr1215 commented Apr 2, 2024

Description of your changes

This PR makes up ctp list kubeconfig context aware, making it independent from the current profile. The profile is automatically derived:

$ kubectl ctx not-space-kube
$ up ctp list
You are not on a space.
$ kubectl ctx space
$ up ctp list
.... controlplanes from the profile matching the kubeconfig context's URL ....
$ up ctp connect ctp
$ up ctp list
In a controlplane, you cannot list controlplanes.
Use 'up ctx ..' to go to group level.

Fixes https://github.com/upbound/spaces/issues/752

NOTE: this breaks the use against a cloud profile. We will bring it back in a follow-up.

I have:

  • Read and followed Upbound's contribution process.
  • Run make reviewable to ensure this PR is ready for review.
  • Added backport release-x.y labels to auto-backport this PR, as appropriate.

How has this code been tested

This can be tested locally by running the following setup:

  • create local kind cluster with spaces
  • create empty kind cluster
  • follow the command logic from description

@Piotr1215 Piotr1215 requested a review from sttts April 2, 2024 14:28
cmd/up/controlplane/list.go Outdated Show resolved Hide resolved
cmd/up/controlplane/list.go Outdated Show resolved Hide resolved
cmd/up/controlplane/list.go Outdated Show resolved Hide resolved
justfile Outdated Show resolved Hide resolved
cmd/up/controlplane/list.go Outdated Show resolved Hide resolved
cmd/up/controlplane/list.go Outdated Show resolved Hide resolved
Comment on lines 57 to 70
po := clientcmd.NewDefaultPathOptions()
conf, err := po.GetStartingConfig()
if err != nil {
return err
}
profiles, err := upCtx.Cfg.GetUpboundProfiles()
if err != nil {
return err
}
if err != nil {
return err
}

_, curprof, ctp, err := profile.FromKubeconfig(ctx, profiles, conf)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Dieser ganze Block wird häufig auftreten. Wir sollten einen Helper dazu erstellen:

profileName, profile, ctp, restConfig, err := upCtx.Cfg.GetCurrentContext(ctx)

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

English translation:

This whole block will occur frequently. We should create a helper for this:

cmd/up/controlplane/list.go Outdated Show resolved Hide resolved
cmd/up/controlplane/list.go Outdated Show resolved Hide resolved
internal/config/config.go Outdated Show resolved Hide resolved
internal/config/config.go Outdated Show resolved Hide resolved
@@ -122,6 +125,26 @@ func (c *Config) GetUpboundProfile(name string) (profile.Profile, error) {
return p, nil
}

// GetCurrentContext returns the current context from the kubeconfig, profile and config
func (c *Config) GetCurrentContext(ctx context.Context) (string, *profile.Profile, types.NamespacedName, error) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

add names to the return values. It's unclear what the string is.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

also to FromKubeconfig please. I also forgot that.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

added return named values to both functions

@Piotr1215 Piotr1215 requested a review from sttts April 15, 2024 13:27
@sttts sttts marked this pull request as ready for review April 16, 2024 15:31
@sttts sttts changed the title WIP [DO NOT MERGE]: Adjust ctp-list logic Make ctp list derive context from kubeconfig Apr 16, 2024
Signed-off-by: Piotr Zaniewski <piotr@upbound.io>
Signed-off-by: Dr. Stefan Schimanski <stefan.schimanski@upbound.com>
c.client = cloud.New(ctpclient, cfgclient, upCtx.Account)
}

func (c *listCmd) AfterApply(kongCtx *kong.Context) error {
kongCtx.Bind(pterm.DefaultTable.WithWriter(kongCtx.Stdout).WithSeparator(" "))
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should we check for mutual exclusivity with -g and -A?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In kube -A trumps over -n afaik. No strong opinion whether we are stricter here.

@jeanduplessis jeanduplessis merged commit 8632f15 into main Apr 30, 2024
6 checks passed
@jeanduplessis jeanduplessis deleted the ctp-list-fix branch April 30, 2024 06:55
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

5 participants