Skip to content

Commit

Permalink
Subnet service refactor
Browse files Browse the repository at this point in the history
Initialize subnet service once then inject it to multiple reconcilers
instead of initializing service in different reconcilers with a lock
ensuring the initialization is invoked once.

tnqn#1 (comment)
  • Loading branch information
jwsui committed Jan 18, 2024
1 parent 43c35d9 commit d9ce4ec
Show file tree
Hide file tree
Showing 4 changed files with 12 additions and 26 deletions.
10 changes: 8 additions & 2 deletions cmd/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ import (
"github.com/vmware-tanzu/nsx-operator/pkg/nsx/services/ippool"
"github.com/vmware-tanzu/nsx-operator/pkg/nsx/services/nsxserviceaccount"
"github.com/vmware-tanzu/nsx-operator/pkg/nsx/services/securitypolicy"
subnetservice "github.com/vmware-tanzu/nsx-operator/pkg/nsx/services/subnet"
"github.com/vmware-tanzu/nsx-operator/pkg/nsx/services/vpc"
)

Expand Down Expand Up @@ -196,15 +197,20 @@ func main() {
log.Error(err, "failed to initialize vpc commonService", "controller", "VPC")
os.Exit(1)
}
subnetService, err := subnetservice.InitializeSubnetService(commonService)
if err != nil {
log.Error(err, "failed to initialize subnet commonService")
os.Exit(1)
}
commonctl.ServiceMediator.VPCService = vpcService
// Start controllers which only supports VPC
StartVPCController(mgr, vpcService)
StartNamespaceController(mgr, vpcService)
// Start subnet/subnetset controller.
if err := subnet.StartSubnetController(mgr, commonService); err != nil {
if err := subnet.StartSubnetController(mgr, subnetService); err != nil {
os.Exit(1)
}
if err := subnetset.StartSubnetSetController(mgr, commonService); err != nil {
if err := subnetset.StartSubnetSetController(mgr, subnetService); err != nil {
os.Exit(1)
}

Expand Down
4 changes: 2 additions & 2 deletions pkg/controllers/subnet/subnet_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -285,12 +285,12 @@ func (r *SubnetReconciler) setupWithManager(mgr ctrl.Manager) error {
Complete(r)
}

func StartSubnetController(mgr ctrl.Manager, commonService servicecommon.Service) error {
func StartSubnetController(mgr ctrl.Manager, subnetService *subnet.SubnetService) error {
subnetReconciler := &SubnetReconciler{
Client: mgr.GetClient(),
Scheme: mgr.GetScheme(),
}
subnetReconciler.Service = subnet.GetSubnetService(commonService)
subnetReconciler.Service = subnetService
common.ServiceMediator.SubnetService = subnetReconciler.Service
if err := subnetReconciler.Start(mgr); err != nil {
log.Error(err, "failed to create controller", "controller", "Subnet")
Expand Down
4 changes: 2 additions & 2 deletions pkg/controllers/subnetset/subnetset_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -300,12 +300,12 @@ func (r *SubnetSetReconciler) DeleteSubnetForSubnetSet(obj v1alpha1.SubnetSet, u
return nil
}

func StartSubnetSetController(mgr ctrl.Manager, commonService servicecommon.Service) error {
func StartSubnetSetController(mgr ctrl.Manager, subnetService *subnet.SubnetService) error {
subnetsetReconciler := &SubnetSetReconciler{
Client: mgr.GetClient(),
Scheme: mgr.GetScheme(),
}
subnetsetReconciler.Service = subnet.GetSubnetService(commonService)
subnetsetReconciler.Service = subnetService
if err := subnetsetReconciler.Start(mgr); err != nil {
log.Error(err, "failed to create controller", "controller", "Subnet")
return err
Expand Down
20 changes: 0 additions & 20 deletions pkg/nsx/services/subnet/subnet.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import (
"context"
"errors"
"fmt"
"os"
"sync"
"time"

Expand Down Expand Up @@ -44,25 +43,6 @@ type SubnetParameters struct {
VPCID string
}

var subnetService *SubnetService
var lock = &sync.Mutex{}

// GetSubnetService get singleton SubnetService instance, subnet/subnetset controller share the same instance.
func GetSubnetService(service common.Service) *SubnetService {
if subnetService == nil {
lock.Lock()
defer lock.Unlock()
if subnetService == nil {
var err error
if subnetService, err = InitializeSubnetService(service); err != nil {
log.Error(err, "failed to initialize subnet commonService")
os.Exit(1)
}
}
}
return subnetService
}

// InitializeSubnetService initialize Subnet service.
func InitializeSubnetService(service common.Service) (*SubnetService, error) {
wg := sync.WaitGroup{}
Expand Down

0 comments on commit d9ce4ec

Please sign in to comment.