diff --git a/cmd/nomos/status/client.go b/cmd/nomos/status/client.go index 321b6e0667..7bbfc0b13f 100644 --- a/cmd/nomos/status/client.go +++ b/cmd/nomos/status/client.go @@ -462,6 +462,16 @@ func ClusterClients(ctx context.Context, contexts []string) (map[string]*Cluster } configs = filterConfigs(contexts, configs) + if klog.V(4).Enabled() { + // Sort contexts for consistent ordering in the log + var contexts []string + for ctxName := range configs { + contexts = append(contexts, ctxName) + } + sort.Strings(contexts) + klog.V(4).Infof("Config contexts after filtering: %s", strings.Join(contexts, ", ")) + } + var mapMutex sync.Mutex var wg sync.WaitGroup clientMap := make(map[string]*ClusterClient) diff --git a/pkg/client/restconfig/config.go b/pkg/client/restconfig/config.go index b4634d7543..0a9537f317 100644 --- a/pkg/client/restconfig/config.go +++ b/pkg/client/restconfig/config.go @@ -19,6 +19,7 @@ import ( "os" "os/user" "path/filepath" + "sort" "strings" "time" @@ -26,6 +27,7 @@ import ( "k8s.io/client-go/rest" "k8s.io/client-go/tools/clientcmd" clientcmdapi "k8s.io/client-go/tools/clientcmd/api" + "k8s.io/klog/v2" ) const kubectlConfigPath = ".kube/config" @@ -95,9 +97,23 @@ func AllKubectlConfigs(timeout time.Duration) (map[string]*rest.Config, error) { if err != nil { return nil, err } + if len(apiCfg.Contexts) == 0 { + return map[string]*rest.Config{}, nil + } + + if klog.V(4).Enabled() { + // Sort contexts for consistent ordering in the log + var contexts []string + for ctxName := range apiCfg.Contexts { + contexts = append(contexts, ctxName) + } + sort.Strings(contexts) + klog.V(4).Infof("Found config contexts: %s", strings.Join(contexts, ", ")) + klog.V(4).Infof("Current config context: %s", apiCfg.CurrentContext) + } var badConfigs []string - configs := map[string]*rest.Config{} + configs := make(map[string]*rest.Config, len(apiCfg.Contexts)) for ctxName := range apiCfg.Contexts { cfg := clientcmd.NewNonInteractiveDeferredLoadingClientConfig( rules, &clientcmd.ConfigOverrides{CurrentContext: ctxName}) @@ -115,9 +131,8 @@ func AllKubectlConfigs(timeout time.Duration) (map[string]*rest.Config, error) { configs[ctxName] = restCfg } - var cfgErrs error if len(badConfigs) > 0 { - cfgErrs = fmt.Errorf("failed to build configs:\n%s", strings.Join(badConfigs, "\n")) + return configs, fmt.Errorf("failed to build configs:\n%s", strings.Join(badConfigs, "\n")) } - return configs, cfgErrs + return configs, nil }