Skip to content

Commit 515745f

Browse files
committed
Add From/To fields in SecurityPolicy
1 parent 1e42bf1 commit 515745f

File tree

16 files changed

+1059
-118
lines changed

16 files changed

+1059
-118
lines changed

build/yaml/crd/legacy/nsx.vmware.com_securitypolicies.yaml

Lines changed: 340 additions & 4 deletions
Large diffs are not rendered by default.

build/yaml/crd/vpc/crd.nsx.vmware.com_securitypolicies.yaml

Lines changed: 340 additions & 4 deletions
Large diffs are not rendered by default.

build/yaml/samples/nsx_v1alpha1_securitypolicy.yaml

Lines changed: 38 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,4 +41,41 @@ spec:
4141
role: frontend
4242
ports:
4343
- protocol: TCP
44-
port: 8000
44+
port: 8000
45+
46+
---
47+
48+
# Example using from/to aliases (Kubernetes NetworkPolicy style)
49+
apiVersion: crd.nsx.vmware.com/v1alpha1
50+
kind: SecurityPolicy
51+
metadata:
52+
name: from-to-policy-1
53+
namespace: ns-1
54+
spec:
55+
appliedTo:
56+
- podSelector:
57+
matchLabels:
58+
role: db
59+
rules:
60+
- direction: Ingress
61+
action: Allow
62+
from:
63+
- podSelector:
64+
matchLabels:
65+
role: frontend
66+
ports:
67+
- protocol: TCP
68+
port: 3306
69+
- direction: Out
70+
action: Allow
71+
to:
72+
- podSelector:
73+
matchLabels:
74+
role: dns
75+
ports:
76+
- protocol: UDP
77+
port: 53
78+
- direction: Ingress
79+
action: Drop
80+
- direction: Out
81+
action: Drop

