Skip to content

Commit 5e896b9

Browse files
authored
Merge pull request #228 from bndw/bndw/kinds-to-reject
Add --reject-kinds flag
2 parents 2cfabe6 + 1093f08 commit 5e896b9

File tree

3 files changed

+43
-0
lines changed

3 files changed

+43
-0
lines changed

kubeval/config.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,9 @@ type Config struct {
4747
// schema validation
4848
KindsToSkip []string
4949

50+
// KindsToReject is a list of case-sensitive prohibited kubernetes resources types
51+
KindsToReject []string
52+
5053
// FileName is the name to be displayed when testing manifests read from stdin
5154
FileName string
5255

@@ -79,6 +82,7 @@ func AddKubevalFlags(cmd *cobra.Command, config *Config) *cobra.Command {
7982
cmd.Flags().BoolVar(&config.Strict, "strict", false, "Disallow additional properties not in schema")
8083
cmd.Flags().StringVarP(&config.FileName, "filename", "f", "stdin", "filename to be displayed when testing manifests read from stdin")
8184
cmd.Flags().StringSliceVar(&config.KindsToSkip, "skip-kinds", []string{}, "Comma-separated list of case-sensitive kinds to skip when validating against schemas")
85+
cmd.Flags().StringSliceVar(&config.KindsToReject, "reject-kinds", []string{}, "Comma-separated list of case-sensitive kinds to prohibit validating against schemas")
8286
cmd.Flags().StringVarP(&config.SchemaLocation, "schema-location", "s", "", "Base URL used to download schemas. Can also be specified with the environment variable KUBEVAL_SCHEMA_LOCATION.")
8387
cmd.Flags().StringSliceVar(&config.AdditionalSchemaLocations, "additional-schema-locations", []string{}, "Comma-seperated list of secondary base URLs used to download schemas")
8488
cmd.Flags().StringVarP(&config.KubernetesVersion, "kubernetes-version", "v", "master", "Version of Kubernetes to validate against")

kubeval/kubeval.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -149,6 +149,10 @@ func validateResource(data []byte, schemaCache map[string]*gojsonschema.Schema,
149149
return result, nil
150150
}
151151

152+
if in(config.KindsToReject, kind) {
153+
return result, fmt.Errorf("Prohibited resourse kind '%s' in %s", kind, result.FileName)
154+
}
155+
152156
schemaErrors, err := validateAgainstSchema(body, &result, schemaCache, config)
153157
if err != nil {
154158
return result, fmt.Errorf("%s: %s", result.FileName, err.Error())

kubeval/kubeval_test.go

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -199,6 +199,41 @@ func TestValidateMultipleResourcesWithErrors(t *testing.T) {
199199
}
200200
}
201201

202+
func TestValidateKindsToReject(t *testing.T) {
203+
var tests = []struct {
204+
Name string
205+
KindsToReject []string
206+
Fixture string
207+
Pass bool
208+
}{
209+
{
210+
Name: "allow_all",
211+
KindsToReject: []string{},
212+
Fixture: "valid.yaml",
213+
Pass: true,
214+
},
215+
{
216+
Name: "reject_one",
217+
KindsToReject: []string{"ReplicationController"},
218+
Fixture: "valid.yaml",
219+
Pass: false,
220+
},
221+
}
222+
schemaCache := make(map[string]*gojsonschema.Schema, 0)
223+
224+
for _, test := range tests {
225+
filePath, _ := filepath.Abs("../fixtures/" + test.Fixture)
226+
fileContents, _ := ioutil.ReadFile(filePath)
227+
config := NewDefaultConfig()
228+
config.FileName = test.Fixture
229+
config.KindsToReject = test.KindsToReject
230+
_, err := ValidateWithCache(fileContents, schemaCache, config)
231+
if err != nil && test.Pass == true {
232+
t.Errorf("Validate should pass when testing valid configuration in " + test.Name)
233+
}
234+
}
235+
}
236+
202237
func TestDetermineSchemaURL(t *testing.T) {
203238
var tests = []struct {
204239
config *Config

0 commit comments

Comments
 (0)