Skip to content

Commit

Permalink
Fix identical GroupMember key across pod IP update
Browse files Browse the repository at this point in the history
  • Loading branch information
Dyanngg committed Feb 2, 2021
1 parent 8555d62 commit 82907f9
Show file tree
Hide file tree
Showing 7 changed files with 110 additions and 20 deletions.
11 changes: 6 additions & 5 deletions pkg/apis/controlplane/sets.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,9 @@ type groupMemberKey string
type GroupMemberSet map[groupMemberKey]*GroupMember

// normalizeGroupMember calculates the groupMemberKey of the provided
// GroupMember based on the Pod's namespaced name or IP.
// GroupMember based on the Pod/ExternalEntity's namespaced name and IPs.
// For GroupMembers in appliedToGroups, the IPs are not set, so the
// generated key does not contain IP information.
func normalizeGroupMember(member *GroupMember) groupMemberKey {
// "/" is illegal in Namespace and name so is safe as the delimiter.
const delimiter = "/"
Expand All @@ -38,10 +40,9 @@ func normalizeGroupMember(member *GroupMember) groupMemberKey {
b.WriteString(member.ExternalEntity.Namespace)
b.WriteString(delimiter)
b.WriteString(member.ExternalEntity.Name)
} else if len(member.IPs) != 0 {
for _, ip := range member.IPs {
b.Write(ip)
}
}
for _, ip := range member.IPs {
b.Write(ip)
}
return groupMemberKey(b.String())
}
Expand Down
11 changes: 6 additions & 5 deletions pkg/apis/controlplane/v1beta2/sets.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,9 @@ type groupMemberKey string
type GroupMemberSet map[groupMemberKey]*GroupMember