docs/ref/apis/legacy.md

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -357,8 +357,10 @@ _Appears in:_
357357
| `action` _[RuleAction](#ruleaction)_ | Action specifies the action to be applied on the rule. | | |
358358
| `appliedTo` _[SecurityPolicyTarget](#securitypolicytarget) array_ | AppliedTo is a list of rule targets.<br />Policy level 'Applied To' will take precedence over rule level. | | |
359359
| `direction` _[RuleDirection](#ruledirection)_ | Direction is the direction of the rule, including 'In' or 'Ingress', 'Out' or 'Egress'. | | |
360-
| `sources` _[SecurityPolicyPeer](#securitypolicypeer) array_ | Sources defines the endpoints where the traffic is from. For ingress rule only. | | |
361-
| `destinations` _[SecurityPolicyPeer](#securitypolicypeer) array_ | Destinations defines the endpoints where the traffic is to. For egress rule only. | | |
360+
| `sources` _[SecurityPolicyPeer](#securitypolicypeer) array_ | **Deprecated: use `from` instead.** Sources defines the endpoints where the traffic is from. For ingress rule only. | | |
361+
| `destinations` _[SecurityPolicyPeer](#securitypolicypeer) array_ | **Deprecated: use `to` instead.** Destinations defines the endpoints where the traffic is to. For egress rule only. | | |
362+
| `from` _[SecurityPolicyPeer](#securitypolicypeer) array_ | From defines the endpoints where the traffic is from. For ingress rule only. This is the preferred field over the deprecated Sources. | | |
363+
| `to` _[SecurityPolicyPeer](#securitypolicypeer) array_ | To defines the endpoints where the traffic is to. For egress rule only. This is the preferred field over the deprecated Destinations. | | |
362364
| `ports` _[SecurityPolicyPort](#securitypolicyport) array_ | Ports is a list of ports to be matched. | | |
363365
| `name` _string_ | Name is the display name of this rule. | | |
364366

docs/ref/apis/vpc.md

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -540,8 +540,10 @@ _Appears in:_
540540
| `action` _[RuleAction](#ruleaction)_ | Action specifies the action to be applied on the rule. | | |
541541
| `appliedTo` _[SecurityPolicyTarget](#securitypolicytarget) array_ | AppliedTo is a list of rule targets.<br />Policy level 'Applied To' will take precedence over rule level. | | |
542542
| `direction` _[RuleDirection](#ruledirection)_ | Direction is the direction of the rule, including 'In' or 'Ingress', 'Out' or 'Egress'. | | |
543-
| `sources` _[SecurityPolicyPeer](#securitypolicypeer) array_ | Sources defines the endpoints where the traffic is from. For ingress rule only. | | |
544-
| `destinations` _[SecurityPolicyPeer](#securitypolicypeer) array_ | Destinations defines the endpoints where the traffic is to. For egress rule only. | | |
543+
| `sources` _[SecurityPolicyPeer](#securitypolicypeer) array_ | **Deprecated: use `from` instead.** Sources defines the endpoints where the traffic is from. For ingress rule only. | | |
544+
| `destinations` _[SecurityPolicyPeer](#securitypolicypeer) array_ | **Deprecated: use `to` instead.** Destinations defines the endpoints where the traffic is to. For egress rule only. | | |
545+
| `from` _[SecurityPolicyPeer](#securitypolicypeer) array_ | From defines the endpoints where the traffic is from. For ingress rule only. This is the preferred field over the deprecated Sources. | | |
546+
| `to` _[SecurityPolicyPeer](#securitypolicypeer) array_ | To defines the endpoints where the traffic is to. For egress rule only. This is the preferred field over the deprecated Destinations. | | |
545547
| `ports` _[SecurityPolicyPort](#securitypolicyport) array_ | Ports is a list of ports to be matched. | | |
546548
| `name` _string_ | Name is the display name of this rule. | | |
547549

docs/security-policy.md

Lines changed: 76 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,57 @@ to access through TCP with port 8000. The second rule allows the selected VMs to
8282
access Pods with label "role: dns" through UDP with port 53. The third and forth
8383
rules are to drop any other ingress and egress traffic to/from the selected VMs.
8484
85+
### Kubernetes NetworkPolicy style (from/to)
86+
87+
SecurityPolicy supports `from` and `to` fields as the preferred way to specify
88+
traffic peers, aligning with the standard Kubernetes NetworkPolicy syntax. The
89+
legacy `sources` and `destinations` fields are deprecated; use `from` and `to`
90+
instead. The following example is equivalent to the one above:
91+
92+
```yaml
93+
apiVersion: nsx.vmware.com/v1alpha1
94+
kind: SecurityPolicy
95+
metadata:
96+
name: db-isolation
97+
namespace: prod-ns
98+
spec:
99+
priority: 1
100+
appliedTo:
101+
- vmSelector:
102+
matchLabels:
103+
role: db
104+
rules:
105+
- direction: in
106+
action: allow
107+
from:
108+
- namespaceSelector:
109+
matchLabels:
110+
role: control
111+
- podSelector:
112+
matchLabels:
113+
role: frontend
114+
ports:
115+
- protocol: TCP
116+
port: 8000
117+
- direction: out
118+
action: allow
119+
to:
120+
- podSelector:
121+
matchLabels:
122+
role: dns
123+
ports:
124+
- protocol: UDP
125+
port: 53
126+
appliedTo:
127+
- vmSelector:
128+
matchLabels:
129+
user: internal
130+
- direction: in
131+
action: drop
132+
- direction: out
133+
action: drop
134+
```
135+
85136
Below are explanations for the fields:
86137

87138
**spec**: defines all the configurations for a SecurityPolicy CR.
@@ -107,29 +158,34 @@ or 'Egress'.
107158
**ports**: define protocol, specific port or port range. `ports.port` will be treated
108159
as destination port. More details refer to section `Targeting a range of Ports`
109160

110-
**sources** and **destinations**: defines a list of peers where the traffic is from/to.
161+
**from** and **to**: defines a list of peers where the traffic is from/to.
111162
It could be `podSelector`, `vmSelector`, `namespaceSelector` and `ipBlocks`.
112163
`podSelector` and `namespaceSelector` in the same entry select particular Pods within
113164
particular Namespaces.
114165
`vmSelector` and `namespaceSelector` in the same entry select particular VMs within
115166
particular Namespaces.
116-
More details refer to section `Behavior of sources and destinations selectors`
167+
More details refer to section `Behavior of from and to selectors`
168+
169+
**sources** and **destinations** *(deprecated)*: legacy names for `from` and `to`.
170+
These fields are still accepted for backward compatibility but users should migrate
171+
to `from`/`to`. If both `sources` and `from` (or `destinations` and `to`) are set
172+
in the same rule, `from`/`to` takes precedence.
117173

118174
**status**: shows CR realization state. If there is any error during realization,
119175
nsx-operator will also update status with error message.
120176

121-
## Behavior of sources and destinations selectors
177+
## Behavior of from and to selectors
122178

123-
There are 6 kinds of selectors that can be specified in an `ingress` `sources` section
124-
or `egress` `destinations` section:
179+
There are 6 kinds of selectors that can be specified in an `ingress` `from`
180+
(or legacy `sources`) section or `egress` `to` (or legacy `destinations`) section:
125181

126182
**podSelector**: This selects particular Pods in the same namespace as the SecurityPolicy
127183
as ingress sources or egress destinations.
128184

129185
**namespaceSelector**: This selects particular namespaces for which all Pods and
130186
VMs as ingress sources or egress destinations.
131187

132-
**namespaceSelector and podSelector**: A single `sources`/`destinations` entry that
188+
**namespaceSelector and podSelector**: A single `from`/`to` entry that
133189
specifies both `namespaceSelector` and `podSelector` selects particular Pods within
134190
particular namespaces. Be careful to use correct YAML syntax; this policy:
135191

@@ -138,7 +194,7 @@ particular namespaces. Be careful to use correct YAML syntax; this policy:
138194
rules:
139195
- direction: in
140196
action: allow
141-
sources:
197+
from:
142198
- namespaceSelector:
143199
matchLabels:
144200
user: alice
@@ -147,15 +203,15 @@ particular namespaces. Be careful to use correct YAML syntax; this policy:
147203
role: client
148204
...
149205
```
150-
contains a single `sources` element allowing connections from Pods with the label
206+
contains a single `from` element allowing connections from Pods with the label
151207
`role=client` in namespaces with the label `user=alice`. But this policy:
152208

153209
```
154210
...
155211
rules:
156212
- direction: in
157213
action: allow
158-
sources:
214+
from:
159215
- namespaceSelector:
160216
matchLabels:
161217
user: alice
@@ -164,7 +220,7 @@ contains a single `sources` element allowing connections from Pods with the labe
164220
role: client
165221
...
166222
```
167-
contains two elements in the sources array, and allows connections from Pods in
223+
contains two elements in the `from` array, and allows connections from Pods in
168224
the current Namespace with the label `role=client`, or from any Pod in the namespaces
169225
with the label `user=alice`.
170226

@@ -176,7 +232,7 @@ the SecurityPolicy as ingress sources or egress destinations. E.g.
176232
rules:
177233
- direction: in
178234
action: allow
179-
sources:
235+
from:
180236
- vmSelector:
181237
matchLabels:
182238
role: client
@@ -185,7 +241,7 @@ the SecurityPolicy as ingress sources or egress destinations. E.g.
185241
allows connections from VirtualMachines with the label `role=client` in the current
186242
namespace.
187243
188-
**namespaceSelector and vmSelector**: A single `sources`/`destinations` entry that
244+
**namespaceSelector and vmSelector**: A single `from`/`to` entry that
189245
specifies both `namespaceSelector` and `vmSelector` selects particular VirtualMachines
190246
within particular namespaces. E.g.
191247
@@ -194,7 +250,7 @@ within particular namespaces. E.g.
194250
rules:
195251
- direction: in
196252
action: allow
197-
sources:
253+
from:
198254
- namespaceSelector:
199255
matchLabels:
200256
user: alice
@@ -203,7 +259,7 @@ within particular namespaces. E.g.
203259
role: client
204260
...
205261
```
206-
contains a single `sources` element allowing connections from VirtualMachines with
262+
contains a single `from` element allowing connections from VirtualMachines with
207263
the label `role=client` in namespaces with the label `user=alice`.
208264
209265
**ipBlocks**: This selects particular IP CIDR ranges to allow as ingress sources
@@ -214,7 +270,7 @@ or egress destinations. E.g.
214270
rules:
215271
- direction: ingress
216272
action: allow
217-
sources:
273+
from:
218274
- ipBlocks:
219275
- cidr: 192.168.0.0/24
220276
...
@@ -227,7 +283,7 @@ Particularly, it can be used for single IP by suffix `/32`. E.g.
227283
rules:
228284
- direction: ingress
229285
action: allow
230-
sources:
286+
from:
231287
- ipBlocks:
232288
- cidr: 100.64.232.1/32
233289
...
@@ -243,7 +299,7 @@ port. E.g.
243299
rules:
244300
- direction: in
245301
action: allow
246-
sources:
302+
from:
247303
- podSelector:
248304
matchLabels:
249305
role: ui
@@ -270,17 +326,17 @@ In the same policy, the higher rule has the higher priority. E.g. in the policy:
270326
rules:
271327
- direction: in
272328
action: allow
273-
sources:
329+
from:
274330
- podSelector:
275331
matchLabels:
276332
role: client
277333
- direction: in
278334
action: drop
279-
sources:
335+
from:
280336
- podSelector: {}
281337
- direction: out
282338
action: drop
283-
destinations:
339+
to:
284340
- podSelector: {}
285341
...
286342
```

pkg/apis/legacy/v1alpha1/securitypolicy_types.go

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,10 +59,18 @@ type SecurityPolicyRule struct {
5959
AppliedTo []SecurityPolicyTarget `json:"appliedTo,omitempty"`
6060
// Direction is the direction of the rule, including 'In' or 'Ingress', 'Out' or 'Egress'.
6161
Direction *RuleDirection `json:"direction"`
62+
// Deprecated: use From instead.
6263
// Sources defines the endpoints where the traffic is from. For ingress rule only.
6364
Sources []SecurityPolicyPeer `json:"sources,omitempty"`
65+
// Deprecated: use To instead.
6466
// Destinations defines the endpoints where the traffic is to. For egress rule only.
6567
Destinations []SecurityPolicyPeer `json:"destinations,omitempty"`
68+
// From defines the endpoints where the traffic is from. For ingress rule only.
69+
// This is the preferred field over the deprecated Sources.
70+
From []SecurityPolicyPeer `json:"from,omitempty"`
71+
// To defines the endpoints where the traffic is to. For egress rule only.
72+
// This is the preferred field over the deprecated Destinations.
73+
To []SecurityPolicyPeer `json:"to,omitempty"`
6674
// Ports is a list of ports to be matched.
6775
Ports []SecurityPolicyPort `json:"ports,omitempty"`
6876
// Name is the display name of this rule.

pkg/apis/legacy/v1alpha1/zz_generated.deepcopy.go

Lines changed: 14 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

pkg/apis/vpc/v1alpha1/securitypolicy_types.go

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,10 +59,18 @@ type SecurityPolicyRule struct {
5959
AppliedTo []SecurityPolicyTarget `json:"appliedTo,omitempty"`
6060
// Direction is the direction of the rule, including 'In' or 'Ingress', 'Out' or 'Egress'.
6161
Direction *RuleDirection `json:"direction"`
62+
// Deprecated: use From instead.
6263
// Sources defines the endpoints where the traffic is from. For ingress rule only.
6364
Sources []SecurityPolicyPeer `json:"sources,omitempty"`
65+
// Deprecated: use To instead.
6466
// Destinations defines the endpoints where the traffic is to. For egress rule only.
6567
Destinations []SecurityPolicyPeer `json:"destinations,omitempty"`
68+
// From defines the endpoints where the traffic is from. For ingress rule only.
69+
// This is the preferred field over the deprecated Sources.
70+
From []SecurityPolicyPeer `json:"from,omitempty"`
71+
// To defines the endpoints where the traffic is to. For egress rule only.
72+
// This is the preferred field over the deprecated Destinations.
73+
To []SecurityPolicyPeer `json:"to,omitempty"`
6674
// Ports is a list of ports to be matched.
6775
Ports []SecurityPolicyPort `json:"ports,omitempty"`
6876
// Name is the display name of this rule.

pkg/apis/vpc/v1alpha1/zz_generated.deepcopy.go

Lines changed: 14 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)