Skip to content

Commit

Permalink
Merge pull request #954 from TaoZou1/initnetconfig
Browse files Browse the repository at this point in the history
Register all networkconfigurations before controller starting
  • Loading branch information
TaoZou1 authored Dec 13, 2024
2 parents 1b65091 + 499ab29 commit 7c783d4
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 7c783d4

Please sign in to comment.