From 2c9e9526b3d62c3661f76162d7b2a6578dd00fe2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Manuel=20R=C3=BCger?= Date: Mon, 17 Feb 2025 17:16:33 +0100 Subject: [PATCH] Add log-level support --- docs/cli/konstraint_create.md | 1 + internal/commands/create.go | 16 ++++++++++++++-- 2 files changed, 15 insertions(+), 2 deletions(-) diff --git a/docs/cli/konstraint_create.md b/docs/cli/konstraint_create.md index 5ab188d2..4317f84a 100644 --- a/docs/cli/konstraint_create.md +++ b/docs/cli/konstraint_create.md @@ -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 diff --git a/internal/commands/create.go b/internal/commands/create.go index 5a06eb6a..f5b30012 100644 --- a/internal/commands/create.go +++ b/internal/commands/create.go @@ -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] @@ -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 } @@ -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