Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: Return error exit code when collectors fail #527

Merged
merged 1 commit into from
Sep 8, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 14 additions & 11 deletions cmd/kubent/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,27 +27,28 @@ var (
)

const (
EXIT_CODE_SUCCESS = 0
EXIT_CODE_FAIL_GENERIC = 1
EXIT_CODE_FOUND_ISSUES = 200
EXIT_CODE_SUCCESS = 0
EXIT_CODE_FAIL_GENERIC = 1
EXIT_CODE_FAIL_GET_COLLECTORS = 100
EXIT_CODE_FOUND_ISSUES = 200
)

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 {
Expand Down Expand Up @@ -87,7 +88,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 @@ -118,12 +118,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_FAIL_GET_COLLECTORS)
}

// this could probably use some error checking in future, but
// schema.ParseKindArg does not return any error
Expand Down
10 changes: 5 additions & 5 deletions cmd/kubent/main_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import (
"encoding/base64"
"encoding/json"
"errors"
"io/ioutil"
"os"
"os/exec"
"path/filepath"
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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-")
dark0dave marked this conversation as resolved.
Show resolved Hide resolved
if err != nil {
t.Fatalf("failed to create temp dir for testing: %v", err)
}
Expand All @@ -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 get collectors", []string{"-o=json", "-f=fail"}, 100, "[]\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},
Expand Down Expand Up @@ -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
Expand Down