Skip to content

Commit

Permalink
Add log-level support
Browse files Browse the repository at this point in the history
  • Loading branch information
mrueg committed Feb 18, 2025
1 parent be7edd7 commit 2c9e952
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 2 deletions.
1 change: 1 addition & 0 deletions docs/cli/konstraint_create.md
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ Create constraints with the Gatekeeper enforcement action set to dryrun
--constraint-template-version string Set the version of ConstraintTemplates (default "v1beta1")
-d, --dryrun Sets the enforcement action of the constraints to dryrun, overriding the @enforcement tag
-h, --help help for create
--log-level string Sets a log level. Options: error, info, debug, trace (default "info")
-o, --output string Specify an output directory for the Gatekeeper resources
--partial-constraints Generate partial Constraints for policies with parameters
--skip-constraints Skip generation of constraints
Expand Down
16 changes: 14 additions & 2 deletions internal/commands/create.go
Original file line number Diff line number Diff line change
Expand Up @@ -64,10 +64,20 @@ Create constraints with the Gatekeeper enforcement action set to dryrun
return fmt.Errorf("bind partial-constraints flag: %w", err)
}

if err := viper.BindPFlag("log-level", cmd.PersistentFlags().Lookup("log-level")); err != nil {
return fmt.Errorf("bind log-level flag: %w", err)
}

if cmd.PersistentFlags().Lookup("constraint-template-custom-template-file").Changed && cmd.PersistentFlags().Lookup("constraint-template-version").Changed {
return fmt.Errorf("need to set either constraint-template-custom-template-file or constraint-template-version")
}

if cmd.PersistentFlags().Lookup("log-level").Changed {
level, err := log.ParseLevel(viper.GetString("log-level"))
if err != nil {
return fmt.Errorf("Unknown log level: Need to use either error, info, debug or trace")
}
log.SetLevel(level)
}
path := "."
if len(args) > 0 {
path = args[0]
Expand All @@ -84,7 +94,7 @@ Create constraints with the Gatekeeper enforcement action set to dryrun
cmd.PersistentFlags().Bool("partial-constraints", false, "Generate partial Constraints for policies with parameters")
cmd.PersistentFlags().String("constraint-template-custom-template-file", "", "Path to a custom template file to generate constraint templates")
cmd.PersistentFlags().String("constraint-custom-template-file", "", "Path to a custom template file to generate constraints")

cmd.PersistentFlags().String("log-level", "info", "Set a log level. Options: error, info, debug, trace")
return &cmd
}

Expand All @@ -100,6 +110,8 @@ func runCreateCommand(path string) error {
"src": violation.Path(),
})

logger.Debug("Rendering policy")

if violation.SkipTemplate() {
logger.Info("Skipping constrainttemplate generation due to configuration")
continue
Expand Down

0 comments on commit 2c9e952

Please sign in to comment.