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

Add exit code flag #155

Merged
merged 4 commits into from
Jun 24, 2019
Merged

Add exit code flag #155

merged 4 commits into from
Jun 24, 2019

Conversation

kimschles
Copy link
Contributor

@kimschles kimschles commented Jun 24, 2019

What this PR does
This PR creates an --set-exit-code-on-error flag. If the user runs an audit and passes in this flag, the audit will exit with a status code of 3. This functionality is most helpful for CI/CD use cases.

This PR addresses issue 143

Copy link
Contributor

@rbren rbren left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👏 Thanks Kim!

This triggered another idea (doesn't have to be addressed in this PR) - set the exit code when the score drops below a particular threshold. We could add another flag like --set-exit-code-below-score and follow the same flow to check audit.ClusterSummary.Score.

main.go Outdated
@@ -50,6 +50,7 @@ func main() {
webhook := flag.Bool("webhook", false, "Runs the webhook webserver.")
audit := flag.Bool("audit", false, "Runs a one-time audit.")
auditPath := flag.String("audit-path", "", "If specified, audits one or more YAML files instead of a cluster")
setExitCode := flag.Bool("exit-code", false, "set an exit code of 2 when the audit contains error-level issues.")
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you change from exit-code to set-exit-code-on-error?

main.go Outdated
@@ -192,6 +193,11 @@ func runAudit(c conf.Configuration, auditPath string, outputFile string, outputU
panic(err)
}

if setExitCode && auditData.ClusterSummary.Results.Totals.Errors > 0 {
logrus.Errorf("Error found. Exiting audit.")
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this can be Infof, since it's expected.

main.go Outdated
@@ -192,6 +193,11 @@ func runAudit(c conf.Configuration, auditPath string, outputFile string, outputU
panic(err)
}

if setExitCode && auditData.ClusterSummary.Results.Totals.Errors > 0 {
logrus.Errorf("Error found. Exiting audit.")
os.Exit(2)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Did some research - apparently 2 has special meaning

Can you change to 3?

@kimschles
Copy link
Contributor Author

kimschles commented Jun 24, 2019

@bobby-brennan I made the changes you requested.

main.go Outdated
@@ -192,6 +193,11 @@ func runAudit(c conf.Configuration, auditPath string, outputFile string, outputU
panic(err)
}

if setExitCode && auditData.ClusterSummary.Results.Totals.Errors > 0 {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sorry, I should have caught this before - this block should go at the very bottom of the function. That way we'll still get to see the audit output.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@bobby-brennan I moved that block to the bottom of the function. 😄

@rbren
Copy link
Contributor

rbren commented Jun 24, 2019

Thanks Kim, great work!

@kimschles kimschles merged commit f784c48 into master Jun 24, 2019
@kimschles kimschles deleted the ks/add-exit-code-flag branch June 24, 2019 21:03
@@ -50,6 +50,7 @@ func main() {
webhook := flag.Bool("webhook", false, "Runs the webhook webserver.")
audit := flag.Bool("audit", false, "Runs a one-time audit.")
auditPath := flag.String("audit-path", "", "If specified, audits one or more YAML files instead of a cluster")
setExitCode := flag.Bool("set-exit-code-on-error", false, "set an exit code of 2 when the audit contains error-level issues.")
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Heads up - this doc doesn't match the os.Exit(3) used below. Also, any consideration on just using 1 as the exit code?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Will fix.

We decided against 1 so that we could differentiate this expected failure from unexpected failures.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants