mirror of
https://github.com/projectcapsule/capsule.git
synced 2026-08-25 16:07:24 +00:00
fix: allow managed metadata defined per tenant (#1947)
* fix: allow managed metadata defined per tenant Signed-off-by: Oliver Baehler <oliver@sudo-i.net> * fix: allow managed metadata defined per tenant Signed-off-by: Oliver Baehler <oliver@sudo-i.net> --------- Signed-off-by: Oliver Baehler <oliver@sudo-i.net>
This commit is contained in:
+19
-3
@@ -20,6 +20,17 @@ var AllowedNamespaceMetadataTemplates = sets.New[string](
|
||||
"namespace",
|
||||
)
|
||||
|
||||
func AllowedNamespaceMetadataTemplatesString() string {
|
||||
templates := AllowedNamespaceMetadataTemplates.UnsortedList()
|
||||
slices.Sort(templates)
|
||||
|
||||
for i, tpl := range templates {
|
||||
templates[i] = fmt.Sprintf("{{%s}}", tpl)
|
||||
}
|
||||
|
||||
return strings.Join(templates, ", ")
|
||||
}
|
||||
|
||||
var FastTemplateExpression = regexp.MustCompile(`{{\s*([^{}]+)\s*}}`)
|
||||
|
||||
func ValidateKubernetesStringOrAllowedTemplates(
|
||||
@@ -63,15 +74,16 @@ func validateAllowedTemplatesAndReplace(
|
||||
|
||||
for _, match := range matches {
|
||||
raw := match[0]
|
||||
name := strings.TrimSpace(match[1])
|
||||
name := FastTemplateNormalize(match[1])
|
||||
|
||||
if !AllowedNamespaceMetadataTemplates.Has(name) {
|
||||
return value, []string{
|
||||
fmt.Sprintf(
|
||||
"%s: unsupported template %q in %q, allowed templates are {{tenant.name}} and {{namespace}}",
|
||||
"%s: unsupported template %q in %q, allowed templates are %s",
|
||||
fieldPath,
|
||||
name,
|
||||
value,
|
||||
AllowedNamespaceMetadataTemplatesString(),
|
||||
),
|
||||
}
|
||||
}
|
||||
@@ -123,7 +135,7 @@ func FastTemplate(
|
||||
t := fasttemplate.New(template, "{{", "}}")
|
||||
|
||||
return t.ExecuteFuncString(func(w io.Writer, tag string) (int, error) {
|
||||
key := strings.TrimSpace(tag)
|
||||
key := FastTemplateNormalize(tag)
|
||||
if v, ok := templateContext[key]; ok {
|
||||
return w.Write([]byte(v))
|
||||
}
|
||||
@@ -132,6 +144,10 @@ func FastTemplate(
|
||||
})
|
||||
}
|
||||
|
||||
func FastTemplateNormalize(key string) string {
|
||||
return strings.TrimSpace(key)
|
||||
}
|
||||
|
||||
// FastTemplateMap applies templating to all values in the provided map in place.
|
||||
func FastTemplateMap(
|
||||
m map[string]string,
|
||||
|
||||
Reference in New Issue
Block a user