FWI-2912: Add logging to improve debugging of JSON Schema (#859)

* Add debug logging for JSON Schema validation and Go templating

* Fix `--help` to display the full Polaris usage

* add valid log possible levels to `--log-level` flag help
This commit is contained in:
ivanfetch-fw
2022-10-05 11:22:42 -06:00
committed by GitHub
parent b3d842a1ba
commit 45be5cbbef
2 changed files with 31 additions and 5 deletions
+1 -5
View File
@@ -15,13 +15,11 @@
package cmd
import (
"flag"
"os"
conf "github.com/fairwindsops/polaris/pkg/config"
"github.com/sirupsen/logrus"
"github.com/spf13/cobra"
"github.com/spf13/pflag"
)
var configPath string
@@ -42,9 +40,7 @@ func init() {
rootCmd.PersistentFlags().BoolVarP(&disallowExemptions, "disallow-exemptions", "", false, "Disallow any configured exemption.")
rootCmd.PersistentFlags().BoolVarP(&disallowConfigExemptions, "disallow-config-exemptions", "", false, "Disallow exemptions set within the configuration file.")
rootCmd.PersistentFlags().BoolVarP(&disallowAnnotationExemptions, "disallow-annotation-exemptions", "", false, "Disallow any exemption defined as a controller annotation.")
rootCmd.PersistentFlags().StringVarP(&logLevel, "log-level", "", logrus.InfoLevel.String(), "Logrus log level.")
flag.Parse()
pflag.CommandLine.AddGoFlagSet(flag.CommandLine)
rootCmd.PersistentFlags().StringVarP(&logLevel, "log-level", "", logrus.InfoLevel.String(), "Logrus log level to be output (trace, debug, info, warning, error, fatal, panic).")
}
var config conf.Configuration
+30
View File
@@ -40,6 +40,25 @@ type schemaTestCase struct {
ResourceProvider *kube.ResourceProvider
}
// ShortString supplies some fields of a schemaTestCase suitable for brief
// output.
func (s schemaTestCase) ShortString() string {
var msg strings.Builder
targetStr := s.Target
if targetStr != "" {
msg.WriteString(fmt.Sprintf("target=%s, ", targetStr))
}
ns := s.Resource.ObjectMeta.GetNamespace()
if ns != "" {
msg.WriteString(fmt.Sprintf("namespace=%s, ", ns))
}
msg.WriteString(fmt.Sprintf("resource=%s/%s", s.Resource.Kind, s.Resource.ObjectMeta.GetName()))
if s.Target == config.TargetContainer {
msg.WriteString(fmt.Sprintf(", container=%s", s.Container.Name))
}
return msg.String()
}
func resolveCheck(conf *config.Configuration, checkID string, test schemaTestCase) (*config.SchemaCheck, error) {
if !conf.DisallowExemptions &&
!conf.DisallowAnnotationExemptions &&
@@ -110,6 +129,7 @@ func getTemplateInput(test schemaTestCase) (map[string]interface{}, error) {
}
}
}
logrus.Debugf("the go template input for schema test-case %s is: %v", test.ShortString(), templateInput)
return templateInput, nil
}
@@ -377,6 +397,16 @@ func applySchemaCheck(conf *config.Configuration, checkID string, test schemaTes
return nil, err
}
}
if len(issues) > 0 {
issueMessages := make([]string, len(issues))
for i, issue := range issues {
issueMessages[i] = issue.Message
}
logrus.Debugf("there were %d issue(s) validating the schema for test-case %s: %v", len(issueMessages), test.ShortString(), issueMessages)
} else {
logrus.Debugf("there were no issues validating the schema for test-case %s", test.ShortString())
}
result := makeResult(conf, check, passes, issues)
if !passes {
if funk.Contains(conf.Mutations, checkID) && len(check.Mutations) > 0 {