Skip to content

Commit 09485e0

Browse files
author
Mikalai Radchuk
committed
PoC: refactor variable sources
Signed-off-by: Mikalai Radchuk <[email protected]>
1 parent 3fc434d commit 09485e0

22 files changed

+1439
-2302
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: 5 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -35,9 +35,7 @@ import (
3535

3636
operatorsv1alpha1 "github.com/operator-framework/operator-controller/api/v1alpha1"
3737
"github.com/operator-framework/operator-controller/internal/catalogmetadata"
38-
"github.com/operator-framework/operator-controller/internal/controllers"
3938
olmvariables "github.com/operator-framework/operator-controller/internal/resolution/variables"
40-
"github.com/operator-framework/operator-controller/internal/resolution/variablesources"
4139
)
4240

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

7371
var packageName string
74-
var packageVersion string
72+
var packageVersionRange string
7573
var packageChannel string
7674
var indexRef string
7775
var inputDir string
7876
flag.StringVar(&packageName, flagNamePackageName, "", "Name of the package to resolve")
79-
flag.StringVar(&packageVersion, flagNamePackageVersion, "", "Version of the package")
77+
flag.StringVar(&packageVersionRange, flagNamePackageVersion, "", "Version or version range of the package")
8078
flag.StringVar(&packageChannel, flagNamePackageChannel, "", "Channel of the package")
8179
// TODO: Consider adding support of multiple refs
8280
flag.StringVar(&indexRef, flagNameIndexRef, "", "Index reference (FBC image or dir)")
@@ -89,7 +87,7 @@ func main() {
8987
os.Exit(1)
9088
}
9189

92-
err := run(ctx, packageName, packageVersion, packageChannel, indexRef, inputDir)
90+
err := run(ctx, packageName, packageChannel, packageVersionRange, indexRef, inputDir)
9391
if err != nil {
9492
fmt.Fprintln(os.Stderr, err)
9593
os.Exit(1)
@@ -108,7 +106,7 @@ func validateFlags(packageName, indexRef string) error {
108106
return nil
109107
}
110108

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

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

126124
resolver := solver.NewDeppySolver(
127-
append(
128-
variablesources.NestedVariableSource{newPackageVariableSource(catalogClient, packageName, packageVersion, packageChannel)},
129-
controllers.NewVariableSource(cl, catalogClient)...,
130-
),
125+
NewOfflineOLMVariableSource(cl, catalogClient, packageName, packageChannel, packageVersionRange),
131126
)
132127

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

cmd/resolutioncli/variable_source.go

Lines changed: 80 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -17,28 +17,90 @@ limitations under the License.
1717
package main
1818

1919
import (
20+
"context"
21+
22+
"github.com/operator-framework/deppy/pkg/deppy"
2023
"github.com/operator-framework/deppy/pkg/deppy/input"
24+
rukpakv1alpha1 "github.com/operator-framework/rukpak/api/v1alpha1"
25+
"sigs.k8s.io/controller-runtime/pkg/client"
2126

27+
operatorsv1alpha1 "github.com/operator-framework/operator-controller/api/v1alpha1"
28+
olmvariables "github.com/operator-framework/operator-controller/internal/resolution/variables"
2229
"github.com/operator-framework/operator-controller/internal/resolution/variablesources"
2330
)
2431

25-
func newPackageVariableSource(catalogClient *indexRefClient, packageName, packageVersion, packageChannel string) func(inputVariableSource input.VariableSource) (input.VariableSource, error) {
26-
return func(inputVariableSource input.VariableSource) (input.VariableSource, error) {
27-
pkgSource, err := variablesources.NewRequiredPackageVariableSource(
28-
catalogClient,
29-
packageName,
30-
variablesources.InVersionRange(packageVersion),
31-
variablesources.InChannel(packageChannel),
32-
)
33-
if err != nil {
34-
return nil, err
35-
}
36-
37-
sliceSource := variablesources.SliceVariableSource{pkgSource}
38-
if inputVariableSource != nil {
39-
sliceSource = append(sliceSource, inputVariableSource)
40-
}
41-
42-
return sliceSource, nil
32+
var _ input.VariableSource = &OfflineOLMVariableSource{}
33+
34+
type OfflineOLMVariableSource struct {
35+
client client.Client
36+
catalogClient *indexRefClient
37+
38+
packageName string
39+
packageChannel string
40+
packageVersionRange string
41+
}
42+
43+
func NewOfflineOLMVariableSource(cl client.Client, catalogClient *indexRefClient, packageName, packageChannel, packageVersionRange string) *OfflineOLMVariableSource {
44+
return &OfflineOLMVariableSource{
45+
client: cl,
46+
catalogClient: catalogClient,
47+
48+
packageName: packageName,
49+
packageChannel: packageChannel,
50+
packageVersionRange: packageVersionRange,
51+
}
52+
}
53+
54+
func (o *OfflineOLMVariableSource) GetVariables(ctx context.Context) ([]deppy.Variable, error) {
55+
operatorList := operatorsv1alpha1.OperatorList{}
56+
if err := o.client.List(ctx, &operatorList); err != nil {
57+
return nil, err
58+
}
59+
60+
bundleDeployments := rukpakv1alpha1.BundleDeploymentList{}
61+
if err := o.client.List(ctx, &bundleDeployments); err != nil {
62+
return nil, err
63+
}
64+
65+
allBundles, err := o.catalogClient.Bundles(ctx)
66+
if err != nil {
67+
return nil, err
68+
}
69+
70+
requiredPackages := []*olmvariables.RequiredPackageVariable{}
71+
requiredPackage, err := variablesources.RequiredPackageVariable(allBundles, o.packageName, o.packageChannel, o.packageVersionRange)
72+
if err != nil {
73+
return nil, err
74+
}
75+
requiredPackages = append(requiredPackages, requiredPackage)
76+
77+
installedPackages, err := variablesources.InstalledPackageVariables(allBundles, bundleDeployments.Items)
78+
if err != nil {
79+
return nil, err
80+
}
81+
82+
bundles, err := variablesources.BundleVariables(allBundles, requiredPackages, installedPackages)
83+
if err != nil {
84+
return nil, err
85+
}
86+
87+
bundleUniqueness, err := variablesources.BundleUniquenessVariables(bundles)
88+
if err != nil {
89+
return nil, err
90+
}
91+
92+
result := []deppy.Variable{}
93+
for _, v := range requiredPackages {
94+
result = append(result, v)
95+
}
96+
for _, v := range installedPackages {
97+
result = append(result, v)
98+
}
99+
for _, v := range bundles {
100+
result = append(result, v)
101+
}
102+
for _, v := range bundleUniqueness {
103+
result = append(result, v)
43104
}
105+
return result, nil
44106
}

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/variablesources/bundle_deployment.go

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

0 commit comments

Comments
 (0)