Skip to content

Commit

Permalink
return list of kubeconfigs
Browse files Browse the repository at this point in the history
  • Loading branch information
Atanas Todorov committed Dec 16, 2024
1 parent c2b88ad commit 6e3900d
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 21 deletions.
19 changes: 12 additions & 7 deletions starlark/ucp_provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down Expand Up @@ -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
Expand Down
35 changes: 21 additions & 14 deletions starlark/use.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (
clientcmdapi "k8s.io/client-go/tools/clientcmd/api"
"log"
"net/url"
"os"
"strings"
)

Expand All @@ -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
Expand All @@ -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 {
Expand All @@ -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
Expand Down Expand Up @@ -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) {
Expand All @@ -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()
Expand All @@ -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
}

0 comments on commit 6e3900d

Please sign in to comment.