diff --git a/pkg/cliplugins/workspace/plugin/kubeconfig.go b/pkg/cliplugins/workspace/plugin/kubeconfig.go index 1efeb95b769..7d41f8aa171 100644 --- a/pkg/cliplugins/workspace/plugin/kubeconfig.go +++ b/pkg/cliplugins/workspace/plugin/kubeconfig.go @@ -215,8 +215,21 @@ func (o *UseWorkspaceOptions) Run(ctx context.Context) error { if err != nil { return err } - newServerHost = homeWorkspace.Spec.URL - + u, homeCluster, err := pluginhelpers.ParseClusterURL(homeWorkspace.Spec.URL) + if err != nil { + return fmt.Errorf("home URL %q does not point to a workspace: %w", homeWorkspace.Spec.URL, err) + } + homeLogicalCluster, err := o.kcpClusterClient.Cluster(homeCluster).CoreV1alpha1().LogicalClusters().Get(ctx, corev1alpha1.LogicalClusterName, metav1.GetOptions{}) + if err != nil { + return fmt.Errorf("failed to get logical cluster of home workspace: %w", err) + } + homePath, ok := homeLogicalCluster.Annotations[core.LogicalClusterPathAnnotationKey] + if !ok { + return fmt.Errorf("cannot find the path info from the home workspace") + } + cluster := logicalcluster.NewPath(homePath) + u.Path = path.Join(u.Path, cluster.RequestPath()) + newServerHost = u.String() case ".": cfg, err := o.ClientConfig.ClientConfig() if err != nil { diff --git a/pkg/cliplugins/workspace/plugin/kubeconfig_test.go b/pkg/cliplugins/workspace/plugin/kubeconfig_test.go index 2aff8683530..f952a24e6f7 100644 --- a/pkg/cliplugins/workspace/plugin/kubeconfig_test.go +++ b/pkg/cliplugins/workspace/plugin/kubeconfig_test.go @@ -1232,6 +1232,25 @@ func TestUse(t *testing.T) { return false, nil, nil }) + client.PrependReactor("get", "logicalclusters", func(action kcptesting.Action) (handled bool, ret runtime.Object, err error) { + getAction := action.(kcptesting.GetAction) + if getAction.GetCluster() != homeWorkspaceLogicalCluster { + return false, nil, nil + } + if getAction.GetName() == corev1alpha1.LogicalClusterName { + logicalCluster := &corev1alpha1.LogicalCluster{ + ObjectMeta: metav1.ObjectMeta{ + Name: corev1alpha1.LogicalClusterName, + Annotations: map[string]string{ + core.LogicalClusterPathAnnotationKey: homeWorkspaceLogicalCluster.String(), + }, + }, + } + return true, logicalCluster, nil + } + return false, nil, nil + }) + // return nothing in the default case. getAPIBindings := func(ctx context.Context, kcpClusterClient kcpclientset.ClusterInterface, host string) ([]apisv1alpha1.APIBinding, error) { return nil, nil