diff --git a/Readme.md b/Readme.md index 4fc2f9b..a9a9b27 100644 --- a/Readme.md +++ b/Readme.md @@ -128,7 +128,7 @@ Usage: ./bin/kubeconform [OPTION]... [FILE OR FOLDER]... -schema-location value override schemas location search path (can be specified multiple times) -skip string - comma-separated list of kinds or GVKs to ignore + comma-separated list of kinds, api versions or GVKs to ignore -strict disallow additional properties not in schema or duplicated keys -summary diff --git a/pkg/validator/validator.go b/pkg/validator/validator.go index f4827c8..79832f9 100644 --- a/pkg/validator/validator.go +++ b/pkg/validator/validator.go @@ -59,7 +59,7 @@ type Opts struct { SkipKinds map[string]struct{} // List of resource Kinds to ignore RejectKinds map[string]struct{} // List of resource Kinds to reject KubernetesVersion string // Kubernetes Version - has to match one in https://github.com/instrumenta/kubernetes-json-schema - Strict bool // thros an error if resources contain undocumented fields + Strict bool // throws an error if resources contain undocumented fields IgnoreMissingSchemas bool // skip a resource if no schema for that resource can be found } @@ -115,6 +115,9 @@ func (val *v) ValidateResource(res resource.Resource) Result { // for skipping/rejecting resources) and the raw Kind. skip := func(signature resource.Signature) bool { + if _, ok := val.opts.SkipKinds[signature.Version]; ok { + return ok + } if _, ok := val.opts.SkipKinds[signature.GroupVersionKind()]; ok { return ok } diff --git a/pkg/validator/validator_test.go b/pkg/validator/validator_test.go index 0776253..9e5e9a8 100644 --- a/pkg/validator/validator_test.go +++ b/pkg/validator/validator_test.go @@ -435,3 +435,37 @@ age: not a number t.Errorf("Expected %+v, got %+v", expectedErrors, got.ValidationErrors) } } + +func TestValidateSkip(t *testing.T) { + resource := resource.Resource{Bytes: []byte(` +apiVersion: random.vendor/v1alpha3 +kind: SomeKind +firstName: foo +lastName: bar`)} + + for _, testCase := range []struct { + name string + skipOption string + }{ + {"skip kind", "SomeKind"}, + {"skip version/kind", "random.vendor/v1alpha3/SomeKind"}, + {"skip apiVersion", "random.vendor/v1alpha3"}, + } { + + validator := v{ + opts: Opts{ + SkipKinds: map[string]struct{}{testCase.skipOption: {}}, + }, + schemaDownload: downloadSchema, + } + + result := validator.ValidateResource(resource) + if result.Status != Skipped { + if result.Err != nil { + t.Errorf("Test '%s' - expected %d, got %d: %s", testCase.name, Skipped, result.Status, result.Err.Error()) + } else { + t.Errorf("Test '%s' - expected %d, got %d", testCase.name, Skipped, result.Status) + } + } + } +}