From 6b382219169e9c1b8c1dc0d76aaf63f8c17a5a95 Mon Sep 17 00:00:00 2001 From: Richard Case Date: Mon, 22 Aug 2022 16:11:05 +0100 Subject: [PATCH] refactor: moved resync to controller The resynce functionality has been moved to the controller. This means the core application know how to do CRUD on the vmspecs and how to reconcile a single vm spec. Functionality such as resynce can be orchestrated via calls to the core app. Signed-off-by: Richard Case --- core/application/reconcile.go | 22 ------------------- core/ports/usecases.go | 3 --- .../controllers/microvm_controller.go | 16 ++++++++++---- .../controllers/microvm_controller_test.go | 3 ++- internal/inject/wire.go | 2 +- internal/inject/wire_gen.go | 3 ++- 6 files changed, 17 insertions(+), 32 deletions(-) diff --git a/core/application/reconcile.go b/core/application/reconcile.go index 63bc9d41..f7456447 100644 --- a/core/application/reconcile.go +++ b/core/application/reconcile.go @@ -35,28 +35,6 @@ func (a *app) ReconcileMicroVM(ctx context.Context, vmid models.VMID) error { return a.reconcile(ctx, spec, logger) } -func (a *app) ResyncMicroVMs(ctx context.Context, namespace string) error { - logger := log.GetLogger(ctx).WithFields(logrus.Fields{ - "action": "resync", - "namespace": "ns", - }) - logger.Info("Resyncing specs") - logger.Debug("Getting all specs") - - specs, err := a.ports.Repo.GetAll(ctx, models.ListMicroVMQuery{"namespace": namespace}) - if err != nil { - return fmt.Errorf("getting all microvm specs for resync: %w", err) - } - - for _, spec := range specs { - if err := a.reconcile(ctx, spec, logger); err != nil { - return fmt.Errorf("resync reconcile for spec %s: %w", spec.ID, err) - } - } - - return nil -} - func (a *app) plan(spec *models.MicroVM, logger *logrus.Entry) planner.Plan { l := logger.WithField("stage", "plan") l.Info("Generate plan") diff --git a/core/ports/usecases.go b/core/ports/usecases.go index 35ce8805..203145b2 100644 --- a/core/ports/usecases.go +++ b/core/ports/usecases.go @@ -26,7 +26,4 @@ type MicroVMQueryUseCases interface { type ReconcileMicroVMsUseCase interface { // ReconcileMicroVM is a use case for reconciling a specific microvm. ReconcileMicroVM(ctx context.Context, vmid models.VMID) error - // ResyncMicroVMs is used to resync the microvms. If a namespace is supplied then it will - // resync only the microvms in that namespaces. - ResyncMicroVMs(ctx context.Context, namespace string) error } diff --git a/infrastructure/controllers/microvm_controller.go b/infrastructure/controllers/microvm_controller.go index 6a36157f..878b9f59 100644 --- a/infrastructure/controllers/microvm_controller.go +++ b/infrastructure/controllers/microvm_controller.go @@ -16,10 +16,11 @@ import ( "github.com/weaveworks-liquidmetal/flintlock/pkg/queue" ) -func New(eventSvc ports.EventService, reconcileUC ports.ReconcileMicroVMsUseCase) *MicroVMController { +func New(eventSvc ports.EventService, reconcileUC ports.ReconcileMicroVMsUseCase, queryUC ports.MicroVMQueryUseCases) *MicroVMController { return &MicroVMController{ eventSvc: eventSvc, reconcileUC: reconcileUC, + queryUC: queryUC, queue: queue.NewSimpleSyncQueue(), } } @@ -27,6 +28,7 @@ func New(eventSvc ports.EventService, reconcileUC ports.ReconcileMicroVMsUseCase type MicroVMController struct { eventSvc ports.EventService reconcileUC ports.ReconcileMicroVMsUseCase + queryUC ports.MicroVMQueryUseCases queue queue.Queue } @@ -186,11 +188,17 @@ func (r *MicroVMController) handleEvent(envelope *ports.EventEnvelope, logger *l func (r *MicroVMController) resyncSpecs(ctx context.Context, logger *logrus.Entry) error { logger.Info("resyncing microvm specs") - err := r.reconcileUC.ResyncMicroVMs(ctx, "") + specs, err := r.queryUC.GetAllMicroVM(ctx, models.ListMicroVMQuery{ + "Namespace": "", + }) + if err != nil { - logger.Errorf("failed to resync microvms: %s", err) + return fmt.Errorf("getting all microvm specs for resync: %w", err) + } - return fmt.Errorf("resyncing microvms: %w", err) + for _, spec := range specs { + logger.Debugf("enqueing vmid %s for resync", spec.ID) + r.queue.Enqueue(spec.ID.String()) } return nil diff --git a/infrastructure/controllers/microvm_controller_test.go b/infrastructure/controllers/microvm_controller_test.go index cea4aaa7..8a5da0bd 100644 --- a/infrastructure/controllers/microvm_controller_test.go +++ b/infrastructure/controllers/microvm_controller_test.go @@ -120,13 +120,14 @@ func TestMicroVMController(t *testing.T) { em := mock.NewMockEventService(mockCtrl) uc := mock.NewMockReconcileMicroVMsUseCase(mockCtrl) + quc := mock.NewMockMicroVMQueryUseCases(mockCtrl) evtCh := make(chan *ports.EventEnvelope) evtErrCh := make(chan error, 1) tc.expect(em.EXPECT(), uc.EXPECT(), evtCh, evtErrCh) - controller := controllers.New(em, uc) + controller := controllers.New(em, uc, quc) ctrlWG := sync.WaitGroup{} ctrlWG.Add(1) diff --git a/internal/inject/wire.go b/internal/inject/wire.go index 8f21aa0f..60850809 100644 --- a/internal/inject/wire.go +++ b/internal/inject/wire.go @@ -47,7 +47,7 @@ func InitializeApp(cfg *config.Config, ports *ports.Collection) application.App } func InializeController(app application.App, ports *ports.Collection) *controllers.MicroVMController { - wire.Build(controllers.New, eventSvcFromScope, reconcileUCFromApp) + wire.Build(controllers.New, eventSvcFromScope, reconcileUCFromApp, queryUCFromApp) return nil } diff --git a/internal/inject/wire_gen.go b/internal/inject/wire_gen.go index 4b11d81e..192063ad 100644 --- a/internal/inject/wire_gen.go +++ b/internal/inject/wire_gen.go @@ -59,7 +59,8 @@ func InitializeApp(cfg *config.Config, ports2 *ports.Collection) application.App func InializeController(app application.App, ports2 *ports.Collection) *controllers.MicroVMController { eventService := eventSvcFromScope(ports2) reconcileMicroVMsUseCase := reconcileUCFromApp(app) - microVMController := controllers.New(eventService, reconcileMicroVMsUseCase) + microVMQueryUseCases := queryUCFromApp(app) + microVMController := controllers.New(eventService, reconcileMicroVMsUseCase, microVMQueryUseCases) return microVMController }