mirror of
https://github.com/projectcapsule/capsule.git
synced 2026-02-14 18:09:58 +00:00
* fix(controller): decode old object for delete requests Signed-off-by: Oliver Bähler <oliverbaehler@hotmail.com> * chore: modernize golang Signed-off-by: Oliver Bähler <oliverbaehler@hotmail.com> * chore: modernize golang Signed-off-by: Oliver Bähler <oliverbaehler@hotmail.com> * chore: modernize golang Signed-off-by: Oliver Bähler <oliverbaehler@hotmail.com> * fix(controller): allow no spaces in template references Signed-off-by: Oliver Bähler <oliverbaehler@hotmail.com> * fix(controller): allow no spaces in template references Signed-off-by: Oliver Bähler <oliverbaehler@hotmail.com> --------- Signed-off-by: Oliver Bähler <oliverbaehler@hotmail.com>
45 lines
1.1 KiB
Go
45 lines
1.1 KiB
Go
// Copyright 2020-2025 Project Capsule Authors
|
|
// SPDX-License-Identifier: Apache-2.0
|
|
|
|
package template
|
|
|
|
import (
|
|
"io"
|
|
"strings"
|
|
|
|
"github.com/valyala/fasttemplate"
|
|
corev1 "k8s.io/api/core/v1"
|
|
|
|
capsulev1beta2 "github.com/projectcapsule/capsule/api/v1beta2"
|
|
)
|
|
|
|
// TemplateForTenantAndNamespace applies templatingto the provided string.
|
|
func TemplateForTenantAndNamespace(template string, tnt *capsulev1beta2.Tenant, ns *corev1.Namespace) string {
|
|
if !strings.Contains(template, "{{") && !strings.Contains(template, "}}") {
|
|
return template
|
|
}
|
|
|
|
t := fasttemplate.New(template, "{{", "}}")
|
|
|
|
values := map[string]string{
|
|
"tenant.name": tnt.Name,
|
|
"namespace": ns.Name,
|
|
}
|
|
|
|
return t.ExecuteFuncString(func(w io.Writer, tag string) (int, error) {
|
|
key := strings.TrimSpace(tag)
|
|
if v, ok := values[key]; ok {
|
|
return w.Write([]byte(v))
|
|
}
|
|
|
|
return 0, nil
|
|
})
|
|
}
|
|
|
|
// TemplateForTenantAndNamespace applies templating to all values in the provided map in place.
|
|
func TemplateForTenantAndNamespaceMap(m map[string]string, tnt *capsulev1beta2.Tenant, ns *corev1.Namespace) {
|
|
for k, v := range m {
|
|
m[k] = TemplateForTenantAndNamespace(v, tnt, ns)
|
|
}
|
|
}
|