Skip to content

Commit

Permalink
Fix endpoint querier rule index
Browse files Browse the repository at this point in the history
The current endpoint querier rule index shows the index
of the rule among all matched rules in the policy for
this endpoint, which is not super useful for the users.
This change updates the rule index to show the rule index
among all rules in the policy.

Fixes #5782

Signed-off-by: Qiyue Yao <yaoq@vmware.com>
  • Loading branch information
qiyueyao committed Dec 9, 2023
1 parent 073a4b0 commit e3e0d16
Show file tree
Hide file tree
Showing 2 changed files with 65 additions and 4 deletions.
13 changes: 9 additions & 4 deletions pkg/controller/networkpolicy/endpoint_querier.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ package networkpolicy
import (
"k8s.io/apimachinery/pkg/types"

"antrea.io/antrea/pkg/apis/controlplane"
cpv1beta "antrea.io/antrea/pkg/apis/controlplane/v1beta2"
"antrea.io/antrea/pkg/controller/networkpolicy/store"
antreatypes "antrea.io/antrea/pkg/controller/types"
Expand Down Expand Up @@ -127,25 +128,29 @@ func (eq *endpointQuerier) QueryNetworkPolicies(namespace string, podName string
return nil, err
}
for _, policy := range policies {
egressIndex := 0
ingressIndex := 0
egressIndex, ingressIndex := 0, 0
for _, rule := range policy.(*antreatypes.NetworkPolicy).Rules {
for _, addressGroupTrial := range rule.To.AddressGroups {
if addressGroupTrial == string(addressGroup.(*antreatypes.AddressGroup).UID) {
egress = append(egress, &ruleTemp{policy: policy.(*antreatypes.NetworkPolicy), index: egressIndex})
egressIndex++
// an AddressGroup can only be referenced in a rule once
break
}
}
for _, addressGroupTrial := range rule.From.AddressGroups {
if addressGroupTrial == string(addressGroup.(*antreatypes.AddressGroup).UID) {
ingress = append(ingress, &ruleTemp{policy: policy.(*antreatypes.NetworkPolicy), index: ingressIndex})
ingressIndex++
// an AddressGroup can only be referenced in a rule once
break
}
}
// ingressIndex/egressIndex indicates the rule index among this policy's ingress/egress rules
// Antrea Native NP rules priorities are set as index, but KNP rules have the same default rule priorities
if rule.Direction == controlplane.DirectionIn {
ingressIndex++
} else {
egressIndex++
}
}
}
}
Expand Down
56 changes: 56 additions & 0 deletions pkg/controller/networkpolicy/endpoint_querier_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,7 @@ var policies = []*networkingv1.NetworkPolicy{
},
},
PolicyTypes: []networkingv1.PolicyType{
networkingv1.PolicyTypeIngress,
networkingv1.PolicyTypeEgress,
},
},
Expand All @@ -137,6 +138,40 @@ var policies = []*networkingv1.NetworkPolicy{
},
},
},
{
ObjectMeta: metav1.ObjectMeta{
Name: "test-multiple-ingress",
Namespace: "testNamespace",
UID: types.UID("uid-3"),
},
Spec: networkingv1.NetworkPolicySpec{
PodSelector: metav1.LabelSelector{
MatchLabels: map[string]string{"foo": "bar"},
},
Ingress: []networkingv1.NetworkPolicyIngressRule{
{
From: []networkingv1.NetworkPolicyPeer{
{
PodSelector: &metav1.LabelSelector{
MatchLabels: map[string]string{"foo": "baz"},
MatchExpressions: nil,
},
},
},
},
{
From: []networkingv1.NetworkPolicyPeer{
{
PodSelector: &metav1.LabelSelector{
MatchLabels: map[string]string{"foo": "bar"},
MatchExpressions: nil,
},
},
},
},
},
},
},
}

var namespaces = []*corev1.Namespace{
Expand Down Expand Up @@ -184,6 +219,7 @@ func makeControllerAndEndpointQuerier(objects ...runtime.Object) *endpointQuerie
func TestEndpointQuery(t *testing.T) {
policyRef0 := PolicyRef{policies[0].Namespace, policies[0].Name, policies[0].UID}
policyRef1 := PolicyRef{policies[1].Namespace, policies[1].Name, policies[1].UID}
policyRef2 := PolicyRef{policies[2].Namespace, policies[2].Name, policies[2].UID}

testCases := []struct {
name string
Expand Down Expand Up @@ -251,6 +287,26 @@ func TestEndpointQuery(t *testing.T) {
},
},
},
{
"MultipleRule", // Pod is selected by policy with multiple rules
[]runtime.Object{namespaces[0], pods[0], policies[2]},
"testNamespace",
"podA",
&EndpointQueryResponse{
[]Endpoint{
{
Namespace: "testNamespace",
Name: "podA",
Policies: []Policy{
{policyRef2},
},
Rules: []Rule{
{policyRef2, v1beta2.DirectionIn, 1},
},
},
},
},
},
}

for _, tc := range testCases {
Expand Down

0 comments on commit e3e0d16

Please sign in to comment.