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 6808a50 commit 42f2555
Show file tree
Hide file tree
Showing 4 changed files with 47 additions and 11 deletions.
9 changes: 4 additions & 5 deletions cmd/manager/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -110,11 +110,10 @@ func main() {
catalogClient := catalogclient.New(cl, cache.NewFilesystemCache(cachePath, &http.Client{Timeout: 10 * time.Second}))

if err = (&controllers.OperatorReconciler{
Client: cl,
Scheme: mgr.GetScheme(),
Resolver: solver.NewDeppySolver(
controllers.NewVariableSource(cl, catalogClient),
),
Client: cl,
Scheme: mgr.GetScheme(),
CatalogClient: catalogClient,
NewSolver: solver.NewDeppySolver,
}).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
10 changes: 7 additions & 3 deletions internal/controllers/operator_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ import (
"github.com/go-logr/logr"
catalogd "github.com/operator-framework/catalogd/api/core/v1alpha1"
"github.com/operator-framework/deppy/pkg/deppy"
"github.com/operator-framework/deppy/pkg/deppy/input"
"github.com/operator-framework/deppy/pkg/deppy/solver"
rukpakv1alpha1 "github.com/operator-framework/rukpak/api/v1alpha1"
"k8s.io/apimachinery/pkg/api/equality"
Expand All @@ -45,15 +46,17 @@ import (

operatorsv1alpha1 "github.com/operator-framework/operator-controller/api/v1alpha1"
"github.com/operator-framework/operator-controller/internal/catalogmetadata"
catalogclient "github.com/operator-framework/operator-controller/internal/catalogmetadata/client"
"github.com/operator-framework/operator-controller/internal/controllers/validators"
olmvariables "github.com/operator-framework/operator-controller/internal/resolution/variables"
)

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

//+kubebuilder:rbac:groups=operators.operatorframework.io,resources=operators,verbs=get;list;watch
Expand Down Expand Up @@ -130,7 +133,8 @@ func (r *OperatorReconciler) reconcile(ctx context.Context, op *operatorsv1alpha
return ctrl.Result{}, nil
}
// run resolution
solution, err := r.Resolver.Solve(ctx)
variableSource := NewVariableSource(r.Client, catalogclient.NewSnapshotClient(r.CatalogClient))
solution, err := r.NewSolver(variableSource).Solve(ctx)
if err != nil {
op.Status.InstalledBundleResource = ""
setInstalledStatusConditionUnknown(&op.Status.Conditions, "installation has not been attempted as resolution failed", op.GetGeneration())
Expand Down
11 changes: 8 additions & 3 deletions internal/controllers/operator_controller_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (

. "github.com/onsi/ginkgo/v2"
. "github.com/onsi/gomega"
"github.com/operator-framework/deppy/pkg/deppy/input"
"github.com/operator-framework/deppy/pkg/deppy/solver"
"github.com/operator-framework/operator-registry/alpha/declcfg"
"github.com/operator-framework/operator-registry/alpha/property"
Expand Down Expand Up @@ -37,9 +38,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(variableSource input.VariableSource) *solver.DeppySolver {
return solver.NewDeppySolver(
controllers.NewVariableSource(cl, &fakeCatalogClient),
)
},
}
})
When("the operator does not exist", func() {
Expand Down

0 comments on commit 42f2555

Please sign in to comment.