Skip to content

Commit

Permalink
Address final comments
Browse files Browse the repository at this point in the history
Signed-off-by: Dyanngg <dingyang@vmware.com>
  • Loading branch information
Dyanngg committed Mar 9, 2023
1 parent d15bff0 commit a8e6a7f
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 24 deletions.
14 changes: 5 additions & 9 deletions multicluster/test/e2e/antreapolicy_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,8 @@ import (
)

const (
// Provide enough time for policies to be enforced & deleted by the CNI plugin.
networkPolicyDelay = 2 * time.Second
// Provide enough time for policies to be imported and enforced by the CNI plugin.
policyRealizedTimeout = 6 * time.Second
acnpIsolationResourceExport = "test-acnp-copy-span-ns-isolation.yml"
acnpIsolationName = "antrea-mc-strict-namespace-isolation"
acnpCrossClusterIsolationResExport = "test-acnp-cross-cluster-ns-isolation.yml"
Expand Down Expand Up @@ -93,8 +93,6 @@ func testAntreaPolicyCopySpanNSIsolation(t *testing.T, data *MCTestData) {
setup := func() {
err := data.deployACNPResourceExport(t, acnpIsolationResourceExport)
failOnError(err, t)
// Sleep 5s to wait resource export/import process to finish resource exchange.
time.Sleep(5 * time.Second)
}
teardown := func() {
err := data.deleteACNPResourceExport(acnpIsolationResourceExport)
Expand All @@ -121,8 +119,6 @@ func testAntreaPolicyCrossClusterNSIsolation(t *testing.T, data *MCTestData) {
setup := func() {
err := data.deployACNPResourceExport(t, acnpCrossClusterIsolationResExport)
failOnError(err, t)
// Sleep 5s to wait resource export/import process to finish resource exchange.
time.Sleep(5 * time.Second)
}
teardown := func() {
err := data.deleteACNPResourceExport(acnpCrossClusterIsolationResExport)
Expand All @@ -147,19 +143,19 @@ func testAntreaPolicyCrossClusterNSIsolation(t *testing.T, data *MCTestData) {

func executeTestsOnAllMemberClusters(t *testing.T, testList []*antreae2e.TestCase, acnpName string, setup, teardown func(), testCrossCluster bool) {
setup()
time.Sleep(networkPolicyDelay)
for _, testCase := range testList {
t.Logf("Running test case %s", testCase.Name)
for _, step := range testCase.Steps {
t.Logf("Running step %s of test case %s", step.Name, testCase.Name)
reachability := step.Reachability
for clusterName, k8sUtils := range clusterK8sUtilsMap {
if clusterName == leaderCluster {
// skip traffic test for the leader cluster
// skip verification for the leader cluster
continue
}
if _, err := k8sUtils.GetACNP(acnpName); err != nil {
if err := k8sUtils.WaitForACNPCreatiionAndRealization(t, acnpName, policyRealizedTimeout); err != nil {
t.Errorf("Failed to get ACNP to be replicated in cluster %s", clusterName)
failOnError(err, t)
}
start := time.Now()
k8sUtils.Validate(allPodsPerCluster, reachability, step.Ports, step.Protocol)
Expand Down
14 changes: 0 additions & 14 deletions pkg/controller/labelidentity/label_group_index.go
Original file line number Diff line number Diff line change
Expand Up @@ -270,20 +270,6 @@ func (i *LabelIdentityIndex) AddSelector(selector *types.GroupSelector, policyKe
return i.getMatchedLabelIdentityIDs(sItem)
}

// Dedup LabelIdentity IDs in-place.
func DedupLabelIdentites(labelIdentityIDs []uint32) []uint32 {
seen := map[uint32]struct{}{}
idx := 0
for _, id := range labelIdentityIDs {
if _, exists := seen[id]; !exists {
seen[id] = struct{}{}
labelIdentityIDs[idx] = id
idx++
}
}
return labelIdentityIDs[:idx]
}

// DeleteSelector removes a selectorItem from referring to the policy being deleted.
func (i *LabelIdentityIndex) DeleteSelector(selectorKey string, policyKey string) {
i.lock.Lock()
Expand Down
16 changes: 15 additions & 1 deletion pkg/controller/labelidentity/label_group_index_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -340,7 +340,7 @@ func TestSetPolicySelectors(t *testing.T) {
labelIDs = append(labelIDs, i.AddSelector(sel, tt.policyKey)...)
}
i.RemoveStalePolicySelectors(tt.selectors, tt.policyKey)
assert.ElementsMatch(t, tt.expIDs, DedupLabelIdentites(labelIDs))
assert.ElementsMatch(t, tt.expIDs, dedupLabelIdentites(labelIDs))
assert.Equalf(t, len(tt.expSelectorItems), len(i.selectorItems.List()), "Unexpected number of cached selectorItems")
for selKey, expSelItem := range tt.expSelectorItems {
s, exists, _ := i.selectorItems.GetByKey(selKey)
Expand All @@ -355,6 +355,20 @@ func TestSetPolicySelectors(t *testing.T) {
}
}

// Dedup LabelIdentity IDs in-place.
func dedupLabelIdentites(labelIdentityIDs []uint32) []uint32 {
seen := map[uint32]struct{}{}
idx := 0
for _, id := range labelIdentityIDs {
if _, exists := seen[id]; !exists {
seen[id] = struct{}{}
labelIdentityIDs[idx] = id
idx++
}
}
return labelIdentityIDs[:idx]
}

func TestAddLabelIdentity(t *testing.T) {
labelIdentityAOriginalID := uint32(1)
tests := []struct {
Expand Down
16 changes: 16 additions & 0 deletions test/e2e/k8s_util.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import (
"fmt"
"strings"
"sync"
"testing"
"time"

"github.com/pkg/errors"
Expand All @@ -28,6 +29,7 @@ import (
v1net "k8s.io/api/networking/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/util/intstr"
"k8s.io/apimachinery/pkg/util/wait"

crdv1alpha1 "antrea.io/antrea/pkg/apis/crd/v1alpha1"
crdv1alpha2 "antrea.io/antrea/pkg/apis/crd/v1alpha2"
Expand Down Expand Up @@ -978,6 +980,20 @@ func (data *TestData) CleanANPs(namespaces []string) error {
return nil
}

func (data *TestData) WaitForACNPCreatiionAndRealization(t *testing.T, name string, timeout time.Duration) error {
t.Logf("Waiting for ACNP '%s' to be created and realized", name)
if err := wait.Poll(100*time.Millisecond, timeout, func() (bool, error) {
acnp, err := data.crdClient.CrdV1alpha1().ClusterNetworkPolicies().Get(context.TODO(), name, metav1.GetOptions{})
if err != nil {
return false, nil
}
return acnp.Status.ObservedGeneration == acnp.Generation && acnp.Status.Phase == crdv1alpha1.NetworkPolicyRealized, nil
}); err != nil {
return fmt.Errorf("error when waiting for ACNP '%s' to be realized: %v", name, err)
}
return nil
}

func (k *KubernetesUtils) waitForPodInNamespace(ns string, pod string) ([]string, error) {
log.Infof("Waiting for Pod '%s/%s'", ns, pod)
for {
Expand Down

0 comments on commit a8e6a7f

Please sign in to comment.