// normalizeGroupMember calculates the groupMemberKey of the provided
// GroupMember based on the Pod's namespaced name or IP.
// GroupMember based on the Pod/ExternalEntity's namespaced name and IPs.
// For GroupMembers in appliedToGroups, the IPs are not set, so the
// generated key does not contain IP information.
func normalizeGroupMember(member *GroupMember) groupMemberKey {
// "/" is illegal in Namespace and name so is safe as the delimiter.
const delimiter = "/"
Expand All @@ -40,10 +42,9 @@ func normalizeGroupMember(member *GroupMember) groupMemberKey {
b.WriteString(member.ExternalEntity.Namespace)
b.WriteString(delimiter)
b.WriteString(member.ExternalEntity.Name)
} else if len(member.IPs) != 0 {
for _, ip := range member.IPs {
b.Write(ip)
}
}
for _, ip := range member.IPs {
b.Write(ip)
}
return groupMemberKey(b.String())
}
Expand Down
2 changes: 1 addition & 1 deletion pkg/controller/networkpolicy/antreanetworkpolicy_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -309,8 +309,8 @@ func TestProcessAntreaNetworkPolicy(t *testing.T) {
},
},
AppliedToGroups: []string{
getNormalizedUID(toGroupSelector("ns3", &selectorA, nil, nil).NormalizedName),
getNormalizedUID(toGroupSelector("ns3", &selectorB, nil, nil).NormalizedName),
getNormalizedUID(toGroupSelector("ns3", &selectorA, nil, nil).NormalizedName),
},
AppliedToPerRule: true,
},
Expand Down
2 changes: 1 addition & 1 deletion pkg/controller/networkpolicy/clustergroup.go
Original file line number Diff line number Diff line change
Expand Up @@ -189,7 +189,7 @@ func (n *NetworkPolicyController) syncInternalGroup(key string) error {
pods, _ := n.processSelector(groupSelector)
memberSet := controlplane.GroupMemberSet{}
for _, pod := range pods {
if pod.Status.PodIP == "" {
if len(pod.Status.PodIPs) == 0 {
// No need to insert Pod IPAddress when it is unset.
continue
}
Expand Down
64 changes: 64 additions & 0 deletions pkg/controller/networkpolicy/external_entity_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
package networkpolicy

import (
"reflect"
"testing"

"github.com/stretchr/testify/assert"
Expand All @@ -30,6 +31,69 @@ import (
antreatypes "github.com/vmware-tanzu/antrea/pkg/controller/types"
)

func TestExternalEntityToGroupMember(t *testing.T) {
testEntity := &v1alpha2.ExternalEntity{
ObjectMeta: metav1.ObjectMeta{
Name: "eeA",
Namespace: "nsA",
Labels: map[string]string{
"app": "test",
},
},
Spec: v1alpha2.ExternalEntitySpec{
Endpoints: []v1alpha2.Endpoint{
{
IP: "22.33.44.55",
Name: "vm1",
},
},
Ports: []v1alpha2.NamedPort{
{
Port: 80,
Name: "http",
Protocol: "tcp",
},
},
ExternalNode: "cloud-node-1",
},
}
tests := []struct {
name string
inputEE *v1alpha2.ExternalEntity
expMemberEE controlplane.GroupMember
}{
{
name: "ee-with-ip",
inputEE: testEntity,
expMemberEE: controlplane.GroupMember{
ExternalEntity: &controlplane.ExternalEntityReference{
Name: testEntity.Name,
Namespace: testEntity.Namespace,
},
IPs: []controlplane.IPAddress{ipStrToIPAddress(testEntity.Spec.Endpoints[0].IP)},
Ports: []controlplane.NamedPort{
{
Port: 80,
Name: "http",
Protocol: "tcp",
},
},
},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
actualMemberEE := externalEntityToGroupMember(tt.inputEE)
if !reflect.DeepEqual(*(*actualMemberEE).ExternalEntity, *(tt.expMemberEE).ExternalEntity) {
t.Errorf("externalEntityToGroupMember() got unexpected EEReference %v, want %v", *(*actualMemberEE).ExternalEntity, *(tt.expMemberEE).ExternalEntity)
}
if !comparePodIPs(actualMemberEE.IPs, tt.expMemberEE.IPs) {
t.Errorf("externalEntityToGroupMember() got unexpected IP %v, want %v", actualMemberEE.IPs, tt.expMemberEE.IPs)
}
})
}
}

func TestAddExternalEntity(t *testing.T) {
selectorSpec := metav1.LabelSelector{
MatchLabels: map[string]string{"group": "appliedTo"},
Expand Down
6 changes: 3 additions & 3 deletions pkg/controller/networkpolicy/networkpolicy_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -83,9 +83,9 @@ var (
// uuidNamespace is a uuid.UUID type generated from a string to be
// used to generate uuid.UUID for internal Antrea objects like
// AppliedToGroup, AddressGroup etc.
// 5a5e7dd9-e3fb-49bb-b263-9bab25c95841 was generated using
// e4f24a48-ca1f-4d5b-819c-ea7632b22115 was generated using
// uuid.NewV4() function.
uuidNamespace = uuid.FromStringOrNil("5a5e7dd9-e3fb-49bb-b263-9bab25c95841")
uuidNamespace = uuid.FromStringOrNil("e4f24a48-ca1f-4d5b-819c-ea7632b22115")

// matchAllPeer is a NetworkPolicyPeer matching all source/destination IP addresses. Both IPv4 Any (0.0.0.0/0) and
// IPv6 Any (::/0) are added into the IPBlocks, and Antrea Agent should decide if both two are used according the
Expand Down Expand Up @@ -1301,7 +1301,7 @@ func (n *NetworkPolicyController) syncAddressGroup(key string) error {
pods, externalEntities := n.processSelector(groupSelector)
memberSet := controlplane.GroupMemberSet{}
for _, pod := range pods {
if pod.Status.PodIP == "" {
if len(pod.Status.PodIPs) == 0 {
// No need to insert Pod IPAddress when it is unset.
continue
}
Expand Down
34 changes: 29 additions & 5 deletions pkg/controller/networkpolicy/networkpolicy_controller_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -965,6 +965,9 @@ func TestAddPod(t *testing.T) {
},
},
PodIP: "1.2.3.4",
PodIPs: []corev1.PodIP{
{IP: "1.2.3.4"},
},
},
},
appGroupMatch: false,
Expand Down Expand Up @@ -994,6 +997,9 @@ func TestAddPod(t *testing.T) {
},
},
PodIP: "1.2.3.4",
PodIPs: []corev1.PodIP{
{IP: "1.2.3.4"},
},
},
},
appGroupMatch: false,
Expand Down Expand Up @@ -1026,6 +1032,9 @@ func TestAddPod(t *testing.T) {
},
},
PodIP: "1.2.3.4",
PodIPs: []corev1.PodIP{
{IP: "1.2.3.4"},
},
},
},
appGroupMatch: true,
Expand Down Expand Up @@ -1055,6 +1064,9 @@ func TestAddPod(t *testing.T) {
},
},
PodIP: "1.2.3.4",
PodIPs: []corev1.PodIP{
{IP: "1.2.3.4"},
},
},
},
appGroupMatch: false,
Expand Down Expand Up @@ -1084,6 +1096,9 @@ func TestAddPod(t *testing.T) {
},
},
PodIP: "1.2.3.4",
PodIPs: []corev1.PodIP{
{IP: "1.2.3.4"},
},
},
},
appGroupMatch: false,
Expand Down Expand Up @@ -1118,6 +1133,9 @@ func TestAddPod(t *testing.T) {
},
},
PodIP: "1.2.3.4",
PodIPs: []corev1.PodIP{
{IP: "1.2.3.4"},
},
},
},
appGroupMatch: true,
Expand Down Expand Up @@ -1203,6 +1221,9 @@ func TestAddPod(t *testing.T) {
},
},
PodIP: "1.2.3.4",
PodIPs: []corev1.PodIP{
{IP: "1.2.3.4"},
},
},
},
appGroupMatch: false,
Expand Down Expand Up @@ -1232,6 +1253,9 @@ func TestAddPod(t *testing.T) {
},
},
PodIP: "1.2.3.4",
PodIPs: []corev1.PodIP{
{IP: "1.2.3.4"},
},
},
},
appGroupMatch: false,
Expand Down Expand Up @@ -2694,22 +2718,22 @@ func TestPodToGroupMember(t *testing.T) {
t.Run(tt.name, func(t *testing.T) {
actualMemberPod := podToGroupMember(tt.inputPod, tt.includeIP)
if !reflect.DeepEqual(*(*actualMemberPod).Pod, *(tt.expMemberPod).Pod) {
t.Errorf("podToMemberPod() got unexpected PodReference %v, want %v", *(*actualMemberPod).Pod, *(tt.expMemberPod).Pod)
t.Errorf("podToGroupMember() got unexpected PodReference %v, want %v", *(*actualMemberPod).Pod, *(tt.expMemberPod).Pod)
}
// Case where the IPAddress must not be populated.
if !tt.includeIP {
if len(actualMemberPod.IPs) > 0 {
t.Errorf("podToMemberPod() got unexpected IP %v, want nil", actualMemberPod.IPs)
t.Errorf("podToGroupMember() got unexpected IP %v, want nil", actualMemberPod.IPs)
}
} else if !comparePodIPs(actualMemberPod.IPs, tt.expMemberPod.IPs) {
t.Errorf("podToMemberPod() got unexpected IP %v, want %v", actualMemberPod.IPs, tt.expMemberPod.IPs)
t.Errorf("podToGroupMember() got unexpected IP %v, want %v", actualMemberPod.IPs, tt.expMemberPod.IPs)
}
if !tt.namedPort {
if len(actualMemberPod.Ports) > 0 {
t.Errorf("podToMemberPod() got unexpected Ports %v, want []", actualMemberPod.Ports)
t.Errorf("podToGroupMember() got unexpected Ports %v, want []", actualMemberPod.Ports)
}
} else if !reflect.DeepEqual(actualMemberPod.Ports, tt.expMemberPod.Ports) {
t.Errorf("podToMemberPod() got unexpected Ports %v, want %v", actualMemberPod.Ports, tt.expMemberPod.Ports)
t.Errorf("podToGroupMember() got unexpected Ports %v, want %v", actualMemberPod.Ports, tt.expMemberPod.Ports)
}
})
}
Expand Down

0 comments on commit 82907f9

Please sign in to comment.