Skip to content

Commit

Permalink
FWI-2912: Add logging to improve debugging of JSON Schema (#859)
Browse files Browse the repository at this point in the history
* 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
  • Loading branch information
ivanfetch-wt authored Oct 5, 2022
1 parent b3d842a commit 45be5cb
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 5 deletions.
6 changes: 1 addition & 5 deletions cmd/polaris/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down
30 changes: 30 additions & 0 deletions pkg/validator/schema.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 &&
Expand Down Expand Up @@ -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
}

Expand Down Expand Up @@ -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 {
Expand Down

0 comments on commit 45be5cb

Please sign in to comment.