Skip to content

Conversation

@jimmidyson
Copy link
Member

What problem does this PR solve?:
Refactoring to simplify the label selector logic. This allows for preallocation of slices.

  • Changed targetNamespaceLabelKey to targetNamespaceLabelSelector in values.yaml for clarity.
  • Updated deployment template to reflect the new label selector usage.
  • Modified main application logic to validate and parse the label selector.
  • Removed deprecated IsTargetNamespace function in favor of a more flexible label selector approach.
  • Adjusted related tests and documentation accordingly.

Also correctly handle context timeouts in copy functions in order to ensure there are no goroutine leaks.

Which issue(s) this PR fixes:
Fixes #

How Has This Been Tested?:

Special notes for your reviewer:

- Changed `targetNamespaceLabelKey` to `targetNamespaceLabelSelector` in `values.yaml` for clarity.
- Updated deployment template to reflect the new label selector usage.
- Modified main application logic to validate and parse the label selector.
- Removed deprecated `IsTargetNamespace` function in favor of a more flexible label selector approach.
- Adjusted related tests and documentation accordingly.
Copy link
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull request overview

This PR refactors the namespace sync configuration to use a more flexible label selector approach instead of a simple label key check. The change allows for more sophisticated namespace targeting using Kubernetes label selectors (e.g., environment=production or team in (platform,infrastructure)). Additionally, it adds context cancellation checks in loops to prevent potential goroutine leaks when operations are cancelled.

Key changes:

  • Replaced targetNamespaceLabelKey with targetNamespaceLabelSelector throughout the codebase
  • Added context cancellation checks in walkReferences and Reconcile loops
  • Removed the deprecated NamespaceHasLabelKey function and its tests
  • Updated configuration files and command-line flags to support the new label selector format

Reviewed changes

Copilot reviewed 12 out of 12 changed files in this pull request and generated 1 comment.

Show a summary per file
File Description
pkg/controllers/namespacesync/suite_test.go Updated test setup to parse and use label selector instead of label key
pkg/controllers/namespacesync/references.go Added context cancellation check in loop to prevent goroutine leaks
pkg/controllers/namespacesync/label_test.go Removed tests for deprecated NamespaceHasLabelKey function
pkg/controllers/namespacesync/label.go Removed deprecated NamespaceHasLabelKey function
pkg/controllers/namespacesync/flags.go Added new label selector flag and deprecated old label key flag
pkg/controllers/namespacesync/copy.go Removed commented-out code for zeroing ManagedFields and OwnerReferences
pkg/controllers/namespacesync/controller.go Refactored to use label selector with pre-allocated slices and added context checks
cmd/main.go Added label selector parsing and validation logic
charts/cluster-api-runtime-extensions-nutanix/values.yaml Updated configuration key from targetNamespaceLabelKey to targetNamespaceLabelSelector
charts/cluster-api-runtime-extensions-nutanix/values.schema.json Updated schema to reflect new configuration key
charts/cluster-api-runtime-extensions-nutanix/templates/deployment.yaml Updated deployment template to use new label selector flag
charts/cluster-api-runtime-extensions-nutanix/README.md Updated documentation to reflect new configuration parameter

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

| namespaceSync.enabled | bool | `true` | |
| namespaceSync.sourceNamespace | string | `""` | |
| namespaceSync.targetNamespaceLabelKey | string | `"caren.nutanix.com/namespace-sync"` | |
| namespaceSync.targetNamespaceLabelSelector | string | `"caren.nutanix.com/namespace-sync"` | |
Copy link
Contributor

Choose a reason for hiding this comment

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

This example needs to change?

Copy link
Member Author

Choose a reason for hiding this comment

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

That's actually the default and it's still fine, it's a label expression which means label exists, matching the existing behaviour.

@jimmidyson jimmidyson force-pushed the jimmi/namespacesync-refactor branch from cff17a1 to 468bb51 Compare November 25, 2025 12:02
Copy link
Contributor

@dlipovetsky dlipovetsky left a comment

Choose a reason for hiding this comment

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

Nice improvement. Thanks!

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.

4 participants