Skip to content

Commit

Permalink
Register all networkconfigurations before controller starting
Browse files Browse the repository at this point in the history
Previous networkconfiguration register depends on networkinfo events.
But other controller will depends on the map. Register all networkconfigurations
before controller starting

Test Done:
1. run in the env to check if all the networkconfigurations have been registered
  • Loading branch information
TaoZou1 committed Dec 12, 2024
1 parent d67f321 commit 499ab29
Show file tree
Hide file tree
Showing 2 changed files with 86 additions and 1 deletion.
27 changes: 26 additions & 1 deletion pkg/controllers/networkinfo/networkinfo_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -362,7 +362,11 @@ func (r *NetworkInfoReconciler) setupWithManager(mgr ctrl.Manager) error {

// Start setup manager and launch GC
func (r *NetworkInfoReconciler) Start(mgr ctrl.Manager) error {
err := r.setupWithManager(mgr)
err := r.RegisterAllNetworkInfo(context.Background())
if err != nil {
return err
}
err = r.setupWithManager(mgr)
if err != nil {
return err
}
Expand All @@ -373,6 +377,27 @@ func (r *NetworkInfoReconciler) Start(mgr ctrl.Manager) error {
return nil
}

func (r *NetworkInfoReconciler) RegisterAllNetworkInfo(ctx context.Context) error {
log.Info("Register all NetworkConfigurations")
var networkConfigList v1alpha1.VPCNetworkConfigurationList
if err := r.Client.List(ctx, &networkConfigList); err != nil {
log.Error(err, "Failed to list VPCNetworkConfigurations")
return err
}
for _, nconfig := range networkConfigList.Items {
vname := nconfig.GetName()
ninfo, err := buildNetworkConfigInfo(nconfig)
if err != nil {
log.Error(err, "build network config failed", "NetworkConfigInfo", vname)
return err
}
r.Service.RegisterVPCNetworkConfig(vname, *ninfo)
log.Info("Create network config and update to map", "NetworkConfigInfo", ninfo)
}
log.Info("Register all NetworkConfigurations successfully")
return nil
}

func (r *NetworkInfoReconciler) listNamespaceCRsNameIDSet(ctx context.Context) (sets.Set[string], sets.Set[string], error) {
// read all Namespaces from K8s
namespaces := &corev1.NamespaceList{}
Expand Down
60 changes: 60 additions & 0 deletions pkg/controllers/networkinfo/networkinfo_controller_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1350,3 +1350,63 @@ func TestSyncPreCreatedVpcIPs(t *testing.T) {
requests := getQueuedReqs(r.queue)
assert.ElementsMatch(t, expRequests, requests)
}

func TestRegisterAllNetworkInfo(t *testing.T) {
// list return nil
r := createNetworkInfoReconciler(nil)
err := r.RegisterAllNetworkInfo(context.Background())
assert.NoError(t, err)

mockClient := mock_client.NewMockClient(gomock.NewController(t))
r.Client = mockClient

// list return error
mockClient.EXPECT().List(gomock.Any(), gomock.Any(), gomock.Any()).Return(errors.New("list error"))
err = r.RegisterAllNetworkInfo(context.Background())
assert.Equal(t, err, errors.New("list error"))

// list return network config which path is invalid
networkConfigList := v1alpha1.VPCNetworkConfigurationList{
Items: []v1alpha1.VPCNetworkConfiguration{
{
// Populate with necessary fields
ObjectMeta: metav1.ObjectMeta{
Name: "vpc-name",
},
},
},
}
mockClient.EXPECT().List(gomock.Any(), gomock.Any(), gomock.Any()).DoAndReturn(
func(ctx context.Context, list client.ObjectList, opts ...client.ListOption) error {
// Set the value of the list parameter
*list.(*v1alpha1.VPCNetworkConfigurationList) = networkConfigList
return nil
},
)
err = r.RegisterAllNetworkInfo(context.Background())
assert.Equal(t, err, errors.New("invalid NSX project path"))

// list return network config which path is valid
networkConfigList = v1alpha1.VPCNetworkConfigurationList{
Items: []v1alpha1.VPCNetworkConfiguration{
{
// Populate with necessary fields
ObjectMeta: metav1.ObjectMeta{
Name: "vpc-name",
},
Spec: v1alpha1.VPCNetworkConfigurationSpec{
NSXProject: "/orgs/defautl/projects/project-quality/vpcs/vpc-test",
},
},
},
}
mockClient.EXPECT().List(gomock.Any(), gomock.Any(), gomock.Any()).DoAndReturn(
func(ctx context.Context, list client.ObjectList, opts ...client.ListOption) error {
// Set the value of the list parameter
*list.(*v1alpha1.VPCNetworkConfigurationList) = networkConfigList
return nil
},
)
err = r.RegisterAllNetworkInfo(context.Background())
assert.NoError(t, err)
}

0 comments on commit 499ab29

Please sign in to comment.