add -ignore-filename-pattern option

This commit is contained in:
Yann Hamon 2020-11-08 19:57:01 +01:00
parent 44710f9053
commit f78b65b025
5 changed files with 50 additions and 17 deletions

View file

@ -51,6 +51,8 @@ Usage: ./bin/kubeconform [OPTION]... [FILE OR FOLDER]...
-exit-on-error
immediately stop execution when the first error is encountered
-h show help information
-ignore-filename-pattern value
regular expression specifying paths to ignore (can be specified multiple times)
-ignore-missing-schemas
skip files with missing schemas instead of failing
-insecure-skip-tls-verify

View file

@ -166,3 +166,9 @@
[ "$status" -eq 1 ]
[ "$output" = "fixtures/valid.yaml - ReplicationController bob failed validation: prohibited resource kind ReplicationController" ]
}
@test "Ignores file that match the --ignore-filename-pattern given" {
run bin/kubeconform -summary --ignore-filename-pattern 'crd' --ignore-filename-pattern '.*invalid.*' fixtures/multi_invalid.yaml fixtures/list_invalid.yaml fixtures/quantity.yaml fixtures/crd_schema.yaml
[ "$status" -eq 0 ]
[ "$output" = "Summary: 1 resource found in 1 file - Valid: 1, Invalid: 0, Errors: 0 Skipped: 0" ]
}

View file

@ -169,7 +169,7 @@ func realMain() int {
if isStdin {
resourcesChan, errors = resource.FromStream(ctx, "stdin", os.Stdin)
} else {
resourcesChan, errors = resource.FromFiles(ctx, cfg.Files...)
resourcesChan, errors = resource.FromFiles(ctx, cfg.IgnoreFilenamePatterns, cfg.Files...)
}
c := cache.New()

View file

@ -9,20 +9,21 @@ import (
)
type Config struct {
ExitOnError bool
Files []string
SchemaLocations []string
SkipTLS bool
SkipKinds map[string]bool
RejectKinds map[string]bool
OutputFormat string
KubernetesVersion string
NumberOfWorkers int
Summary bool
Strict bool
Verbose bool
IgnoreMissingSchemas bool
Help bool
ExitOnError bool
Files []string
SchemaLocations []string
SkipTLS bool
SkipKinds map[string]bool
RejectKinds map[string]bool
OutputFormat string
KubernetesVersion string
NumberOfWorkers int
Summary bool
Strict bool
Verbose bool
IgnoreMissingSchemas bool
IgnoreFilenamePatterns []string
Help bool
}
type arrayParam []string
@ -50,7 +51,7 @@ func splitCSV(csvStr string) map[string]bool {
}
func FromFlags(progName string, args []string) (Config, string, error) {
var schemaLocationsParam arrayParam
var schemaLocationsParam, ignoreFilenamePatterns arrayParam
var skipKindsCSV, rejectKindsCSV string
flags := flag.NewFlagSet(progName, flag.ExitOnError)
var buf bytes.Buffer
@ -65,6 +66,7 @@ func FromFlags(progName string, args []string) (Config, string, error) {
flags.StringVar(&rejectKindsCSV, "reject", "", "comma-separated list of kinds to reject")
flags.BoolVar(&c.ExitOnError, "exit-on-error", false, "immediately stop execution when the first error is encountered")
flags.BoolVar(&c.IgnoreMissingSchemas, "ignore-missing-schemas", false, "skip files with missing schemas instead of failing")
flags.Var(&ignoreFilenamePatterns, "ignore-filename-pattern", "regular expression specifying paths to ignore (can be specified multiple times)")
flags.BoolVar(&c.Summary, "summary", false, "print a summary at the end")
flags.IntVar(&c.NumberOfWorkers, "n", 4, "number of goroutines to run concurrently")
flags.BoolVar(&c.Strict, "strict", false, "disallow additional properties not in schema")
@ -83,6 +85,7 @@ func FromFlags(progName string, args []string) (Config, string, error) {
c.SkipKinds = splitCSV(skipKindsCSV)
c.RejectKinds = splitCSV(rejectKindsCSV)
c.IgnoreFilenamePatterns = ignoreFilenamePatterns
c.SchemaLocations = schemaLocationsParam
if len(c.SchemaLocations) == 0 {
c.SchemaLocations = append(c.SchemaLocations, "https://kubernetesjsonschema.dev") // if not specified, default behaviour is to use kubernetesjson-schema.dev as registry

View file

@ -7,6 +7,7 @@ import (
"io/ioutil"
"os"
"path/filepath"
"regexp"
"strings"
)
@ -27,7 +28,20 @@ func (de DiscoveryError) Error() string {
return de.Err.Error()
}
func FromFiles(ctx context.Context, paths ...string) (<-chan Resource, <-chan error) {
func isIgnored(path string, ignoreFilePatterns []string) (bool, error) {
for _, p := range ignoreFilePatterns {
m, err := regexp.MatchString(p, path)
if err != nil {
return false, err
}
if m {
return true, nil
}
}
return false, nil
}
func FromFiles(ctx context.Context, ignoreFilePatterns []string, paths ...string) (<-chan Resource, <-chan error) {
resources := make(chan Resource)
errors := make(chan error)
stop := false
@ -53,6 +67,14 @@ func FromFiles(ctx context.Context, paths ...string) (<-chan Resource, <-chan er
return nil
}
ignored, err := isIgnored(path, ignoreFilePatterns)
if err != nil {
return err
}
if ignored {
return nil
}
f, err := os.Open(p)
if err != nil {
return err