Skip to content

Commit 3f4e824

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 3f4e824

File tree

3 files changed

+27
-13
lines changed

3 files changed

+27
-13
lines changed

cmd/kubent/main.go

+10-8
Original file line numberDiff line numberDiff line change
@@ -36,18 +36,18 @@ 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-
} else {
46-
inputs = append(inputs, rs...)
47-
log.Info().Str("name", c.Name()).Msgf("Retrieved %d resources from collector", len(rs))
45+
return nil, err
4846
}
47+
inputs = append(inputs, rs...)
48+
log.Info().Str("name", c.Name()).Msgf("Retrieved %d resources from collector", len(rs))
4949
}
50-
return inputs
50+
return inputs, nil
5151
}
5252

5353
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
8787
if err != nil {
8888
return nil, fmt.Errorf("failed to detect k8s version: %w", err)
8989
}
90-
9190
return version, nil
9291
}
9392
}
@@ -118,12 +117,15 @@ func main() {
118117
log.Info().Msg("Initializing collectors and retrieving data")
119118
initCollectors := initCollectors(config)
120119

121-
config.TargetVersion, err = getServerVersion(config.TargetVersion, initCollectors)
120+
config.TargetVersion, _ = getServerVersion(config.TargetVersion, initCollectors)
122121
if config.TargetVersion != nil {
123122
log.Info().Msgf("Target K8s version is %s", config.TargetVersion.String())
124123
}
125124

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

128130
// this could probably use some error checking in future, but
129131
// 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+
{"fail to initialize collectors", []string{"-o=json", "-f=fail"}, 200, "[]\n", "", false},
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

scripts/integration-test.sh

+12
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
#!/usr/bin/env sh
2+
3+
# Set strict error checking
4+
set -euf
5+
LC_CTYPE=C
6+
7+
i=1
8+
while [ "${i}" -le 5 ]; do
9+
helm install -n nginx-${i} nginx-${i} nginx/nginx &
10+
done
11+
12+
wait

0 commit comments

Comments
 (0)