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

🐛 Show workspace name in kubectl kcp ws tree #2719

Merged
merged 2 commits into from
Feb 1, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 21 additions & 16 deletions pkg/cliplugins/workspace/plugin/kubeconfig.go
Original file line number Diff line number Diff line change
Expand Up @@ -784,7 +784,7 @@ func NewTreeOptions(streams genericclioptions.IOStreams) *TreeOptions {
// BindFlags binds fields to cmd's flagset.
func (o *TreeOptions) BindFlags(cmd *cobra.Command) {
o.Options.BindFlags(cmd)
cmd.Flags().BoolVarP(&o.Full, "full", "f", o.Full, "Show full workspaces names")
cmd.Flags().BoolVarP(&o.Full, "full", "f", o.Full, "Show full workspace names")
}

// Complete ensures all dynamically populated fields are initialized.
Expand All @@ -808,30 +808,29 @@ func (o *TreeOptions) Run(ctx context.Context) error {
if err != nil {
return err
}
_, currentClusterName, err := pluginhelpers.ParseClusterURL(config.Host)
_, current, err := pluginhelpers.ParseClusterURL(config.Host)
if err != nil {
return fmt.Errorf("current config context URL %q does not point to workspace", config.Host)
}

tree := treeprint.New()
err = o.populateBranch(ctx, tree, currentClusterName)
if err != nil {
// NOTE(hasheddan): the cluster URL can be used for only the tree root as
// the friendly name is used in kubeconfig.
name := current.String()
if !o.Full {
name = name[strings.LastIndex(name, ":")+1:]
}
branch := tree.AddBranch(name)
if err := o.populateBranch(ctx, branch, current, name); err != nil {
return err
}

fmt.Println(tree.String())
return nil
}

func (o *TreeOptions) populateBranch(ctx context.Context, tree treeprint.Tree, name logicalcluster.Path) error {
var b treeprint.Tree
if o.Full {
b = tree.AddBranch(name.String())
} else {
b = tree.AddBranch(name.Base())
}

results, err := o.kcpClusterClient.Cluster(name).TenancyV1alpha1().Workspaces().List(ctx, metav1.ListOptions{})
func (o *TreeOptions) populateBranch(ctx context.Context, tree treeprint.Tree, parent logicalcluster.Path, parentName string) error {
results, err := o.kcpClusterClient.Cluster(parent).TenancyV1alpha1().Workspaces().List(ctx, metav1.ListOptions{})
if err != nil {
if apierrors.IsNotFound(err) {
return nil
Expand All @@ -840,12 +839,18 @@ func (o *TreeOptions) populateBranch(ctx context.Context, tree treeprint.Tree, n
}

for _, workspace := range results.Items {
_, currentClusterName, err := pluginhelpers.ParseClusterURL(workspace.Spec.URL)
_, current, err := pluginhelpers.ParseClusterURL(workspace.Spec.URL)
if err != nil {
return fmt.Errorf("current config context URL %q does not point to workspace", workspace.Spec.URL)
}
err = o.populateBranch(ctx, b, currentClusterName)
if err != nil {
// NOTE(hasheddan): the cluster URL from the Workspace does not use the
// friendly name, so we use the Workspace name instead.
name := workspace.Name
if o.Full {
name = parentName + ":" + name
}
branch := tree.AddBranch(name)
if err := o.populateBranch(ctx, branch, current, name); err != nil {
return err
}
}
Expand Down