Skip to content

Commit

Permalink
feat: Fix auth error, errors out properly closes #450 and increased l…
Browse files Browse the repository at this point in the history
…og level for silent resource failures

Signed-off-by: dark0dave <dark0dave@mykolab.com>
  • Loading branch information
dark0dave committed Sep 5, 2023
1 parent f6ebd85 commit bde2771
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 5 deletions.
16 changes: 12 additions & 4 deletions cmd/kubent/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,18 +36,21 @@ func generateUserAgent() string {
return fmt.Sprintf("kubent (%s/%s)", version, gitSha)
}

func getCollectors(collectors []collector.Collector) []map[string]interface{} {
func getCollectors(collectors []collector.Collector, exitError bool) ([]map[string]interface{}, error) {
var inputs []map[string]interface{}
for _, c := range collectors {
rs, err := c.Get()
if err != nil {
log.Error().Err(err).Str("name", c.Name()).Msg("Failed to retrieve data from collector")
if exitError {
return nil, err
}
} else {
inputs = append(inputs, rs...)
log.Info().Str("name", c.Name()).Msgf("Retrieved %d resources from collector", len(rs))
}
}
return inputs
return inputs, nil
}

func storeCollector(collector collector.Collector, err error, collectors []collector.Collector) []collector.Collector {
Expand Down Expand Up @@ -87,7 +90,6 @@ func getServerVersion(cv *judge.Version, collectors []collector.Collector) (*jud
if err != nil {
return nil, fmt.Errorf("failed to detect k8s version: %w", err)
}

return version, nil
}
}
Expand Down Expand Up @@ -119,11 +121,17 @@ func main() {
initCollectors := initCollectors(config)

config.TargetVersion, err = getServerVersion(config.TargetVersion, initCollectors)
if config.ExitError && err != nil {
os.Exit(exitCode)
}
if config.TargetVersion != nil {
log.Info().Msgf("Target K8s version is %s", config.TargetVersion.String())
}

collectors := getCollectors(initCollectors)
collectors, err := getCollectors(initCollectors, config.ExitError)
if config.ExitError && err != nil {
os.Exit(exitCode)
}

// this could probably use some error checking in future, but
// schema.ParseKindArg does not return any error
Expand Down
2 changes: 1 addition & 1 deletion cmd/kubent/main_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ func TestGetCollectors(t *testing.T) {
initCollectors := []collector.Collector{}
initCollectors = append(initCollectors, fileCollector)

collectors := getCollectors(initCollectors)
collectors, _ := getCollectors(initCollectors, false)

if collectors != nil && len(collectors) != 1 {
t.Errorf("Did not get file collector correctly with error: %s", err)
Expand Down

0 comments on commit bde2771

Please sign in to comment.