Compile kubernetes ingress annotation regex only once

This commit is contained in:
Henning 2021-01-07 18:56:03 +01:00 committed by GitHub
parent 5d800ba5fe
commit 63929b0341
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

@ -13,6 +13,8 @@ const (
annotationsPrefix = "traefik.ingress.kubernetes.io/" annotationsPrefix = "traefik.ingress.kubernetes.io/"
) )
var annotationsRegex = regexp.MustCompile(`(.+)\.(\w+)\.(\d+)\.(.+)`)
// RouterConfig is the router's root configuration from annotations. // RouterConfig is the router's root configuration from annotations.
type RouterConfig struct { type RouterConfig struct {
Router *RouterIng `json:"router,omitempty"` Router *RouterIng `json:"router,omitempty"`
@ -86,8 +88,6 @@ func convertAnnotations(annotations map[string]string) map[string]string {
return nil return nil
} }
exp := regexp.MustCompile(`(.+)\.(\w+)\.(\d+)\.(.+)`)
result := make(map[string]string) result := make(map[string]string)
for key, value := range annotations { for key, value := range annotations {
@ -97,8 +97,8 @@ func convertAnnotations(annotations map[string]string) map[string]string {
newKey := strings.ReplaceAll(key, "ingress.kubernetes.io/", "") newKey := strings.ReplaceAll(key, "ingress.kubernetes.io/", "")
if exp.MatchString(newKey) { if annotationsRegex.MatchString(newKey) {
newKey = exp.ReplaceAllString(newKey, "$1.$2[$3].$4") newKey = annotationsRegex.ReplaceAllString(newKey, "$1.$2[$3].$4")
} }
result[newKey] = value result[newKey] = value