Skip to content

Add From/To fields in SecurityPolicy#1391

Open
wenqiq wants to merge 1 commit intovmware-tanzu:mainfrom
wenqiq:topic/wenqi/securityPolicy
Open

Add From/To fields in SecurityPolicy#1391
wenqiq wants to merge 1 commit intovmware-tanzu:mainfrom
wenqiq:topic/wenqi/securityPolicy

Conversation

@wenqiq
Copy link
Copy Markdown
Contributor

@wenqiq wenqiq commented Mar 12, 2026

Replace the current Sources/Destinations fields with From/To in SecurityPolicy CRD, this aligns with Kubernetes NetworkPolicy naming conventions.
Introduce from/to peer selectors as the preferred fields in SecurityPolicy rules.
Keep sources/destinations for backward compatibility and normalize aliases so from/to take precedence.
Update CRD schemas, samples, docs, and unit tests accordingly.

@codecov-commenter
Copy link
Copy Markdown

codecov-commenter commented Mar 13, 2026

Codecov Report

❌ Patch coverage is 74.41860% with 11 lines in your changes missing coverage. Please review.
✅ Project coverage is 73.75%. Comparing base (315ffc8) to head (b9716ad).

Files with missing lines Patch % Lines
pkg/nsx/services/securitypolicy/firewall.go 64.70% 0 Missing and 6 partials ⚠️
pkg/nsx/services/securitypolicy/builder.go 80.95% 0 Missing and 4 partials ⚠️
pkg/nsx/services/securitypolicy/expand.go 80.00% 0 Missing and 1 partial ⚠️
Additional details and impacted files

Impacted file tree graph

@@            Coverage Diff             @@
##             main    #1391      +/-   ##
==========================================
- Coverage   75.34%   73.75%   -1.59%     
==========================================
  Files         151      151              
  Lines       21317    21375      +58     
==========================================
- Hits        16062    15766     -296     
- Misses       3784     3815      +31     
- Partials     1471     1794     +323     
Flag Coverage Δ
unit-tests 73.75% <74.41%> (-1.59%) ⬇️
Files with missing lines Coverage Δ
pkg/nsx/services/securitypolicy/expand.go 69.49% <80.00%> (-3.47%) ⬇️
pkg/nsx/services/securitypolicy/builder.go 71.15% <80.95%> (-16.45%) ⬇️
pkg/nsx/services/securitypolicy/firewall.go 60.46% <64.70%> (-11.39%) ⬇️
🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@wenqiq wenqiq marked this pull request as ready for review March 17, 2026 13:37
Sources []SecurityPolicyPeer `json:"sources,omitempty"`
// Destinations defines the endpoints where the traffic is to. For egress rule only.
Destinations []SecurityPolicyPeer `json:"destinations,omitempty"`
// From is an alias of Sources for ingress rules.
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

This is for T1 mode, I think we don't need to change it, it's in deprecated stage.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

but internally controller logic, we don't have a real internal version of CRD, in fact, we use v1alpha1.SecurityPolicy acting as an internal version, and do CRD conversion.
https://github.com/vmware-tanzu/nsx-operator/blob/main/pkg/controllers/securitypolicy/securitypolicy_controller.go#L149
So, if necessary, we can keep this change in T1 and make both CRD definitions identical except group version.

// fields so that rule hashing and tagging are independent of which alias the user used.
func normalizeRulePeers(rule *v1alpha1.SecurityPolicyRule) {
if len(rule.Sources) == 0 && len(rule.From) > 0 {
rule.Sources = append(rule.Sources, rule.From...)
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

we might want to keep rule.From and remove rule.Sources later, so, is better copy to rule.From.
This might need more changes where it's referring to rule.Source.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

updated

@wenqiq wenqiq force-pushed the topic/wenqi/securityPolicy branch from 8359c7b to 2a64255 Compare March 23, 2026 12:27
Sources []SecurityPolicyPeer `json:"sources,omitempty"`
// Destinations defines the endpoints where the traffic is to. For egress rule only.
Destinations []SecurityPolicyPeer `json:"destinations,omitempty"`
// From is an alias of Sources for ingress rules.
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

but internally controller logic, we don't have a real internal version of CRD, in fact, we use v1alpha1.SecurityPolicy acting as an internal version, and do CRD conversion.
https://github.com/vmware-tanzu/nsx-operator/blob/main/pkg/controllers/securitypolicy/securitypolicy_controller.go#L149
So, if necessary, we can keep this change in T1 and make both CRD definitions identical except group version.

//
//nolint:staticcheck // SA1019: must touch deprecated Sources/Destinations to accept legacy CR YAML.
func normalizeRulePeers(rule *v1alpha1.SecurityPolicyRule) {
if len(rule.From) > 0 { //nolint:staticcheck
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

we should keep rule.From, since we want to deprecate rule.Source.
And fro upgrade case, we need to consider support both rule.Source and rule.From.
For upgrade, copy rule.Source to rule.From.
For greenfield, we can only support From.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Thanks for reviewing. The current code handles this by ensuring compatibility between the two fields. When both "From" and "Sources" are present, the content of "From" is prioritized. To minimize changes, "Sources" is still used internally. Therefore, the condition here is to replace "Sources" with "From" if the "From" field is not empty.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

It's ok. we can reduce changes.

@wenqiq wenqiq force-pushed the topic/wenqi/securityPolicy branch 2 times, most recently from 4f93a5f to 515745f Compare March 26, 2026 07:31
Copy link
Copy Markdown
Contributor

@heypnus heypnus left a comment

Choose a reason for hiding this comment

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

Could you please update the "Testing done" in the patch, especially the upgrade case? Thanks.

nsxRuleDstGroupPath = nsxRule.DestinationGroups[0]
} else {
if len(rule.Destinations) > 0 {
destionations := getRuleDestinationPeers(rule)
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

typo: destinations


// normalizeRulePeers migrates deprecated Sources/Destinations into From/To and then clears
// the deprecated fields so that rule hashing and downstream logic only use From/To.
// For upgrade: if only Sources/Destinations are set, they are copied to From/To.
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

For the upgrade case, all security policy's hash will change, right? Will it cause the security policy downtime during the upgrade?

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Right, please added test cases.

@wenqiq wenqiq force-pushed the topic/wenqi/securityPolicy branch from 52c503c to 6cdf841 Compare April 1, 2026 05:45
Signed-off-by: Wenqi Qiu <wenqi.qiu@broadcom.com>
@wenqiq wenqiq force-pushed the topic/wenqi/securityPolicy branch from 6cdf841 to b9716ad Compare April 1, 2026 10:58
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