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

44 lines
1.2 KiB
Go
Raw Normal View History

2019-05-10 00:05:21 -07:00
package event
import (
"fmt"
"regexp"
)
2019-06-26 12:19:11 -07:00
//MsgKey is an identified to determine the preset message formats
type MsgKey int
//Message id for pre-defined messages
const (
FPolicyApplyBlockCreate MsgKey = iota
2019-06-26 12:19:11 -07:00
FPolicyApplyBlockUpdate
2019-06-27 11:36:10 -07:00
FPolicyBlockResourceUpdate
2020-02-19 19:24:34 -08:00
FPolicyApplyFailed
FResourcePolicyFailed
2019-06-26 12:19:11 -07:00
)
2019-05-10 10:38:38 -07:00
func (k MsgKey) String() string {
2019-05-10 00:05:21 -07:00
return [...]string{
2019-06-27 11:36:10 -07:00
"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",
2020-02-19 19:24:34 -08:00
"Rule(s) '%s' failed to apply on resource %s",
"Rule(s) '%s' of policy '%s' failed to apply on the resource",
2019-05-10 00:05:21 -07:00
}[k]
}
const argRegex = "%[s,d,v]"
2019-05-13 18:17:28 -07:00
var re = regexp.MustCompile(argRegex)
2019-05-10 00:05:21 -07:00
//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
2019-05-10 10:38:38 -07:00
func getEventMsg(key MsgKey, args ...interface{}) (string, error) {
2019-05-10 00:05:21 -07:00
// 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
}