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) }