Add From/To fields in SecurityPolicy#1391
Conversation
Codecov Report❌ Patch coverage is Additional details and impacted files@@ 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
🚀 New features to boost your workflow:
|
| 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. |
There was a problem hiding this comment.
This is for T1 mode, I think we don't need to change it, it's in deprecated stage.
There was a problem hiding this comment.
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...) |
There was a problem hiding this comment.
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.
8359c7b to
2a64255
Compare
| 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. |
There was a problem hiding this comment.
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 |
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
It's ok. we can reduce changes.
4f93a5f to
515745f
Compare
heypnus
left a comment
There was a problem hiding this comment.
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) |
|
|
||
| // 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. |
There was a problem hiding this comment.
For the upgrade case, all security policy's hash will change, right? Will it cause the security policy downtime during the upgrade?
There was a problem hiding this comment.
Right, please added test cases.
52c503c to
6cdf841
Compare
Signed-off-by: Wenqi Qiu <wenqi.qiu@broadcom.com>
6cdf841 to
b9716ad
Compare
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.