Skip to content

Commit 5658b95

Browse files
committed
Implement inline CIDR egress peer
This PR adds support for implementing inline CIDR peer blocks. Signed-off-by: Surya Seetharaman <[email protected]>
1 parent dc00b36 commit 5658b95

7 files changed

+234
-0
lines changed

apis/v1alpha1/shared_types.go

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -121,6 +121,13 @@ type PortRange struct {
121121
}
122122

123123
// AdminNetworkPolicyIngressPeer defines an in-cluster peer to allow traffic from.
124+
//
125+
// Note that presence of a Service object with this policy subject as its backend
126+
// has no impact on the behavior of the policy applied to the peer
127+
// trying to talk to the Service. It will work in the same way as if the
128+
// Service didn't exist since policy is applied after ServiceVIP (clusterIP,
129+
// externalIP, loadBalancerIngressVIP) is rewritten to the backendIPs.
130+
//
124131
// Exactly one of the selector pointers must be set for a given peer. If a
125132
// consumer observes none of its fields are set, they must assume an unknown
126133
// option has been specified and fail closed.
@@ -145,6 +152,13 @@ type AdminNetworkPolicyIngressPeer struct {
145152
}
146153

147154
// AdminNetworkPolicyEgressPeer defines a peer to allow traffic to.
155+
//
156+
// Note that presence of a Service object with this peer as its backend
157+
// has no impact on the behavior of the policy applied to the subject
158+
// trying to talk to the Service. It will work in the same way as if the
159+
// Service didn't exist since policy is applied after ServiceVIP (clusterIP,
160+
// externalIP, loadBalancerIngressVIP) is rewritten to the backendIPs.
161+
//
148162
// Exactly one of the selector pointers must be set for a given peer. If a
149163
// consumer observes none of its fields are set, they must assume an unknown
150164
// option has been specified and fail closed.
@@ -175,6 +189,33 @@ type AdminNetworkPolicyEgressPeer struct {
175189
// <network-policy-api:experimental>
176190
// +optional
177191
Nodes *metav1.LabelSelector `json:"nodes,omitempty"`
192+
// Networks defines a way to select peers via CIDR blocks (both v4 & v6).
193+
// This is intended for representing entities that live outside the cluster,
194+
// which can't be selected by pods, namespaces and nodes peers, but note
195+
// that cluster-internal traffic will be checked against the rule as
196+
// well. So if you Allow or Deny traffic to `"0.0.0.0/0"`, that will allow
197+
// or deny all IPv4 pod-to-pod traffic as well. If you don't want that,
198+
// add a rule that Passes all pod traffic before the Networks rule.
199+
//
200+
// Do not provide serviceCIDR range in `networks` peer. This is NOT supported.
201+
// While 0.0.0.0/0 will match on the serviceVIP as well, it does not make much sense
202+
// anyways in that case because policies are expected to be applied after the serviceVIP
203+
// (clusterIP, externalIP, loadbalancer.Ingress.VIP) is rewritten to the actual backend IPs.
204+
// So policy match finally gets applied on the endpointIPs not serviceVIPs.
205+
//
206+
// Each item in Networks should be provided in the CIDR format and should be
207+
// IPv4 or IPv6, for example "10.0.0.0/8" or "fd00::/8". IPv4 address embedded
208+
// in IPv6 addresses are not supported.
209+
//
210+
// Networks can have upto 25 CIDRs specified.
211+
//
212+
// Support: Extended
213+
//
214+
// <network-policy-api:experimental>
215+
// +optional
216+
// +kubebuilder:validation:MinItems=1
217+
// +kubebuilder:validation:MaxItems=25
218+
Networks []CIDR `json:"networks,omitempty"`
178219
}
179220

