-
Notifications
You must be signed in to change notification settings - Fork 260
[NPM Lite] Bypassing IPSets for IP CIDR Block Based Network Policies #4107
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
base: master
Are you sure you want to change the base?
Changes from 8 commits
3179b1f
c32653b
0f96f04
af8d266
48168b1
ce8cb5a
351d8fb
55ec65e
19fbf1e
eb7319d
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 |
|---|---|---|
|
|
@@ -362,6 +362,55 @@ | |
| return nil | ||
| } | ||
|
|
||
| func directPeerAndPortRule(npmNetPol *policies.NPMNetworkPolicy, direction policies.Direction, ports []networkingv1.NetworkPolicyPort, cidr string, npmLiteToggle bool) error { | ||
| if len(ports) == 0 { | ||
| acl := policies.NewACLPolicy(policies.Allowed, direction) | ||
| // bypasses ipset creation for /32 cidrs and directly creates an acl with the cidr | ||
| if direction == policies.Ingress { | ||
| acl.SrcDirectIPs = []string{cidr} | ||
| } else { | ||
| acl.DstDirectIPs = []string{cidr} | ||
| } | ||
| npmNetPol.ACLs = append(npmNetPol.ACLs, acl) | ||
| return nil | ||
| } else { | ||
|
Check failure on line 376 in npm/pkg/controlplane/translation/translatePolicy.go
|
||
| // handle each port separately | ||
| for i := range ports { | ||
| portKind, err := portType(ports[i]) | ||
| if err != nil { | ||
| return err | ||
| } | ||
|
|
||
| err = checkForNamedPortType(portKind, npmLiteToggle) | ||
| if err != nil { | ||
| return err | ||
| } | ||
|
|
||
| acl := policies.NewACLPolicy(policies.Allowed, direction) | ||
|
|
||
| // Set direct IP based on direction | ||
| if direction == policies.Ingress { | ||
| acl.SrcDirectIPs = []string{cidr} | ||
| } else { | ||
| acl.DstDirectIPs = []string{cidr} | ||
| } | ||
|
|
||
| // Handle ports | ||
| if portKind == namedPortType { | ||
| return ErrUnsupportedNamedPort | ||
|
||
| } | ||
rejain789 marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| if portKind == numericPortType { | ||
| portInfo, protocol := numericPortRule(&ports[i]) | ||
| acl.DstPorts = portInfo | ||
| acl.Protocol = policies.Protocol(protocol) | ||
| } | ||
| npmNetPol.ACLs = append(npmNetPol.ACLs, acl) | ||
|
|
||
| } | ||
| } | ||
| return nil | ||
| } | ||
|
|
||
| // translateRule translates ingress or egress rules and update npmNetPol object. | ||
| func translateRule(npmNetPol *policies.NPMNetworkPolicy, | ||
| netPolName string, | ||
|
|
@@ -405,6 +454,14 @@ | |
| // #2.1 Handle IPBlock and port if exist | ||
| if peer.IPBlock != nil { | ||
| if len(peer.IPBlock.CIDR) > 0 { | ||
| if npmLiteToggle { | ||
| err = directPeerAndPortRule(npmNetPol, direction, ports, peer.IPBlock.CIDR, npmLiteToggle) | ||
| if err != nil { | ||
| return err | ||
|
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. + error wrapping here |
||
| } | ||
| continue | ||
| } | ||
|
|
||
| ipBlockIPSet, ipBlockSetInfo, err := ipBlockRule(netPolName, npmNetPol.Namespace, direction, matchType, ruleIndex, peerIdx, peer.IPBlock) | ||
| if err != nil { | ||
| return err | ||
|
|
@@ -417,12 +474,6 @@ | |
| } | ||
| } | ||
|
|
||
| // if npm lite is configured, check network policy only consists of CIDR blocks | ||
| err := npmLiteValidPolicy(peer, npmLiteToggle) | ||
| if err != nil { | ||
| return err | ||
| } | ||
|
|
||
| // Do not need to run below code to translate PodSelector and NamespaceSelector | ||
| // since IPBlock field is exclusive in NetworkPolicyPeer (i.e., peer in this code). | ||
|
|
||
|
|
@@ -642,14 +693,6 @@ | |
| return npmNetPol, nil | ||
| } | ||
|
|
||
| // validates only CIDR based peer is present + no combination of CIDR with pod/namespace selectors are present | ||
| func npmLiteValidPolicy(peer networkingv1.NetworkPolicyPeer, npmLiteEnabled bool) error { | ||
| if npmLiteEnabled && (peer.PodSelector != nil || peer.NamespaceSelector != nil) { | ||
| return ErrUnsupportedNonCIDR | ||
| } | ||
| return nil | ||
| } | ||
|
|
||
| func checkForNamedPortType(portKind netpolPortType, npmLiteToggle bool) error { | ||
| if npmLiteToggle && portKind == namedPortType { | ||
| return ErrUnsupportedNonCIDR | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
probably include
Allowsomewhere in this func to be extra explicitThere was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
updated thanks