mirror of
https://github.com/kyverno/kyverno.git
synced 2025-03-31 03:45:17 +00:00
[Bug] Enforce message wrong (#7208)
* 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:
parent
3af6ba90ac
commit
10ef443006
2 changed files with 4 additions and 9 deletions
pkg/webhooks/utils
|
@ -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
|
||||
}
|
||||
|
|
|
@ -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) {
|
||||
|
|
Loading…
Add table
Reference in a new issue