From f78b65b02587e154a1c54881eed71db48f972ad1 Mon Sep 17 00:00:00 2001 From: Yann Hamon Date: Sun, 8 Nov 2020 19:57:01 +0100 Subject: [PATCH] add -ignore-filename-pattern option --- Readme.md | 2 ++ acceptance.bats | 6 ++++++ cmd/kubeconform/main.go | 2 +- pkg/config/config.go | 33 ++++++++++++++++++--------------- pkg/resource/files.go | 24 +++++++++++++++++++++++- 5 files changed, 50 insertions(+), 17 deletions(-) diff --git a/Readme.md b/Readme.md index f61cebc..86333d7 100644 --- a/Readme.md +++ b/Readme.md @@ -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 diff --git a/acceptance.bats b/acceptance.bats index 77c940a..8f7ebd6 100755 --- a/acceptance.bats +++ b/acceptance.bats @@ -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" ] +} diff --git a/cmd/kubeconform/main.go b/cmd/kubeconform/main.go index 451e77d..6c2d7e1 100644 --- a/cmd/kubeconform/main.go +++ b/cmd/kubeconform/main.go @@ -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() diff --git a/pkg/config/config.go b/pkg/config/config.go index a9c85e3..d4e76c0 100644 --- a/pkg/config/config.go +++ b/pkg/config/config.go @@ -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 diff --git a/pkg/resource/files.go b/pkg/resource/files.go index 4719840..f953400 100644 --- a/pkg/resource/files.go +++ b/pkg/resource/files.go @@ -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