180221
// NamespacedPeer defines a flexible way to select Namespaces in a cluster.
@@ -237,3 +278,13 @@ type NamespacedPodPeer struct {
237278
//
238279
PodSelector metav1.LabelSelector `json:"podSelector"`
239280
}
281+
282+
// CIDR is an IP address range in CIDR notation (for example, "10.0.0.0/8" or "fd00::/8").
283+
// The regex for the IPv4 and IPv6 CIDR range was taken from
284+
// https://blog.markhatton.co.uk/2011/03/15/regular-expressions-for-ip-addresses-cidr-ranges-and-hostnames/
285+
// The resulting regex is an OR of both regexes. IPv4 address embedded in IPv6 addresses are not supported.
286+
// TODO: Change the CIDR's validation regex to use CEL isCIDR() in Kube 1.31 when it is available.
287+
// +kubebuilder:validation:Pattern=`(^(([0-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5])\.){3}([0-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5])(\/(3[0-2]|[1-2][0-9]|[0-9]))$)|(^s*((([0-9A-Fa-f]{1,4}:){7}([0-9A-Fa-f]{1,4}|:))|(([0-9A-Fa-f]{1,4}:){6}(:[0-9A-Fa-f]{1,4}|((25[0-5]|2[0-4]d|1dd|[1-9]?d)(.(25[0-5]|2[0-4]d|1dd|[1-9]?d)){3})|:))|(([0-9A-Fa-f]{1,4}:){5}(((:[0-9A-Fa-f]{1,4}){1,2})|:((25[0-5]|2[0-4]d|1dd|[1-9]?d)(.(25[0-5]|2[0-4]d|1dd|[1-9]?d)){3})|:))|(([0-9A-Fa-f]{1,4}:){4}(((:[0-9A-Fa-f]{1,4}){1,3})|((:[0-9A-Fa-f]{1,4})?:((25[0-5]|2[0-4]d|1dd|[1-9]?d)(.(25[0-5]|2[0-4]d|1dd|[1-9]?d)){3}))|:))|(([0-9A-Fa-f]{1,4}:){3}(((:[0-9A-Fa-f]{1,4}){1,4})|((:[0-9A-Fa-f]{1,4}){0,2}:((25[0-5]|2[0-4]d|1dd|[1-9]?d)(.(25[0-5]|2[0-4]d|1dd|[1-9]?d)){3}))|:))|(([0-9A-Fa-f]{1,4}:){2}(((:[0-9A-Fa-f]{1,4}){1,5})|((:[0-9A-Fa-f]{1,4}){0,3}:((25[0-5]|2[0-4]d|1dd|[1-9]?d)(.(25[0-5]|2[0-4]d|1dd|[1-9]?d)){3}))|:))|(([0-9A-Fa-f]{1,4}:){1}(((:[0-9A-Fa-f]{1,4}){1,6})|((:[0-9A-Fa-f]{1,4}){0,4}:((25[0-5]|2[0-4]d|1dd|[1-9]?d)(.(25[0-5]|2[0-4]d|1dd|[1-9]?d)){3}))|:))|(:(((:[0-9A-Fa-f]{1,4}){1,7})|((:[0-9A-Fa-f]{1,4}){0,5}:((25[0-5]|2[0-4]d|1dd|[1-9]?d)(.(25[0-5]|2[0-4]d|1dd|[1-9]?d)){3}))|:)))(%.+)?s*(\/(12[0-8]|1[0-1][0-9]|[1-9][0-9]|[0-9]))$)`
288+
// +kubebuilder:validation:XValidation:rule="self.contains(':') != self.contains('.')",message="CIDR must be either an IPv4 or IPv6 address. IPv4 address embedded in IPv6 addresses are not supported"
289+
// +kubebuilder:validation:MaxLength=43
290+
type CIDR string

apis/v1alpha1/zz_generated.deepcopy.go

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

config/crd/experimental/policy.networking.k8s.io_adminnetworkpolicies.yaml

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -208,6 +208,15 @@ spec:
208208
items:
209209
description: |-
210210
AdminNetworkPolicyEgressPeer defines a peer to allow traffic to.
211+
212+
213+
Note that presence of a Service object with this peer as its backend
214+
has no impact on the behavior of the policy applied to the subject
215+
trying to talk to the Service. It will work in the same way as if the
216+
Service didn't exist since policy is applied after ServiceVIP (clusterIP,
217+
externalIP, loadBalancerIngressVIP) is rewritten to the backendIPs.
218+
219+
211220
Exactly one of the selector pointers must be set for a given peer. If a
212221
consumer observes none of its fields are set, they must assume an unknown
213222
option has been specified and fail closed.
@@ -310,6 +319,54 @@ spec:
310319
maxItems: 100
311320
type: array
312321
type: object
322+
networks:
323+
description: |-
324+
Networks defines a way to select peers via CIDR blocks (both v4 & v6).
325+
This is intended for representing entities that live outside the cluster,
326+
which can't be selected by pods, namespaces and nodes peers, but note
327+
that cluster-internal traffic will be checked against the rule as
328+
well. So if you Allow or Deny traffic to `"0.0.0.0/0"`, that will allow
329+
or deny all IPv4 pod-to-pod traffic as well. If you don't want that,
330+
add a rule that Passes all pod traffic before the Networks rule.
331+
332+
333+
Do not provide serviceCIDR range in `networks` peer. This is NOT supported.
334+
While 0.0.0.0/0 will match on the serviceVIP as well, it does not make much sense
335+
anyways in that case because policies are expected to be applied after the serviceVIP
336+
(clusterIP, externalIP, loadbalancer.Ingress.VIP) is rewritten to the actual backend IPs.
337+
So policy match finally gets applied on the endpointIPs not serviceVIPs.
338+
339+
340+
Each item in Networks should be provided in the CIDR format and should be
341+
IPv4 or IPv6, for example "10.0.0.0/8" or "fd00::/8". IPv4 address embedded
342+
in IPv6 addresses are not supported.
343+
344+
345+
Networks can have upto 25 CIDRs specified.
346+
347+
348+
Support: Extended
349+
350+
351+
<network-policy-api:experimental>
352+
items:
353+
description: |-
354+
CIDR is an IP address range in CIDR notation (for example, "10.0.0.0/8" or "fd00::/8").
355+
The regex for the IPv4 and IPv6 CIDR range was taken from
356+
https://blog.markhatton.co.uk/2011/03/15/regular-expressions-for-ip-addresses-cidr-ranges-and-hostnames/
357+
The resulting regex is an OR of both regexes. IPv4 address embedded in IPv6 addresses are not supported.
358+
TODO: Change the CIDR's validation regex to use CEL isCIDR() in Kube 1.31 when it is available.
359+
maxLength: 43
360+
pattern: (^(([0-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5])\.){3}([0-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5])(\/(3[0-2]|[1-2][0-9]|[0-9]))$)|(^s*((([0-9A-Fa-f]{1,4}:){7}([0-9A-Fa-f]{1,4}|:))|(([0-9A-Fa-f]{1,4}:){6}(:[0-9A-Fa-f]{1,4}|((25[0-5]|2[0-4]d|1dd|[1-9]?d)(.(25[0-5]|2[0-4]d|1dd|[1-9]?d)){3})|:))|(([0-9A-Fa-f]{1,4}:){5}(((:[0-9A-Fa-f]{1,4}){1,2})|:((25[0-5]|2[0-4]d|1dd|[1-9]?d)(.(25[0-5]|2[0-4]d|1dd|[1-9]?d)){3})|:))|(([0-9A-Fa-f]{1,4}:){4}(((:[0-9A-Fa-f]{1,4}){1,3})|((:[0-9A-Fa-f]{1,4})?:((25[0-5]|2[0-4]d|1dd|[1-9]?d)(.(25[0-5]|2[0-4]d|1dd|[1-9]?d)){3}))|:))|(([0-9A-Fa-f]{1,4}:){3}(((:[0-9A-Fa-f]{1,4}){1,4})|((:[0-9A-Fa-f]{1,4}){0,2}:((25[0-5]|2[0-4]d|1dd|[1-9]?d)(.(25[0-5]|2[0-4]d|1dd|[1-9]?d)){3}))|:))|(([0-9A-Fa-f]{1,4}:){2}(((:[0-9A-Fa-f]{1,4}){1,5})|((:[0-9A-Fa-f]{1,4}){0,3}:((25[0-5]|2[0-4]d|1dd|[1-9]?d)(.(25[0-5]|2[0-4]d|1dd|[1-9]?d)){3}))|:))|(([0-9A-Fa-f]{1,4}:){1}(((:[0-9A-Fa-f]{1,4}){1,6})|((:[0-9A-Fa-f]{1,4}){0,4}:((25[0-5]|2[0-4]d|1dd|[1-9]?d)(.(25[0-5]|2[0-4]d|1dd|[1-9]?d)){3}))|:))|(:(((:[0-9A-Fa-f]{1,4}){1,7})|((:[0-9A-Fa-f]{1,4}){0,5}:((25[0-5]|2[0-4]d|1dd|[1-9]?d)(.(25[0-5]|2[0-4]d|1dd|[1-9]?d)){3}))|:)))(%.+)?s*(\/(12[0-8]|1[0-1][0-9]|[1-9][0-9]|[0-9]))$)
361+
type: string
362+
x-kubernetes-validations:
363+
- message: CIDR must be either an IPv4 or IPv6 address.
364+
IPv4 address embedded in IPv6 addresses are not
365+
supported
366+
rule: self.contains(':') != self.contains('.')
367+
maxItems: 25
368+
minItems: 1
369+
type: array
313370
nodes:
314371
description: |-
315372
Nodes defines a way to select a set of nodes in
@@ -576,6 +633,15 @@ spec:
576633
items:
577634
description: |-
578635
AdminNetworkPolicyIngressPeer defines an in-cluster peer to allow traffic from.
636+
637+
638+
Note that presence of a Service object with this policy subject as its backend
639+
has no impact on the behavior of the policy applied to the peer
640+
trying to talk to the Service. It will work in the same way as if the
641+
Service didn't exist since policy is applied after ServiceVIP (clusterIP,
642+
externalIP, loadBalancerIngressVIP) is rewritten to the backendIPs.
643+
644+
579645
Exactly one of the selector pointers must be set for a given peer. If a
580646
consumer observes none of its fields are set, they must assume an unknown
581647
option has been specified and fail closed.

config/crd/experimental/policy.networking.k8s.io_baselineadminnetworkpolicies.yaml

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -199,6 +199,15 @@ spec:
199199
items:
200200
description: |-
201201
AdminNetworkPolicyEgressPeer defines a peer to allow traffic to.
202+
203+
204+
Note that presence of a Service object with this peer as its backend
205+
has no impact on the behavior of the policy applied to the subject
206+
trying to talk to the Service. It will work in the same way as if the
207+
Service didn't exist since policy is applied after ServiceVIP (clusterIP,
208+
externalIP, loadBalancerIngressVIP) is rewritten to the backendIPs.
209+
210+
202211
Exactly one of the selector pointers must be set for a given peer. If a
203212
consumer observes none of its fields are set, they must assume an unknown
204213
option has been specified and fail closed.
@@ -301,6 +310,54 @@ spec:
301310
maxItems: 100
302311
type: array
303312
type: object
313+
networks:
314+
description: |-
315+
Networks defines a way to select peers via CIDR blocks (both v4 & v6).
316+
This is intended for representing entities that live outside the cluster,
317+
which can't be selected by pods, namespaces and nodes peers, but note
318+
that cluster-internal traffic will be checked against the rule as
319+
well. So if you Allow or Deny traffic to `"0.0.0.0/0"`, that will allow
320+
or deny all IPv4 pod-to-pod traffic as well. If you don't want that,
321+
add a rule that Passes all pod traffic before the Networks rule.
322+
323+
324+
Do not provide serviceCIDR range in `networks` peer. This is NOT supported.
325+
While 0.0.0.0/0 will match on the serviceVIP as well, it does not make much sense
326+
anyways in that case because policies are expected to be applied after the serviceVIP
327+
(clusterIP, externalIP, loadbalancer.Ingress.VIP) is rewritten to the actual backend IPs.
328+
So policy match finally gets applied on the endpointIPs not serviceVIPs.
329+
330+
331+
Each item in Networks should be provided in the CIDR format and should be
332+
IPv4 or IPv6, for example "10.0.0.0/8" or "fd00::/8". IPv4 address embedded
333+
in IPv6 addresses are not supported.
334+
335+
336+
Networks can have upto 25 CIDRs specified.
337+
338+
339+
Support: Extended
340+
341+
342+
<network-policy-api:experimental>
343+
items:
344+
description: |-
345+
CIDR is an IP address range in CIDR notation (for example, "10.0.0.0/8" or "fd00::/8").
346+
The regex for the IPv4 and IPv6 CIDR range was taken from
347+
https://blog.markhatton.co.uk/2011/03/15/regular-expressions-for-ip-addresses-cidr-ranges-and-hostnames/
348+
The resulting regex is an OR of both regexes. IPv4 address embedded in IPv6 addresses are not supported.
349+
TODO: Change the CIDR's validation regex to use CEL isCIDR() in Kube 1.31 when it is available.
350+
maxLength: 43
351+
pattern: (^(([0-9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-5]|2[0-4]d|1dd|[1-9]?d)){3}))|:)))(%.+)?s*(\/(12[0-8]|1[0-1][0-9]|[1-9][0-9]|[0-9]))$)
352+
type: string
353+
x-kubernetes-validations:
354+
- message: CIDR must be either an IPv4 or IPv6 address.
355+
IPv4 address embedded in IPv6 addresses are not
356+
supported
357+
rule: self.contains(':') != self.contains('.')
358+
maxItems: 25
359+
minItems: 1
360+
type: array
304361
nodes:
305362
description: |-
306363
Nodes defines a way to select a set of nodes in
@@ -564,6 +621,15 @@ spec:
564621
items:
565622
description: |-
566623
AdminNetworkPolicyIngressPeer defines an in-cluster peer to allow traffic from.
624+
625+
626+
Note that presence of a Service object with this policy subject as its backend
627+
has no impact on the behavior of the policy applied to the peer
628+
trying to talk to the Service. It will work in the same way as if the
629+
Service didn't exist since policy is applied after ServiceVIP (clusterIP,
630+
externalIP, loadBalancerIngressVIP) is rewritten to the backendIPs.
631+
632+
567633
Exactly one of the selector pointers must be set for a given peer. If a
568634
consumer observes none of its fields are set, they must assume an unknown
569635
option has been specified and fail closed.

0 commit comments

Comments
 (0)