Commit Graph
6 Commits
Author SHA1 Message Date
Mattia Eleuteri addf7d1258 fix(lineage-webhook): leave a running Pod's scheduler alone on update
spec.schedulerName cannot change once a Pod exists, yet the webhook set
it on every Pod admission, updates included. A Pod created before its
tenant got a class, or before its application named one, then had
every later update refused by the apiserver, labels and annotations
written by controllers included. A class can only take effect when the
Pod is created.

Assisted-by: LLM
Signed-off-by: Mattia Eleuteri <mattia@hidora.io>
2026-11-23 08:05:10 +01:00
Mattia Eleuteri e67a514903 fix(lineage-webhook): keep the tenant class when an application's is refused
The webhook fell back to the tenant class only when an application
left spec.schedulingClass empty. A value naming a class that does not
exist, or one the tenant does not allow, left the pod with no
scheduler at all, so a typo, or a deliberate bogus name, in any
application lifted the placement constraint the tenant administrator
had set.

Admission sees the application only once, so a class removed from the
allowed list afterwards has to be caught here as well. Naming the
tenant's own class needs no entry in the list, being the same as
leaving the field empty.

The application class is looked up once, so a class deleted between
deciding and injecting cannot leave the pod with neither class. A
lookup error fails the admission instead of counting as absence:
otherwise a timeout or a 5xx from the apiserver would admit the Pod on
the default scheduler, outside the tenant's constraint, until it was
recreated. Under failurePolicy Fail its controller retries. Only an
absent class, or an absent SchedulingClass CRD, means there is nothing
to inject.

Assisted-by: LLM
Signed-off-by: Mattia Eleuteri <mattia@hidora.io>
2026-11-23 08:05:08 +01:00
Mattia Eleuteri aed71ed610 test(lineage-webhook): cover the per-application scheduling class
The applySchedulingClass tests only ever passed a nil owner, so the
path where an application's class takes over from the tenant's, and
the guard that keeps an over-long class name off the Pod labels, could
break unnoticed. The owner is built as unstructured, as the lineage
walk returns it, because its conversion into Application.Spec.Raw is
part of that path.

Assisted-by: LLM
Signed-off-by: Mattia Eleuteri <mattia@hidora.io>
2026-11-23 08:05:07 +01:00
mattia-eleuteri 250f89dc72 feat(scheduler): set scheduling-class label on mutated pods
The lineage-controller-webhook now sets a
scheduler.cozystack.io/scheduling-class label on mutated pods (in
addition to the existing annotation of the same name).

This unlocks SchedulingClass authors writing podAffinity rules whose
labelSelector targets pods across different applications that share
the same class. A natural pattern becomes: define a class whose name
is the affinity-group identifier, attach it to several applications,
and have the scheduler co-locate them at the chosen topology key
(region, zone, hostname).

Behavior preserved when the SchedulingClass CR is missing: pods are
left untouched (no scheduler injection, no label) so they do not stay
Pending on a non-existent scheduler.

Tests cover label/annotation/schedulerName injection, existing label
preservation, non-Pod no-op, no-class no-op, and missing-CR no-op.

Signed-off-by: mattia-eleuteri <mattia@hidora.io>
2026-10-01 05:59:24 +03:00
Mattia Eleuteri ca24d8f5f6 perf(lineage): cache owner GETs and short-circuit re-admission
Every admission walks the owner chain (Pod, StatefulSet, HelmRelease,
application CR) with one uncached GET per level, and sibling Pods
repeat the same lookups. On a cluster with many tenants that put the
lineage webhook at 300-500ms p99, the largest share of apiserver
admission latency.

Owners are now reused for 60 seconds, successful lookups only. A failed
lookup, whether RESTMapping, NotFound or a timeout, leaves the walk
without an owner, so the object is admitted with no lineage labels and
stays unlabeled until its next UPDATE runs the walk again. Secrets,
Services and PVCs can go a long time between updates, so a cached
failure would leave every object admitted within the TTL unlabeled for
that long, and the aggregated cozystack-api makes such failures
routine: discovery of apps.cozystack.io fails while it rolls.

Non-Pod objects that already carry every lineage label, tenantresource
included, skip the walk, since the objectSelector still matches an
UPDATE whose old object lacks the label. Pods go through it regardless,
as each Pod admission also sets the scheduling class.

Assisted-by: LLM
Signed-off-by: Mattia Eleuteri <mattia@hidora.io>
2026-09-29 17:32:13 +02:00
Aleksei Sviridkin 77b8dc8935 fix(lineage-webhook): keep large integers intact when patching
With no decoder injected, the webhook decodes the admitted object with
encoding/json, which turns every number into float64, so an int64
above 2^53 comes back rounded. jsonpatch up to v2.4 compared both
sides as float64 and hid this; from v2.5 it compares json.Number, and
the webhook would emit a replace that silently rewrites such a field.
Decode with apimachinery's json, which keeps integers as int64, and
pin that the patch touches only the lineage labels and scheduling.

Assisted-by: LLM
Signed-off-by: Aleksei Sviridkin <f@lex.la>
2026-09-24 16:55:03 +03:00