-
Notifications
You must be signed in to change notification settings - Fork 215
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
Add exit code flag #155
Conversation
There was a problem hiding this 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.") |
There was a problem hiding this comment.
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.") |
There was a problem hiding this comment.
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) |
There was a problem hiding this comment.
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
?
@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 { |
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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. 😄
Thanks Kim, great work! |
@@ -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.") |
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
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.
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 of3
. This functionality is most helpful for CI/CD use cases.This PR addresses issue 143