Skip to content

Commit

Permalink
Merge pull request #263 from pdettori/issue-260
Browse files Browse the repository at this point in the history
🐛 fix setting missing context for cp of type host
  • Loading branch information
MikeSpreitzer authored May 28, 2024
2 parents 9cb2ef9 + b6ce0ca commit 3ff4a1c
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 27 deletions.
22 changes: 18 additions & 4 deletions cmd/kflex/ctx/ctx.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ import (

type CPCtx struct {
common.CP
Type tenancyv1alpha1.ControlPlaneType
}

// Context switch context in Kubeconfig
Expand Down Expand Up @@ -75,7 +76,7 @@ func (c *CPCtx) Context(chattyStatus, failIfNone bool) {
ctxName := certs.GenerateContextName(c.Name)
util.PrintStatus(fmt.Sprintf("Switching to context %s...", ctxName), done, &wg, chattyStatus)
if err = kubeconfig.SwitchContext(kconf, c.Name); err != nil {
fmt.Fprintf(os.Stderr, "kubeconfig context %s not found, trying to load from server...\n", err)
fmt.Fprintf(os.Stderr, "kubeconfig context %s not found, trying to load from server...\n", c.Name)
if err := c.switchToHostingClusterContextAndWrite(kconf); err != nil {
fmt.Fprintf(os.Stderr, "Error switching back to hosting cluster context: %s\n", err)
os.Exit(1)
Expand All @@ -84,9 +85,15 @@ func (c *CPCtx) Context(chattyStatus, failIfNone bool) {
fmt.Fprintf(os.Stderr, "Error loading kubeconfig context from server: %s\n", err)
os.Exit(1)
}
if err = kubeconfig.SwitchContext(kconf, c.Name); err != nil {
fmt.Fprintf(os.Stderr, "Error switching kubeconfig context after loading from server: %s\n", err)
os.Exit(1)
// context exists only for CPs that are not of type host
if c.Type != tenancyv1alpha1.ControlPlaneTypeHost {
if err = kubeconfig.SwitchContext(kconf, c.Name); err != nil {
fmt.Fprintf(os.Stderr, "Error switching kubeconfig context after loading from server: %s\n", err)
os.Exit(1)
}
} else {
fmt.Fprintf(os.Stderr, "control plane %s is of type 'host', using hosting cluster context\n", c.Name)
os.Exit(0)
}
}
done <- true
Expand Down Expand Up @@ -115,7 +122,14 @@ func (c *CPCtx) loadAndMergeFromServer(kconfig *api.Config) error {
if err := kfcClient.Get(context.TODO(), client.ObjectKeyFromObject(cp), cp, &client.GetOptions{}); err != nil {
return fmt.Errorf("control plane not found on server: %s", err)
}
c.Type = cp.Spec.Type

// for control plane of type host just switch to initial context
if cp.Spec.Type == tenancyv1alpha1.ControlPlaneTypeHost {
return kubeconfig.SwitchToHostingClusterContext(kconfig, false)
}

// for all other control planes need to get secret with off-cluster kubeconfig
clientsetp, err := kfclient.GetClientSet(c.Kubeconfig)
if err != nil {
fmt.Fprintf(os.Stderr, "Error getting clientset: %s\n", err)
Expand Down
24 changes: 1 addition & 23 deletions pkg/kubeconfig/kubeconfig.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ func LoadAndMerge(ctx context.Context, client kubernetes.Clientset, name, contro
return err
}
} else {
err = copyHostContextAndSetItToDefault(konfig, name)
err = SwitchToHostingClusterContext(konfig, false)
if err != nil {
return err
}
Expand Down Expand Up @@ -188,25 +188,3 @@ func renameKey(m interface{}, oldKey string, newKey string) interface{} {
}
return m
}

func copyHostContextAndSetItToDefault(config *clientcmdapi.Config, name string) error {
if _, ok := config.Contexts[name]; ok {
return fmt.Errorf("there is already a context with name %s", name)
}

// current context must be pointing at the hosting cluster
cContext := config.CurrentContext

hostContext, ok := config.Contexts[cContext]
if !ok {
return fmt.Errorf("current context with name %s not found", cContext)
}

config.Contexts[name] = &clientcmdapi.Context{
Cluster: hostContext.Cluster,
AuthInfo: hostContext.AuthInfo,
}

config.CurrentContext = name
return nil
}

0 comments on commit 3ff4a1c

Please sign in to comment.