-
Notifications
You must be signed in to change notification settings - Fork 1.6k
ClusterNetworkPolicy: support generic protocols #11804
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 2 commits
9af1487
394ff5e
d642887
a5cb659
3d96bcd
44b4e84
8bca115
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,4 +1,4 @@ | ||
| // Copyright (c) 2025 Tigera, Inc. All rights reserved. | ||
| // Copyright (c) 2025-2026 Tigera, Inc. All rights reserved. | ||
|
|
||
| // Licensed under the Apache License, Version 2.0 (the "License"); | ||
| // you may not use this file except in compliance with the License. | ||
|
|
@@ -165,15 +165,15 @@ func k8sClusterNetPolIngressRuleToCalico(rule clusternetpol.ClusterNetworkPolicy | |
| if err != nil { | ||
| return nil, err | ||
| } | ||
| return combinePortsWithCNPIngressPeers(rule.Ports, rule.From, rule.Name, action) | ||
| return combinePortsWithCNPIngressPeers(rule.Protocols, rule.From, rule.Name, action) | ||
| } | ||
|
|
||
| func k8sClusterNetPolEgressRuleToCalico(rule clusternetpol.ClusterNetworkPolicyEgressRule) ([]apiv3.Rule, error) { | ||
| action, err := K8sClusterNetworkPolicyActionToCalico(rule.Action) | ||
| if err != nil { | ||
| return nil, err | ||
| } | ||
| return combinePortsWithCNPEgressPeers(rule.Ports, rule.To, rule.Name, action) | ||
| return combinePortsWithCNPEgressPeers(rule.Protocols, rule.To, rule.Name, action) | ||
| } | ||
|
|
||
| func K8sClusterNetworkPolicyActionToCalico(action clusternetpol.ClusterNetworkPolicyRuleAction) (apiv3.Action, error) { | ||
|
|
@@ -189,12 +189,12 @@ func K8sClusterNetworkPolicyActionToCalico(action clusternetpol.ClusterNetworkPo | |
| } | ||
|
|
||
| func combinePortsWithCNPIngressPeers( | ||
| cnpPorts *[]clusternetpol.ClusterNetworkPolicyPort, | ||
| cnpProtocols []clusternetpol.ClusterNetworkPolicyProtocol, | ||
| cnpPeers []clusternetpol.ClusterNetworkPolicyIngressPeer, | ||
| ruleName string, | ||
| action apiv3.Action, | ||
| ) (rules []apiv3.Rule, err error) { | ||
| protocolPorts, sortedProtocols, err := unpackCNPPorts(cnpPorts) | ||
| protocolPorts, sortedProtocols, err := unpackCNPProtocols(cnpProtocols) | ||
| if err != nil { | ||
| return nil, err | ||
| } | ||
|
|
@@ -248,12 +248,12 @@ func combinePortsWithCNPIngressPeers( | |
| } | ||
|
|
||
| func combinePortsWithCNPEgressPeers( | ||
| cnpPorts *[]clusternetpol.ClusterNetworkPolicyPort, | ||
| cnpProtocols []clusternetpol.ClusterNetworkPolicyProtocol, | ||
| cnpPeers []clusternetpol.ClusterNetworkPolicyEgressPeer, | ||
| ruleName string, | ||
| action apiv3.Action, | ||
| ) (rules []apiv3.Rule, err error) { | ||
| protocolPorts, sortedProtocols, err := unpackCNPPorts(cnpPorts) | ||
| protocolPorts, sortedProtocols, err := unpackCNPProtocols(cnpProtocols) | ||
| if err != nil { | ||
| return nil, err | ||
| } | ||
|
|
@@ -317,20 +317,20 @@ func combinePortsWithCNPEgressPeers( | |
| return rules, nil | ||
| } | ||
|
|
||
| func unpackCNPPorts(k8sPorts *[]clusternetpol.ClusterNetworkPolicyPort) ( | ||
| func unpackCNPProtocols(cnpProtocols []clusternetpol.ClusterNetworkPolicyProtocol) ( | ||
| map[string][]numorstring.Port, | ||
| []string, error, | ||
| ) { | ||
| // If there are no ports, represent that as zero struct. | ||
| ports := []clusternetpol.ClusterNetworkPolicyPort{{}} | ||
| if k8sPorts != nil && len(*k8sPorts) != 0 { | ||
| ports = *k8sPorts | ||
| protocols := []clusternetpol.ClusterNetworkPolicyProtocol{{}} | ||
| if cnpProtocols != nil && len(cnpProtocols) != 0 { | ||
| protocols = cnpProtocols | ||
| } | ||
|
|
||
| protocolPorts := map[string][]numorstring.Port{} | ||
|
|
||
| for _, port := range ports { | ||
| protocol, calicoPort, err := k8sCNPPortToCalicoFields(&port) | ||
| for _, p := range protocols { | ||
| protocol, calicoPort, err := k8sCNPPortToCalicoFields(&p) | ||
| if err != nil { | ||
| return nil, nil, fmt.Errorf("failed to parse k8s port: %s", err) | ||
| } | ||
|
|
@@ -354,61 +354,63 @@ func unpackCNPPorts(k8sPorts *[]clusternetpol.ClusterNetworkPolicyPort) ( | |
| } | ||
| } | ||
|
|
||
| protocols := make([]string, 0, len(protocolPorts)) | ||
| for k := range protocolPorts { | ||
| protocols = append(protocols, k) | ||
| protos := make([]string, 0, len(protocolPorts)) | ||
| for p := range protocolPorts { | ||
| protos = append(protos, p) | ||
| } | ||
| // Ensure deterministic output | ||
| sort.Strings(protocols) | ||
| return protocolPorts, protocols, nil | ||
| sort.Strings(protos) | ||
| return protocolPorts, protos, nil | ||
| } | ||
|
|
||
| func k8sCNPPortToCalicoFields(port *clusternetpol.ClusterNetworkPolicyPort) ( | ||
| func k8sCNPPortToCalicoFields(cnpProto *clusternetpol.ClusterNetworkPolicyProtocol) ( | ||
| protocol *numorstring.Protocol, | ||
| dstPort *numorstring.Port, | ||
| err error, | ||
| ) { | ||
| // If no port info, return zero values for all fields (protocol, dstPorts). | ||
| if port == nil { | ||
| if cnpProto == nil { | ||
| return | ||
| } | ||
| // Only one of the PortNumber or PortRange is set. | ||
| if port.PortNumber != nil { | ||
| dstPort = k8sCNPPortToCalico(port.PortNumber) | ||
| proto := ensureProtocol(port.PortNumber.Protocol) | ||
| protocol = k8sProtocolToCalico(&proto) | ||
|
|
||
| if cnpProto.TCP != nil { | ||
| dstPort, err = k8sCNPPortToCalico(cnpProto.TCP.DestinationPort) | ||
| p := numorstring.ProtocolFromString(numorstring.ProtocolTCP) | ||
| protocol = &p | ||
| return | ||
| } | ||
| if port.PortRange != nil { | ||
| dstPort, err = k8sCNPPortRangeToCalico(port.PortRange) | ||
| if err != nil { | ||
| return | ||
| } | ||
| proto := ensureProtocol(port.PortRange.Protocol) | ||
| protocol = k8sProtocolToCalico(&proto) | ||
|
|
||
| if cnpProto.UDP != nil { | ||
| dstPort, err = k8sCNPPortToCalico(cnpProto.UDP.DestinationPort) | ||
| p := numorstring.ProtocolFromString(numorstring.ProtocolUDP) | ||
| protocol = &p | ||
| return | ||
| } | ||
| // TODO: Add support for NamedPorts | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. What about named ports? We should at least fail/skip the rule if the proto struct has an unknown field, I think.
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. As we discussed, I updated the code to return error for named ports for the moment. |
||
| return | ||
| } | ||
|
|
||
| func k8sCNPPortToCalico(port *clusternetpol.Port) *numorstring.Port { | ||
| if port == nil { | ||
| return nil | ||
| if cnpProto.SCTP != nil { | ||
| dstPort, err = k8sCNPPortToCalico(cnpProto.SCTP.DestinationPort) | ||
| p := numorstring.ProtocolFromString(numorstring.ProtocolSCTP) | ||
|
mazdakn marked this conversation as resolved.
|
||
| protocol = &p | ||
| return | ||
| } | ||
| p := numorstring.SinglePort(uint16(port.Port)) | ||
| return &p | ||
|
|
||
| return | ||
| } | ||
|
|
||
| func k8sCNPPortRangeToCalico(port *clusternetpol.PortRange) (*numorstring.Port, error) { | ||
| if port == nil { | ||
| return nil, nil | ||
| func k8sCNPPortToCalico(port *clusternetpol.Port) (*numorstring.Port, error) { | ||
| // Only one of the Number or Range is set. | ||
| if port.Number != 0 { | ||
| p := numorstring.SinglePort(uint16(port.Number)) | ||
| return &p, nil | ||
| } | ||
|
mazdakn marked this conversation as resolved.
|
||
| p, err := numorstring.PortFromRange(uint16(port.Start), uint16(port.End)) | ||
| if err != nil { | ||
| return nil, err | ||
| if port.Range != nil { | ||
| p, err := numorstring.PortFromRange(uint16(port.Range.Start), uint16(port.Range.End)) | ||
| if err != nil { | ||
| return nil, err | ||
| } | ||
| return &p, nil | ||
| } | ||
| return &p, nil | ||
| return nil, nil | ||
| } | ||
|
|
||
| func k8sClusterNetworkPolicyToCalicoMetadata(ruleName string) *apiv3.RuleMetadata { | ||
|
|
||
Uh oh!
There was an error while loading. Please reload this page.