Skip to content

Commit 9c20cf0

Browse files
committed
Changes to implement option #2 for the port matching
Example: apiVersion: policy.networking.k8s.io/v1alpha2 kind: ClusterNetworkPolicy metadata: name: cluster-wide-deny spec: tier: Admin priority: 0 subject: namespaces: matchLabels: kubernetes.io/metadata.name: sensitive-ns ingress: - action: Deny name: select-all-deny-all from: - pods: namespaceSelector: matchLabels: {} podSelector: matchLabels: {} match: - tcp: - port: number: 80 - udp: - port: number: 50 - tcp: - port: range: start: 1000 end: 1050 - namedPort: "www"
1 parent deabb1d commit 9c20cf0

19 files changed

+755
-505
lines changed

apis/v1alpha2/clusternetworkpolicy_types.go

Lines changed: 55 additions & 70 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,6 @@ limitations under the License.
2020
package v1alpha2
2121

2222
import (
23-
corev1 "k8s.io/api/core/v1"
2423
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
2524
)
2625

@@ -206,16 +205,14 @@ type ClusterNetworkPolicyIngressRule struct {
206205
// +kubebuilder:validation:MaxItems=25
207206
From []ClusterNetworkPolicyIngressPeer `json:"from"`
208207

209-
// Ports allows for matching traffic based on port and protocols.
210-
// This field is a list of ports which should be matched on
211-
// the pods selected for this policy i.e the subject of the policy.
212-
// So it matches on the destination port for the ingress traffic.
213-
// If Ports is not set then the rule does not filter traffic via port.
208+
// Match allows for more fine-grain matching of traffic on protocol-specific
209+
// attributes such as the port. If match is empty, then the rule will match
210+
// all traffic.
214211
//
215212
// +optional
216-
// +kubebuilder:validation:MinItems=1
213+
// +kubebuilder:validation:MinItems=0
217214
// +kubebuilder:validation:MaxItems=25
218-
Ports *[]ClusterNetworkPolicyPort `json:"ports,omitempty"`
215+
Match []ClusterNetworkPolicyMatch `json:"match,omitempty"`
219216
}
220217

221218
// ClusterNetworkPolicyEgressRule describes an action to take on a particular
@@ -258,14 +255,14 @@ type ClusterNetworkPolicyEgressRule struct {
258255
// +kubebuilder:validation:MaxItems=25
259256
To []ClusterNetworkPolicyEgressPeer `json:"to"`
260257

261-
// Ports allows for matching traffic based on port and protocols.
262-
// This field is a list of destination ports for the outgoing egress traffic.
263-
// If Ports is not set then the rule does not filter traffic via port.
258+
// Match allows for more fine-grain matching of traffic on protocol-specific
259+
// attributes such as the port. If match is empty, then this rule will match
260+
// all traffic.
264261
//
265262
// +optional
266-
// +kubebuilder:validation:MinItems=1
263+
// +kubebuilder:validation:MinItems=0
267264
// +kubebuilder:validation:MaxItems=25
268-
Ports *[]ClusterNetworkPolicyPort `json:"ports,omitempty"`
265+
Match []ClusterNetworkPolicyMatch `json:"match,omitempty"`
269266
}
270267

271268
// ClusterNetworkPolicyRuleAction string describes the ClusterNetworkPolicy
@@ -320,31 +317,58 @@ type ClusterNetworkPolicyIngressPeer struct {
320317
Pods *NamespacedPod `json:"pods,omitempty"`
321318
}
322319

