Files
cozystack/internal/lineagecontrollerwebhook/webhook.go
T
Timofei Larkin 65cb73e380 fix(lineage-webhook): keep an explicit tenantresource false (#4920)
## What this PR does

Lets a chart narrow the lineage webhook's
`internal.cozystack.io/tenantresource` verdict without being able to
widen it. The webhook computes the verdict from the
ApplicationDefinition's `secrets` selectors on first admission and
copied it over whatever label the chart rendered, so a Secret the
definition still selected was projected through
`core.cozystack.io/tenantsecrets` whatever the chart said. A `"false"`
rendered on a Secret the webhook admits for the first time (CREATE) is
now kept; a rendered `"true"` is still replaced by the computed verdict.
A re-admission (UPDATE with `managed-by-cozystack` dropped, which the
cacert controller does on a selector change) still gets the definition's
verdict, because the `"false"` it carries is the one this webhook wrote
earlier.

This is rollout step 2 of the accepted [user secrets API
proposal](https://github.com/cozystack/community/blob/main/design-proposals/user-secrets-api/README.md):
during a kind's conversion the converted chart renders the platform-only
`<release>-credentials` Secret with `tenantresource: "false"` while the
old definition may still select the name, and the label is what keeps
that Secret off the projection in the window. It ships on its own
because nothing else depends on it and the conversions do.

Behaviour otherwise unchanged: an object with no rendered label gets the
definition's verdict as before, and the re-admission short-circuit on
`managed-by-cozystack` is untouched.

Covered by a handler test: on CREATE, a rendered `false` kept where the
selector matches, a rendered `true` overwritten where it does not, and
no rendered label taking the verdict; on a re-admission UPDATE, a grant
where the selector now matches and a revoke where it no longer does.

### Screenshots

No UI change.

### Downstream repositories

Walked the trigger map against the diff: two files under
`internal/lineagecontrollerwebhook`, no CRD, no package, no chart, no
values, no `hack/` change. The ApplicationDefinition's selector
semantics gain one exception, but the website's ApplicationDefinition
page does not describe the projection verdict at that depth, so there is
nothing there to update.

- [x] No downstream repository is affected by this change
- [ ] [cozystack/website](https://github.com/cozystack/website) -
follow-up:
- [ ]
[cozystack/terraform-provider-cozystack](https://github.com/cozystack/terraform-provider-cozystack)
- follow-up:
- [ ]
[cozystack/ansible-cozystack](https://github.com/cozystack/ansible-cozystack)
- follow-up:
- [ ] [cozystack/ccp](https://github.com/cozystack/ccp) - follow-up:
- [ ] [cozystack/talm](https://github.com/cozystack/talm) - follow-up:
- [ ] [cozystack/cozyhr](https://github.com/cozystack/cozyhr) -
follow-up:
- [ ] [cozystack/cozy-proxy](https://github.com/cozystack/cozy-proxy) -
follow-up:
- [ ]
[cozystack/cozystack-telemetry-server](https://github.com/cozystack/cozystack-telemetry-server)
- follow-up:
- [ ]
[cozystack/external-apps-example](https://github.com/cozystack/external-apps-example)
- follow-up:
- [ ] [cozystack/examples](https://github.com/cozystack/examples) -
follow-up:
- [ ] [cozystack/community](https://github.com/cozystack/community) -
follow-up:

### Release note

```release-note
fix(lineage-webhook): a Secret created with `internal.cozystack.io/tenantresource: "false"` keeps that label even where its ApplicationDefinition selects the name, so a chart can take a Secret off the tenantsecrets projection; a rendered `"true"` is still overwritten by the definition's verdict.
```


<!-- This is an auto-generated comment: release notes by coderabbit.ai
-->
<!-- coderabbit-ai-generated:v1 -->

## Summary by CodeRabbit

* **Bug Fixes**
* On resource creation, an existing `false` tenant-resource label is
preserved even when the configured selector matches. Otherwise, the
label reflects the selector’s verdict. On updates, the label is set
according to the selector.
* **Tests**
* Added coverage for tenant-resource label behavior on creation and
updates, including existing and missing labels.
<!-- end of auto-generated comment: release notes by coderabbit.ai -->
2026-11-22 23:53:22 +04:00

340 lines
12 KiB
Go

package lineagecontrollerwebhook
import (
"context"
"encoding/json"
"errors"
"fmt"
"maps"
"strings"
"github.com/cozystack/cozystack/pkg/lineage"
admissionv1 "k8s.io/api/admission/v1"
corev1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
"k8s.io/apimachinery/pkg/runtime"
"k8s.io/apimachinery/pkg/runtime/schema"
utiljson "k8s.io/apimachinery/pkg/util/json"
"k8s.io/client-go/dynamic"
"k8s.io/client-go/rest"
ctrl "sigs.k8s.io/controller-runtime"
"sigs.k8s.io/controller-runtime/pkg/client"
"sigs.k8s.io/controller-runtime/pkg/client/apiutil"
"sigs.k8s.io/controller-runtime/pkg/log"
"sigs.k8s.io/controller-runtime/pkg/webhook/admission"
schedulerapi "github.com/cozystack/cozystack-scheduler/pkg/apis/v1alpha1"
cozyv1alpha1 "github.com/cozystack/cozystack/api/v1alpha1"
appsv1alpha1 "github.com/cozystack/cozystack/pkg/apis/apps/v1alpha1"
corev1alpha1 "github.com/cozystack/cozystack/pkg/apis/core/v1alpha1"
)
var (
ErrNoAncestors = errors.New("no managed apps found in lineage")
ErrAncestryAmbiguous = errors.New("object ancestry is ambiguous")
)
const (
ManagedObjectKey = "internal.cozystack.io/managed-by-cozystack"
ManagerGroupKey = "apps.cozystack.io/application.group"
ManagerKindKey = "apps.cozystack.io/application.kind"
ManagerNameKey = "apps.cozystack.io/application.name"
)
// getResourceSelectors returns the appropriate ApplicationDefinitionResources for a given GroupKind
func (h *LineageControllerWebhook) getResourceSelectors(gk schema.GroupKind, crd *cozyv1alpha1.ApplicationDefinition) *cozyv1alpha1.ApplicationDefinitionResources {
switch {
case gk.Group == "" && gk.Kind == "Secret":
return &crd.Spec.Secrets
case gk.Group == "" && gk.Kind == "Service":
return &crd.Spec.Services
case gk.Group == "networking.k8s.io" && gk.Kind == "Ingress":
return &crd.Spec.Ingresses
default:
return nil
}
}
// SetupWithManager registers the handler with the webhook server.
func (h *LineageControllerWebhook) SetupWithManagerAsWebhook(mgr ctrl.Manager) error {
cfg := rest.CopyConfig(mgr.GetConfig())
var err error
h.dynClient, err = dynamic.NewForConfig(cfg)
if err != nil {
return err
}
httpClient, err := rest.HTTPClientFor(cfg)
if err != nil {
return err
}
h.mapper, err = apiutil.NewDynamicRESTMapper(cfg, httpClient)
if err != nil {
return err
}
h.ownerCache = lineage.NewObjectCache(ownerCacheTTL)
h.initConfig()
// Register HTTP path -> handler.
mgr.GetWebhookServer().Register("/mutate-lineage", &admission.Webhook{Handler: h})
return nil
}
// InjectDecoder lets controller-runtime give us a decoder for AdmissionReview requests.
func (h *LineageControllerWebhook) InjectDecoder(d admission.Decoder) error {
h.decoder = d
return nil
}
// Handle is called for each AdmissionReview that matches the webhook config.
func (h *LineageControllerWebhook) Handle(ctx context.Context, req admission.Request) admission.Response {
logger := log.FromContext(ctx).WithValues(
"gvk", req.Kind.String(),
"namespace", req.Namespace,
"name", req.Name,
"operation", req.Operation,
)
warn := make(admission.Warnings, 0)
obj := &unstructured.Unstructured{}
if err := h.decodeUnstructured(req, obj); err != nil {
return admission.Errored(400, fmt.Errorf("decode object: %w", err))
}
// The objectSelector skips objects carrying the managed label, but on UPDATE
// it matches if either the old or the new object does. Pods go on, since
// every Pod admission also needs applySchedulingClass.
if obj.GetKind() != "Pod" && hasAllLineageLabels(obj) {
logger.V(1).Info("object already carries lineage labels, skipping owner walk")
return admission.Allowed("lineage labels already present")
}
owner, err := h.getOwner(ctx, obj)
switch {
case err != nil && errors.Is(err, ErrAncestryAmbiguous):
warn = append(warn, "object ancestry ambiguous, using first ancestor found")
case err != nil && errors.Is(err, ErrNoAncestors):
// not a problem, mark object as unmanaged
case err != nil:
logger.Error(err, "error computing lineage labels")
return admission.Errored(500, fmt.Errorf("error computing lineage labels: %w", err))
}
labels, err := h.computeLabels(ctx, obj, owner, req.Operation)
if err != nil {
logger.Error(err, "error computing lineage labels")
return admission.Errored(500, fmt.Errorf("error computing lineage labels: %w", err))
}
h.applyLabels(obj, labels)
if err := h.applySchedulingClass(ctx, obj, owner, req.Namespace); err != nil {
logger.Error(err, "error applying scheduling class")
return admission.Errored(500, fmt.Errorf("error applying scheduling class: %w", err))
}
mutated, err := json.Marshal(obj)
if err != nil {
return admission.Errored(500, fmt.Errorf("marshal mutated pod: %w", err))
}
logger.V(1).Info("mutated pod", "namespace", obj.GetNamespace(), "name", obj.GetName())
return admission.PatchResponseFromRaw(req.Object.Raw, mutated).WithWarnings(warn...)
}
func (h *LineageControllerWebhook) getOwner(ctx context.Context, o *unstructured.Unstructured) (*unstructured.Unstructured, error) {
owners := lineage.WalkOwnershipGraphWithCache(ctx, h.dynClient, h.mapper, h, h.ownerCache, o)
if len(owners) == 0 {
return nil, ErrNoAncestors
}
obj, err := owners[0].GetUnstructuredCached(ctx, h.dynClient, h.mapper, h.ownerCache)
if err != nil {
return nil, err
}
if len(owners) > 1 {
err = ErrAncestryAmbiguous
}
return obj, err
}
func (h *LineageControllerWebhook) computeLabels(ctx context.Context, obj *unstructured.Unstructured, owner *unstructured.Unstructured, op admissionv1.Operation) (map[string]string, error) {
if owner == nil {
return nil, nil
}
gv, err := schema.ParseGroupVersion(owner.GetAPIVersion())
if err != nil {
// should never happen, we got an APIVersion right from the API
return nil, fmt.Errorf("could not parse APIVersion %s to a group and version: %w", owner.GetAPIVersion(), err)
}
labels := map[string]string{
// truncate apigroup to first 63 chars
ManagedObjectKey: "true",
ManagerGroupKey: func(s string) string {
if len(s) < 63 {
return s
}
s = s[:63]
for b := s[62]; (b < 'a' || b > 'z') && (b < 'A' || b > 'Z') && (b < '0' || b > '9'); s = s[:len(s)-1] {
b = s[len(s)-1]
}
return s
}(gv.Group),
ManagerKindKey: owner.GetKind(),
ManagerNameKey: owner.GetName(),
}
templateLabels := map[string]string{
"kind": strings.ToLower(owner.GetKind()),
"name": owner.GetName(),
"namespace": obj.GetNamespace(),
}
cfg := h.config.Load().(*runtimeConfig)
crd := cfg.appCRDMap[appRef{gv.Group, owner.GetKind()}]
resourceSelectors := h.getResourceSelectors(obj.GroupVersionKind().GroupKind(), crd)
selected := matchResourceToExcludeInclude(ctx, obj.GetName(), templateLabels, obj.GetLabels(), resourceSelectors)
// On CREATE the object may narrow the verdict and never widen it: a
// rendered "false" is kept even where the definition selects the name, a
// rendered "true" is replaced. Only on CREATE: a re-admission (UPDATE with
// managed-by-cozystack dropped, as the cacert controller does on a selector
// change) carries the "false" this webhook wrote earlier, and keeping it
// would block a grant.
if op == admissionv1.Create && obj.GetLabels()[corev1alpha1.TenantResourceLabelKey] == "false" {
selected = false
}
labels[corev1alpha1.TenantResourceLabelKey] = "false"
if selected {
labels[corev1alpha1.TenantResourceLabelKey] = corev1alpha1.TenantResourceLabelValue
}
return labels, nil
}
func (h *LineageControllerWebhook) applyLabels(o *unstructured.Unstructured, labels map[string]string) {
existing := o.GetLabels()
if existing == nil {
existing = make(map[string]string)
}
maps.Copy(existing, labels)
o.SetLabels(existing)
}
// hasAllLineageLabels needs every key computeLabels writes, not just
// ManagedObjectKey: an UPDATE can carry a partial set written by another client,
// and the tenantsecret registry filters on TenantResourceLabelKey.
func hasAllLineageLabels(o *unstructured.Unstructured) bool {
labels := o.GetLabels()
if labels == nil {
return false
}
if labels[ManagedObjectKey] != "true" {
return false
}
for _, k := range []string{ManagerGroupKey, ManagerKindKey, ManagerNameKey, corev1alpha1.TenantResourceLabelKey} {
if _, ok := labels[k]; !ok {
return false
}
}
return true
}
// applySchedulingClass injects schedulerName, scheduling-class annotation, and
// scheduling-class label into Pods whose namespace carries the
// scheduler.cozystack.io/scheduling-class label.
//
// The label (in addition to the annotation) lets SchedulingClass authors write
// podAffinity rules whose labelSelector matches pods across applications that
// share the same class.
//
// If the referenced SchedulingClass CR does not exist (e.g. the scheduler
// package is not installed), the injection is silently skipped so that pods
// are not left Pending.
func (h *LineageControllerWebhook) applySchedulingClass(ctx context.Context, obj, owner *unstructured.Unstructured, namespace string) error {
if obj.GetKind() != "Pod" {
return nil
}
// Determine scheduling class: owner Application field takes priority,
// then fall back to namespace label.
var schedulingClass string
if owner != nil {
var app appsv1alpha1.Application
if err := runtime.DefaultUnstructuredConverter.FromUnstructured(owner.Object, &app); err == nil {
schedulingClass = app.SchedulingClass()
}
}
if schedulingClass == "" {
ns := &corev1.Namespace{}
if err := h.Get(ctx, client.ObjectKey{Name: namespace}, ns); err != nil {
return fmt.Errorf("getting namespace %s: %w", namespace, err)
}
schedulingClass = ns.Labels[schedulerapi.SchedulingClassLabel]
}
if schedulingClass == "" {
return nil
}
// Verify that the referenced SchedulingClass CR exists.
// If the CRD is not installed or the CR is missing, skip injection
// so that pods are not stuck Pending on a non-existent scheduler.
_, err := h.dynClient.Resource(schedulingClassGVR).Get(ctx, schedulingClass, metav1.GetOptions{})
if err != nil {
logger := log.FromContext(ctx)
logger.Info("SchedulingClass not found, skipping scheduler injection",
"schedulingClass", schedulingClass, "namespace", namespace)
return nil
}
if err := unstructured.SetNestedField(obj.Object, schedulerapi.SchedulerName, "spec", "schedulerName"); err != nil {
return fmt.Errorf("setting schedulerName: %w", err)
}
annotations := obj.GetAnnotations()
if annotations == nil {
annotations = make(map[string]string)
}
annotations[schedulerapi.SchedulingClassAnnotation] = schedulingClass
obj.SetAnnotations(annotations)
podLabels := obj.GetLabels()
if podLabels == nil {
podLabels = make(map[string]string)
}
podLabels[schedulerapi.SchedulingClassLabel] = schedulingClass
obj.SetLabels(podLabels)
return nil
}
var schedulingClassGVR = schema.GroupVersionResource{
Group: schedulerapi.Group,
Version: schedulerapi.Version,
Resource: schedulerapi.Resource,
}
func (h *LineageControllerWebhook) decodeUnstructured(req admission.Request, out *unstructured.Unstructured) error {
if h.decoder != nil {
if err := h.decoder.Decode(req, out); err == nil {
return nil
}
if req.Kind.Group != "" || req.Kind.Kind != "" || req.Kind.Version != "" {
out.SetGroupVersionKind(schema.GroupVersionKind{
Group: req.Kind.Group,
Version: req.Kind.Version,
Kind: req.Kind.Kind,
})
if err := h.decoder.Decode(req, out); err == nil {
return nil
}
}
}
if len(req.Object.Raw) == 0 {
return errors.New("empty admission object")
}
// encoding/json would turn every number into float64, and an int64 above
// 2^53 would come back altered in the patch computed against req.Object.Raw.
return utiljson.Unmarshal(req.Object.Raw, &out.Object)
}