Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[v2.8] Fix panic on context switch #388

Merged
merged 1 commit into from
Sep 4, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .github/workflows/fossa.yml
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ on:
- v*
branches:
- v*
- main

jobs:
fossa:
Expand Down
23 changes: 16 additions & 7 deletions cmd/common.go
Original file line number Diff line number Diff line change
Expand Up @@ -148,7 +148,11 @@ func getKubeConfigForUser(ctx *cli.Context, user string) (*api.Config, error) {
return nil, err
}

focusedServer := cf.FocusedServer()
focusedServer, err := cf.FocusedServer()
if err != nil {
return nil, err
}

kubeConfig := focusedServer.KubeConfigs[fmt.Sprintf(kubeConfigKeyFormat, user, focusedServer.FocusedCluster())]
return kubeConfig, nil
}
Expand All @@ -159,10 +163,15 @@ func setKubeConfigForUser(ctx *cli.Context, user string, kubeConfig *api.Config)
return err
}

if cf.FocusedServer().KubeConfigs == nil {
cf.FocusedServer().KubeConfigs = make(map[string]*api.Config)
focusedServer, err := cf.FocusedServer()
if err != nil {
return err
}
focusedServer := cf.FocusedServer()

if focusedServer.KubeConfigs == nil {
focusedServer.KubeConfigs = make(map[string]*api.Config)
}

focusedServer.KubeConfigs[fmt.Sprintf(kubeConfigKeyFormat, user, focusedServer.FocusedCluster())] = kubeConfig
return cf.Write()
}
Expand Down Expand Up @@ -273,9 +282,9 @@ func lookupConfig(ctx *cli.Context) (*config.ServerConfig, error) {
return nil, err
}

cs := cf.FocusedServer()
if cs == nil {
return nil, errors.New("no configuration found, run `login`")
cs, err := cf.FocusedServer()
if err != nil {
return nil, err
}

return cs, nil
Expand Down
6 changes: 5 additions & 1 deletion cmd/context.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,11 @@ func contextSwitch(ctx *cli.Context) error {
return err
}

server := cf.FocusedServer()
server, err := cf.FocusedServer()
if err != nil {
return err
}

c, err := cliclient.NewManagementClient(server)
if err != nil {
return err
Expand Down
6 changes: 3 additions & 3 deletions cmd/kubectl.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,9 +46,9 @@ func runKubectl(ctx *cli.Context) error {
return err
}

currentRancherServer := config.FocusedServer()
if currentRancherServer == nil {
return fmt.Errorf("no focused server")
currentRancherServer, err := config.FocusedServer()
if err != nil {
return err
}

currentToken := currentRancherServer.AccessKey
Expand Down
11 changes: 9 additions & 2 deletions config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package config

import (
"encoding/json"
"errors"
"fmt"
"net/url"
"os"
Expand All @@ -12,6 +13,8 @@ import (
"k8s.io/client-go/tools/clientcmd/api"
)

var ErrNoServerSelected = errors.New("no server selected")

// Config holds the main config for the user
type Config struct {
Servers map[string]*ServerConfig
Expand Down Expand Up @@ -100,8 +103,12 @@ func (c Config) Write() error {
return json.NewEncoder(output).Encode(c)
}

func (c Config) FocusedServer() *ServerConfig {
return c.Servers[c.CurrentServer]
func (c Config) FocusedServer() (*ServerConfig, error) {
currentServer, found := c.Servers[c.CurrentServer]
if !found || currentServer == nil {
return nil, ErrNoServerSelected
}
return currentServer, nil
}

func (c ServerConfig) FocusedCluster() string {
Expand Down
Loading