Skip to content

Conversation

@stanfordpeng
Copy link

Summary

Fixes #93

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.

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
@k8s-ci-robot k8s-ci-robot requested a review from JeremyOT November 6, 2025 06:26
@k8s-ci-robot
Copy link
Contributor

[APPROVALNOTIFIER] This PR is NOT APPROVED

This pull-request has been approved by: stanfordpeng
Once this PR has been reviewed and has the lgtm label, please assign sttts for approval. For more information see the Code Review Process.

The full list of commands accepted by this bot can be found 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 k8s-ci-robot requested a review from sttts November 6, 2025 06:26
@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 (cbd7586)

@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
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#93
@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
@k8s-ci-robot
Copy link
Contributor

Keywords which can automatically close issues and at(@) or hashtag(#) mentions are not allowed in commit messages.

The list of commits with invalid commit messages:

  • cbd7586 Fix builder engage options to merge instead of replace

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-sigs/prow repository. I understand the commands that are listed here.

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

Labels

cncf-cla: yes Indicates the PR's author has signed the CNCF CLA. do-not-merge/invalid-commit-message Indicates that a PR should not merge because it has an invalid commit message. 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.

Builder: Cannot set both WithEngageWithLocalCluster and WithEngageWithProviderClusters - ApplyToFor overwrites options

2 participants