feat: restore nested cross-resource references after Create and Update - #267
feat: restore nested cross-resource references after Create and Update#267gustavodiaz7722 wants to merge 1 commit into
Conversation
|
[APPROVALNOTIFIER] This PR is NOT APPROVED This pull-request has been approved by: gustavodiaz7722 The full list of commands accepted by this bot can be found here. DetailsNeeds approval from an approver in each of these files:Approvers can indicate their approval by writing |
e30dc94 to
c279560
Compare
A cross-resource reference (*Ref) is generated as a sibling of the concrete field it resolves into. A resource manager builds its return value from an AWS API response, which has no concept of a reference, so rebuilding the containing struct drops every *Ref inside it. That disables ClearResolvedReferences, which suppresses a resolved value only while the sibling *Ref is visible, so the spec patch deletes the declared *Ref and stores the resolved value in its place. The next apply of the manifest puts the *Ref back beside that value, a pair validateReferenceFields rejects, stopping reconciliation. Add an optional ReferenceEnsurer interface and invoke it on the object a resource manager hands back from Create and from Update, sourcing the references from the declared resource. It is kept separate from ReferenceManager and reached through a type assertion, so controllers generated before the method existed still satisfy AWSResourceManager and compile unchanged; they opt in by regenerating. The source is `desired`, not `reconcileDesired`: the latter is handed to Update, and a manager may mutate what it is given, so it is not a reliable record of what the user declared. The restoration is not hooked into patchResourceMetadataAndSpec because the late-initialization patch uses the AWS-observed object as its base, which carries no references. Pairs with aws-controllers-k8s/code-generator#738, which generates the method. Issue aws-controllers-k8s/community#2361 Issue aws-controllers-k8s/community#2431
c279560 to
c2f3d04
Compare
|
/retest |
|
@gustavodiaz7722: The following test failed, say
Full PR test history. Your PR dashboard. DetailsInstructions for interacting with me using PR comments are available here. If you have questions or suggestions related to my behavior, please file an issue against the kubernetes-sigs/prow repository. I understand the commands that are listed here. |
Summary
A cross-resource reference (
*Ref) is generated as a sibling of the concrete field it resolves into —spec.vpcConfig.subnetRefsnext tospec.vpcConfig.subnetIDs. A resource manager builds its return value from an AWS API response, which has no concept of a reference, so rebuilding the containing struct drops every*Refinside it.That disables
ClearResolvedReferences, which suppresses a resolved value only while it can still see the sibling:So the spec patch deletes the declared
*Refand stores the resolved value in its place — what aws-controllers-k8s/community#2431 reports: a declaredsecurityGroupRefsreplaced bysecurityGroupIDs.Reconciliation continues until the manifest is applied again, from Helm, Argo, Flux or
kubectl apply. That apply restores the*Refbeside the now-stored value, andvalidateReferenceFieldsrejects the pair:This PR adds an optional
ReferenceEnsurerinterface and invokes it on the object a resource manager hands back fromCreateand fromUpdate, sourcing the references from the declared resource.Fixes aws-controllers-k8s/community#2431. Pairs with aws-controllers-k8s/code-generator#738, which generates the method.
Backwards compatible
ReferenceEnsureris deliberately separate fromReferenceManagerand reached through a type assertion, so every controller generated before the method existed still satisfiesAWSResourceManagerand compiles unchanged. Those controllers take the existing path untouched and opt in by regenerating.TestReconcilerUpdate_WithoutEnsurerIsUnaffectedpins that.Why these two call sites
The restoration runs where a manager returns an object whose spec is about to be patched back, not at
patchResourceMetadataAndSpec, because the source has to be the declared, reference-resolved resource — and that is only in scope on these paths.lateInitializeResourcepatches with the AWS-observedlatestas its base, so hooked into the shared patch path the restoration would have been handed a source carrying no references at all.TestReconcilerUpdate_LateInitializeIsNotAffectedByEnsureReferenceskeeps that boundary asserted.Why
desiredand notreconcileDesiredreconcileDesiredis what gets handed torm.Update, and a resource manager may mutate the object it is given.apigateway'sApiKeysdkUpdateassignsdesired.ko.Spec.StageKeysstraight from theUpdateApiKeyresponse (sdk.go:331), so by the timeUpdatereturns it is no longer a record of what the user declared.applyIgnoredFieldslikewise merges observed values into it for a resource carrying the ignore-field-drift annotation. Onlydesiredis clean, andTestReconcilerUpdate_EnsuresReferencesAfterUpdateasserts it is what gets passed.What the generated method does
Detailed in aws-controllers-k8s/code-generator#738. Summarised here because it bounds this PR's blast radius:
*Ref— nothing emitted; it cannot be lost, since every write path starts from aDeepCopyof the object it was handed.eks/cluster,lambda/functionandopensearchservice/domainalready hand-maintain insdk_*_post_set_outputhooks.So 117 struct-nested references across 37 resources in 25 controllers change behaviour; the 315 top-level and 38 list-nested ones are untouched.
Testing
Four tests in
pkg/runtime/reconciler_test.go:TestReconcilerUpdate_EnsuresReferencesAfterUpdateUpdate, sourced fromdesired, and the returned object is what gets patchedTestReconcilerCreate_EnsuresReferencesAfterCreateTestReconcilerUpdate_LateInitializeIsNotAffectedByEnsureReferencesTestReconcilerUpdate_WithoutEnsurerIsUnaffectedFull runtime suite passes. Verified on a cluster against real generated code (
lambdaandec2regenerated against code-generator#738): aFunctiondeclaringvpcConfig.securityGroupRefs/subnetRefskeeps both in its stored spec through create and across repeated resyncs, with no resolved IDs written and noReference resolution failedcondition.Not addressed
The read path. This runs after
Createand afterUpdate, not afterReadOne. Two spec writes consume aReadOne-derived object and so can still delete a nested*Ref: theAdoptionPolicy_Adoptbranch ofSync, anddeleteResource. Both use the stored CR as the patch base and theReadOneresult as the target. Confirmed on a cluster for the adoption path, which is why the existingsdk_read_one_post_set_outputhooks in the three controllers above must stay. Covering it is a separate change: the unresolveddesiredon those paths is a valid source, since a*Refis user-declared and resolution only fills the concrete sibling.The delta. It is computed against the raw
ReadOneresult, which this does not touch. A dropped struct-nested*Refwas invisible to the delta — the generated delta compares*Reffields only at the top level — so it never drove a redundantUpdateand there is nothing left to fix for the shapes this PR covers.By submitting this pull request, I confirm that my contribution is made under the terms of the Apache 2.0 license.