Skip to content

Commit 25b0733

Browse files
committed
Update ports to protocols
Makes the `ports` clause a more generic `protocols` block to allow for future expansion. Example ```yaml apiVersion: policy.networking.k8s.io/v1alpha2 kind: ClusterNetworkPolicy metadata: name: pub-svc-delegate-example spec: tier: Admin priority: 20 subject: namespaces: {} egress: - action: Pass to: - pods: namespaceSelector: matchLabels: kubernetes.io/metadata.name: bar-ns-1 podSelector: matchLabels: app: svc-pub protocols: #< - protocol: TCP #< port: #< number: 8080 #< ``` Another example: ``` protocols: - protocol: TCP port: range: start: 1000 end: 2000 - protocol: UDP port: number: 53 ``` Ref: #187
1 parent dcc96c8 commit 25b0733

File tree

9 files changed

+289
-306
lines changed

9 files changed

+289
-306
lines changed

apis/v1alpha2/clusternetworkpolicy_types.go

Lines changed: 39 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -202,16 +202,15 @@ type ClusterNetworkPolicyIngressRule struct {
202202
// +kubebuilder:validation:MaxItems=25
203203
From []ClusterNetworkPolicyIngressPeer `json:"from"`
204204

205-
// Ports allows for matching traffic based on port and protocols.
206-
// This field is a list of ports which should be matched on
207-
// the pods selected for this policy i.e the subject of the policy.
208-
// So it matches on the destination port for the ingress traffic.
209-
// If Ports is not set then the rule does not filter traffic via port.
205+
// Protocols this rule matches. This rule matches if any of
206+
// the elements in the list match the incoming traffic.
207+
//
208+
// This field must contain at least one item.
210209
//
211210
// +optional
212211
// +kubebuilder:validation:MinItems=1
213-
// +kubebuilder:validation:MaxItems=25
214-
Ports *[]ClusterNetworkPolicyPort `json:"ports,omitempty"`
212+
// +kubebuilder:validation:MaxItems=100
213+
Protocols *[]ClusterNetworkPolicyProtocol `json:"protocols,omitempty"`
215214
}
216215

217216
// ClusterNetworkPolicyEgressRule describes an action to take on a particular
@@ -316,29 +315,49 @@ type ClusterNetworkPolicyIngressPeer struct {
316315
Pods *NamespacedPod `json:"pods,omitempty"`
317316
}
318317

319-
// ClusterNetworkPolicyPort describes how to select destination network ports.
320-
// Exactly one field must be set.
318+
// ClusterNetworkPolicyProtocol describes how to select traffic by
319+
// protocol-specific attributes.
320+
//
321+
// +kubebuilder:validation:XValidation:rule="!(self.protocol in ['TCP', 'UDP', 'SCTP']) || has(self.port)",message="port must be specified for protocols that support ports"
322+
type ClusterNetworkPolicyProtocol struct {
323+
// Protocol is the network protocol (TCP, UDP, or SCTP) which
324+
// traffic must match. If not specified, this field defaults
325+
// to TCP.
326+
//
327+
// +kubebuilder:default=TCP
328+
Protocol corev1.Protocol `json:"protocol,omitempty"`
329+
330+
// Specific port to match against.
331+
//
332+
// +optional
333+
Port *ClusterNetworkPolicyPort `json:"port,omitempty"`
334+
}
335+
336+
// ClusterNetworkPolicyPort describes how to match by port. This can
337+
// only be used with protocols that use port numbers (e.g. TCP, UDP).
338+
//
339+
// Exactly one of the fields in this struct must be set.
340+
//
321341
// +kubebuilder:validation:MaxProperties=1
322342
// +kubebuilder:validation:MinProperties=1
323343
type ClusterNetworkPolicyPort struct {
324-
// Port selects a destination port based on protocol and port number.
344+
// Port selects the port by number.
325345
//
326346
// +optional
327-
PortNumber *Port `json:"portNumber,omitempty"`
347+
Number *int32 `json:"number,omitempty"`
328348

329-
// PortRange selects a destination port range based on protocol and
330-
// start and end port numbers.
349+
// PortRange selects the port by range.
331350
//
332351
// +optional
333-
PortRange *PortRange `json:"portRange,omitempty"`
352+
Range *PortRange `json:"range,omitempty"`
334353

335-
// NamedPort selects a destination port on a pod based on the ContainerPort
336-
// name. You can't use this in a rule with Nodes or Networks peers,
337-
// because they do not have named ports.
354+
// NamedPort selects a destination port on a pod based on the
355+
// ContainerPort name. You can't use this in a rule with Nodes
356+
// or Networks peers, because they do not have named ports.
338357
//
339358
// <network-policy-api:experimental>
340359
// +optional
341-
NamedPort *string `json:"namedPort,omitempty"`
360+
Name *string `json:"name,omitempty"`
342361
}
343362

344363
// ClusterNetworkPolicyEgressPeer defines a peer to allow traffic to.
@@ -424,39 +443,21 @@ type NamespacedPod struct {
424443
PodSelector metav1.LabelSelector `json:"podSelector"`
425444
}
426445

427-
type Port struct {
428-
// Protocol is the network protocol (TCP, UDP, or SCTP) which traffic must
429-
// match. If not specified, this field defaults to TCP.
430-
// +kubebuilder:default=TCP
431-
//
432-
Protocol corev1.Protocol `json:"protocol"`
433-
434-
// Number defines a network port value.
435-
// +kubebuilder:validation:Minimum=1
436-
// +kubebuilder:validation:Maximum=65535
437-
//
438-
Port int32 `json:"port"`
439-
}
440-
441446
// PortRange defines an inclusive range of ports from the assigned
442447
// Start value to End value.
443448
// +kubebuilder:validation:XValidation:rule="self.start < self.end", message="Start port must be less than End port"
444449
type PortRange struct {
445-
// Protocol is the network protocol (TCP, UDP, or SCTP) which traffic must
446-
// match. If not specified, this field defaults to TCP.
447-
// +kubebuilder:default=TCP
448-
//
449-
Protocol corev1.Protocol `json:"protocol,omitempty"`
450-
451450
// Start defines a network port that is the start of a port range, the Start
452451
// value must be less than End.
452+
//
453453
// +kubebuilder:validation:Minimum=1
454454
// +kubebuilder:validation:Maximum=65535
455455
//
456456
Start int32 `json:"start"`
457457

458458
// End defines a network port that is the end of a port range, the End value
459459
// must be greater than Start.
460+
//
460461
// +kubebuilder:validation:Minimum=1
461462
// +kubebuilder:validation:Maximum=65535
462463
//

apis/v1alpha2/zz_generated.deepcopy.go

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

0 commit comments

Comments
 (0)