Skip to content

Commit 2fb88de

Browse files
authored
Add log-level support (#587)
1 parent be7edd7 commit 2fb88de

File tree

2 files changed

+17
-4
lines changed

2 files changed

+17
-4
lines changed

docs/cli/konstraint_create.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,8 +25,9 @@ Create constraints with the Gatekeeper enforcement action set to dryrun
2525
--constraint-custom-template-file string Path to a custom template file to generate constraints
2626
--constraint-template-custom-template-file string Path to a custom template file to generate constraint templates
2727
--constraint-template-version string Set the version of ConstraintTemplates (default "v1beta1")
28-
-d, --dryrun Sets the enforcement action of the constraints to dryrun, overriding the @enforcement tag
28+
-d, --dryrun Set the enforcement action of the constraints to dryrun, overriding the @enforcement tag
2929
-h, --help help for create
30+
--log-level string Set a log level. Options: error, info, debug, trace (default "info")
3031
-o, --output string Specify an output directory for the Gatekeeper resources
3132
--partial-constraints Generate partial Constraints for policies with parameters
3233
--skip-constraints Skip generation of constraints

internal/commands/create.go

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -64,10 +64,20 @@ Create constraints with the Gatekeeper enforcement action set to dryrun
6464
return fmt.Errorf("bind partial-constraints flag: %w", err)
6565
}
6666

67+
if err := viper.BindPFlag("log-level", cmd.PersistentFlags().Lookup("log-level")); err != nil {
68+
return fmt.Errorf("bind log-level flag: %w", err)
69+
}
70+
6771
if cmd.PersistentFlags().Lookup("constraint-template-custom-template-file").Changed && cmd.PersistentFlags().Lookup("constraint-template-version").Changed {
6872
return fmt.Errorf("need to set either constraint-template-custom-template-file or constraint-template-version")
6973
}
70-
74+
if cmd.PersistentFlags().Lookup("log-level").Changed {
75+
level, err := log.ParseLevel(viper.GetString("log-level"))
76+
if err != nil {
77+
return fmt.Errorf("Unknown log level: Need to use either error, info, debug or trace")
78+
}
79+
log.SetLevel(level)
80+
}
7181
path := "."
7282
if len(args) > 0 {
7383
path = args[0]
@@ -78,13 +88,13 @@ Create constraints with the Gatekeeper enforcement action set to dryrun
7888
}
7989

8090
cmd.PersistentFlags().StringP("output", "o", "", "Specify an output directory for the Gatekeeper resources")
81-
cmd.PersistentFlags().BoolP("dryrun", "d", false, "Sets the enforcement action of the constraints to dryrun, overriding the @enforcement tag")
91+
cmd.PersistentFlags().BoolP("dryrun", "d", false, "Set the enforcement action of the constraints to dryrun, overriding the @enforcement tag")
8292
cmd.PersistentFlags().Bool("skip-constraints", false, "Skip generation of constraints")
8393
cmd.PersistentFlags().String("constraint-template-version", "v1beta1", "Set the version of ConstraintTemplates")
8494
cmd.PersistentFlags().Bool("partial-constraints", false, "Generate partial Constraints for policies with parameters")
8595
cmd.PersistentFlags().String("constraint-template-custom-template-file", "", "Path to a custom template file to generate constraint templates")
8696
cmd.PersistentFlags().String("constraint-custom-template-file", "", "Path to a custom template file to generate constraints")
87-
97+
cmd.PersistentFlags().String("log-level", "info", "Set a log level. Options: error, info, debug, trace")
8898
return &cmd
8999
}
90100

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

113+
logger.Debug("Rendering policy")
114+
103115
if violation.SkipTemplate() {
104116
logger.Info("Skipping constrainttemplate generation due to configuration")
105117
continue

0 commit comments

Comments
 (0)