Skip to content

Commit

Permalink
Fix panic on context switch (#386) (#387)
Browse files Browse the repository at this point in the history
* added bool check for missing focused server, fixed panic

* added main branch to FOSSA workflow

* return error instead of bool
  • Loading branch information
enrichman authored Sep 4, 2024
1 parent fbbf205 commit 95f73b5
Show file tree
Hide file tree
Showing 5 changed files with 34 additions and 13 deletions.
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

0 comments on commit 95f73b5

Please sign in to comment.