Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
71 changes: 57 additions & 14 deletions npm/pkg/controlplane/translation/translatePolicy.go
Original file line number Diff line number Diff line change
Expand Up @@ -362,6 +362,55 @@
return nil
}

func directPeerAndPortRule(npmNetPol *policies.NPMNetworkPolicy, direction policies.Direction, ports []networkingv1.NetworkPolicyPort, cidr string, npmLiteToggle bool) error {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

probably include Allow somewhere in this func to be extra explicit

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

View workflow job for this annotation

GitHub Actions / Lint (windows-latest)

indent-error-flow: if block ends with a return statement, so drop this else and outdent its block (revive)

Check failure on line 376 in npm/pkg/controlplane/translation/translatePolicy.go

View workflow job for this annotation

GitHub Actions / Lint (ubuntu-latest)

indent-error-flow: if block ends with a return statement, so drop this else and outdent its block (revive)
// 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
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would also wrap this with some additional context from the npmNetPol in case you're doing a trace later on

}
Comment on lines +399 to +401
Copy link

Copilot AI Nov 5, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The check for namedPortType is redundant since checkForNamedPortType() already validates this on line 384 and would return an error. This unreachable code should be removed.

Suggested change
if portKind == namedPortType {
return ErrUnsupportedNamedPort
}

Copilot uses AI. Check for mistakes.
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,
Expand Down Expand Up @@ -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
Copy link
Member

Choose a reason for hiding this comment

The 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
Expand All @@ -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).

Expand Down Expand Up @@ -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
Expand Down
Loading
Loading