1
0
Fork 0
mirror of https://github.com/kyverno/kyverno.git synced 2025-03-06 16:06:56 +00:00
kyverno/pkg/event/msgbuilder.go
shuting 75a7543c6d
Events fix (#1006)
* remove success event

* remove event success message

* remove events generated on clusterpolicy
2020-07-20 20:30:02 +05:30

43 lines
1.2 KiB
Go

package event
import (
"fmt"
"regexp"
)
//MsgKey is an identified to determine the preset message formats
type MsgKey int
//Message id for pre-defined messages
const (
FPolicyApplyBlockCreate MsgKey = iota
FPolicyApplyBlockUpdate
FPolicyBlockResourceUpdate
FPolicyApplyFailed
FResourcePolicyFailed
)
func (k MsgKey) String() string {
return [...]string{
"Resource %s creation blocked by rule(s) %s",
"Rule(s) '%s' of policy '%s' blocked update of the resource",
"Resource %s update blocked by rule(s) %s",
"Rule(s) '%s' failed to apply on resource %s",
"Rule(s) '%s' of policy '%s' failed to apply on the resource",
}[k]
}
const argRegex = "%[s,d,v]"
var re = regexp.MustCompile(argRegex)
//GetEventMsg return the application message based on the message id and the arguments,
// if the number of arguments passed to the message are incorrect generate an error
func getEventMsg(key MsgKey, args ...interface{}) (string, error) {
// Verify the number of arguments
argsCount := len(re.FindAllString(key.String(), -1))
if argsCount != len(args) {
return "", fmt.Errorf("message expects %d arguments, but %d arguments passed", argsCount, len(args))
}
return fmt.Sprintf(key.String(), args...), nil
}