Skip to content

Commit

Permalink
Show workspace name in kubectl kcp ws tree
Browse files Browse the repository at this point in the history
With the changing structure of the URL stored in the Workspace spec, we
stopped showing the Workspace name in kubectl kcp ws tree. This updates
to use the Workspace name and fixes the output when --full is supplied.

Signed-off-by: hasheddan <georgedanielmangum@gmail.com>
  • Loading branch information
hasheddan committed Feb 1, 2023
1 parent 669dd8d commit 055a328
Showing 1 changed file with 20 additions and 15 deletions.
35 changes: 20 additions & 15 deletions pkg/cliplugins/workspace/plugin/kubeconfig.go
Original file line number Diff line number Diff line change
Expand Up @@ -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

0 comments on commit 055a328

Please sign in to comment.