Skip to content

Commit 37eee73

Browse files
author
Mikalai Radchuk
committed
Reduce number of variable sources
Signed-off-by: Mikalai Radchuk <[email protected]>
1 parent a5baf62 commit 37eee73

27 files changed

+1196
-1900
lines changed

cmd/manager/main.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@ import (
4040
"github.com/operator-framework/operator-controller/internal/catalogmetadata/cache"
4141
catalogclient "github.com/operator-framework/operator-controller/internal/catalogmetadata/client"
4242
"github.com/operator-framework/operator-controller/internal/controllers"
43+
"github.com/operator-framework/operator-controller/internal/resolution/variablesources"
4344
"github.com/operator-framework/operator-controller/pkg/features"
4445
)
4546

@@ -113,7 +114,7 @@ func main() {
113114
Client: cl,
114115
Scheme: mgr.GetScheme(),
115116
Resolver: solver.NewDeppySolver(
116-
controllers.NewVariableSource(cl, catalogClient),
117+
variablesources.NewOLMVariableSource(cl, catalogClient),
117118
),
118119
}).SetupWithManager(mgr); err != nil {
119120
setupLog.Error(err, "unable to create controller", "controller", "Operator")

cmd/resolutioncli/main.go

Lines changed: 31 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,7 @@ import (
2222
"fmt"
2323
"os"
2424

25-
"github.com/operator-framework/deppy/pkg/deppy/solver"
26-
rukpakv1alpha1 "github.com/operator-framework/rukpak/api/v1alpha1"
25+
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
2726
"k8s.io/apimachinery/pkg/runtime"
2827
"k8s.io/apimachinery/pkg/runtime/serializer"
2928
utilruntime "k8s.io/apimachinery/pkg/util/runtime"
@@ -32,10 +31,11 @@ import (
3231
"sigs.k8s.io/controller-runtime/pkg/client/fake"
3332

3433
catalogd "github.com/operator-framework/catalogd/api/core/v1alpha1"
34+
"github.com/operator-framework/deppy/pkg/deppy/solver"
35+
rukpakv1alpha1 "github.com/operator-framework/rukpak/api/v1alpha1"
3536

3637
operatorsv1alpha1 "github.com/operator-framework/operator-controller/api/v1alpha1"
3738
"github.com/operator-framework/operator-controller/internal/catalogmetadata"
38-
"github.com/operator-framework/operator-controller/internal/controllers"
3939
olmvariables "github.com/operator-framework/operator-controller/internal/resolution/variables"
4040
"github.com/operator-framework/operator-controller/internal/resolution/variablesources"
4141
)
@@ -71,12 +71,12 @@ func main() {
7171
ctx := context.Background()
7272

7373
var packageName string
74-
var packageVersion string
74+
var packageVersionRange string
7575
var packageChannel string
7676
var indexRef string
7777
var inputDir string
7878
flag.StringVar(&packageName, flagNamePackageName, "", "Name of the package to resolve")
79-
flag.StringVar(&packageVersion, flagNamePackageVersion, "", "Version of the package")
79+
flag.StringVar(&packageVersionRange, flagNamePackageVersion, "", "Version or version range of the package")
8080
flag.StringVar(&packageChannel, flagNamePackageChannel, "", "Channel of the package")
8181
// TODO: Consider adding support of multiple refs
8282
flag.StringVar(&indexRef, flagNameIndexRef, "", "Index reference (FBC image or dir)")
@@ -89,7 +89,7 @@ func main() {
8989
os.Exit(1)
9090
}
9191

92-
err := run(ctx, packageName, packageVersion, packageChannel, indexRef, inputDir)
92+
err := run(ctx, packageName, packageChannel, packageVersionRange, indexRef, inputDir)
9393
if err != nil {
9494
fmt.Fprintln(os.Stderr, err)
9595
os.Exit(1)
@@ -108,7 +108,19 @@ func validateFlags(packageName, indexRef string) error {
108108
return nil
109109
}
110110

111-
func run(ctx context.Context, packageName, packageVersion, packageChannel, indexRef, inputDir string) error {
111+
func run(ctx context.Context, packageName, packageChannel, packageVersionRange, indexRef, inputDir string) error {
112+
// Using the fake Kubernetes client and creating objects
113+
// in it from manifests & CLI flags is fine for PoC.
114+
// But when/if we decide to proceed with CLI/offline resolution
115+
// we will need to come up with a better way to create inputs
116+
// for resolver when working with CLI.
117+
//
118+
// We will need to think about multiple types of inputs:
119+
// - How to read required package (what we want to install/update)
120+
// - How to read bundles from multiple catalogs
121+
// - How to take into account cluster information. Some package
122+
// will have constraints like "need Kubernetes version to be >= X"
123+
// or "need >= 3 worker nodes").
112124
clientBuilder := fake.NewClientBuilder().WithScheme(scheme)
113125

114126
if inputDir != "" {
@@ -120,14 +132,22 @@ func run(ctx context.Context, packageName, packageVersion, packageChannel, index
120132
clientBuilder.WithRuntimeObjects(objects...)
121133
}
122134

135+
clientBuilder.WithRuntimeObjects(&operatorsv1alpha1.Operator{
136+
ObjectMeta: metav1.ObjectMeta{
137+
Name: "resolutioncli-input-operator",
138+
},
139+
Spec: operatorsv1alpha1.OperatorSpec{
140+
PackageName: packageName,
141+
Channel: packageChannel,
142+
Version: packageVersionRange,
143+
},
144+
})
145+
123146
cl := clientBuilder.Build()
124147
catalogClient := newIndexRefClient(indexRef)
125148

126149
resolver := solver.NewDeppySolver(
127-
append(
128-
variablesources.NestedVariableSource{newPackageVariableSource(catalogClient, packageName, packageVersion, packageChannel)},
129-
controllers.NewVariableSource(cl, catalogClient)...,
130-
),
150+
variablesources.NewOLMVariableSource(cl, catalogClient),
131151
)
132152

133153
bundleImage, err := resolve(ctx, resolver, packageName)

cmd/resolutioncli/variable_source.go

Lines changed: 0 additions & 44 deletions
This file was deleted.

internal/controllers/operator_controller_test.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ import (
2828
"github.com/operator-framework/operator-controller/internal/catalogmetadata"
2929
"github.com/operator-framework/operator-controller/internal/conditionsets"
3030
"github.com/operator-framework/operator-controller/internal/controllers"
31+
"github.com/operator-framework/operator-controller/internal/resolution/variablesources"
3132
"github.com/operator-framework/operator-controller/pkg/features"
3233
testutil "github.com/operator-framework/operator-controller/test/util"
3334
)
@@ -44,7 +45,7 @@ var _ = Describe("Operator Controller Test", func() {
4445
reconciler = &controllers.OperatorReconciler{
4546
Client: cl,
4647
Scheme: sch,
47-
Resolver: solver.NewDeppySolver(controllers.NewVariableSource(cl, &fakeCatalogClient)),
48+
Resolver: solver.NewDeppySolver(variablesources.NewOLMVariableSource(cl, &fakeCatalogClient)),
4849
}
4950
})
5051
When("the operator does not exist", func() {
@@ -1059,7 +1060,7 @@ func TestOperatorUpgrade(t *testing.T) {
10591060
reconciler := &controllers.OperatorReconciler{
10601061
Client: cl,
10611062
Scheme: sch,
1062-
Resolver: solver.NewDeppySolver(controllers.NewVariableSource(cl, &fakeCatalogClient)),
1063+
Resolver: solver.NewDeppySolver(variablesources.NewOLMVariableSource(cl, &fakeCatalogClient)),
10631064
}
10641065

10651066
t.Run("semver upgrade constraints", func(t *testing.T) {

internal/controllers/variable_source.go

Lines changed: 0 additions & 42 deletions
This file was deleted.

internal/resolution/variables/required_package_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ import (
1313
olmvariables "github.com/operator-framework/operator-controller/internal/resolution/variables"
1414
)
1515

16-
func TestRequiredPackageVariable(t *testing.T) {
16+
func TestRequiredPackageVariables(t *testing.T) {
1717
packageName := "test-package"
1818
bundles := []*catalogmetadata.Bundle{
1919
{Bundle: declcfg.Bundle{Name: "bundle-1", Properties: []property.Property{

internal/resolution/variablesources/bundles_and_dependencies.go renamed to internal/resolution/variablesources/bundle.go

Lines changed: 20 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -1,63 +1,34 @@
11
package variablesources
22

33
import (
4-
"context"
54
"fmt"
65
"sort"
76

8-
"github.com/operator-framework/deppy/pkg/deppy"
9-
"github.com/operator-framework/deppy/pkg/deppy/input"
107
"k8s.io/apimachinery/pkg/util/sets"
118

9+
"github.com/operator-framework/deppy/pkg/deppy"
10+
1211
"github.com/operator-framework/operator-controller/internal/catalogmetadata"
1312
catalogfilter "github.com/operator-framework/operator-controller/internal/catalogmetadata/filter"
1413
catalogsort "github.com/operator-framework/operator-controller/internal/catalogmetadata/sort"
1514
olmvariables "github.com/operator-framework/operator-controller/internal/resolution/variables"
1615
)
1716

18-
var _ input.VariableSource = &BundlesAndDepsVariableSource{}
19-
20-
type BundlesAndDepsVariableSource struct {
21-
catalogClient BundleProvider
22-
variableSources []input.VariableSource
23-
}
24-
25-
func NewBundlesAndDepsVariableSource(catalogClient BundleProvider, inputVariableSources ...input.VariableSource) *BundlesAndDepsVariableSource {
26-
return &BundlesAndDepsVariableSource{
27-
catalogClient: catalogClient,
28-
variableSources: inputVariableSources,
29-
}
30-
}
31-
32-
func (b *BundlesAndDepsVariableSource) GetVariables(ctx context.Context) ([]deppy.Variable, error) {
33-
var variables []deppy.Variable
34-
35-
// extract required package variables
36-
for _, variableSource := range b.variableSources {
37-
inputVariables, err := variableSource.GetVariables(ctx)
38-
if err != nil {
39-
return nil, err
40-
}
41-
variables = append(variables, inputVariables...)
42-
}
43-
44-
// create bundle queue for dependency resolution
17+
func MakeBundleVariables(
18+
allBundles []*catalogmetadata.Bundle,
19+
requiredPackages []*olmvariables.RequiredPackageVariable,
20+
installedPackages []*olmvariables.InstalledPackageVariable,
21+
) ([]*olmvariables.BundleVariable, error) {
4522
var bundleQueue []*catalogmetadata.Bundle
46-
for _, variable := range variables {
47-
switch v := variable.(type) {
48-
case *olmvariables.RequiredPackageVariable:
49-
bundleQueue = append(bundleQueue, v.Bundles()...)
50-
case *olmvariables.InstalledPackageVariable:
51-
bundleQueue = append(bundleQueue, v.Bundles()...)
52-
}
23+
for _, variable := range requiredPackages {
24+
bundleQueue = append(bundleQueue, variable.Bundles()...)
5325
}
54-
55-
allBundles, err := b.catalogClient.Bundles(ctx)
56-
if err != nil {
57-
return nil, err
26+
for _, variable := range installedPackages {
27+
bundleQueue = append(bundleQueue, variable.Bundles()...)
5828
}
5929

6030
// build bundle and dependency variables
31+
var result []*olmvariables.BundleVariable
6132
visited := sets.Set[deppy.Identifier]{}
6233
for len(bundleQueue) > 0 {
6334
// pop head of queue
@@ -73,7 +44,7 @@ func (b *BundlesAndDepsVariableSource) GetVariables(ctx context.Context) ([]depp
7344
visited.Insert(id)
7445

7546
// get bundle dependencies
76-
dependencies, err := b.filterBundleDependencies(allBundles, head)
47+
dependencies, err := filterBundleDependencies(allBundles, head)
7748
if err != nil {
7849
return nil, fmt.Errorf("could not determine dependencies for bundle with id '%s': %w", id, err)
7950
}
@@ -82,21 +53,23 @@ func (b *BundlesAndDepsVariableSource) GetVariables(ctx context.Context) ([]depp
8253
bundleQueue = append(bundleQueue, dependencies...)
8354

8455
// create variable
85-
variables = append(variables, olmvariables.NewBundleVariable(head, dependencies))
56+
result = append(result, olmvariables.NewBundleVariable(head, dependencies))
8657
}
8758

88-
return variables, nil
59+
return result, nil
8960
}
9061

91-
func (b *BundlesAndDepsVariableSource) filterBundleDependencies(allBundles []*catalogmetadata.Bundle, bundle *catalogmetadata.Bundle) ([]*catalogmetadata.Bundle, error) {
62+
func filterBundleDependencies(allBundles []*catalogmetadata.Bundle, bundle *catalogmetadata.Bundle) ([]*catalogmetadata.Bundle, error) {
9263
var dependencies []*catalogmetadata.Bundle
9364
added := sets.Set[deppy.Identifier]{}
9465

9566
// gather required package dependencies
96-
// todo(perdasilva): disambiguate between not found and actual errors
9767
requiredPackages, _ := bundle.RequiredPackages()
9868
for _, requiredPackage := range requiredPackages {
99-
packageDependencyBundles := catalogfilter.Filter(allBundles, catalogfilter.And(catalogfilter.WithPackageName(requiredPackage.PackageName), catalogfilter.InBlangSemverRange(requiredPackage.SemverRange)))
69+
packageDependencyBundles := catalogfilter.Filter(allBundles, catalogfilter.And(
70+
catalogfilter.WithPackageName(requiredPackage.PackageName),
71+
catalogfilter.InBlangSemverRange(requiredPackage.SemverRange),
72+
))
10073
if len(packageDependencyBundles) == 0 {
10174
return nil, fmt.Errorf("could not find package dependencies for bundle '%s'", bundle.Name)
10275
}

0 commit comments

Comments
 (0)