diff --git a/pkg/controllers/networkinfo/networkinfo_controller.go b/pkg/controllers/networkinfo/networkinfo_controller.go index 19b69db9a..23a3da1ce 100644 --- a/pkg/controllers/networkinfo/networkinfo_controller.go +++ b/pkg/controllers/networkinfo/networkinfo_controller.go @@ -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 } @@ -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{} diff --git a/pkg/controllers/networkinfo/networkinfo_controller_test.go b/pkg/controllers/networkinfo/networkinfo_controller_test.go index 33f2df288..816b9583c 100644 --- a/pkg/controllers/networkinfo/networkinfo_controller_test.go +++ b/pkg/controllers/networkinfo/networkinfo_controller_test.go @@ -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) +}