From ac6a888ec346290b060a9830d22de0bb3c908a2d Mon Sep 17 00:00:00 2001 From: dark0dave Date: Tue, 5 Sep 2023 14:08:47 +0100 Subject: [PATCH] feat: Fix auth error, errors out properly closes #450 and increased log level for silent resource failures Signed-off-by: dark0dave --- cmd/kubent/main.go | 18 ++++++++++-------- cmd/kubent/main_test.go | 10 +++++----- 2 files changed, 15 insertions(+), 13 deletions(-) diff --git a/cmd/kubent/main.go b/cmd/kubent/main.go index d9f93d79..a7b50b49 100644 --- a/cmd/kubent/main.go +++ b/cmd/kubent/main.go @@ -36,18 +36,18 @@ func generateUserAgent() string { return fmt.Sprintf("kubent (%s/%s)", version, gitSha) } -func getCollectors(collectors []collector.Collector) []map[string]interface{} { +func getCollectors(collectors []collector.Collector) ([]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") - } else { - inputs = append(inputs, rs...) - log.Info().Str("name", c.Name()).Msgf("Retrieved %d resources from collector", len(rs)) + return nil, err } + 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 { @@ -87,7 +87,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 } } @@ -118,12 +117,15 @@ func main() { log.Info().Msg("Initializing collectors and retrieving data") initCollectors := initCollectors(config) - config.TargetVersion, err = getServerVersion(config.TargetVersion, initCollectors) + config.TargetVersion, _ = getServerVersion(config.TargetVersion, initCollectors) if config.TargetVersion != nil { log.Info().Msgf("Target K8s version is %s", config.TargetVersion.String()) } - collectors := getCollectors(initCollectors) + collectors, err := getCollectors(initCollectors) + if err != nil { + os.Exit(EXIT_CODE_FOUND_ISSUES) + } // this could probably use some error checking in future, but // schema.ParseKindArg does not return any error diff --git a/cmd/kubent/main_test.go b/cmd/kubent/main_test.go index 80fc2195..c70b41cb 100644 --- a/cmd/kubent/main_test.go +++ b/cmd/kubent/main_test.go @@ -5,7 +5,6 @@ import ( "encoding/base64" "encoding/json" "errors" - "io/ioutil" "os" "os/exec" "path/filepath" @@ -49,7 +48,7 @@ func TestGetCollectors(t *testing.T) { initCollectors := []collector.Collector{} initCollectors = append(initCollectors, fileCollector) - collectors := getCollectors(initCollectors) + collectors, _ := getCollectors(initCollectors) if collectors != nil && len(collectors) != 1 { t.Errorf("Did not get file collector correctly with error: %s", err) @@ -102,7 +101,7 @@ func TestStoreCollectorError(t *testing.T) { } func TestMainExitCodes(t *testing.T) { - tmpDir, err := ioutil.TempDir(os.TempDir(), "kubent-tests-") + tmpDir, err := os.MkdirTemp(os.TempDir(), "kubent-tests-") if err != nil { t.Fatalf("failed to create temp dir for testing: %v", err) } @@ -128,6 +127,7 @@ func TestMainExitCodes(t *testing.T) { {"version long flag set", []string{"--version"}, 0, "", "", false}, {"empty text output", []string{clusterFlagDisabled, helm3FlagDisabled}, 0, "", "", false}, {"empty json output", []string{"-o=json", clusterFlagDisabled, helm3FlagDisabled}, 0, "[]\n", "", false}, + {"fail to initialize collectors", []string{"-o=json", "-f=fail"}, 200, "[]\n", "", false}, {"json-file", []string{"-o=json", clusterFlagDisabled, helm3FlagDisabled, "-f=" + filepath.Join(FIXTURES_DIR, "deployment-v1beta1.yaml")}, 0, "", filepath.Join(tmpDir, "json-file.out"), false}, {"text-file", []string{"-o=text", clusterFlagDisabled, helm3FlagDisabled, "-f=" + filepath.Join(FIXTURES_DIR, "deployment-v1beta1.yaml")}, 0, "", filepath.Join(tmpDir, "text-file.out"), false}, {"json-stdout", []string{"-o=json", clusterFlagDisabled, helm3FlagDisabled, "-f=" + filepath.Join(FIXTURES_DIR, "deployment-v1beta1.yaml")}, 0, string(expectedJsonOutput), "-", false}, @@ -268,8 +268,8 @@ func decodeBase64(dst *[]string, encoded string) error { func Test_outputResults(t *testing.T) { testVersion, _ := judge.NewVersion("4.5.6") - testResults := []judge.Result{{"name", "ns", "kind", - "1.2.3", "rs", "rep", testVersion}} + testResults := []judge.Result{{Name: "name", Namespace: "ns", Kind: "kind", + ApiVersion: "1.2.3", RuleSet: "rs", ReplaceWith: "rep", Since: testVersion}} type args struct { results []judge.Result