Skip to content

fix(no-anti-affinity): support topology spread constraints#1224

Open
mwozniakk wants to merge 2 commits into
stackrox:mainfrom
mwozniakk:fix/no-anti-affinity-topology-spread
Open

fix(no-anti-affinity): support topology spread constraints#1224
mwozniakk wants to merge 2 commits into
stackrox:mainfrom
mwozniakk:fix/no-anti-affinity-topology-spread

Conversation

@mwozniakk

Copy link
Copy Markdown

Fixes #894.

Summary:

  • Treats matching topologySpreadConstraints as satisfying the no-anti-affinity check.
  • Requires the spread constraint to match the configured topology key and pod template labels.
  • Adds unit tests for matching and non-matching topology spread constraints.
  • Updates generated check docs.

Testing:

  • go test ./pkg/templates/antiaffinity ./pkg/builtinchecks
  • go test ./...

@mwozniakk mwozniakk requested a review from rhybrillou as a code owner July 9, 2026 12:06
@coderabbitai

coderabbitai Bot commented Jul 9, 2026

Copy link
Copy Markdown

Review Change Stack

No actionable comments were generated in the recent review. 🎉

ℹ️ Recent review info
⚙️ Run configuration

Configuration used: Central YAML (base), Organization UI (inherited)

Review profile: CHILL

Plan: Enterprise

Run ID: 6ab1a58e-0335-451e-8623-f2f27ef78a5c

📥 Commits

Reviewing files that changed from the base of the PR and between 20f4ba9 and f63de09.

📒 Files selected for processing (1)
  • pkg/templates/antiaffinity/template_test.go
🚧 Files skipped from review as they are similar to previous changes (1)
  • pkg/templates/antiaffinity/template_test.go

📝 Walkthrough

Summary by CodeRabbit

  • Bug Fixes
    • Anti-affinity validation now short-circuits when a workload’s topologySpreadConstraints already match the configured criteria, reducing false warnings.
  • Documentation
    • Updated the no-anti-affinity remediation and description to include topologySpreadConstraints alongside podAntiAffinity, with clearer labelSelector and kubernetes.io/hostname examples.

Walkthrough

The anti-affinity checker now treats matching topologySpreadConstraints as sufficient, updates the builtin check text to mention that alternative, and adds tests covering matching, mismatching, and invalid selector cases.

Changes

TopologySpreadConstraints support

Layer / File(s) Summary
Core validation logic and early-return guard
pkg/templates/antiaffinity/template.go
Adds topologySpreadConstraintsMatchAgainstNodes and uses it to return no diagnostics when a pod template’s spread constraints already satisfy the topology-key and label-match checks.
Test coverage and check documentation
pkg/templates/antiaffinity/template_test.go, pkg/builtinchecks/yamls/no-anti-affinity.yaml
Adds topology-spread deployment helpers and validation coverage across topology-key, selector, and unsatisfiable-policy cases, and updates the builtin check description and remediation to include topologySpreadConstraints.

Estimated code review effort: 2 (Simple) | ~12 minutes

🚥 Pre-merge checks | ✅ 5
✅ Passed checks (5 passed)
Check name Status Explanation
Title check ✅ Passed The title is concise and accurately summarizes the main change: adding topology spread constraint support to the no-anti-affinity check.
Description check ✅ Passed The description is clearly related to the change and matches the implementation and tests.
Linked Issues check ✅ Passed The change addresses #894 by accepting matching topologySpreadConstraints as satisfying the no-anti-affinity rule.
Out of Scope Changes check ✅ Passed The modified docs, validation logic, and tests all support the stated topologySpreadConstraints feature.
Docstring Coverage ✅ Passed No functions found in the changed files to evaluate docstring coverage. Skipping docstring coverage check.
✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create PR with unit tests

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands.

@coderabbitai coderabbitai Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Actionable comments posted: 2

🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

Inline comments:
In `@pkg/templates/antiaffinity/template_test.go`:
- Around line 135-152: Add a focused test covering TopologySpreadConstraints
with WhenUnsatisfiable set to ScheduleAnyway in the AntiAffinityTestSuite
helpers and related deployment setup. The current
addDeploymentWithTopologySpreadConstraint path only exercises DoNotSchedule, so
extend the test suite with a case that uses ScheduleAnyway and asserts the
intended behavior in topologySpreadConstraintsMatchAgainstNodes and the
deployment validation flow. Use the existing
addDeploymentWithTopologySpreadConstraint, addDeploymentWithReplicas, and
ctx.ModifyDeployment helpers to keep the new case aligned with the current test
structure.

In `@pkg/templates/antiaffinity/template.go`:
- Around line 115-127: The helper topologySpreadConstraintsMatchAgainstNodes
currently treats every matching TopologySpreadConstraint as enforcing spread,
including soft preferences. Update this function to only return true when the
constraint’s WhenUnsatisfiable is DoNotSchedule, in addition to the existing
topologyKeyMatcher and labelSelector match. Keep the LabelSelectorAsSelector
error handling as-is, and add the new guard directly in the conditional so
ScheduleAnyway constraints are ignored.
🪄 Autofix (Beta)

Fix all unresolved CodeRabbit comments on this PR:

  • Push a commit to this branch (recommended)
  • Create a new PR with the fixes

ℹ️ Review info
⚙️ Run configuration

Configuration used: Central YAML (base), Organization UI (inherited)

Review profile: CHILL

Plan: Enterprise

Run ID: 8c1c8f5e-a8f2-43c2-a656-48d1c2fa212a

📥 Commits

Reviewing files that changed from the base of the PR and between 1521d3a and 20f4ba9.

⛔ Files ignored due to path filters (1)
  • docs/generated/checks.md is excluded by !**/generated/**
📒 Files selected for processing (3)
  • pkg/builtinchecks/yamls/no-anti-affinity.yaml
  • pkg/templates/antiaffinity/template.go
  • pkg/templates/antiaffinity/template_test.go

Comment thread pkg/templates/antiaffinity/template_test.go
Comment thread pkg/templates/antiaffinity/template.go
@codecov

codecov Bot commented Jul 9, 2026

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 81.81818% with 2 lines in your changes missing coverage. Please review.
✅ Project coverage is 31.42%. Comparing base (dbd7529) to head (20f4ba9).
⚠️ Report is 332 commits behind head on main.

Files with missing lines Patch % Lines
pkg/templates/antiaffinity/template.go 81.81% 1 Missing and 1 partial ⚠️
Additional details and impacted files
@@             Coverage Diff             @@
##             main    #1224       +/-   ##
===========================================
- Coverage   62.36%   31.42%   -30.94%     
===========================================
  Files         197      239       +42     
  Lines        4854     6572     +1718     
===========================================
- Hits         3027     2065      -962     
- Misses       1439     4329     +2890     
+ Partials      388      178      -210     
Flag Coverage Δ
unit 31.42% <81.81%> (-30.94%) ⬇️

Flags with carried forward coverage won't be shown. Click here to find out more.

☔ View full report in Codecov by Harness.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

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.

[FEATURE_REQUEST] Support topologySpreadConstraints for no-anti-affinity check

1 participant