From 24e244ca821e7aa7f15a475caff36904ce2cb6be Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Cl=C3=A9ment=20Loiselet?= Date: Sun, 12 Nov 2023 17:42:50 +0100 Subject: [PATCH] fix(#1359): add option to keep missing clusters in config (#2213) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Co-authored-by: Clément Loiselet --- README.md | 2 ++ internal/config/config_test.go | 2 ++ internal/config/k9s.go | 9 +++++++++ 3 files changed, 13 insertions(+) diff --git a/README.md b/README.md index d4e6108d..7eb680e4 100644 --- a/README.md +++ b/README.md @@ -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: diff --git a/internal/config/config_test.go b/internal/config/config_test.go index c57a45ed..9530a353 100644 --- a/internal/config/config_test.go +++ b/internal/config/config_test.go @@ -297,6 +297,7 @@ var expectedConfig = `k9s: showTime: false currentContext: blee currentCluster: blee + keepMissingClusters: false clusters: blee: namespace: @@ -397,6 +398,7 @@ var resetConfig = `k9s: showTime: false currentContext: blee currentCluster: blee + keepMissingClusters: false clusters: blee: namespace: diff --git a/internal/config/k9s.go b/internal/config/k9s.go index 6bac7aff..37bb6044 100644 --- a/internal/config/k9s.go +++ b/internal/config/k9s.go @@ -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"` @@ -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 = "" }