Skip to content

Commit

Permalink
Reduce number of variable sources
Browse files Browse the repository at this point in the history
Signed-off-by: Mikalai Radchuk <[email protected]>
  • Loading branch information
m1kola committed Oct 27, 2023
1 parent 082dd0e commit f55d83b
Show file tree
Hide file tree
Showing 22 changed files with 1,562 additions and 2,328 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
27 changes: 17 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,19 @@ 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 {
// Using the fake Kubernetes client and creating objects
// in it from manifests & CLI flags is fine for PoC.
// But when/if we decide to proceed with CLI/offline resolution
// we will need to come up with a better way to create inputs
// for resolver when working with CLI.
//
// We will need to think about multiple types of inputs:
// - How to read required package (what we want to install/update)
// - How to read bundles from multiple catalogs
// - How to take into account cluster information. Some package
// will have constraints like "need Kubernetes version to be >= X"
// or "need >= 3 worker nodes").
clientBuilder := fake.NewClientBuilder().WithScheme(scheme)

if inputDir != "" {
Expand All @@ -124,10 +134,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
97 changes: 78 additions & 19 deletions cmd/resolutioncli/variable_source.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,28 +17,87 @@ limitations under the License.
package main

import (
"github.com/operator-framework/deppy/pkg/deppy/input"
"context"

"github.com/operator-framework/deppy/pkg/deppy"
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
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
}
5 changes: 3 additions & 2 deletions internal/controllers/operator_controller_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,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"
"github.com/operator-framework/operator-controller/pkg/features"
testutil "github.com/operator-framework/operator-controller/test/util"
)
Expand All @@ -44,7 +45,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 Expand Up @@ -1059,7 +1060,7 @@ func TestOperatorUpgrade(t *testing.T) {
reconciler := &controllers.OperatorReconciler{
Client: cl,
Scheme: sch,
Resolver: solver.NewDeppySolver(controllers.NewVariableSource(cl, &fakeCatalogClient)),
Resolver: solver.NewDeppySolver(variablesources.NewOLMVariableSource(cl, &fakeCatalogClient)),
}

t.Run("semver upgrade constraints", func(t *testing.T) {
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 f55d83b

Please sign in to comment.