This commit is contained in:
Aleksey Levenstein 2023-12-24 18:33:26 +01:00 committed by GitHub
commit 29123734e8
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 44 additions and 3 deletions

View file

@ -128,7 +128,7 @@ Usage: ./bin/kubeconform [OPTION]... [FILE OR FOLDER]...
-schema-location value -schema-location value
override schemas location search path (can be specified multiple times) override schemas location search path (can be specified multiple times)
-skip string -skip string
comma-separated list of kinds or GVKs to ignore comma-separated list of kinds, api versions or GVKs to ignore
-strict -strict
disallow additional properties not in schema or duplicated keys disallow additional properties not in schema or duplicated keys
-summary -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 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 # This will ignore ReplicationController for all apiVersions
$ kubeconform -summary -skip ReplicationController fixtures/valid.yaml $ 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 # This will ignore ReplicationController only for apiVersion v1
$ kubeconform -summary -skip v1/ReplicationController fixtures/valid.yaml $ kubeconform -summary -skip v1/ReplicationController fixtures/valid.yaml
Summary: 1 resource found in 1 file - Valid: 0, Invalid: 0, Errors: 0, Skipped: 1 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 * Validating a folder, increasing the number of parallel workers

View file

@ -60,7 +60,7 @@ type Opts struct {
SkipKinds map[string]struct{} // List of resource Kinds to ignore SkipKinds map[string]struct{} // List of resource Kinds to ignore
RejectKinds map[string]struct{} // List of resource Kinds to reject 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 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 IgnoreMissingSchemas bool // skip a resource if no schema for that resource can be found
} }
@ -116,6 +116,9 @@ func (val *v) ValidateResource(res resource.Resource) Result {
// for skipping/rejecting resources) and the raw Kind. // for skipping/rejecting resources) and the raw Kind.
skip := func(signature resource.Signature) bool { skip := func(signature resource.Signature) bool {
if _, ok := val.opts.SkipKinds[signature.Version]; ok {
return ok
}
if _, ok := val.opts.SkipKinds[signature.GroupVersionKind()]; ok { if _, ok := val.opts.SkipKinds[signature.GroupVersionKind()]; ok {
return ok return ok
} }

View file

@ -435,3 +435,37 @@ age: not a number
t.Errorf("Expected %+v, got %+v", expectedErrors, got.ValidationErrors) 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)
}
}
}
}