Skip to content

Commit feaff6d

Browse files
committed
feat: Fix auth error, errors out properly closes #450 and increased log level for silent resource failures
Signed-off-by: dark0dave <[email protected]>
1 parent f6ebd85 commit feaff6d

File tree

2 files changed

+13
-10
lines changed

2 files changed

+13
-10
lines changed

cmd/kubent/main.go

+8-5
Original file line numberDiff line numberDiff line change
@@ -36,18 +36,19 @@ func generateUserAgent() string {
3636
return fmt.Sprintf("kubent (%s/%s)", version, gitSha)
3737
}
3838

39-
func getCollectors(collectors []collector.Collector) []map[string]interface{} {
39+
func getCollectors(collectors []collector.Collector) ([]map[string]interface{}, error) {
4040
var inputs []map[string]interface{}
4141
for _, c := range collectors {
4242
rs, err := c.Get()
4343
if err != nil {
4444
log.Error().Err(err).Str("name", c.Name()).Msg("Failed to retrieve data from collector")
45+
return nil, err
4546
} else {
4647
inputs = append(inputs, rs...)
4748
log.Info().Str("name", c.Name()).Msgf("Retrieved %d resources from collector", len(rs))
4849
}
4950
}
50-
return inputs
51+
return inputs, nil
5152
}
5253

5354
func storeCollector(collector collector.Collector, err error, collectors []collector.Collector) []collector.Collector {
@@ -87,7 +88,6 @@ func getServerVersion(cv *judge.Version, collectors []collector.Collector) (*jud
8788
if err != nil {
8889
return nil, fmt.Errorf("failed to detect k8s version: %w", err)
8990
}
90-
9191
return version, nil
9292
}
9393
}
@@ -118,12 +118,15 @@ func main() {
118118
log.Info().Msg("Initializing collectors and retrieving data")
119119
initCollectors := initCollectors(config)
120120

121-
config.TargetVersion, err = getServerVersion(config.TargetVersion, initCollectors)
121+
config.TargetVersion, _ = getServerVersion(config.TargetVersion, initCollectors)
122122
if config.TargetVersion != nil {
123123
log.Info().Msgf("Target K8s version is %s", config.TargetVersion.String())
124124
}
125125

126-
collectors := getCollectors(initCollectors)
126+
collectors, err := getCollectors(initCollectors)
127+
if err != nil {
128+
os.Exit(EXIT_CODE_FOUND_ISSUES)
129+
}
127130

128131
// this could probably use some error checking in future, but
129132
// schema.ParseKindArg does not return any error

cmd/kubent/main_test.go

+5-5
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@ import (
55
"encoding/base64"
66
"encoding/json"
77
"errors"
8-
"io/ioutil"
98
"os"
109
"os/exec"
1110
"path/filepath"
@@ -49,7 +48,7 @@ func TestGetCollectors(t *testing.T) {
4948
initCollectors := []collector.Collector{}
5049
initCollectors = append(initCollectors, fileCollector)
5150

52-
collectors := getCollectors(initCollectors)
51+
collectors, _ := getCollectors(initCollectors)
5352

5453
if collectors != nil && len(collectors) != 1 {
5554
t.Errorf("Did not get file collector correctly with error: %s", err)
@@ -102,7 +101,7 @@ func TestStoreCollectorError(t *testing.T) {
102101
}
103102

104103
func TestMainExitCodes(t *testing.T) {
105-
tmpDir, err := ioutil.TempDir(os.TempDir(), "kubent-tests-")
104+
tmpDir, err := os.MkdirTemp(os.TempDir(), "kubent-tests-")
106105
if err != nil {
107106
t.Fatalf("failed to create temp dir for testing: %v", err)
108107
}
@@ -128,6 +127,7 @@ func TestMainExitCodes(t *testing.T) {
128127
{"version long flag set", []string{"--version"}, 0, "", "", false},
129128
{"empty text output", []string{clusterFlagDisabled, helm3FlagDisabled}, 0, "", "", false},
130129
{"empty json output", []string{"-o=json", clusterFlagDisabled, helm3FlagDisabled}, 0, "[]\n", "", false},
130+
{"Failed to initialize collectors", []string{"-o=json", "-l=disabled"}, 200, "[]\n", "", true},
131131
{"json-file", []string{"-o=json", clusterFlagDisabled, helm3FlagDisabled, "-f=" + filepath.Join(FIXTURES_DIR, "deployment-v1beta1.yaml")}, 0, "", filepath.Join(tmpDir, "json-file.out"), false},
132132
{"text-file", []string{"-o=text", clusterFlagDisabled, helm3FlagDisabled, "-f=" + filepath.Join(FIXTURES_DIR, "deployment-v1beta1.yaml")}, 0, "", filepath.Join(tmpDir, "text-file.out"), false},
133133
{"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 {
268268

269269
func Test_outputResults(t *testing.T) {
270270
testVersion, _ := judge.NewVersion("4.5.6")
271-
testResults := []judge.Result{{"name", "ns", "kind",
272-
"1.2.3", "rs", "rep", testVersion}}
271+
testResults := []judge.Result{{Name: "name", Namespace: "ns", Kind: "kind",
272+
ApiVersion: "1.2.3", RuleSet: "rs", ReplaceWith: "rep", Since: testVersion}}
273273

274274
type args struct {
275275
results []judge.Result

0 commit comments

Comments
 (0)