From 8d82a95ad208f3c78abccf08f096d8ed79a652b3 Mon Sep 17 00:00:00 2001 From: Enrico Candino Date: Wed, 4 Sep 2024 15:05:55 +0200 Subject: [PATCH] Fix panic on `context switch` (#386) (#388) * added bool check for missing focused server, fixed panic * added main branch to FOSSA workflow * return error instead of bool --- .github/workflows/fossa.yml | 1 + cmd/common.go | 23 ++++++++++++++++------- cmd/context.go | 6 +++++- cmd/kubectl.go | 6 +++--- config/config.go | 11 +++++++++-- 5 files changed, 34 insertions(+), 13 deletions(-) diff --git a/.github/workflows/fossa.yml b/.github/workflows/fossa.yml index 074c9923c..55a5a2e99 100644 --- a/.github/workflows/fossa.yml +++ b/.github/workflows/fossa.yml @@ -7,6 +7,7 @@ on: - v* branches: - v* + - main jobs: fossa: diff --git a/cmd/common.go b/cmd/common.go index db3856e1e..3fe36eae5 100644 --- a/cmd/common.go +++ b/cmd/common.go @@ -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 } @@ -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() } @@ -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 diff --git a/cmd/context.go b/cmd/context.go index 58f603894..ca8034fca 100644 --- a/cmd/context.go +++ b/cmd/context.go @@ -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 diff --git a/cmd/kubectl.go b/cmd/kubectl.go index 8cf27577d..f56562b78 100644 --- a/cmd/kubectl.go +++ b/cmd/kubectl.go @@ -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 diff --git a/config/config.go b/config/config.go index cac6a08fc..d3d98a830 100644 --- a/config/config.go +++ b/config/config.go @@ -2,6 +2,7 @@ package config import ( "encoding/json" + "errors" "fmt" "net/url" "os" @@ -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 @@ -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 {