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 Apr 23, 2023
1 parent 8bc9f42 commit e6479f3
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 3 deletions.
8 changes: 6 additions & 2 deletions Readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand All @@ -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
Expand Down
5 changes: 4 additions & 1 deletion pkg/validator/validator.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

Expand Down Expand Up @@ -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
}
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 @@ -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)
}
}
}
}

0 comments on commit e6479f3

Please sign in to comment.