From c3b009d0e6e3ce65b823ffd7462c54d7741f2cd5 Mon Sep 17 00:00:00 2001 From: Don Browne Date: Thu, 2 May 2024 15:35:42 +0100 Subject: [PATCH] Use ProviderManager in reconciler Relates to #2845 Part of the ongoing decoupling of Provider-specific details from various parts of Minder. --- internal/reconcilers/reconcilers.go | 42 ++++++++--------------------- internal/reconcilers/repository.go | 39 +++++++++++---------------- internal/service/service.go | 2 +- 3 files changed, 27 insertions(+), 56 deletions(-) diff --git a/internal/reconcilers/reconcilers.go b/internal/reconcilers/reconcilers.go index 957cfc41bb..f6c196002e 100644 --- a/internal/reconcilers/reconcilers.go +++ b/internal/reconcilers/reconcilers.go @@ -17,52 +17,32 @@ package reconcilers import ( - gogithub "github.com/google/go-github/v61/github" - - serverconfig "github.com/stacklok/minder/internal/config/server" "github.com/stacklok/minder/internal/crypto" "github.com/stacklok/minder/internal/db" "github.com/stacklok/minder/internal/events" - "github.com/stacklok/minder/internal/providers/github" - "github.com/stacklok/minder/internal/providers/ratecache" - providertelemetry "github.com/stacklok/minder/internal/providers/telemetry" + "github.com/stacklok/minder/internal/providers/manager" ) // Reconciler is a helper that reconciles entities type Reconciler struct { - store db.Store - evt events.Publisher - crypteng crypto.Engine - restClientCache ratecache.RestClientCache - provCfg *serverconfig.ProviderConfig - provMt providertelemetry.ProviderMetrics - fallbackTokenClient *gogithub.Client + store db.Store + evt events.Publisher + crypteng crypto.Engine + providerManager manager.ProviderManager } // NewReconciler creates a new reconciler object func NewReconciler( store db.Store, evt events.Publisher, - authCfg *serverconfig.AuthConfig, - provCfg *serverconfig.ProviderConfig, - restClientCache ratecache.RestClientCache, - providerMetrics providertelemetry.ProviderMetrics, + cryptoEngine crypto.Engine, + providerManager manager.ProviderManager, ) (*Reconciler, error) { - crypteng, err := crypto.EngineFromAuthConfig(authCfg) - if err != nil { - return nil, err - } - - fallbackTokenClient := github.NewFallbackTokenClient(*provCfg) - return &Reconciler{ - store: store, - evt: evt, - crypteng: crypteng, - provCfg: provCfg, - fallbackTokenClient: fallbackTokenClient, - restClientCache: restClientCache, - provMt: providerMetrics, + store: store, + evt: evt, + crypteng: cryptoEngine, + providerManager: providerManager, }, nil } diff --git a/internal/reconcilers/repository.go b/internal/reconcilers/repository.go index 9c8a41acf5..8ea0cf2261 100644 --- a/internal/reconcilers/repository.go +++ b/internal/reconcilers/repository.go @@ -30,11 +30,11 @@ import ( "github.com/stacklok/minder/internal/db" "github.com/stacklok/minder/internal/engine/entities" - "github.com/stacklok/minder/internal/providers" "github.com/stacklok/minder/internal/providers/github" "github.com/stacklok/minder/internal/repositories" "github.com/stacklok/minder/internal/verifier/verifyif" pb "github.com/stacklok/minder/pkg/api/protobuf/go/minder/v1" + v1 "github.com/stacklok/minder/pkg/providers/v1" ) // RepoReconcilerEvent is an event that is sent to the reconciler topic @@ -93,25 +93,26 @@ func (r *Reconciler) handleArtifactsReconcilerEvent(ctx context.Context, evt *Re return fmt.Errorf("error retrieving repository: %w", err) } - prov, err := r.store.GetProviderByID(ctx, repository.ProviderID) + providerID := repository.ProviderID + p, err := r.providerManager.InstantiateFromID(ctx, providerID) if err != nil { - return fmt.Errorf("error retrieving provider: %w", err) + return fmt.Errorf("error instantiating provider: %w", err) } - pbOpts := []providers.ProviderBuilderOption{ - providers.WithProviderMetrics(r.provMt), - providers.WithRestClientCache(r.restClientCache), - } - p, err := providers.GetProviderBuilder(ctx, prov, r.store, r.crypteng, r.provCfg, r.fallbackTokenClient, pbOpts...) + cli, err := v1.As[v1.GitHub](p) if err != nil { - return fmt.Errorf("error building client: %w", err) + // Keeping this behaviour to match the existing logic + // This reconciler logic needs to be split between GitHub-specific + // and generic parts. + log.Printf("provider %s is not supported for artifacts reconciler", providerID) + return nil } // evaluate profile for repo repo := repositories.PBRepositoryFromDB(repository) err = entities.NewEntityInfoWrapper(). - WithProviderID(prov.ID). + WithProviderID(providerID). WithRepository(repo). WithProjectID(evt.Project). WithRepositoryID(repository.ID). @@ -120,16 +121,6 @@ func (r *Reconciler) handleArtifactsReconcilerEvent(ctx context.Context, evt *Re return fmt.Errorf("error publishing message: %w", err) } - if !p.Implements(db.ProviderTypeGithub) { - log.Printf("provider %s is not supported for artifacts reconciler", prov.Name) - return nil - } - - cli, err := p.GetGitHub() - if err != nil { - return fmt.Errorf("error getting github client: %w", err) - } - // todo: add another type of artifacts artifacts, err := cli.ListPackagesByRepository(ctx, repository.RepoOwner, string(verifyif.ArtifactTypeContainer), int64(repository.RepoID), 1, 100) @@ -142,7 +133,7 @@ func (r *Reconciler) handleArtifactsReconcilerEvent(ctx context.Context, evt *Re // not a hard error, just misconfiguration or the user doesn't want to put a token // into the provider config zerolog.Ctx(ctx).Info(). - Str("provider", prov.ID.String()). + Str("provider", providerID.String()). Msg("No package listing client available for provider") return nil } @@ -162,8 +153,8 @@ func (r *Reconciler) handleArtifactsReconcilerEvent(ctx context.Context, evt *Re ArtifactType: typeLower, ArtifactVisibility: artifact.GetVisibility(), ProjectID: evt.Project, - ProviderName: prov.Name, - ProviderID: prov.ID, + ProviderName: repository.Provider, + ProviderID: providerID, }) if err != nil { @@ -184,7 +175,7 @@ func (r *Reconciler) handleArtifactsReconcilerEvent(ctx context.Context, evt *Re CreatedAt: timestamppb.New(artifact.GetCreatedAt().Time), } err = entities.NewEntityInfoWrapper(). - WithProviderID(prov.ID). + WithProviderID(providerID). WithArtifact(pbArtifact). WithProjectID(evt.Project). WithArtifactID(newArtifact.ID). diff --git a/internal/service/service.go b/internal/service/service.go index 3d71660082..42ceefb9f0 100644 --- a/internal/service/service.go +++ b/internal/service/service.go @@ -161,7 +161,7 @@ func AllInOneServerService( evt.ConsumeEvents(exec) - rec, err := reconcilers.NewReconciler(store, evt, &cfg.Auth, &cfg.Provider, restClientCache, providerMetrics) + rec, err := reconcilers.NewReconciler(store, evt, cryptoEngine, providerManager) if err != nil { return fmt.Errorf("unable to create reconciler: %w", err) }