Skip to content

Commit d8e348a

Browse files
atosattoyannh
andauthoredOct 16, 2022
Allow to skip resources using the GVK notation (#92)
* Allow to skip resources using the GVK notation * Update flags description, add integration tests and update readme Co-authored-by: Yann Hamon <yann@mandragor.org>
1 parent 466ec73 commit d8e348a

File tree

6 files changed

+52
-10
lines changed

6 files changed

+52
-10
lines changed
 

‎Readme.md

+13-2
Original file line numberDiff line numberDiff line change
@@ -92,11 +92,11 @@ Usage: ./bin/kubeconform [OPTION]... [FILE OR FOLDER]...
9292
-output string
9393
output format - json, junit, tap, text (default "text")
9494
-reject string
95-
comma-separated list of kinds to reject
95+
comma-separated list of kinds or GVKs to reject
9696
-schema-location value
9797
override schemas location search path (can be specified multiple times)
9898
-skip string
99-
comma-separated list of kinds to ignore
99+
comma-separated list of kinds or GVKs to ignore
100100
-strict
101101
disallow additional properties not in schema or duplicated keys
102102
-summary
@@ -145,6 +145,17 @@ cat fixtures/valid.yaml | ./bin/kubeconform -summary
145145
Summary: 1 resource found parsing stdin - Valid: 1, Invalid: 0, Errors: 0 Skipped: 0
146146
```
147147

148+
* Validating a file, ignoring its resource using both Kind, and GVK (Group, Version, Kind) notations
149+
```
150+
# This will ignore ReplicationController for all apiVersions
151+
./bin/kubeconform -summary -skip ReplicationController fixtures/valid.yaml
152+
Summary: 1 resource found in 1 file - Valid: 0, Invalid: 0, Errors: 0, Skipped: 1
153+
154+
# This will ignore ReplicationController only for apiVersion v1
155+
$ ./bin/kubeconform -summary -skip v1/ReplicationController fixtures/valid.yaml
156+
Summary: 1 resource found in 1 file - Valid: 0, Invalid: 0, Errors: 0, Skipped: 1
157+
```
158+
148159
* Validating a folder, increasing the number of parallel workers
149160
```
150161
$ ./bin/kubeconform -summary -n 16 fixtures

‎acceptance.bats

+12
Original file line numberDiff line numberDiff line change
@@ -218,6 +218,18 @@ resetCacheFolder() {
218218
[ "$output" = "fixtures/valid.yaml - bob ReplicationController skipped" ]
219219
}
220220

221+
@test "Skip when parsing a resource with a GVK to skip" {
222+
run bin/kubeconform -verbose -skip v1/ReplicationController fixtures/valid.yaml
223+
[ "$status" -eq 0 ]
224+
[ "$output" = "fixtures/valid.yaml - bob ReplicationController skipped" ]
225+
}
226+
227+
@test "Do not skip when parsing a resource with a GVK to skip, where the Kind matches but not the version" {
228+
run bin/kubeconform -verbose -skip v2/ReplicationController fixtures/valid.yaml
229+
[ "$status" -eq 0 ]
230+
[ "$output" = "fixtures/valid.yaml - ReplicationController bob is valid" ]
231+
}
232+
221233
@test "Fail when parsing a resource from a kind to reject" {
222234
run bin/kubeconform -verbose -reject ReplicationController fixtures/valid.yaml
223235
[ "$status" -eq 1 ]

‎pkg/config/config.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -65,8 +65,8 @@ func FromFlags(progName string, args []string) (Config, string, error) {
6565

6666
flags.StringVar(&c.KubernetesVersion, "kubernetes-version", "master", "version of Kubernetes to validate against, e.g.: 1.18.0")
6767
flags.Var(&schemaLocationsParam, "schema-location", "override schemas location search path (can be specified multiple times)")
68-
flags.StringVar(&skipKindsCSV, "skip", "", "comma-separated list of kinds to ignore")
69-
flags.StringVar(&rejectKindsCSV, "reject", "", "comma-separated list of kinds to reject")
68+
flags.StringVar(&skipKindsCSV, "skip", "", "comma-separated list of kinds or GVKs to ignore")
69+
flags.StringVar(&rejectKindsCSV, "reject", "", "comma-separated list of kinds or GVKs to reject")
7070
flags.BoolVar(&c.Debug, "debug", false, "print debug information")
7171
flags.BoolVar(&c.ExitOnError, "exit-on-error", false, "immediately stop execution when the first error is encountered")
7272
flags.BoolVar(&c.IgnoreMissingSchemas, "ignore-missing-schemas", false, "skip files with missing schemas instead of failing")

‎pkg/resource/resource.go

+12-5
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,18 @@ type Signature struct {
2020
Kind, Version, Namespace, Name string
2121
}
2222

23+
// GroupVersionKind returns a string with the GVK encoding of a resource signature.
24+
// This encoding slightly differs from the Kubernetes upstream implementation
25+
// in order to be suitable for being used in the kubeconform command-line arguments.
26+
func (sig *Signature) GroupVersionKind() string {
27+
return fmt.Sprintf("%s/%s", sig.Version, sig.Kind)
28+
}
29+
30+
// QualifiedName returns a string for a signature in the format version/kind/namespace/name
31+
func (sig *Signature) QualifiedName() string {
32+
return fmt.Sprintf("%s/%s/%s/%s", sig.Version, sig.Kind, sig.Namespace, sig.Name)
33+
}
34+
2335
// Signature computes a signature for a resource, based on its Kind, Version, Namespace & Name
2436
func (res *Resource) Signature() (*Signature, error) {
2537
if res.sig != nil {
@@ -119,8 +131,3 @@ func (res *Resource) Resources() []Resource {
119131

120132
return []Resource{*res}
121133
}
122-
123-
// QualifiedName returns a string for a signature in the format version/kind/namespace/name
124-
func (sig *Signature) QualifiedName() string {
125-
return fmt.Sprintf("%s/%s/%s/%s", sig.Version, sig.Kind, sig.Namespace, sig.Name)
126-
}

‎pkg/validator/validator.go

+11
Original file line numberDiff line numberDiff line change
@@ -98,12 +98,23 @@ type v struct {
9898
// ValidateResource validates a single resource. This allows to validate
9999
// large resource streams using multiple Go Routines.
100100
func (val *v) ValidateResource(res resource.Resource) Result {
101+
// For backward compatibility reasons when determining whether
102+
// a resource should be skipped or rejected we use both
103+
// the GVK encoding of the resource signatures (the recommended method
104+
// for skipping/rejecting resources) and the raw Kind.
105+
101106
skip := func(signature resource.Signature) bool {
107+
if _, ok := val.opts.SkipKinds[signature.GroupVersionKind()]; ok {
108+
return ok
109+
}
102110
_, ok := val.opts.SkipKinds[signature.Kind]
103111
return ok
104112
}
105113

106114
reject := func(signature resource.Signature) bool {
115+
if _, ok := val.opts.RejectKinds[signature.GroupVersionKind()]; ok {
116+
return ok
117+
}
107118
_, ok := val.opts.RejectKinds[signature.Kind]
108119
return ok
109120
}

‎pkg/validator/validator_test.go

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
11
package validator
22

33
import (
4-
"github.com/yannh/kubeconform/pkg/registry"
54
"testing"
65

6+
"github.com/yannh/kubeconform/pkg/registry"
7+
78
"github.com/yannh/kubeconform/pkg/resource"
89
)
910

0 commit comments

Comments
 (0)
Please sign in to comment.