1
0
Fork 0
mirror of https://github.com/kyverno/kyverno.git synced 2025-03-31 03:45:17 +00:00

[Bug] Enforce message wrong ()

* fix

Signed-off-by: Ved Ratan <vedratan8@gmail.com>

* fixed tests

Signed-off-by: Ved Ratan <vedratan8@gmail.com>

---------

Signed-off-by: Ved Ratan <vedratan8@gmail.com>
This commit is contained in:
Ved Ratan 2023-05-16 15:24:26 +05:30 committed by GitHub
parent 3af6ba90ac
commit 10ef443006
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 4 additions and 9 deletions
pkg/webhooks/utils

View file

@ -40,15 +40,11 @@ func GetBlockedMessages(engineResponses []engineapi.EngineResponse) string {
return ""
}
failures := make(map[string]interface{})
hasViolations := false
for _, er := range engineResponses {
ruleToReason := make(map[string]string)
for _, rule := range er.PolicyResponse.Rules {
if rule.Status() != engineapi.RuleStatusPass {
ruleToReason[rule.Name()] = rule.Message()
if rule.Status() == engineapi.RuleStatusFail {
hasViolations = true
}
}
}
if len(ruleToReason) != 0 {
@ -60,8 +56,7 @@ func GetBlockedMessages(engineResponses []engineapi.EngineResponse) string {
}
r := engineResponses[0].Resource
resourceName := fmt.Sprintf("%s/%s/%s", r.GetKind(), r.GetNamespace(), r.GetName())
action := getAction(hasViolations, len(failures))
results, _ := yaml.Marshal(failures)
msg := fmt.Sprintf("\n\npolicy %s for resource %s: \n\n%s", resourceName, action, results)
msg := fmt.Sprintf("\n\nresource %s was blocked due to the following policies \n\n%s", resourceName, results)
return msg
}

View file

@ -209,7 +209,7 @@ func TestGetBlockedMessages(t *testing.T) {
}),
},
},
want: "\n\npolicy foo/bar/baz for resource violation: \n\ntest:\n rule-fail: message fail\n",
want: "\n\nresource foo/bar/baz was blocked due to the following policies \n\ntest:\n rule-fail: message fail\n",
}, {
name: "error - enforce",
args: args{
@ -221,7 +221,7 @@ func TestGetBlockedMessages(t *testing.T) {
}),
},
},
want: "\n\npolicy foo/bar/baz for resource error: \n\ntest:\n rule-error: message error\n",
want: "\n\nresource foo/bar/baz was blocked due to the following policies \n\ntest:\n rule-error: message error\n",
}, {
name: "error and failure - enforce",
args: args{
@ -234,7 +234,7 @@ func TestGetBlockedMessages(t *testing.T) {
}),
},
},
want: "\n\npolicy foo/bar/baz for resource violation: \n\ntest:\n rule-error: message error\n rule-fail: message fail\n",
want: "\n\nresource foo/bar/baz was blocked due to the following policies \n\ntest:\n rule-error: message error\n rule-fail: message fail\n",
}}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {