Skip to content

Commit

Permalink
feat: support skipping api version
Browse files Browse the repository at this point in the history
  • Loading branch information
levenleven committed Feb 15, 2023
1 parent 9860cde commit 5f02d62
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 2 deletions.
2 changes: 1 addition & 1 deletion Readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,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
Expand Down
5 changes: 4 additions & 1 deletion pkg/validator/validator.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,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
}

Expand Down Expand Up @@ -104,6 +104,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
}
Expand Down
34 changes: 34 additions & 0 deletions pkg/validator/validator_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -379,3 +379,37 @@ lastName: bar
}
}
}

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)
}
}
}
}

0 comments on commit 5f02d62

Please sign in to comment.