Skip to content

Commit

Permalink
fix(derailed#1359): add option to keep missing clusters in config (de…
Browse files Browse the repository at this point in the history
…railed#2213)

Co-authored-by: Clément Loiselet <clement.loiselet@cbp-group.com>
  • Loading branch information
kalioz and Clément Loiselet authored Nov 12, 2023
1 parent c9f09e6 commit 6d8a95a
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 0 deletions.
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -357,6 +357,8 @@ K9s uses aliases to navigate most K8s resources.
currentContext: minikube
# Indicates the current kube cluster. Defaults to current context cluster
currentCluster: minikube
# KeepMissingClusters will keep clusters in the config if they are missing from the current kubeconfig file. Default false
KeepMissingClusters: false
# Persists per cluster preferences for favorite namespaces and view.
clusters:
coolio:
Expand Down
2 changes: 2 additions & 0 deletions internal/config/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -297,6 +297,7 @@ var expectedConfig = `k9s:
showTime: false
currentContext: blee
currentCluster: blee
keepMissingClusters: false
clusters:
blee:
namespace:
Expand Down Expand Up @@ -397,6 +398,7 @@ var resetConfig = `k9s:
showTime: false
currentContext: blee
currentCluster: blee
keepMissingClusters: false
clusters:
blee:
namespace:
Expand Down
9 changes: 9 additions & 0 deletions internal/config/k9s.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ type K9s struct {
Logger *Logger `yaml:"logger"`
CurrentContext string `yaml:"currentContext"`
CurrentCluster string `yaml:"currentCluster"`
KeepMissingClusters bool `yaml:"keepMissingClusters"`
Clusters map[string]*Cluster `yaml:"clusters,omitempty"`
Thresholds Threshold `yaml:"thresholds"`
ScreenDumpDir string `yaml:"screenDumpDir"`
Expand Down Expand Up @@ -206,9 +207,17 @@ func (k *K9s) validateClusters(c client.Connection, ks KubeSettings) {
}
for key, cluster := range k.Clusters {
cluster.Validate(c, ks)
// if the cluster is defined in the $KUBECONFIG file, keep it in the k9s config file
if _, ok := cc[key]; ok {
continue
}

// if we asked to keep the clusters in the config file
if k.KeepMissingClusters {
continue
}

// else remove it from the k9s config file
if k.CurrentCluster == key {
k.CurrentCluster = ""
}
Expand Down

0 comments on commit 6d8a95a

Please sign in to comment.