323-
// ClusterNetworkPolicyPort describes how to select destination network ports.
320+
// ClusterNetworkPolicyMatch describes additional protocol-specific match rules.
324321
// Exactly one field must be set.
322+
//
325323
// +kubebuilder:validation:MaxProperties=1
326324
// +kubebuilder:validation:MinProperties=1
327-
type ClusterNetworkPolicyPort struct {
328-
// Port selects a destination port based on protocol and port number.
329-
//
330-
// +optional
331-
PortNumber *Port `json:"portNumber,omitempty"`
332-
333-
// PortRange selects a destination port range based on protocol and
334-
// start and end port numbers.
335-
//
336-
// +optional
337-
PortRange *PortRange `json:"portRange,omitempty"`
338-
325+
type ClusterNetworkPolicyMatch struct {
326+
// TCP specific protocol matches.
327+
TCP []ClusterNetworkPolicyMatchTCP `json:"tcp,omitempty"`
328+
// UDP specific protocol matches.
329+
UDP []ClusterNetworkPolicyMatchUDP `json:"udp,omitempty"`
330+
// ICMP specific protocol matches.
331+
ICMP []ClusterNetworkPolicyMatchICMP `json:"icmp,omitempty"`
339332
// NamedPort selects a destination port on a pod based on the ContainerPort
340-
// name. You can't use this in a rule with Nodes or Networks peers,
341-
// because they do not have named ports.
342-
//
343-
// <network-policy-api:experimental>
344-
// +optional
333+
// name. You can't use this in a rule with Nodes or Networks peers, because
334+
// they do not have named ports.
345335
NamedPort *string `json:"namedPort,omitempty"`
346336
}
347337

338+
// ClusterNetworkPolicyMatchTCP are TCP attributes to be matched.
339+
type ClusterNetworkPolicyMatchTCP struct {
340+
Port *ClusterNetworkPolicyMatchPort
341+
}
342+
343+
// ClusterNetworkPolicyMatchUDP are UDP attributes to be matched.
344+
type ClusterNetworkPolicyMatchUDP struct {
345+
Port *ClusterNetworkPolicyMatchPort
346+
}
347+
348+
// ClusterNetworkPolicyMatchICMP is a placeholder to illustrate what it looks
349+
// like to another protocol
350+
//
351+
// TODO: This is just an example.
352+
type ClusterNetworkPolicyMatchICMP struct {
353+
Type *int32 `json:"type,omitempty"`
354+
Code *int32 `json:"code,omitempty"`
355+
}
356+
357+
// ClusterNetworkPolicyMatchPort matches on port number.
358+
//
359+
// +kubebuilder:validation:MaxProperties=1
360+
// +kubebuilder:validation:MinProperties=1
361+
type ClusterNetworkPolicyMatchPort struct {
362+
Number *int32 `json:"number,omitempty"`
363+
Range *ClusterNetworkPolicyMatchPortRange `json:"portRange,omitempty"`
364+
}
365+
366+
// ClusterNetworkPolicyMatchTCP are TCP attributes to be matched.
367+
type ClusterNetworkPolicyMatchPortRange struct {
368+
Start int32 `json:"start,omitempty"`
369+
End int32 `json:"end,omitempty"`
370+
}
371+
348372
// ClusterNetworkPolicyEgressPeer defines a peer to allow traffic to.
349373
//
350374
// Exactly one of the fields must be set for a given peer and this is enforced
@@ -428,45 +452,6 @@ type NamespacedPod struct {
428452
PodSelector metav1.LabelSelector `json:"podSelector"`
429453
}
430454

431-
type Port struct {
432-
// Protocol is the network protocol (TCP, UDP, or SCTP) which traffic must
433-
// match. If not specified, this field defaults to TCP.
434-
// +kubebuilder:default=TCP
435-
//
436-
Protocol corev1.Protocol `json:"protocol"`
437-
438-
// Number defines a network port value.
439-
// +kubebuilder:validation:Minimum=1
440-
// +kubebuilder:validation:Maximum=65535
441-
//
442-
Port int32 `json:"port"`
443-
}
444-
445-
// PortRange defines an inclusive range of ports from the assigned
446-
// Start value to End value.
447-
// +kubebuilder:validation:XValidation:rule="self.start < self.end", message="Start port must be less than End port"
448-
type PortRange struct {
449-
// Protocol is the network protocol (TCP, UDP, or SCTP) which traffic must
450-
// match. If not specified, this field defaults to TCP.
451-
// +kubebuilder:default=TCP
452-
//
453-
Protocol corev1.Protocol `json:"protocol,omitempty"`
454-
455-
// Start defines a network port that is the start of a port range, the Start
456-
// value must be less than End.
457-
// +kubebuilder:validation:Minimum=1
458-
// +kubebuilder:validation:Maximum=65535
459-
//
460-
Start int32 `json:"start"`
461-
462-
// End defines a network port that is the end of a port range, the End value
463-
// must be greater than Start.
464-
// +kubebuilder:validation:Minimum=1
465-
// +kubebuilder:validation:Maximum=65535
466-
//
467-
End int32 `json:"end"`
468-
}
469-
470455
// CIDR is an IP address range in CIDR notation
471456
// (for example, "10.0.0.0/8" or "fd00::/8").
472457
// +kubebuilder:validation:XValidation:rule="isCIDR(self)",message="Invalid CIDR format provided"

0 commit comments

Comments
 (0)