Skip to content

Commit

Permalink
Log config contexts from nomos commands at -v=4 (#982)
Browse files Browse the repository at this point in the history
Add logging to help debug nomos commands run in various environments.
  • Loading branch information
karlkfi authored Oct 30, 2023
1 parent fc50fec commit e79862c
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 4 deletions.
10 changes: 10 additions & 0 deletions cmd/nomos/status/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
23 changes: 19 additions & 4 deletions pkg/client/restconfig/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,13 +19,15 @@ import (
"os"
"os/user"
"path/filepath"
"sort"
"strings"
"time"

"github.com/pkg/errors"
"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"
Expand Down Expand Up @@ -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})
Expand All @@ -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
}

0 comments on commit e79862c

Please sign in to comment.