Skip to content

Commit

Permalink
Use ProviderManager in reconciler
Browse files Browse the repository at this point in the history
Relates to #2845

Part of the ongoing decoupling of Provider-specific details from various
parts of Minder.
  • Loading branch information
dmjb committed May 2, 2024
1 parent 7fc9740 commit c3b009d
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 56 deletions.
42 changes: 11 additions & 31 deletions internal/reconcilers/reconcilers.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

Expand Down
39 changes: 15 additions & 24 deletions internal/reconcilers/repository.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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).
Expand All @@ -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)
Expand All @@ -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
}
Expand All @@ -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 {
Expand All @@ -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).
Expand Down
2 changes: 1 addition & 1 deletion internal/service/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
Expand Down

0 comments on commit c3b009d

Please sign in to comment.