Skip to content

Commit

Permalink
Adds bundle data snapshotting for resolution
Browse files Browse the repository at this point in the history
We now snapshot bundle data in the client for the duration of single resolution.

This reduces reads (from network or cache) and unmarshalling
since data in memory. And it also ensures that different variable sources
involved in the resolution process have the same view of catalogs and bundles.

Signed-off-by: Mikalai Radchuk <mradchuk@redhat.com>
  • Loading branch information
m1kola committed Oct 4, 2023
1 parent 47534eb commit a29337a
Show file tree
Hide file tree
Showing 4 changed files with 50 additions and 9 deletions.
8 changes: 5 additions & 3 deletions cmd/manager/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -112,9 +112,11 @@ func main() {
if err = (&controllers.OperatorReconciler{
Client: cl,
Scheme: mgr.GetScheme(),
Resolver: solver.NewDeppySolver(
controllers.NewVariableSource(cl, catalogClient),
),
NewSolver: func() *solver.DeppySolver {
return solver.NewDeppySolver(
controllers.NewVariableSource(cl, catalogclient.NewSnapshotClient(catalogClient)),
)
},
}).SetupWithManager(mgr); err != nil {
setupLog.Error(err, "unable to create controller", "controller", "Operator")
os.Exit(1)
Expand Down
28 changes: 28 additions & 0 deletions internal/catalogmetadata/client/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,34 @@ func (c *Client) Bundles(ctx context.Context) ([]*catalogmetadata.Bundle, error)
return allBundles, nil
}

func NewSnapshotClient(catalogClient *Client) *SnapshotClient {
return &SnapshotClient{
Client: catalogClient,
}
}

// SnapshotClient fetches data from catalogs and caches them for the lifetime of the
// SnapshotClient instance. Meaning that if new catalog or new bundles get created
// in between calls to the same instance of the client they will not appear in the result.
// This is convenient for bundle resolution process where we want all components
// to have the same view of catalogs.
type SnapshotClient struct {
*Client
bundlesSnapshot []*catalogmetadata.Bundle
}

func (c *SnapshotClient) Bundles(ctx context.Context) ([]*catalogmetadata.Bundle, error) {
if c.bundlesSnapshot == nil {
allBundles, err := c.Client.Bundles(ctx)
if err != nil {
return nil, err
}
c.bundlesSnapshot = allBundles
}

return c.bundlesSnapshot, nil
}

func PopulateExtraFields(catalogName string, channels []*catalogmetadata.Channel, bundles []*catalogmetadata.Bundle) ([]*catalogmetadata.Bundle, error) {
bundlesMap := map[string]*catalogmetadata.Bundle{}
for i := range bundles {
Expand Down
13 changes: 10 additions & 3 deletions internal/controllers/operator_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,11 +49,18 @@ import (
olmvariables "github.com/operator-framework/operator-controller/internal/resolution/variables"
)

// BundleProvider provides the Bundles method through which we can retrieve
// a list of Bundles from any source, generally from a catalog client of
// some kind.
type BundleProvider interface {
Bundles(ctx context.Context) ([]*catalogmetadata.Bundle, error)
}

// OperatorReconciler reconciles a Operator object
type OperatorReconciler struct {
client.Client
Scheme *runtime.Scheme
Resolver *solver.DeppySolver
Scheme *runtime.Scheme
NewSolver func() *solver.DeppySolver
}

//+kubebuilder:rbac:groups=operators.operatorframework.io,resources=operators,verbs=get;list;watch
Expand Down Expand Up @@ -130,7 +137,7 @@ func (r *OperatorReconciler) reconcile(ctx context.Context, op *operatorsv1alpha
return ctrl.Result{}, nil
}
// run resolution
solution, err := r.Resolver.Solve(ctx)
solution, err := r.NewSolver().Solve(ctx)
if err != nil {
op.Status.InstalledBundleResource = ""
setInstalledStatusConditionUnknown(&op.Status.Conditions, "installation has not been attempted as resolution failed", op.GetGeneration())
Expand Down
10 changes: 7 additions & 3 deletions internal/controllers/operator_controller_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,9 +37,13 @@ var _ = Describe("Operator Controller Test", func() {
ctx = context.Background()
fakeCatalogClient = testutil.NewFakeCatalogClient(testBundleList)
reconciler = &controllers.OperatorReconciler{
Client: cl,
Scheme: sch,
Resolver: solver.NewDeppySolver(controllers.NewVariableSource(cl, &fakeCatalogClient)),
Client: cl,
Scheme: sch,
NewSolver: func() *solver.DeppySolver {
return solver.NewDeppySolver(
controllers.NewVariableSource(cl, &fakeCatalogClient),
)
},
}
})
When("the operator does not exist", func() {
Expand Down

0 comments on commit a29337a

Please sign in to comment.