mirror of
https://github.com/cozystack/cozystack.git
synced 2026-12-06 07:03:45 +09:00
The webhook decides a Secret's tenant visibility from the owning ApplicationDefinition on every write that drops the lineage labels, as an operator's rewrite does. A conversion that hides an operator Secret has to know whether the definition in the API already excludes it, because a label it writes earlier is undone by the next rewrite. SecretTenantVisible evaluates a definition exactly as the webhook does. The tests also load the converted RabbitMQ, MongoDB, MariaDB and ClickHouse definitions and pin that each one hides the platform Secrets its conversion keeps, as an operator rewrite would present them. Assisted-by: LLM Signed-off-by: Andrei Kvapil <kvapss@gmail.com>
93 lines
3.2 KiB
Go
93 lines
3.2 KiB
Go
package lineagecontrollerwebhook
|
|
|
|
import (
|
|
"bytes"
|
|
"context"
|
|
"strings"
|
|
"text/template"
|
|
|
|
cozyv1alpha1 "github.com/cozystack/cozystack/api/v1alpha1"
|
|
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
|
|
"k8s.io/apimachinery/pkg/labels"
|
|
"sigs.k8s.io/controller-runtime/pkg/log"
|
|
)
|
|
|
|
// matchName checks if the provided name matches any of the resource names in the array.
|
|
// Each entry in resourceNames is treated as a Go template that gets rendered using the passed context.
|
|
// A nil resourceNames array matches any string.
|
|
func matchName(ctx context.Context, name string, templateContext map[string]string, resourceNames []string) bool {
|
|
if resourceNames == nil {
|
|
return true
|
|
}
|
|
|
|
logger := log.FromContext(ctx)
|
|
for _, templateStr := range resourceNames {
|
|
tmpl, err := template.New("resourceName").Parse(templateStr)
|
|
if err != nil {
|
|
logger.Error(err, "failed to parse resource name template", "template", templateStr)
|
|
continue
|
|
}
|
|
|
|
var buf bytes.Buffer
|
|
err = tmpl.Execute(&buf, templateContext)
|
|
if err != nil {
|
|
logger.Error(err, "failed to execute resource name template", "template", templateStr, "context", templateContext)
|
|
continue
|
|
}
|
|
|
|
if buf.String() == name {
|
|
return true
|
|
}
|
|
}
|
|
|
|
return false
|
|
}
|
|
|
|
func matchResourceToSelector(ctx context.Context, name string, templateContext, l map[string]string, s *cozyv1alpha1.ApplicationDefinitionResourceSelector) bool {
|
|
sel, err := metav1.LabelSelectorAsSelector(&s.LabelSelector)
|
|
if err != nil {
|
|
log.FromContext(ctx).Error(err, "failed to convert label selector to selector")
|
|
return false
|
|
}
|
|
labelMatches := sel.Matches(labels.Set(l))
|
|
nameMatches := matchName(ctx, name, templateContext, s.ResourceNames)
|
|
return labelMatches && nameMatches
|
|
}
|
|
|
|
func matchResourceToSelectorArray(ctx context.Context, name string, templateContext, l map[string]string, ss []*cozyv1alpha1.ApplicationDefinitionResourceSelector) bool {
|
|
for _, s := range ss {
|
|
if matchResourceToSelector(ctx, name, templateContext, l, s) {
|
|
return true
|
|
}
|
|
}
|
|
return false
|
|
}
|
|
|
|
func matchResourceToExcludeInclude(ctx context.Context, name string, templateContext, l map[string]string, resources *cozyv1alpha1.ApplicationDefinitionResources) bool {
|
|
if resources == nil {
|
|
return false
|
|
}
|
|
if matchResourceToSelectorArray(ctx, name, templateContext, l, resources.Exclude) {
|
|
return false
|
|
}
|
|
return matchResourceToSelectorArray(ctx, name, templateContext, l, resources.Include)
|
|
}
|
|
|
|
// SecretTenantVisible reports whether the webhook stamps a Secret of the
|
|
// named application tenant-visible under the definition's selectors, the way
|
|
// computeLabels evaluates them on admission. Callers outside the webhook use
|
|
// it to tell whether a definition already in the API hides a Secret before
|
|
// they label it, since the webhook re-evaluates on every write that drops the
|
|
// lineage labels.
|
|
func SecretTenantVisible(ctx context.Context, appDef *cozyv1alpha1.ApplicationDefinition, appName, namespace, secretName string, secretLabels map[string]string) bool {
|
|
if appDef == nil {
|
|
return false
|
|
}
|
|
templateContext := map[string]string{
|
|
"kind": strings.ToLower(appDef.Spec.Application.Kind),
|
|
"name": appName,
|
|
"namespace": namespace,
|
|
}
|
|
return matchResourceToExcludeInclude(ctx, secretName, templateContext, secretLabels, &appDef.Spec.Secrets)
|
|
}
|