From e6479f3f588add7b7e4c23c9ddb3404fd0f03c87 Mon Sep 17 00:00:00 2001 From: Aleksey Levenstein Date: Wed, 15 Feb 2023 19:08:48 +0200 Subject: [PATCH] feat: support skipping api version --- Readme.md | 8 ++++++-- pkg/validator/validator.go | 5 ++++- pkg/validator/validator_test.go | 34 +++++++++++++++++++++++++++++++++ 3 files changed, 44 insertions(+), 3 deletions(-) diff --git a/Readme.md b/Readme.md index 4fc2f9b..09a784e 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 @@ -177,7 +177,7 @@ cat fixtures/valid.yaml | ./bin/kubeconform -summary Summary: 1 resource found parsing stdin - Valid: 1, Invalid: 0, Errors: 0 Skipped: 0 ``` -* Validating a file, ignoring its resource using both Kind, and GVK (Group, Version, Kind) notations +* Validating a file, ignoring its resource using Kind, GVK (Group, Version, Kind), and Version notations ``` # This will ignore ReplicationController for all apiVersions $ kubeconform -summary -skip ReplicationController fixtures/valid.yaml @@ -186,6 +186,10 @@ Summary: 1 resource found in 1 file - Valid: 0, Invalid: 0, Errors: 0, Skipped: # This will ignore ReplicationController only for apiVersion v1 $ kubeconform -summary -skip v1/ReplicationController fixtures/valid.yaml Summary: 1 resource found in 1 file - Valid: 0, Invalid: 0, Errors: 0, Skipped: 1 + +# This will ignore any resource with apiVersion v1 +$ kubeconform -summary -skip v1 fixtures/valid.yaml +Summary: 1 resource found in 1 file - Valid: 0, Invalid: 0, Errors: 0, Skipped: 1 ``` * Validating a folder, increasing the number of parallel workers 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) + } + } + } +}