Skip to content

Commit

Permalink
[Maint] Fix race condition issue
Browse files Browse the repository at this point in the history
  • Loading branch information
derailed committed Feb 7, 2024
1 parent 763a6b0 commit 0ab7b14
Show file tree
Hide file tree
Showing 5 changed files with 27 additions and 25 deletions.
19 changes: 2 additions & 17 deletions internal/config/data/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ package data
import (
"fmt"
"io"
"os"
"sync"

"github.com/derailed/k9s/internal/client"
Expand All @@ -29,6 +28,8 @@ func NewConfig(ct *api.Context) *Config {

// Validate ensures config is in norms.
func (c *Config) Validate(conn client.Connection, ks KubeSettings) {
c.mx.Lock()
defer c.mx.Unlock()

if c.Context == nil {
c.Context = NewContext()
Expand All @@ -42,19 +43,3 @@ func (c *Config) Dump(w io.Writer) {

fmt.Fprintf(w, "%s\n", string(bb))
}

// Save saves the config to disk.
func (c *Config) Save(path string) error {
c.mx.RLock()
defer c.mx.RUnlock()

if err := EnsureDirPath(path, DefaultDirMod); err != nil {
return err
}
cfg, err := yaml.Marshal(c)
if err != nil {
return err
}

return os.WriteFile(path, cfg, DefaultFileMod)
}
22 changes: 21 additions & 1 deletion internal/config/data/dir.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"fmt"
"os"
"path/filepath"
"sync"

"github.com/derailed/k9s/internal/config/json"
"github.com/rs/zerolog/log"
Expand All @@ -18,6 +19,7 @@ import (
// Dir tracks context configurations.
type Dir struct {
root string
mx sync.Mutex
}

// NewDir returns a new instance.
Expand Down Expand Up @@ -50,14 +52,32 @@ func (d *Dir) Load(n string, ct *api.Context) (*Config, error) {

func (d *Dir) genConfig(path string, ct *api.Context) (*Config, error) {
cfg := NewConfig(ct)
if err := cfg.Save(path); err != nil {
if err := d.Save(path, cfg); err != nil {
return nil, err
}

return cfg, nil
}

func (d *Dir) Save(path string, c *Config) error {
d.mx.Lock()
defer d.mx.Unlock()

if err := EnsureDirPath(path, DefaultDirMod); err != nil {
return err
}
cfg, err := yaml.Marshal(c)
if err != nil {
return err
}

return os.WriteFile(path, cfg, DefaultFileMod)
}

func (d *Dir) loadConfig(path string) (*Config, error) {
d.mx.Lock()
defer d.mx.Unlock()

bb, err := os.ReadFile(path)
if err != nil {
return nil, err
Expand Down
3 changes: 1 addition & 2 deletions internal/config/k9s.go
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ func (k *K9s) Save() error {
data.MainConfigFile,
)

return k.getActiveConfig().Save(path)
return k.dir.Save(path, k.getActiveConfig())
}

// Merge merges k9s configs.
Expand Down Expand Up @@ -157,7 +157,6 @@ func (k *K9s) ActiveContextName() string {

// ActiveContext returns the currently active context.
func (k *K9s) ActiveContext() (*data.Context, error) {

if cfg := k.getActiveConfig(); cfg != nil && cfg.Context != nil {
return cfg.Context, nil
}
Expand Down
7 changes: 3 additions & 4 deletions internal/ui/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -214,7 +214,7 @@ func (c *Configurator) activeConfig() (cluster string, context string, ok bool)
if err != nil {
return
}
cluster, context = ct.ClusterName, c.Config.K9s.ActiveContextName()
cluster, context = ct.GetClusterName(), c.Config.K9s.ActiveContextName()
if cluster != "" && context != "" {
ok = true
}
Expand Down Expand Up @@ -254,14 +254,13 @@ func (c *Configurator) loadSkinFile(s synchronizer) {
log.Debug().Msgf("Loading skin file: %q", skinFile)
if err := c.Styles.Load(skinFile); err != nil {
if errors.Is(err, os.ErrNotExist) {
s.Flash().Warnf("Skin file %q not found in skins dir: %s", filepath.Base(skinFile), config.AppSkinsDir)
log.Warn().Msgf("Skin file %q not found in skins dir: %s", filepath.Base(skinFile), config.AppSkinsDir)
c.updateStyles("")
} else {
s.Flash().Errf("Failed to parse skin file -- %s: %s.", filepath.Base(skinFile), err)
log.Error().Msgf("Failed to parse skin file -- %s: %s.", filepath.Base(skinFile), err)
c.updateStyles(skinFile)
}
} else {
s.Flash().Infof("Skin file loaded: %q", skinFile)
c.updateStyles(skinFile)
}
}
Expand Down
1 change: 0 additions & 1 deletion internal/view/ns.go
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,6 @@ func (n *Namespace) useNamespace(fqn string) {
return
}

n.App().Flash().Infof("Namespace %s is now active!", ns)
if err := n.App().Config.Save(); err != nil {
log.Error().Err(err).Msg("Config file save failed!")
}
Expand Down

0 comments on commit 0ab7b14

Please sign in to comment.