Skip to content

Commit

Permalink
wip: fix main
Browse files Browse the repository at this point in the history
  • Loading branch information
ushitora-anqou committed Aug 27, 2024
1 parent 7c57db5 commit 337687f
Showing 1 changed file with 45 additions and 27 deletions.
72 changes: 45 additions & 27 deletions cmd/controller/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -107,43 +107,20 @@ func checkCommandlineArgs() error {
return nil
}

func setupPrimary(ctx context.Context, mgr manager.Manager, wg *sync.WaitGroup) error {
func setupReconcilers(mgr manager.Manager, primarySettings *controller.PrimarySettings) error {
managedCephClusterID := os.Getenv("POD_NAMESPACE")
if managedCephClusterID == "" {
setupLog.Error(errors.New("POD_NAMESPACE is empty"), "POD_NAMESPACE is empty")
return errors.New("POD_NAMESPACE is empty")
}

conn, err := grpc.NewClient(
mantleServiceEndpoint,
grpc.WithTransportCredentials(insecure.NewCredentials()),
grpc.WithKeepaliveParams(keepalive.ClientParameters{
Time: 1 * time.Minute,
}),
)
if err != nil {
setupLog.Error(err, "failed to create a new client for the secondary mantle")
return err
}
go func() {
defer wg.Done()
<-ctx.Done()
_ = conn.Close()
}()

primarySettings := &controller.PrimarySettings{
ServiceEndpoint: mantleServiceEndpoint,
Conn: conn,
Client: proto.NewMantleServiceClient(conn),
}

backupReconciler := controller.NewMantleBackupReconciler(
mgr.GetClient(),
mgr.GetScheme(),
managedCephClusterID,
primarySettings,
)
if err = backupReconciler.SetupWithManager(mgr); err != nil {
if err := backupReconciler.SetupWithManager(mgr); err != nil {
setupLog.Error(err, "unable to create controller", "controller", "MantleBackup")
return err
}
Expand All @@ -153,11 +130,11 @@ func setupPrimary(ctx context.Context, mgr manager.Manager, wg *sync.WaitGroup)
mgr.GetScheme(),
managedCephClusterID,
)
if err = restoreReconciler.SetupWithManager(mgr); err != nil {
if err := restoreReconciler.SetupWithManager(mgr); err != nil {
setupLog.Error(err, "unable to create controller", "controller", "MantleRestore")
return err
}
if err = controller.NewMantleBackupConfigReconciler(
if err := controller.NewMantleBackupConfigReconciler(
mgr.GetClient(),
mgr.GetScheme(),
managedCephClusterID,
Expand All @@ -172,6 +149,43 @@ func setupPrimary(ctx context.Context, mgr manager.Manager, wg *sync.WaitGroup)
return nil
}

func setupStandalone(mgr manager.Manager) error {
return setupReconcilers(mgr, nil)
}

func setupPrimary(ctx context.Context, mgr manager.Manager, wg *sync.WaitGroup) error {
managedCephClusterID := os.Getenv("POD_NAMESPACE")
if managedCephClusterID == "" {
setupLog.Error(errors.New("POD_NAMESPACE is empty"), "POD_NAMESPACE is empty")
return errors.New("POD_NAMESPACE is empty")
}

conn, err := grpc.NewClient(
mantleServiceEndpoint,
grpc.WithTransportCredentials(insecure.NewCredentials()),
grpc.WithKeepaliveParams(keepalive.ClientParameters{
Time: 1 * time.Minute,
}),
)
if err != nil {
setupLog.Error(err, "failed to create a new client for the secondary mantle")
return err
}
go func() {
defer wg.Done()
<-ctx.Done()
_ = conn.Close()
}()

primarySettings := &controller.PrimarySettings{
ServiceEndpoint: mantleServiceEndpoint,
Conn: conn,
Client: proto.NewMantleServiceClient(conn),
}

return setupReconcilers(mgr, primarySettings)
}

func setupSecondary(ctx context.Context, wg *sync.WaitGroup, cancel context.CancelFunc) error {
logger := ctrl.Log.WithName("grpc")

Expand Down Expand Up @@ -244,6 +258,10 @@ func subMain() error {
defer cancel()

switch role {
case RoleStandalone:
if err := setupStandalone(mgr); err != nil {
return err
}
case RolePrimary:
if err := setupPrimary(ctx, mgr, &wg); err != nil {
return err
Expand Down

0 comments on commit 337687f

Please sign in to comment.