Skip to content

Commit 652107c

Browse files
authored
Fix the issue that NSX VPC not deleted when the Namespace is terminating (#948)
* Fix issues with NetworkInfo reconciler that not delete NSX VPC in time This change is to fix an issue with the NetworkInfo deletion logic that NSX VPC is not removed if the CR's Namesapce still exists. The fix is to add a wather on Namespace deletion event and ensure the NSX VPC is deleted when the K8s Namespace is deleted. * Filter out the Namespace which is in terminating state
1 parent 7c783d4 commit 652107c

File tree

5 files changed

+144
-141
lines changed

5 files changed

+144
-141
lines changed

pkg/controllers/networkinfo/networkinfo_controller.go

Lines changed: 23 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -136,7 +136,7 @@ func (r *NetworkInfoReconciler) Reconcile(ctx context.Context, req ctrl.Request)
136136
networkInfoCR := &v1alpha1.NetworkInfo{}
137137
if err := r.Client.Get(ctx, req.NamespacedName, networkInfoCR); err != nil {
138138
if apierrors.IsNotFound(err) {
139-
if err := r.deleteVPCsByName(ctx, req.Namespace); err != nil {
139+
if err := r.deleteVPCsByNamespace(ctx, req.Namespace); err != nil {
140140
r.StatusUpdater.DeleteFail(req.NamespacedName, nil, err)
141141
return common.ResultRequeue, err
142142
}
@@ -150,7 +150,7 @@ func (r *NetworkInfoReconciler) Reconcile(ctx context.Context, req ctrl.Request)
150150
// Check if the CR is marked for deletion
151151
if !networkInfoCR.ObjectMeta.DeletionTimestamp.IsZero() {
152152
r.StatusUpdater.IncreaseDeleteTotal()
153-
if err := r.deleteVPCsByID(ctx, networkInfoCR.GetNamespace(), string(networkInfoCR.UID)); err != nil {
153+
if err := r.deleteVPCsByNamespace(ctx, networkInfoCR.GetNamespace()); err != nil {
154154
r.StatusUpdater.DeleteFail(req.NamespacedName, nil, err)
155155
return common.ResultRequeue, err
156156
}
@@ -408,8 +408,11 @@ func (r *NetworkInfoReconciler) listNamespaceCRsNameIDSet(ctx context.Context) (
408408
nsSet := sets.Set[string]{}
409409
idSet := sets.Set[string]{}
410410
for _, ns := range namespaces.Items {
411-
nsSet.Insert(ns.Name)
412-
idSet.Insert(string(ns.UID))
411+
// Ignore the terminating Namespaces in the list results.
412+
if ns.ObjectMeta.DeletionTimestamp.IsZero() {
413+
nsSet.Insert(ns.Name)
414+
idSet.Insert(string(ns.UID))
415+
}
413416
}
414417
return nsSet, idSet, nil
415418
}
@@ -459,36 +462,18 @@ func (r *NetworkInfoReconciler) CollectGarbage(ctx context.Context) {
459462
}
460463
}
461464

462-
func (r *NetworkInfoReconciler) fetchStaleVPCsByNamespace(ctx context.Context, ns string) ([]*model.Vpc, error) {
463-
isShared, err := r.Service.IsSharedVPCNamespaceByNS(ctx, ns)
464-
if err != nil {
465-
if apierrors.IsNotFound(err) {
466-
// if the Namespace has been deleted, we never know whether it`s a shared Namespace, The GC will delete the stale VPCs
467-
log.Info("Namespace does not exist while fetching stale VPCs", "Namespace", ns)
468-
return nil, nil
469-
}
470-
return nil, fmt.Errorf("failed to check if Namespace is shared for NS %s: %w", ns, err)
471-
}
472-
if isShared {
473-
log.Info("Shared Namespace, skipping deletion of NSX VPC", "Namespace", ns)
474-
return nil, nil
465+
func (r *NetworkInfoReconciler) deleteVPCsByNamespace(ctx context.Context, ns string) error {
466+
staleVPCs := r.Service.GetVPCsByNamespace(ns)
467+
if len(staleVPCs) == 0 {
468+
return nil
475469
}
476470

477-
return r.Service.GetVPCsByNamespace(ns), nil
478-
}
479-
480-
func (r *NetworkInfoReconciler) deleteVPCsByName(ctx context.Context, ns string) error {
481471
_, idSet, err := r.listNamespaceCRsNameIDSet(ctx)
482472
if err != nil {
483473
log.Error(err, "Failed to list Kubernetes Namespaces")
484474
return fmt.Errorf("failed to list Kubernetes Namespaces while deleting VPCs: %v", err)
485475
}
486476

487-
staleVPCs, err := r.fetchStaleVPCsByNamespace(ctx, ns)
488-
if err != nil {
489-
return err
490-
}
491-
492477
var vpcToDelete []*model.Vpc
493478
for _, nsxVPC := range staleVPCs {
494479
namespaceIDofVPC := filterTagFromNSXVPC(nsxVPC, commonservice.TagScopeNamespaceUID)
@@ -501,22 +486,6 @@ func (r *NetworkInfoReconciler) deleteVPCsByName(ctx context.Context, ns string)
501486
return r.deleteVPCs(ctx, vpcToDelete, ns)
502487
}
503488

504-
func (r *NetworkInfoReconciler) deleteVPCsByID(ctx context.Context, ns, id string) error {
505-
staleVPCs, err := r.fetchStaleVPCsByNamespace(ctx, ns)
506-
if err != nil {
507-
return err
508-
}
509-
510-
var vpcToDelete []*model.Vpc
511-
for _, nsxVPC := range staleVPCs {
512-
namespaceIDofVPC := filterTagFromNSXVPC(nsxVPC, commonservice.TagScopeNamespaceUID)
513-
if namespaceIDofVPC == id {
514-
vpcToDelete = append(vpcToDelete, nsxVPC)
515-
}
516-
}
517-
return r.deleteVPCs(ctx, vpcToDelete, ns)
518-
}
519-
520489
func (r *NetworkInfoReconciler) deleteVPCs(ctx context.Context, staleVPCs []*model.Vpc, ns string) error {
521490
if len(staleVPCs) == 0 {
522491
log.Info("There is no VPCs found in store, skipping deletion of NSX VPC", "Namespace", ns)
@@ -538,14 +507,22 @@ func (r *NetworkInfoReconciler) deleteVPCs(ctx context.Context, staleVPCs []*mod
538507
}
539508

540509
// Update the VPCNetworkConfiguration Status
541-
ncName, err := r.Service.GetNetworkconfigNameFromNS(ctx, ns)
542-
if err != nil {
543-
return fmt.Errorf("failed to get VPCNetworkConfiguration for Namespace when deleting stale VPCs %s: %w", ns, err)
510+
vpcNetConfig := r.Service.GetVPCNetworkConfigByNamespace(ns)
511+
if vpcNetConfig != nil {
512+
updateVPCNetworkConfigurationStatusWithAliveVPCs(ctx, r.Client, vpcNetConfig.Name, r.listVPCsByNetworkConfigName)
544513
}
545-
deleteVPCNetworkConfigurationStatus(ctx, r.Client, ncName, staleVPCs, r.Service.ListVPC())
546514
return nil
547515
}
548516

517+
func (r *NetworkInfoReconciler) listVPCsByNetworkConfigName(ncName string) []*model.Vpc {
518+
namespacesUsingNC := r.Service.GetNamespacesByNetworkconfigName(ncName)
519+
aliveVPCs := make([]*model.Vpc, 0)
520+
for _, namespace := range namespacesUsingNC {
521+
aliveVPCs = append(aliveVPCs, r.Service.GetVPCsByNamespace(namespace)...)
522+
}
523+
return aliveVPCs
524+
}
525+
549526
func (r *NetworkInfoReconciler) syncPreCreatedVpcIPs(ctx context.Context) {
550527
// Construct a map for the existing NetworkInfo CRs, the key is its Namespace, and the value is
551528
// the NetworkInfo CR.

pkg/controllers/networkinfo/networkinfo_controller_test.go

Lines changed: 101 additions & 73 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ import (
2121
"k8s.io/apimachinery/pkg/runtime"
2222
"k8s.io/apimachinery/pkg/types"
2323
utilruntime "k8s.io/apimachinery/pkg/util/runtime"
24+
"k8s.io/apimachinery/pkg/util/sets"
2425
clientgoscheme "k8s.io/client-go/kubernetes/scheme"
2526
"k8s.io/client-go/util/workqueue"
2627
controllerruntime "sigs.k8s.io/controller-runtime"
@@ -154,20 +155,8 @@ func TestNetworkInfoReconciler_Reconcile(t *testing.T) {
154155
{
155156
name: "Empty",
156157
prepareFunc: func(t *testing.T, r *NetworkInfoReconciler, ctx context.Context) (patches *gomonkey.Patches) {
157-
patches = gomonkey.ApplyMethod(reflect.TypeOf(r.Service), "IsSharedVPCNamespaceByNS", func(_ *vpc.VPCService, ctx context.Context, _ string) (bool, error) {
158-
return false, nil
159-
})
160-
patches.ApplyMethod(reflect.TypeOf(r.Service), "GetVPCsByNamespace", func(_ *vpc.VPCService, _ string) []*model.Vpc {
161-
return nil
162-
})
163-
patches.ApplyMethod(reflect.TypeOf(r.Service), "GetNetworkconfigNameFromNS", func(_ *vpc.VPCService, ctx context.Context, _ string) (string, error) {
164-
return "", nil
165-
})
166-
patches.ApplyMethod(reflect.TypeOf(r.Service), "ListVPC", func(_ *vpc.VPCService) []model.Vpc {
167-
return nil
168-
})
169-
patches.ApplyFunc(deleteVPCNetworkConfigurationStatus, func(ctx context.Context, client client.Client, ncName string, staleVPCs []*model.Vpc, aliveVPCs []model.Vpc) {
170-
return
158+
patches = gomonkey.ApplyMethod(reflect.TypeOf(r.Service), "GetVPCsByNamespace", func(_ *vpc.VPCService, _ string) []*model.Vpc {
159+
return []*model.Vpc{}
171160
})
172161
return patches
173162
},
@@ -779,35 +768,41 @@ func TestNetworkInfoReconciler_deleteStaleVPCs(t *testing.T) {
779768
expectErrStr string
780769
}{
781770
{
782-
name: "shared namespace, skip deletion",
771+
name: "no VPC exists for the Namespace,, skip deletion",
783772
prepareFuncs: func(r *NetworkInfoReconciler) *gomonkey.Patches {
784-
patches := gomonkey.ApplyMethod(reflect.TypeOf(r.Service), "IsSharedVPCNamespaceByNS", func(_ *vpc.VPCService, ctx context.Context, _ string) (bool, error) {
785-
return true, nil
773+
patches := gomonkey.ApplyMethod(reflect.TypeOf(r.Service), "GetVPCsByNamespace", func(_ *vpc.VPCService, _ string) []*model.Vpc {
774+
return []*model.Vpc{}
786775
})
787776
return patches
788777
},
789-
},
790-
{
791-
name: "non-shared namespace, no VPCs found",
778+
}, {
779+
name: "NSX VPC is used by a valid Namespace,, skip deletion",
792780
prepareFuncs: func(r *NetworkInfoReconciler) *gomonkey.Patches {
793-
patches := gomonkey.ApplyMethod(reflect.TypeOf(r.Service), "IsSharedVPCNamespaceByNS", func(_ *vpc.VPCService, ctx context.Context, _ string) (bool, error) {
794-
return false, nil
781+
patches := gomonkey.ApplyMethod(reflect.TypeOf(r.Service), "GetVPCsByNamespace", func(_ *vpc.VPCService, _ string) []*model.Vpc {
782+
return []*model.Vpc{
783+
{Tags: []model.Tag{
784+
{Scope: servicecommon.String(servicecommon.TagScopeNamespaceUID), Tag: servicecommon.String("vpc1")}}}}
795785
})
796-
patches.ApplyMethod(reflect.TypeOf(r.Service), "GetVPCsByNamespace", func(_ *vpc.VPCService, _ string) []*model.Vpc {
797-
return nil
786+
patches.ApplyPrivateMethod(reflect.TypeOf(r), "listNamespaceCRsNameIDSet", func(_ *NetworkInfoReconciler, _ context.Context) (sets.Set[string], sets.Set[string], error) {
787+
return sets.Set[string]{}, sets.New[string]("vpc1"), nil
798788
})
799789
return patches
800790
},
801791
},
802792
{
803793
name: "failed to delete VPC",
804794
prepareFuncs: func(r *NetworkInfoReconciler) *gomonkey.Patches {
805-
patches := gomonkey.ApplyMethod(reflect.TypeOf(r.Service), "IsSharedVPCNamespaceByNS", func(_ *vpc.VPCService, ctx context.Context, _ string) (bool, error) {
806-
return false, nil
795+
patches := gomonkey.ApplyMethod(reflect.TypeOf(r.Service), "GetVPCsByNamespace", func(_ *vpc.VPCService, _ string) []*model.Vpc {
796+
return []*model.Vpc{
797+
{
798+
Tags: []model.Tag{
799+
{Scope: servicecommon.String(servicecommon.TagScopeNamespaceUID), Tag: servicecommon.String("vpc1")},
800+
},
801+
Path: servicecommon.String("/orgs/default/projects/default/vpcs/vpc1"),
802+
}}
807803
})
808-
patches.ApplyMethod(reflect.TypeOf(r.Service), "GetVPCsByNamespace", func(_ *vpc.VPCService, _ string) []*model.Vpc {
809-
vpcPath := "/vpc/1"
810-
return []*model.Vpc{{Path: &vpcPath}}
804+
patches.ApplyPrivateMethod(reflect.TypeOf(r), "listNamespaceCRsNameIDSet", func(_ *NetworkInfoReconciler, _ context.Context) (sets.Set[string], sets.Set[string], error) {
805+
return sets.Set[string]{}, sets.Set[string]{}, nil
811806
})
812807
patches.ApplyMethod(reflect.TypeOf(r.Service), "DeleteVPC", func(_ *vpc.VPCService, _ string) error {
813808
return fmt.Errorf("delete failed")
@@ -819,26 +814,23 @@ func TestNetworkInfoReconciler_deleteStaleVPCs(t *testing.T) {
819814
{
820815
name: "successful deletion of VPCs",
821816
prepareFuncs: func(r *NetworkInfoReconciler) *gomonkey.Patches {
822-
patches := gomonkey.ApplyMethod(reflect.TypeOf(r.Service), "IsSharedVPCNamespaceByNS", func(_ *vpc.VPCService, ctx context.Context, _ string) (bool, error) {
823-
return false, nil
817+
patches := gomonkey.ApplyMethod(reflect.TypeOf(r.Service), "GetVPCsByNamespace", func(_ *vpc.VPCService, _ string) []*model.Vpc {
818+
return []*model.Vpc{
819+
{
820+
Tags: []model.Tag{
821+
{Scope: servicecommon.String(servicecommon.TagScopeNamespaceUID), Tag: servicecommon.String("vpc1")},
822+
},
823+
Path: servicecommon.String("/orgs/default/projects/default/vpcs/vpc1"),
824+
}}
824825
})
825-
patches.ApplyMethod(reflect.TypeOf(r.Service), "GetVPCsByNamespace", func(_ *vpc.VPCService, _ string) []*model.Vpc {
826-
vpcPath1 := "/vpc/1"
827-
vpcPath2 := "/vpc/2"
828-
displayName1 := "fakeDisplayName"
829-
displayName2 := "fakeDisplayName"
830-
return []*model.Vpc{{Path: &vpcPath1, DisplayName: &displayName1}, {Path: &vpcPath2, DisplayName: &displayName2}}
826+
patches.ApplyPrivateMethod(reflect.TypeOf(r), "listNamespaceCRsNameIDSet", func(_ *NetworkInfoReconciler, _ context.Context) (sets.Set[string], sets.Set[string], error) {
827+
return sets.Set[string]{}, sets.Set[string]{}, nil
831828
})
832829
patches.ApplyMethod(reflect.TypeOf(r.Service), "DeleteVPC", func(_ *vpc.VPCService, _ string) error {
833830
return nil
834831
})
835-
patches.ApplyMethod(reflect.TypeOf(r.Service), "ListVPC", func(_ *vpc.VPCService) []model.Vpc {
836-
vpcPath := "/vpc/1"
837-
displayName := "fakeDisplayName"
838-
return []model.Vpc{{Path: &vpcPath, DisplayName: &displayName}}
839-
})
840-
patches.ApplyMethod(reflect.TypeOf(r.Service), "GetNetworkconfigNameFromNS", func(_ *vpc.VPCService, ctx context.Context, _ string) (string, error) {
841-
return "", nil
832+
patches.ApplyMethod(reflect.TypeOf(r.Service), "GetVPCNetworkConfigByNamespace", func(_ *vpc.VPCService, _ string) *servicecommon.VPCNetworkConfigInfo {
833+
return nil
842834
})
843835
return patches
844836
},
@@ -860,26 +852,37 @@ func TestNetworkInfoReconciler_deleteStaleVPCs(t *testing.T) {
860852
},
861853
},
862854
prepareFuncs: func(r *NetworkInfoReconciler) *gomonkey.Patches {
863-
patches := gomonkey.ApplyMethod(reflect.TypeOf(r.Service), "IsSharedVPCNamespaceByNS", func(_ *vpc.VPCService, ctx context.Context, _ string) (bool, error) {
864-
return false, nil
855+
patches := gomonkey.ApplyMethod(reflect.TypeOf(r.Service), "GetVPCsByNamespace", func(_ *vpc.VPCService, _ string) []*model.Vpc {
856+
return []*model.Vpc{
857+
{
858+
Tags: []model.Tag{
859+
{Scope: servicecommon.String(servicecommon.TagScopeNamespaceUID), Tag: servicecommon.String("vpc1")},
860+
},
861+
Path: servicecommon.String("/orgs/default/projects/default/vpcs/vpc1"),
862+
DisplayName: servicecommon.String("fakeDisplayName1"),
863+
}}
865864
})
866-
patches.ApplyMethod(reflect.TypeOf(r.Service), "GetVPCsByNamespace", func(_ *vpc.VPCService, _ string) []*model.Vpc {
867-
vpcPath1 := "/vpc/1"
868-
vpcPath2 := "/vpc/2"
869-
displayName1 := "fakeDisplayName1"
870-
displayName2 := "fakeDisplayName2"
871-
return []*model.Vpc{{Path: &vpcPath1, DisplayName: &displayName1}, {Path: &vpcPath2, DisplayName: &displayName2}}
865+
patches.ApplyPrivateMethod(reflect.TypeOf(r), "listNamespaceCRsNameIDSet", func(_ *NetworkInfoReconciler, _ context.Context) (sets.Set[string], sets.Set[string], error) {
866+
return sets.Set[string]{}, sets.Set[string]{}, nil
872867
})
873868
patches.ApplyMethod(reflect.TypeOf(r.Service), "DeleteVPC", func(_ *vpc.VPCService, _ string) error {
874869
return nil
875870
})
876-
patches.ApplyMethod(reflect.TypeOf(r.Service), "ListVPC", func(_ *vpc.VPCService) []model.Vpc {
877-
vpcPath := "/vpc/1"
878-
displayName := "fakeDisplayName1"
879-
return []model.Vpc{{Path: &vpcPath, DisplayName: &displayName}}
871+
patches.ApplyPrivateMethod(reflect.TypeOf(r), "listVPCsByNetworkConfigName", func(_ *NetworkInfoReconciler, _ string) []*model.Vpc {
872+
return []*model.Vpc{
873+
{
874+
Tags: []model.Tag{
875+
{Scope: servicecommon.String(servicecommon.TagScopeNamespaceUID), Tag: servicecommon.String("vpc1")},
876+
},
877+
Path: servicecommon.String("/orgs/default/projects/default/vpcs/vpc2"),
878+
DisplayName: servicecommon.String("fakeDisplayName2"),
879+
},
880+
}
880881
})
881-
patches.ApplyMethod(reflect.TypeOf(r.Service), "GetNetworkconfigNameFromNS", func(_ *vpc.VPCService, ctx context.Context, _ string) (string, error) {
882-
return "fakeNetworkconfigName", nil
882+
patches.ApplyMethod(reflect.TypeOf(r.Service), "GetVPCNetworkConfigByNamespace", func(_ *vpc.VPCService, _ string) *servicecommon.VPCNetworkConfigInfo {
883+
return &servicecommon.VPCNetworkConfigInfo{
884+
Name: "fakeNetworkconfigName",
885+
}
883886
})
884887
return patches
885888
},
@@ -902,7 +905,7 @@ func TestNetworkInfoReconciler_deleteStaleVPCs(t *testing.T) {
902905
defer patches.Reset()
903906
}
904907

905-
err := r.deleteVPCsByName(ctx, namespace)
908+
err := r.deleteVPCsByNamespace(ctx, namespace)
906909

907910
if tc.expectErrStr != "" {
908911
assert.ErrorContains(t, err, tc.expectErrStr)
@@ -926,28 +929,30 @@ func TestNetworkInfoReconciler_DeleteNetworkInfo(t *testing.T) {
926929
{
927930
name: "Delete NetworkInfo and Namespace not existed",
928931
existingNamespace: nil,
929-
expectErrStr: "",
930-
expectRes: common.ResultNormal,
931-
req: reconcile.Request{NamespacedName: types.NamespacedName{Namespace: "testNamespace", Name: "testNetworkInfo"}},
932+
prepareFuncs: func(r *NetworkInfoReconciler) *gomonkey.Patches {
933+
patches := gomonkey.ApplyMethod(reflect.TypeOf(r.Service), "GetVPCsByNamespace", func(_ *vpc.VPCService, _ string) []*model.Vpc {
934+
return []*model.Vpc{}
935+
})
936+
return patches
937+
},
938+
expectErrStr: "",
939+
expectRes: common.ResultNormal,
940+
req: reconcile.Request{NamespacedName: types.NamespacedName{Namespace: "testNamespace", Name: "testNetworkInfo"}},
932941
},
933942
{
934943
name: "Delete NetworkInfo with delete stale VPC error",
935944
existingNamespace: &corev1.Namespace{
936945
ObjectMeta: metav1.ObjectMeta{Name: "testNamespace", UID: "fakeNamespaceUID"},
937946
},
938947
prepareFuncs: func(r *NetworkInfoReconciler) *gomonkey.Patches {
939-
patches := gomonkey.ApplyMethod(reflect.TypeOf(r.Service.VpcStore), "GetByIndex", func(_ *vpc.VPCStore, key string, value string) []*model.Vpc {
940-
vpcName := "fakeVPCName"
941-
vpcPath := "fakeVPCPath"
948+
patches := gomonkey.ApplyMethod(reflect.TypeOf(r.Service), "GetVPCsByNamespace", func(_ *vpc.VPCService, _ string) []*model.Vpc {
942949
return []*model.Vpc{
943950
{
944-
DisplayName: &vpcName,
945-
Path: &vpcPath,
946-
},
947-
}
948-
})
949-
patches.ApplyMethod(reflect.TypeOf(r.Service), "IsSharedVPCNamespaceByNS", func(_ *vpc.VPCService, ctx context.Context, _ string) (bool, error) {
950-
return false, nil
951+
Tags: []model.Tag{
952+
{Scope: servicecommon.String(servicecommon.TagScopeNamespaceUID), Tag: servicecommon.String("vpc1")},
953+
},
954+
Path: servicecommon.String("/orgs/default/projects/default/vpcs/vpc1"),
955+
}}
951956
})
952957
patches.ApplyMethod(reflect.TypeOf(r.Service), "DeleteVPC", func(_ *vpc.VPCService, _ string) error {
953958
return fmt.Errorf("delete failed")
@@ -987,7 +992,7 @@ func TestNetworkInfoReconciler_DeleteNetworkInfo(t *testing.T) {
987992
Status: corev1.NamespaceStatus{},
988993
},
989994
prepareFuncs: func(r *NetworkInfoReconciler) *gomonkey.Patches {
990-
return gomonkey.ApplyMethod(reflect.TypeOf(r.Service.VpcStore), "GetByIndex", func(_ *vpc.VPCStore, key string, value string) []*model.Vpc {
995+
patches := gomonkey.ApplyMethod(reflect.TypeOf(r.Service.VpcStore), "GetByIndex", func(_ *vpc.VPCStore, key string, value string) []*model.Vpc {
991996
id := "fakeNamespaceUID"
992997
scope := servicecommon.TagScopeNamespaceUID
993998
tag1 := []model.Tag{
@@ -1004,6 +1009,10 @@ func TestNetworkInfoReconciler_DeleteNetworkInfo(t *testing.T) {
10041009
},
10051010
}
10061011
})
1012+
patches.ApplyPrivateMethod(reflect.TypeOf(r), "deleteVPCs", func(_ *NetworkInfoReconciler, _ context.Context, _ []*model.Vpc, ns string) error {
1013+
return fmt.Errorf("failed to get VPCNetworkConfiguration for Namespace when deleting stale VPCs")
1014+
})
1015+
return patches
10071016
},
10081017
expectErrStr: "failed to get VPCNetworkConfiguration for Namespace when deleting stale VPCs",
10091018
existingNetworkInfo: &v1alpha1.NetworkInfo{
@@ -1410,3 +1419,22 @@ func TestRegisterAllNetworkInfo(t *testing.T) {
14101419
err = r.RegisterAllNetworkInfo(context.Background())
14111420
assert.NoError(t, err)
14121421
}
1422+
1423+
func TestListVPCsByNetworkConfigName(t *testing.T) {
1424+
r := &NetworkInfoReconciler{
1425+
Service: &vpc.VPCService{},
1426+
}
1427+
patches := gomonkey.ApplyMethod(reflect.TypeOf(r.Service), "GetNamespacesByNetworkconfigName", func(_ *vpc.VPCService, _c string) []string {
1428+
return []string{"ns1", "ns2"}
1429+
})
1430+
patches.ApplyMethod(reflect.TypeOf(r.Service), "GetVPCsByNamespace", func(_ *vpc.VPCService, ns string) []*model.Vpc {
1431+
if ns == "ns1" {
1432+
return []*model.Vpc{{Id: servicecommon.String("vpc1")}}
1433+
}
1434+
return []*model.Vpc{{Id: servicecommon.String("vpc2")}}
1435+
})
1436+
defer patches.Reset()
1437+
expVPCs := []*model.Vpc{{Id: servicecommon.String("vpc1")}, {Id: servicecommon.String("vpc2")}}
1438+
actVPCs := r.listVPCsByNetworkConfigName("nc1")
1439+
assert.ElementsMatch(t, expVPCs, actVPCs)
1440+
}

0 commit comments

Comments
 (0)