Skip to content

Commit eb7319d

Browse files
committed
resolved copilot comments
1 parent 19fbf1e commit eb7319d

File tree

3 files changed

+29
-12
lines changed

3 files changed

+29
-12
lines changed

npm/pkg/controlplane/translation/translatePolicy.go

Lines changed: 6 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -349,7 +349,7 @@ func peerAndPortRule(npmNetPol *policies.NPMNetworkPolicy, direction policies.Di
349349
return err
350350
}
351351

352-
err = checkForNamedPortType(portKind, npmLiteToggle)
352+
err = checkForNamedPortType(npmNetPol, portKind, npmLiteToggle, direction, &ports[i], "")
353353
if err != nil {
354354
return err
355355
}
@@ -381,7 +381,7 @@ func directPeerAndPortAllowRule(npmNetPol *policies.NPMNetworkPolicy, direction
381381
return err
382382
}
383383

384-
err = checkForNamedPortType(portKind, npmLiteToggle)
384+
err = checkForNamedPortType(npmNetPol, portKind, npmLiteToggle, direction, &ports[i], cidr)
385385
if err != nil {
386386
return err
387387
}
@@ -396,10 +396,6 @@ func directPeerAndPortAllowRule(npmNetPol *policies.NPMNetworkPolicy, direction
396396
}
397397

398398
// Handle ports
399-
if portKind == namedPortType {
400-
return fmt.Errorf("named port not supported in policy %s (namespace: %s, direction: %s, cidr: %s, port: %v): %w",
401-
npmNetPol.PolicyKey, npmNetPol.Namespace, direction, cidr, ports[i].Port, ErrUnsupportedNamedPort)
402-
}
403399
if portKind == numericPortType {
404400
portInfo, protocol := numericPortRule(&ports[i])
405401
acl.DstPorts = portInfo
@@ -694,9 +690,10 @@ func TranslatePolicy(npObj *networkingv1.NetworkPolicy, npmLiteToggle bool) (*po
694690
return npmNetPol, nil
695691
}
696692

697-
func checkForNamedPortType(portKind netpolPortType, npmLiteToggle bool) error {
693+
func checkForNamedPortType(npmNetPol *policies.NPMNetworkPolicy, portKind netpolPortType, npmLiteToggle bool, direction policies.Direction, port *networkingv1.NetworkPolicyPort, cidr string) error {
698694
if npmLiteToggle && portKind == namedPortType {
699-
return ErrUnsupportedNonCIDR
695+
return fmt.Errorf("named port not supported in policy %s (namespace: %s, direction: %s, cidr: %s, port: %v, protocol: %v): %w",
696+
npmNetPol.PolicyKey, npmNetPol.Namespace, direction, cidr, port.Port, port.Protocol, ErrUnsupportedNamedPort)
700697
}
701698
return nil
702699
}
@@ -717,7 +714,7 @@ func checkOnlyPortRuleExists(
717714
if err != nil {
718715
return err
719716
}
720-
err = checkForNamedPortType(portKind, npmLiteToggle)
717+
err = checkForNamedPortType(npmNetPol, portKind, npmLiteToggle, direction, &ports[i], "")
721718
if err != nil {
722719
return err
723720
}

npm/pkg/controlplane/translation/translatePolicy_test.go

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3153,8 +3153,28 @@ func TestCheckForNamedPortType(t *testing.T) {
31533153
for _, tt := range tests {
31543154
tt := tt
31553155
t.Run(tt.name, func(t *testing.T) {
3156-
// run the function passing in peers and a flag indicating whether npm lite is enabled
3157-
err := checkForNamedPortType(tt.portKind, tt.npmLiteEnabled)
3156+
// Create a mock NPM network policy for testing
3157+
npmNetPol := &policies.NPMNetworkPolicy{
3158+
PolicyKey: "test-policy/test",
3159+
Namespace: "test-namespace",
3160+
}
3161+
3162+
// Use the first port from test data, or create a default one if ports are empty
3163+
var testPort *networkingv1.NetworkPolicyPort
3164+
if len(tt.ports) > 0 {
3165+
testPort = &tt.ports[0]
3166+
} else {
3167+
// Create a default port for tests without specific port data
3168+
port := intstr.FromInt(8080)
3169+
protocol := v1.ProtocolTCP
3170+
testPort = &networkingv1.NetworkPolicyPort{
3171+
Protocol: &protocol,
3172+
Port: &port,
3173+
}
3174+
}
3175+
3176+
// run the function passing in all required parameters
3177+
err := checkForNamedPortType(npmNetPol, tt.portKind, tt.npmLiteEnabled, policies.Ingress, testPort, "10.0.0.0/24")
31583178
if tt.wantErr {
31593179
require.Error(t, err)
31603180
} else {

npm/pkg/dataplane/policies/policy_windows.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -101,7 +101,7 @@ func (acl *ACLPolicy) convertToAclSettings(aclID string) (*NPMACLPolSettings, er
101101
// Ignore adding ruletype for now as there is a bug
102102
// policySettings.RuleType = hcn.RuleTypeSwitch
103103

104-
// HNS has confusing Local and Remote address defintions
104+
// HNS has confusing Local and Remote address definitions
105105
// For Traffic Direction INGRESS
106106
// LocalAddresses = Source Sets
107107
// RemoteAddresses = Destination Sets

0 commit comments

Comments
 (0)