1
0
Fork 0
mirror of https://github.com/kyverno/kyverno.git synced 2025-03-06 16:06:56 +00:00

fix panic when rules are empty (#11821)

Signed-off-by: MUzairS15 <muzair.shaikh810@gmail.com>
Co-authored-by: Mariam Fahmy <mariam.fahmy@nirmata.com>
Co-authored-by: shuting <shuting@nirmata.com>
This commit is contained in:
Mohd Uzair 2025-01-03 15:21:44 +05:30 committed by GitHub
parent 5573e5cded
commit d84fc7b4e1
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
2 changed files with 19 additions and 0 deletions

View file

@ -114,6 +114,10 @@ func checkPolicy(spec *kyvernov1.Spec) (bool, string) {
func checkRuleCount(spec *kyvernov1.Spec) (bool, string) {
var msg string
if len(spec.Rules) == 0 {
msg = "skip generating ValidatingAdmissionPolicy: no rules found."
return false, msg
}
if len(spec.Rules) > 1 {
msg = "skip generating ValidatingAdmissionPolicy: multiple rules are not applicable."
return false, msg

View file

@ -841,6 +841,21 @@ func Test_Can_Generate_ValidatingAdmissionPolicy(t *testing.T) {
`),
expected: true,
},
{
name: "policy-with-no-rules",
policy: []byte(`
{
"apiVersion": "kyverno.io/v1",
"kind": "ClusterPolicy",
"metadata": {
"name": "empty-policy"
},
"spec": {
"rules": []
}
}`),
expected: false,
},
}
for _, test := range testCases {