Skip to content

Conversation

@stanfordpeng
Copy link
Contributor

@stanfordpeng stanfordpeng commented Nov 6, 2025

Summary

This PR fixes the EngageOptions behavior in ApplyToFor() and ApplyToOwns() to merge options field-by-field instead of replacing the entire struct. This makes the behavior consistent with ApplyToWatches(), which already correctly merges options. See #93

Problem

When using the multicluster builder with multiple EngageOptions, the last option would overwrite all previous options instead of merging them:

mcbuilder.ControllerManagedBy(mcMgr).
    For(&MyResource{},
        mcbuilder.WithEngageWithLocalCluster(true),
        mcbuilder.WithEngageWithProviderClusters(false),
    ).
    Complete(reconciler)

Due to the implementation in ApplyToFor() and ApplyToOwns(), only engageWithProviderClusters=false would be applied, while engageWithLocalCluster would remain nil and fall back to the default.

This made it impossible to configure hub-and-spoke patterns where you want to watch resources in the local cluster but access remote clusters via GetCluster().

Solution

Changed ApplyToFor() and ApplyToOwns() to merge options field-by-field, matching the existing pattern in ApplyToWatches():

func (w EngageOptions) ApplyToFor(opts *ForInput) {
    if w.engageWithLocalCluster != nil {
        local := *w.engageWithLocalCluster
        opts.engageWithLocalCluster = &local
    }
    if w.engageWithProviderClusters != nil {
        provider := *w.engageWithProviderClusters
        opts.engageWithProviderClusters = &provider
    }
}

This properly merges each field independently while creating new pointer instances to avoid aliasing issues.

Testing

Verified that the hub-and-spoke pattern now works correctly by testing with the following configuration:

mcbuilder.ControllerManagedBy(mcMgr).
    For(&XCR{},
        mcbuilder.WithEngageWithLocalCluster(true),
        mcbuilder.WithEngageWithProviderClusters(false),
    ).
    Complete(reconciler)

The controller now correctly watches only the local cluster while maintaining access to remote clusters via the manager.

@k8s-ci-robot k8s-ci-robot added the do-not-merge/invalid-commit-message Indicates that a PR should not merge because it has an invalid commit message. label Nov 6, 2025
@linux-foundation-easycla
Copy link

linux-foundation-easycla bot commented Nov 6, 2025

CLA Signed

The committers listed above are authorized under a signed CLA.

  • ✅ login: stanfordpeng / name: Stan (9e8b8a8)

@k8s-ci-robot
Copy link
Contributor

Welcome @stanfordpeng!

It looks like this is your first PR to kubernetes-sigs/multicluster-runtime 🎉. Please refer to our pull request process documentation to help your PR have a smooth ride to approval.

You will be prompted by a bot to use commands during the review process. Do not be afraid to follow the prompts! It is okay to experiment. Here is the bot commands documentation.

You can also check if kubernetes-sigs/multicluster-runtime has its own contribution guidelines.

You may want to refer to our testing guide if you run into trouble with your tests not passing.

If you are having difficulty getting your pull request seen, please follow the recommended escalation practices. Also, for tips and tricks in the contribution process you may want to read the Kubernetes contributor cheat sheet. We want to make sure your contribution gets all the attention it needs!

Thank you, and welcome to Kubernetes. 😃

@k8s-ci-robot k8s-ci-robot added cncf-cla: no Indicates the PR's author has not signed the CNCF CLA. size/S Denotes a PR that changes 10-29 lines, ignoring generated files. cncf-cla: yes Indicates the PR's author has signed the CNCF CLA. and removed cncf-cla: no Indicates the PR's author has not signed the CNCF CLA. labels Nov 6, 2025
@stanfordpeng stanfordpeng force-pushed the fix/builder-engage-options-merge branch from 026f0a1 to cbd7586 Compare November 6, 2025 06:32
@stanfordpeng stanfordpeng changed the title Fix builder engage options to merge instead of replace Patch fix: Fix builder engage options to merge instead of replace Nov 6, 2025
@stanfordpeng stanfordpeng changed the title Patch fix: Fix builder engage options to merge instead of replace Patch fix: 🐛 Fix builder engage options to merge instead of replace Nov 6, 2025
@stanfordpeng stanfordpeng changed the title Patch fix: 🐛 Fix builder engage options to merge instead of replace 🐛 Fix builder engage options to merge instead of replace Nov 6, 2025
@embik
Copy link
Member

embik commented Nov 9, 2025

This looks good, please just remove the "Fixes" part from the commit message (as you can see, the bot doesn't like that).

When multiple EngageOptions are passed to For() or Owns(), they should
merge field-by-field rather than replacing the entire struct. This makes
the behavior consistent with ApplyToWatches(), which already correctly
merges options.

Before this fix, passing both WithEngageWithLocalCluster(true) and
WithEngageWithProviderClusters(false) would result in only the last
option being applied, making hub-and-spoke patterns impossible.

This change allows both options to be set simultaneously by merging
each field independently, creating new pointer instances to avoid
aliasing issues.

Fixes kubernetes-sigs/issues/93
@stanfordpeng stanfordpeng force-pushed the fix/builder-engage-options-merge branch from cbd7586 to 9e8b8a8 Compare November 10, 2025 07:07
@k8s-ci-robot k8s-ci-robot removed the do-not-merge/invalid-commit-message Indicates that a PR should not merge because it has an invalid commit message. label Nov 10, 2025
Copy link
Member

@embik embik left a comment

Choose a reason for hiding this comment

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

/approve

Thank you for the contribution!

@k8s-ci-robot k8s-ci-robot added the lgtm "Looks good to me", indicates that a PR is ready to be merged. label Nov 10, 2025
@k8s-ci-robot
Copy link
Contributor

[APPROVALNOTIFIER] This PR is APPROVED

This pull-request has been approved by: embik, stanfordpeng

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

The pull request process is described here

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

@k8s-ci-robot
Copy link
Contributor

LGTM label has been added.

Git tree hash: f696cb1ee6db96d64bed2a00724e10e3406c12fd

@k8s-ci-robot k8s-ci-robot added the approved Indicates a PR has been approved by an approver from all required OWNERS files. label Nov 10, 2025
@k8s-ci-robot k8s-ci-robot merged commit 9d082f3 into kubernetes-sigs:main Nov 10, 2025
20 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

approved Indicates a PR has been approved by an approver from all required OWNERS files. cncf-cla: yes Indicates the PR's author has signed the CNCF CLA. lgtm "Looks good to me", indicates that a PR is ready to be merged. size/S Denotes a PR that changes 10-29 lines, ignoring generated files.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants