feat: Add support for Topology Spread Constraints in Karmada components#7558
feat: Add support for Topology Spread Constraints in Karmada components#7558Ebraam-Ashraf wants to merge 3 commits into
Conversation
|
Welcome @Ebraam-Ashraf! It looks like this is your first PR to karmada-io/karmada 🎉 |
Summary of ChangesHello, I'm Gemini Code Assist1! I'm currently reviewing this pull request and will post my feedback shortly. In the meantime, here's a summary to help you and other reviewers quickly get up to speed! This pull request introduces support for Kubernetes Topology Spread Constraints within the Karmada operator. By exposing this configuration in the Highlights
Using Gemini Code AssistThe full guide for Gemini Code Assist can be found on our documentation page, here are some quick tips. Invoking Gemini You can request assistance from Gemini at any point by creating a comment using either
Customization To customize the Gemini Code Assist for GitHub experience, repository maintainers can create a configuration file and/or provide a custom code review style guide (such as PEP-8 for Python) by creating and adding files to a Limitations & Feedback Gemini Code Assist may make mistakes. Please leave feedback on any instances where its feedback is incorrect or counterproductive. You can react with 👍 and 👎 on @gemini-code-assist comments. If you're interested in giving your feedback about your experience with Gemini Code Assist for GitHub and other Google products, sign up here. Footnotes
|
There was a problem hiding this comment.
Pull request overview
Note
Copilot was unable to run its full agentic suite in this review.
This PR adds support for configuring Kubernetes topologySpreadConstraints across operator-managed components by extending the shared patcher, API types, and CRD schemas, plus adding/update unit tests to validate propagation into generated Deployments/StatefulSets.
Changes:
- Extend the
Patcherto accept and applyTopologySpreadConstraintsto Deployments and StatefulSets. - Add
topologySpreadConstraintstoCommonSettings(API type + CRD schemas, including Helm chart CRD template). - Add tests across patcher and controlplane components to verify constraints are applied.
Reviewed changes
Copilot reviewed 15 out of 29 changed files in this pull request and generated 7 comments.
Show a summary per file
| File | Description |
|---|---|
| operator/pkg/util/patcher/pather.go | Add topologySpreadConstraints field + setter and apply to pod specs. |
| operator/pkg/util/patcher/pather_test.go | Add patcher-level tests for constraints patching / not patching. |
| operator/pkg/controlplane/webhook/webhook.go | Pass TopologySpreadConstraints into patcher for webhook deployment. |
| operator/pkg/controlplane/webhook/webhook_test.go | Add webhook install test covering topology spread constraints. |
| operator/pkg/controlplane/search/search.go | Pass TopologySpreadConstraints into patcher for search deployment. |
| operator/pkg/controlplane/search/search_test.go | Add search install test covering topology spread constraints. |
| operator/pkg/controlplane/metricsadapter/metricsadapter.go | Pass TopologySpreadConstraints into patcher for metrics-adapter deployment. |
| operator/pkg/controlplane/metricsadapter/metricsadapter_test.go | Add metrics-adapter install test covering topology spread constraints. |
| operator/pkg/controlplane/etcd/etcd.go | Pass TopologySpreadConstraints into patcher for etcd statefulset. |
| operator/pkg/controlplane/etcd/etcd_test.go | Add etcd install test covering topology spread constraints. |
| operator/pkg/controlplane/controlplane.go | Pass TopologySpreadConstraints into patcher for controlplane deployments. |
| operator/pkg/controlplane/controlplane_test.go | Add manifest tests covering topology spread constraints across controlplane components. |
| operator/pkg/controlplane/apiserver/apiserver.go | Pass TopologySpreadConstraints into patcher for API server deployments. |
| operator/pkg/controlplane/apiserver/apiserver_test.go | Add API server install tests covering topology spread constraints. |
| operator/pkg/apis/operator/v1alpha1/type.go | Add TopologySpreadConstraints to CommonSettings API type. |
| operator/config/crds/operator.karmada.io_karmadas.yaml | Extend CRD OpenAPI schema with topologySpreadConstraints. |
| charts/karmada-operator/templates/crd-operator.karmada.io_karmadas.yaml | Mirror CRD schema changes in Helm chart template. |
Files not reviewed (1)
- operator/pkg/apis/operator/v1alpha1/zz_generated.deepcopy.go: Language not supported
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
| // WithTopologySpreadConstraints sets topology spread constraints to the patcher. | ||
| func (p *Patcher) WithTopologySpreadConstraints(constraints []corev1.TopologySpreadConstraint) *Patcher { | ||
| p.topologySpreadConstraints = constraints | ||
| return p | ||
| } |
| if len(p.topologySpreadConstraints) > 0 { | ||
| deployment.Spec.Template.Spec.TopologySpreadConstraints = p.topologySpreadConstraints | ||
| } |
| if len(deployment.Spec.Template.Spec.TopologySpreadConstraints) != 1 { | ||
| t.Fatalf("expected 1 topology spread constraint, but got %d", len(deployment.Spec.Template.Spec.TopologySpreadConstraints)) | ||
| } | ||
| if deployment.Spec.Template.Spec.TopologySpreadConstraints[0].TopologyKey != "topology.kubernetes.io/zone" { | ||
| t.Errorf("expected topology key 'topology.kubernetes.io/zone', but got '%s'", deployment.Spec.Template.Spec.TopologySpreadConstraints[0].TopologyKey) | ||
| } |
| } | ||
| } | ||
|
|
||
| func TestGetKubeControllerManagerManifestWithTopologySpreadConstraints(t *testing.T) { |
| } | ||
| } | ||
|
|
||
| func TestGetKarmadaControllerManagerManifestWithTopologySpreadConstraints(t *testing.T) { |
| } | ||
| } | ||
|
|
||
| func TestGetKarmadaSchedulerManifestWithTopologySpreadConstraints(t *testing.T) { |
| } | ||
| } | ||
|
|
||
| func TestGetKarmadaDeschedulerManifestWithTopologySpreadConstraints(t *testing.T) { |
There was a problem hiding this comment.
Code Review
This pull request introduces support for TopologySpreadConstraints across various Karmada control plane components (including APIServer, Controller Manager, Scheduler, Descheduler, Etcd, Metrics Adapter, Search, and Webhook). This is achieved by adding the field to the CommonSettings API definition, updating the Patcher utility to apply these constraints to Deployments and StatefulSets, and adding corresponding unit tests. One review comment was kept, which suggests improving code clarity in etcd.go by explicitly accessing embedded fields via cfg.CommonSettings rather than directly through cfg.
| WithTolerations(cfg.Tolerations).WithAffinity(cfg.Affinity). | ||
| WithTopologySpreadConstraints(cfg.TopologySpreadConstraints).WithVolumeData(cfg.VolumeData).ForStatefulSet(etcdStatefulSet) |
There was a problem hiding this comment.
When accessing fields from an embedded struct, prefer explicit access through the struct name for clarity, even if direct access is possible. Please access Tolerations, Affinity, and TopologySpreadConstraints through cfg.CommonSettings.
| WithTolerations(cfg.Tolerations).WithAffinity(cfg.Affinity). | |
| WithTopologySpreadConstraints(cfg.TopologySpreadConstraints).WithVolumeData(cfg.VolumeData).ForStatefulSet(etcdStatefulSet) | |
| WithTolerations(cfg.CommonSettings.Tolerations).WithAffinity(cfg.CommonSettings.Affinity). | |
| WithTopologySpreadConstraints(cfg.CommonSettings.TopologySpreadConstraints).WithVolumeData(cfg.VolumeData).ForStatefulSet(etcdStatefulSet) |
References
- When accessing fields from an embedded struct, prefer explicit access through the struct name for clarity, even if direct access is possible.
|
Codecov Report❌ Patch coverage is
Additional details and impacted files@@ Coverage Diff @@
## master #7558 +/- ##
==========================================
- Coverage 42.12% 42.10% -0.03%
==========================================
Files 879 879
Lines 54649 54767 +118
==========================================
+ Hits 23022 23060 +38
- Misses 29898 29973 +75
- Partials 1729 1734 +5
Flags with carried forward coverage won't be shown. Click here to find out more. ☔ View full report in Codecov by Sentry. 🚀 New features to boost your workflow:
|
Wow! @Ebraam-Ashraf , I'm very happy to celebrate this milestone with you. Working on such an essential feature for you first PR is quite impressive. |
|
Thanks! Will take a look. |
|
@Ebraam-Ashraf, please get the DCO done. I see that step failling. |
|
/assign |
|
[APPROVALNOTIFIER] This PR is NOT APPROVED This pull-request has been approved by: 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 |
Signed-off-by: Ebraam <ebraam.ashraf@gmail.com>
Signed-off-by: Ebraam <ebraam.ashraf@gmail.com>
Signed-off-by: Ebraam <ebraam.ashraf@gmail.com>
952b9fc to
670aafb
Compare
What type of PR is this?
/kind feature
/kind api-change
What this PR does / why we need it:
Adds a
topologySpreadConstraintsfield to theCommonSettingsstruct sothat users can control how control plane component pod replicas are distributed
across topology domains (zones, nodes, racks, etc.) using the standard
Kubernetes Topology Spread Constraints mechanism.
This complements the existing
affinityfield. UnlikepodAntiAffinity, whichblocks scheduling entirely when replicas exceed node count, topology spread
constraints keep distribution as even as possible — making them the safer choice
for node pools that may grow or shrink over time.
Key use cases:
Which issue(s) this PR fixes:
Part of #7436
Special notes for your reviewer:
topologySpreadConstraintsandaffinity(podAntiAffinity) fields arefully complementary and can be used together. When both are specified,
Kubernetes evaluates them jointly.
applyconfigurationsfiles were updated to use the correct*corev1.TopologySpreadConstraintApplyConfigurationpointer type, matchingthe existing pattern for
TolerationsandAffinity.hack/update-crdgen.sh.requirement of at least one Deployment and one StatefulSet.
Does this PR introduce a user-facing change?: