From 6e3900d762ef0614beab2d7498fc56fd3a678487 Mon Sep 17 00:00:00 2001 From: Atanas Todorov Date: Mon, 16 Dec 2024 22:17:01 +0200 Subject: [PATCH] return list of kubeconfigs --- starlark/ucp_provider.go | 19 ++++++++++++------- starlark/use.go | 35 +++++++++++++++++++++-------------- 2 files changed, 33 insertions(+), 21 deletions(-) diff --git a/starlark/ucp_provider.go b/starlark/ucp_provider.go index a46654a..a9cea0e 100644 --- a/starlark/ucp_provider.go +++ b/starlark/ucp_provider.go @@ -90,11 +90,11 @@ func UcpProviderFn(thread *starlark.Thread, _ *starlark.Builtin, args starlark.T log.Fatalf("Failed to load kubeconfig: %v", err) } - switchWorkspace := NewUseWorkspaceCommand(workspace, config, mgmtKubeConfigPath) - err = switchWorkspace.Run(context.Background()) - if err != nil { - return nil, err - } + //switchWorkspace := NewUseWorkspaceCommand(workspace, config, mgmtKubeConfigPath) + //err = switchWorkspace.Run(context.Background()) + //if err != nil { + // return nil, err + //} // Step 3: Create a KCP client using the kubeconfig kcpConfig, err := clientcmd.NewNonInteractiveDeferredLoadingClientConfig( @@ -140,8 +140,13 @@ func UcpProviderFn(thread *starlark.Thread, _ *starlark.Builtin, args starlark.T wcs := make([]starlark.Value, 0) fmt.Println("Available Workspaces:") - for _, ws := range workspaces.Items { - wcs = append(wcs, starlark.String(ws.Name)) + for _, _ = range workspaces.Items { + switchWorkspace := NewUseWorkspaceCommand(workspace, config, mgmtKubeConfigPath) + kubeConfigPath, err := switchWorkspace.Run(context.Background()) + if err != nil { + return nil, err + } + wcs = append(wcs, starlark.String(kubeConfigPath)) } //kubconfigs := []string{"1", "2", "3", "4", "5", "6", "7", "8", "9"} //// add node info to dictionary diff --git a/starlark/use.go b/starlark/use.go index c2936d1..abc5e40 100644 --- a/starlark/use.go +++ b/starlark/use.go @@ -10,6 +10,7 @@ import ( clientcmdapi "k8s.io/client-go/tools/clientcmd/api" "log" "net/url" + "os" "strings" ) @@ -25,7 +26,7 @@ type UseWorkspaceCommand struct { // ShortWorkspaceOutput indicates only the workspace name should be printed. ShortWorkspaceOutput bool - modifyConfig func(newConfig *clientcmdapi.Config, kubeconfigPath string) error + modifyConfig func(name string, newConfig *clientcmdapi.Config) (string, error) adminUcpConfig *clientcmdapi.Config kubeconfigPath string kcpclient *kcpclient.Clientset @@ -52,7 +53,7 @@ func NewUseWorkspaceCommand(workspace string, kubeConfig *clientcmdapi.Config, k kcpclient: kcpClient, Name: workspace, - modifyConfig: func(newConfig *clientcmdapi.Config, kubeconfigPath string) error { + modifyConfig: func(workspace string, newConfig *clientcmdapi.Config) (string, error) { // Convert *api.Config back to raw string format rawConfig, err := clientcmd.Write(*newConfig) if err != nil { @@ -61,23 +62,28 @@ func NewUseWorkspaceCommand(workspace string, kubeConfig *clientcmdapi.Config, k // Print the raw kubeconfig string fmt.Println(string(rawConfig)) - err = clientcmd.WriteToFile(*newConfig, kubeconfigPath) + kubeconfig, err := os.CreateTemp("ucp-kubeconfigs", workspace+".*.kubeconfig") if err != nil { - return err + log.Fatalf("Failed to create temporary file: %v", err) } - return nil + + err = clientcmd.WriteToFile(*newConfig, kubeconfig.Name()) + if err != nil { + return "", err + } + return kubeconfig.Name(), nil }, } } // Run executes the "use workspace" logic based on the supplied options. -func (o *UseWorkspaceCommand) Run(ctx context.Context) (err error) { +func (o *UseWorkspaceCommand) Run(ctx context.Context) (kubeconfigPath string, err error) { name := o.Name cluster := o.adminUcpConfig.Clusters["workspace.kcp.io/current"] currentContext, found := o.adminUcpConfig.Contexts[o.adminUcpConfig.CurrentContext] if !found { - return fmt.Errorf("current %q context not found", currentContext) + return "", fmt.Errorf("current %q context not found", currentContext) } // make relative paths absolute @@ -132,9 +138,9 @@ func (o *UseWorkspaceCommand) Run(ctx context.Context) (err error) { //u.Path = path.Join(u.Path, pth.RequestPath()) u, err := url.Parse(name) if err != nil { - return err + return "", err } - return o.commitConfig(ctx, currentContext, u) + return o.commitConfig(name, currentContext, u) } func resolveDots(pth string) (logicalcluster.Path, error) { @@ -160,12 +166,12 @@ func resolveDots(pth string) (logicalcluster.Path, error) { // This method already commits. Do not use with commitConfig // commitConfig will take in current config, new host and optional workspaceType and update the kubeconfig. -func (o *UseWorkspaceCommand) commitConfig(ctx context.Context, currentContext *clientcmdapi.Context, u *url.URL) error { +func (o *UseWorkspaceCommand) commitConfig(workspace string, currentContext *clientcmdapi.Context, u *url.URL) (string, error) { // modify kubeconfig, using the "workspace" context and cluster newKubeConfig := o.adminUcpConfig.DeepCopy() oldCluster, found := o.adminUcpConfig.Clusters[currentContext.Cluster] if !found { - return fmt.Errorf("cluster %q not found in kubeconfig", currentContext.Cluster) + return "", fmt.Errorf("cluster %q not found in kubeconfig", currentContext.Cluster) } newCluster := *oldCluster newCluster.Server = u.String() @@ -184,9 +190,10 @@ func (o *UseWorkspaceCommand) commitConfig(ctx context.Context, currentContext * newKubeConfig.CurrentContext = kcpCurrentWorkspaceContextKey - if err := o.modifyConfig(newKubeConfig, o.kubeconfigPath); err != nil { - return err + kubeConfigPath, err := o.modifyConfig(workspace, newKubeConfig) + if err != nil { + return "", err } - return nil + return kubeConfigPath, nil }