Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

#658 - Make 'kinds' in match / exclude block optional #670

Merged
merged 11 commits into from
Feb 7, 2020
3 changes: 1 addition & 2 deletions definitions/install.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -71,8 +71,7 @@ spec:
type: string
resources:
type: object
required:
- kinds
minProperties: 1
properties:
kinds:
type: array
Expand Down
3 changes: 1 addition & 2 deletions definitions/install_debug.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -71,8 +71,7 @@ spec:
type: string
resources:
type: object
required:
- kinds
minProperties: 1
properties:
kinds:
type: array
Expand Down
4 changes: 0 additions & 4 deletions pkg/engine/policy/validate.go
Original file line number Diff line number Diff line change
Expand Up @@ -135,10 +135,6 @@ func validateMatchedResourceDescription(rd kyverno.ResourceDescription) (string,
return "", fmt.Errorf("match resources not specified")
}

if len(rd.Kinds) == 0 {
return "match", fmt.Errorf("kind is mandatory")
}

if err := validateResourceDescription(rd); err != nil {
return "match", err
}
Expand Down
35 changes: 18 additions & 17 deletions pkg/engine/policy/validate_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -248,24 +248,25 @@ func Test_Validate_ResourceDescription_MatchedValid(t *testing.T) {
_, err = validateMatchedResourceDescription(rd)
assert.NilError(t, err)
}
func Test_Validate_ResourceDescription_MissingKindsOnMatched(t *testing.T) {
var err error
matchedResourcedescirption := []byte(`
{
"selector": {
"matchLabels": {
"app.type": "prod"
}
}
}`)

var rd kyverno.ResourceDescription
err = json.Unmarshal(matchedResourcedescirption, &rd)
assert.NilError(t, err)

_, err = validateMatchedResourceDescription(rd)
assert.Assert(t, err != nil)
}
//func Test_Validate_ResourceDescription_MissingKindsOnMatched(t *testing.T) {
// var err error
// matchedResourcedescirption := []byte(`
// {
// "selector": {
// "matchLabels": {
// "app.type": "prod"
// }
// }
// }`)
//
// var rd kyverno.ResourceDescription
// err = json.Unmarshal(matchedResourcedescirption, &rd)
// assert.NilError(t, err)
//
// _, err = validateMatchedResourceDescription(rd)
// assert.Assert(t, err != nil)
//}

func Test_Validate_ResourceDescription_MissingKindsOnExclude(t *testing.T) {
var err error
Expand Down
6 changes: 4 additions & 2 deletions pkg/engine/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,10 @@ func MatchesResourceDescription(resource unstructured.Unstructured, rule kyverno
matches := rule.MatchResources.ResourceDescription
exclude := rule.ExcludeResources.ResourceDescription

if !findKind(matches.Kinds, resource.GetKind()) {
return false
if len(matches.Kinds) > 0 {
if !findKind(matches.Kinds, resource.GetKind()) {
return false
}
}

name := resource.GetName()
Expand Down
Loading