Skip to content

Commit

Permalink
PoC: refactor variable sources
Browse files Browse the repository at this point in the history
Signed-off-by: Mikalai Radchuk <mradchuk@redhat.com>
  • Loading branch information
m1kola committed Oct 17, 2023
1 parent 7161a7b commit 9acd1a9
Show file tree
Hide file tree
Showing 22 changed files with 1,032 additions and 2,142 deletions.
3 changes: 2 additions & 1 deletion cmd/manager/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ import (
"github.com/operator-framework/operator-controller/internal/catalogmetadata/cache"
catalogclient "github.com/operator-framework/operator-controller/internal/catalogmetadata/client"
"github.com/operator-framework/operator-controller/internal/controllers"
"github.com/operator-framework/operator-controller/internal/resolution/variablesources"
"github.com/operator-framework/operator-controller/pkg/features"
)

Expand Down Expand Up @@ -113,7 +114,7 @@ func main() {
Client: cl,
Scheme: mgr.GetScheme(),
Resolver: solver.NewDeppySolver(
controllers.NewVariableSource(cl, catalogClient),
variablesources.NewOLMVariableSource(cl, catalogClient),
),
}).SetupWithManager(mgr); err != nil {
setupLog.Error(err, "unable to create controller", "controller", "Operator")
Expand Down
15 changes: 5 additions & 10 deletions cmd/resolutioncli/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,9 +35,7 @@ import (

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

const pocMessage = `This command is a proof of concept for off-cluster resolution and is not intended for production use!
Expand Down Expand Up @@ -71,12 +69,12 @@ func main() {
ctx := context.Background()

var packageName string
var packageVersion string
var packageVersionRange string
var packageChannel string
var indexRef string
var inputDir string
flag.StringVar(&packageName, flagNamePackageName, "", "Name of the package to resolve")
flag.StringVar(&packageVersion, flagNamePackageVersion, "", "Version of the package")
flag.StringVar(&packageVersionRange, flagNamePackageVersion, "", "Version or version range of the package")
flag.StringVar(&packageChannel, flagNamePackageChannel, "", "Channel of the package")
// TODO: Consider adding support of multiple refs
flag.StringVar(&indexRef, flagNameIndexRef, "", "Index reference (FBC image or dir)")
Expand All @@ -89,7 +87,7 @@ func main() {
os.Exit(1)
}

err := run(ctx, packageName, packageVersion, packageChannel, indexRef, inputDir)
err := run(ctx, packageName, packageChannel, packageVersionRange, indexRef, inputDir)
if err != nil {
fmt.Fprintln(os.Stderr, err)
os.Exit(1)
Expand All @@ -108,7 +106,7 @@ func validateFlags(packageName, indexRef string) error {
return nil
}

func run(ctx context.Context, packageName, packageVersion, packageChannel, indexRef, inputDir string) error {
func run(ctx context.Context, packageName, packageChannel, packageVersionRange, indexRef, inputDir string) error {
clientBuilder := fake.NewClientBuilder().WithScheme(scheme)

if inputDir != "" {
Expand All @@ -124,10 +122,7 @@ func run(ctx context.Context, packageName, packageVersion, packageChannel, index
catalogClient := newIndexRefClient(indexRef)

resolver := solver.NewDeppySolver(
append(
variablesources.NestedVariableSource{newPackageVariableSource(catalogClient, packageName, packageVersion, packageChannel)},
controllers.NewVariableSource(cl, catalogClient)...,
),
NewOfflineOLMVariableSource(cl, catalogClient, packageName, packageChannel, packageVersionRange),
)

bundleImage, err := resolve(ctx, resolver, packageName)
Expand Down
98 changes: 80 additions & 18 deletions cmd/resolutioncli/variable_source.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,28 +17,90 @@ limitations under the License.
package main

import (
"context"

"github.com/operator-framework/deppy/pkg/deppy"
"github.com/operator-framework/deppy/pkg/deppy/input"
rukpakv1alpha1 "github.com/operator-framework/rukpak/api/v1alpha1"
"sigs.k8s.io/controller-runtime/pkg/client"

operatorsv1alpha1 "github.com/operator-framework/operator-controller/api/v1alpha1"
olmvariables "github.com/operator-framework/operator-controller/internal/resolution/variables"
"github.com/operator-framework/operator-controller/internal/resolution/variablesources"
)

func newPackageVariableSource(catalogClient *indexRefClient, packageName, packageVersion, packageChannel string) func(inputVariableSource input.VariableSource) (input.VariableSource, error) {
return func(inputVariableSource input.VariableSource) (input.VariableSource, error) {
pkgSource, err := variablesources.NewRequiredPackageVariableSource(
catalogClient,
packageName,
variablesources.InVersionRange(packageVersion),
variablesources.InChannel(packageChannel),
)
if err != nil {
return nil, err
}

sliceSource := variablesources.SliceVariableSource{pkgSource}
if inputVariableSource != nil {
sliceSource = append(sliceSource, inputVariableSource)
}

return sliceSource, nil
var _ input.VariableSource = &OfflineOLMVariableSource{}

type OfflineOLMVariableSource struct {
client client.Client
catalogClient *indexRefClient

packageName string
packageChannel string
packageVersionRange string
}

func NewOfflineOLMVariableSource(cl client.Client, catalogClient *indexRefClient, packageName, packageChannel, packageVersionRange string) *OfflineOLMVariableSource {
return &OfflineOLMVariableSource{
client: cl,
catalogClient: catalogClient,

packageName: packageName,
packageChannel: packageChannel,
packageVersionRange: packageVersionRange,
}
}

func (o *OfflineOLMVariableSource) GetVariables(ctx context.Context) ([]deppy.Variable, error) {
operatorList := operatorsv1alpha1.OperatorList{}
if err := o.client.List(ctx, &operatorList); err != nil {
return nil, err
}

bundleDeployments := rukpakv1alpha1.BundleDeploymentList{}
if err := o.client.List(ctx, &bundleDeployments); err != nil {
return nil, err
}

allBundles, err := o.catalogClient.Bundles(ctx)
if err != nil {
return nil, err
}

requiredPackages := []*olmvariables.RequiredPackageVariable{}
requiredPackage, err := variablesources.RequiredPackageVariable(allBundles, o.packageName, o.packageChannel, o.packageVersionRange)
if err != nil {
return nil, err
}
requiredPackages = append(requiredPackages, requiredPackage)

installedPackages, err := variablesources.InstalledPackageVariables(allBundles, bundleDeployments.Items)
if err != nil {
return nil, err
}

bundles, err := variablesources.BundleVariables(allBundles, requiredPackages, installedPackages)
if err != nil {
return nil, err
}

bundleUniqueness, err := variablesources.BundleUniquenessVariables(bundles)
if err != nil {
return nil, err
}

result := []deppy.Variable{}
for _, v := range requiredPackages {
result = append(result, v)
}
for _, v := range installedPackages {
result = append(result, v)
}
for _, v := range bundles {
result = append(result, v)
}
for _, v := range bundleUniqueness {
result = append(result, v)
}
return result, nil
}
3 changes: 2 additions & 1 deletion internal/controllers/operator_controller_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ import (
"github.com/operator-framework/operator-controller/internal/catalogmetadata"
"github.com/operator-framework/operator-controller/internal/conditionsets"
"github.com/operator-framework/operator-controller/internal/controllers"
"github.com/operator-framework/operator-controller/internal/resolution/variablesources"
testutil "github.com/operator-framework/operator-controller/test/util"
)

Expand All @@ -39,7 +40,7 @@ var _ = Describe("Operator Controller Test", func() {
reconciler = &controllers.OperatorReconciler{
Client: cl,
Scheme: sch,
Resolver: solver.NewDeppySolver(controllers.NewVariableSource(cl, &fakeCatalogClient)),
Resolver: solver.NewDeppySolver(variablesources.NewOLMVariableSource(cl, &fakeCatalogClient)),
}
})
When("the operator does not exist", func() {
Expand Down
42 changes: 0 additions & 42 deletions internal/controllers/variable_source.go

This file was deleted.

57 changes: 0 additions & 57 deletions internal/resolution/variablesources/bundle_deployment.go

This file was deleted.

Loading

0 comments on commit 9acd1a9

Please sign in to comment.