Skip to content

fix(resources): key generator origins per target instead of slice index - #2325

Open
zaidrain06 wants to merge 1 commit into
projectcapsule:mainfrom
zaidrain06:fix/generator-non-positional-origin-2324
Open

zaidrain06 wants to merge 1 commit into
projectcapsule:mainfrom
zaidrain06:fix/generator-non-positional-origin-2324

Conversation

@zaidrain06

Copy link
Copy Markdown

Fixes #2324

Summary

Generator output in Collector.Collect previously derived Origin from the global slice position (<resourceIndex>/generator-<generatorIndex>-<i>), which becomes part of ResourceID.GetKey and ResourceID.FieldOwner. Whenever an earlier item was removed (or an unsorted informer-backed context list returned items in a different order), every subsequent generated object shifted to a new origin, causing pruneProcessedItems to delete the live object under its previous origin and applyAccumulatedItems to recreate it.

Changes

  • Per-target generator occurrence origin (internal/controllers/resources/collect.go): Track the occurrence index per rendered target (GVK + targetNamespace + name) within each generator instead of using the global slice position i. Unique targets consistently receive <resourceIndex>/generator-<generatorIndex>-0, while multiple documents emitted by the same generator for the same target still get distinct SSA field managers (-0, -1, ...).
  • In-place legacy origin & SSA field-owner migration (pkg/api/processor/processor_func.go): Before pruning in Reconcile (and before applying in ReconcileNamespace), match existing <resourceIndex>/generator-<generatorIndex>-<pos> ProcessedItems entries whose target GVK + namespace + name + tenant matches an unmatched new generator item from the same <resourceIndex>/generator-<generatorIndex>- prefix, rename the live object's managedFields manager in place with optimistic resourceVersion locking, and update the tracked Origin without recreating the object.
  • Deterministic context list ordering (pkg/template/reference.go): Sort UnstructuredList items returned by ResourceReference.loadResources by (namespace, name) so templates iterating .items directly over cache-backed list reads receive a deterministic order across reconciles.
  • Unit tests: Added TestCollectorGeneratorOriginIsIndependentOfSlicePosition (collect_test.go) and TestProcessorMigratesPositionalGeneratorOriginsWithoutRecreatingObjects (processor_func_test.go).

Verification

  • go test ./internal/controllers/resources/... ./pkg/api/processor/... ./pkg/template/...
  • go test -race ./internal/controllers/... -run '^$' -bench '^BenchmarkController' -benchtime=1x
  • golangci-lint run -c .golangci.yaml

Assign generator origins per rendered target occurrence rather than
the global output slice index so adding or removing an earlier item
does not shift later items and recreate unchanged objects.

- Scope generator occurrence counters per target GVK/namespace/name
  while preserving distinct SSA field managers when a single generator
  emits multiple documents for the same target.
- Migrate existing positional generator origins and SSA managedFields
  entries in place before pruning so upgrades do not trigger a one-time
  recreation of existing generated objects.
- Sort selector-listed context resources deterministically by namespace
  and name in ResourceReference.loadResources.

Fixes projectcapsule#2324

Signed-off-by: Ömer <[email protected]>

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

GlobalTenantResource: generated objects are identified by their position in the generator output, so removing an earlier item recreates all later ones

1 participant