Skip to content

Support cross-resource references in nested map types#681

Open
gustavodiaz7722 wants to merge 2 commits intoaws-controllers-k8s:mainfrom
gustavodiaz7722:nested-map-x-reference
Open

Support cross-resource references in nested map types#681
gustavodiaz7722 wants to merge 2 commits intoaws-controllers-k8s:mainfrom
gustavodiaz7722:nested-map-x-reference

Conversation

@gustavodiaz7722
Copy link
Copy Markdown
Member

Support cross-resource references in nested map types

Problem

The ACK code generator did not support cross-resource references for fields nested inside map types. When a references config pointed to a field inside a map (e.g., PhysicalTableMap.S3Source.DataSourceARN in QuickSight DataSet), the generator would produce an error during code generation. This blocked controllers like QuickSight from using K8s-native resource references for fields within map-typed structures.

Solution

Extended two functions in pkg/generate/code/resource_reference.go:

  • iterReferenceValues() — Added a "map" case to the shape type switch. The new case mirrors the existing "list" case: it generates a for key, val := range loop over map entries, tracks iteration depth, and advances the field access prefix through the map value. Previously this function only handled "structure" and "list" shapes, returning an error for maps.

  • buildIndexBasedFieldAccessorWithOffset() — Extended the list-shape condition to also match "map" shapes (cur.ShapeRef.Shape.Type == "list" || cur.ShapeRef.Shape.Type == "map"). This ensures index-based field accessors are correctly built when traversing map entries during reference resolution and clearing.

Testing

  • Added a QuickSight test fixture (generator-with-nested-references.yaml) that configures PhysicalTableMap.CustomSql.DataSourceArn as a cross-resource reference to DataSource.
  • Added 3 unit tests:
    • Test_ResolveReferencesForField_SingleReference_WithinMap — verifies reference resolution code generation for map-nested fields
    • Test_ClearResolvedReferencesForField_SingleReference_WithinMap — verifies reference clearing code generation
    • Test_ReferenceFieldsValidation_MapNestedReference — verifies field validation accepts map-nested references
  • All 113 existing tests continue to pass (backward compatible).

Affected resources

Any ACK controller with references inside map-typed fields. Immediate use case: QuickSight DataSet's PhysicalTableMap entries referencing DataSource ARNs.

By submitting this pull request, I confirm that my contribution is made under the terms of the Apache 2.0 license.

@ack-prow ack-prow bot requested review from jlbutler and knottnt March 23, 2026 21:26
@gustavodiaz7722
Copy link
Copy Markdown
Member Author

/retest

1 similar comment
@gustavodiaz7722
Copy link
Copy Markdown
Member Author

/retest

Copy link
Copy Markdown
Contributor

@knottnt knottnt left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks @gustavodiaz7722. Left a couple comments. Also have you been able to test the generated code locally with the quicksight-controller?

hasReferences = true
arr := f0iter.CustomSQL.DataSourceRef.From
if arr.Name == nil || *arr.Name == "" {
return hasReferences, fmt.Errorf("provided resource reference is nil or empty: PhysicalTableMap.CustomSQL.DataSourceRef")
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Q: should we include the map key in this error message? If there are multiple entries of the same object type it would help users identify the problematic part of their spec.

Copy link
Copy Markdown
Member Author

@gustavodiaz7722 gustavodiaz7722 Mar 25, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think for now we can leave as is, since it follows similar behavior for list type references. I agree though it's poor UX if we don't include key/element identifier.

For now I think it's okay to leave as is. I can follow up on this in another PR if that's okay

require.NotNil(crd)

expected :=
` for f0idx, f0iter := range ko.Spec.PhysicalTableMap {
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: It looks like we're re-using the list variables names here. Changing the name to be map specific could make the generated code more readable. Example names would be f0key and f0value.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yup, agreed. Will change this in new revision.

@gustavodiaz7722
Copy link
Copy Markdown
Member Author

@knottnt Yes, I have tested these changes locally with new e2e test for verifying cross referencing is working correctly. I haven't updated the existing open PR with the new test yet, have them locally staged.

@ack-prow
Copy link
Copy Markdown

ack-prow bot commented Mar 25, 2026

@gustavodiaz7722: The following test failed, say /retest to rerun all failed tests or /retest-required to rerun all mandatory failed tests:

Test name Commit Details Required Rerun command
eks-controller-test 718b277 link true /test eks-controller-test

Full PR test history. Your PR dashboard.

Details

Instructions 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/test-infra repository. I understand the commands that are listed here.

@ack-prow
Copy link
Copy Markdown

ack-prow bot commented Mar 26, 2026

[APPROVALNOTIFIER] This PR is APPROVED

This pull-request has been approved by: gustavodiaz7722, michaelhtm

The full list of commands accepted by this bot can be found here.

The pull request process is described here

Details Needs approval from an approver in each of these files:

Approvers can indicate their approval by writing /approve in a comment
Approvers can cancel approval by writing /approve cancel in a comment

@ack-prow ack-prow bot added the approved label Mar 26, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants