@@ -175,6 +175,26 @@ type AdminNetworkPolicyEgressPeer struct {
175175 // <network-policy-api:experimental>
176176 // +optional
177177 Nodes * metav1.LabelSelector `json:"nodes,omitempty"`
178+ // Networks defines a way to select peers via CIDR blocks.
179+ // This is intended for representing entities that live outside the cluster,
180+ // which can't be selected by pods, namespaces and nodes peers, but note
181+ // that cluster-internal traffic will be checked against the rule as
182+ // well. So if you Allow or Deny traffic to `"0.0.0.0/0"`, that will allow
183+ // or deny all IPv4 pod-to-pod traffic as well. If you don't want that,
184+ // add a rule that Passes all pod traffic before the Networks rule.
185+ //
186+ // Each item in Networks should be provided in the CIDR format and should be
187+ // IPv4 or IPv6, for example "10.0.0.0/8" or "fd00::/8".
188+ //
189+ // Networks can have upto 25 CIDRs specified.
190+ //
191+ // Support: Extended
192+ //
193+ // <network-policy-api:experimental>
194+ // +optional
195+ // +kubebuilder:validation:MinItems=1
196+ // +kubebuilder:validation:MaxItems=25
197+ Networks []CIDR `json:"networks,omitempty"`
178198}
179199
180200// NamespacedPeer defines a flexible way to select Namespaces in a cluster.
@@ -237,3 +257,13 @@ type NamespacedPodPeer struct {
237257 //
238258 PodSelector metav1.LabelSelector `json:"podSelector"`
239259}
260+
261+ // CIDR is an IP address range in CIDR notation (for example, "10.0.0.0/8" or "fd00::/8").
262+ // The regex for the IPv4 and IPv6 CIDR range was taken from
263+ // https://blog.markhatton.co.uk/2011/03/15/regular-expressions-for-ip-addresses-cidr-ranges-and-hostnames/
264+ // The resulting regex is an OR of both regexes. IPv4 address embedded in IPv6 addresses are not supported.
265+ // TODO: Change the CIDR's validation regex to use CEL isCIDR() in Kube 1.31 when it is available.
266+ // +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]))$)`
267+ // +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"
268+ // +kubebuilder:validation:MaxLength=43
269+ type CIDR string
0 commit comments