Skip to content

Commit a4c5228

Browse files
author
Mikalai Radchuk
committed
Refactor controller variable source
Signed-off-by: Mikalai Radchuk <[email protected]>
1 parent 6585f6e commit a4c5228

File tree

2 files changed

+169
-17
lines changed

2 files changed

+169
-17
lines changed

internal/controllers/variable_source.go

Lines changed: 30 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,6 @@ import (
2222
"sigs.k8s.io/controller-runtime/pkg/client"
2323

2424
"github.com/operator-framework/deppy/pkg/deppy"
25-
"github.com/operator-framework/deppy/pkg/deppy/input"
2625
rukpakv1alpha1 "github.com/operator-framework/rukpak/api/v1alpha1"
2726

2827
operatorsv1alpha1 "github.com/operator-framework/operator-controller/api/v1alpha1"
@@ -64,21 +63,35 @@ func (v *VariableSource) GetVariables(ctx context.Context) ([]deppy.Variable, er
6463
return nil, err
6564
}
6665

67-
// We are in process of getting rid of extra variable sources.
68-
// See this for progress: https://github.com/operator-framework/operator-controller/issues/437
69-
vs := variablesources.NestedVariableSource{
70-
func(inputVariableSource input.VariableSource) (input.VariableSource, error) {
71-
return variablesources.NewOperatorVariableSource(operatorList.Items, allBundles, inputVariableSource), nil
72-
},
73-
func(inputVariableSource input.VariableSource) (input.VariableSource, error) {
74-
return variablesources.NewBundleDeploymentVariableSource(operatorList.Items, bundleDeploymentList.Items, allBundles, inputVariableSource), nil
75-
},
76-
func(inputVariableSource input.VariableSource) (input.VariableSource, error) {
77-
return variablesources.NewBundlesAndDepsVariableSource(allBundles, inputVariableSource), nil
78-
},
79-
func(inputVariableSource input.VariableSource) (input.VariableSource, error) {
80-
return variablesources.NewCRDUniquenessConstraintsVariableSource(inputVariableSource), nil
81-
},
66+
requiredPackages, err := variablesources.MakeRequiredPackageVariables(allBundles, operatorList.Items)
67+
if err != nil {
68+
return nil, err
69+
}
70+
71+
installedPackages, err := variablesources.MakeInstalledPackageVariables(allBundles, operatorList.Items, bundleDeploymentList.Items)
72+
if err != nil {
73+
return nil, err
74+
}
75+
76+
bundles, err := variablesources.MakeBundleVariables(allBundles, requiredPackages, installedPackages)
77+
if err != nil {
78+
return nil, err
79+
}
80+
81+
bundleUniqueness := variablesources.MakeBundleUniquenessVariables(bundles)
82+
83+
result := []deppy.Variable{}
84+
for _, v := range requiredPackages {
85+
result = append(result, v)
86+
}
87+
for _, v := range installedPackages {
88+
result = append(result, v)
89+
}
90+
for _, v := range bundles {
91+
result = append(result, v)
92+
}
93+
for _, v := range bundleUniqueness {
94+
result = append(result, v)
8295
}
83-
return vs.GetVariables(ctx)
96+
return result, nil
8497
}
Lines changed: 139 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,139 @@
1+
package controllers_test
2+
3+
import (
4+
"context"
5+
"encoding/json"
6+
"fmt"
7+
"testing"
8+
9+
"github.com/google/go-cmp/cmp"
10+
"github.com/google/go-cmp/cmp/cmpopts"
11+
"github.com/stretchr/testify/require"
12+
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
13+
"k8s.io/apimachinery/pkg/runtime"
14+
"k8s.io/apimachinery/pkg/util/rand"
15+
utilruntime "k8s.io/apimachinery/pkg/util/runtime"
16+
"k8s.io/utils/pointer"
17+
"sigs.k8s.io/controller-runtime/pkg/client/fake"
18+
19+
"github.com/operator-framework/deppy/pkg/deppy"
20+
"github.com/operator-framework/deppy/pkg/deppy/constraint"
21+
"github.com/operator-framework/deppy/pkg/deppy/input"
22+
"github.com/operator-framework/operator-registry/alpha/declcfg"
23+
"github.com/operator-framework/operator-registry/alpha/property"
24+
rukpakv1alpha1 "github.com/operator-framework/rukpak/api/v1alpha1"
25+
26+
operatorsv1alpha1 "github.com/operator-framework/operator-controller/api/v1alpha1"
27+
"github.com/operator-framework/operator-controller/internal/catalogmetadata"
28+
"github.com/operator-framework/operator-controller/internal/controllers"
29+
olmvariables "github.com/operator-framework/operator-controller/internal/resolution/variables"
30+
testutil "github.com/operator-framework/operator-controller/test/util"
31+
)
32+
33+
func TestVariableSource(t *testing.T) {
34+
sch := runtime.NewScheme()
35+
utilruntime.Must(operatorsv1alpha1.AddToScheme(sch))
36+
utilruntime.Must(rukpakv1alpha1.AddToScheme(sch))
37+
38+
stableChannel := catalogmetadata.Channel{Channel: declcfg.Channel{
39+
Name: "stable",
40+
Entries: []declcfg.ChannelEntry{
41+
{
42+
Name: "packageA.v2.0.0",
43+
},
44+
},
45+
}}
46+
bundleSet := map[string]*catalogmetadata.Bundle{
47+
"packageA.v2.0.0": {
48+
Bundle: declcfg.Bundle{
49+
Name: "packageA.v2.0.0",
50+
Package: "packageA",
51+
Image: "foo.io/packageA/packageA:v2.0.0",
52+
Properties: []property.Property{
53+
{Type: property.TypePackage, Value: json.RawMessage(`{"packageName":"packageA","version":"2.0.0"}`)},
54+
},
55+
},
56+
CatalogName: "fake-catalog",
57+
InChannels: []*catalogmetadata.Channel{&stableChannel},
58+
},
59+
}
60+
allBundles := make([]*catalogmetadata.Bundle, 0, len(bundleSet))
61+
for _, bundle := range bundleSet {
62+
allBundles = append(allBundles, bundle)
63+
}
64+
65+
pkgName := "packageA"
66+
opName := fmt.Sprintf("operator-test-%s", rand.String(8))
67+
operator := &operatorsv1alpha1.Operator{
68+
ObjectMeta: metav1.ObjectMeta{Name: opName},
69+
Spec: operatorsv1alpha1.OperatorSpec{
70+
PackageName: pkgName,
71+
Channel: "stable",
72+
Version: "2.0.0",
73+
},
74+
}
75+
76+
bd := &rukpakv1alpha1.BundleDeployment{
77+
ObjectMeta: metav1.ObjectMeta{
78+
Name: opName,
79+
OwnerReferences: []metav1.OwnerReference{
80+
{
81+
APIVersion: operatorsv1alpha1.GroupVersion.String(),
82+
Kind: "Operator",
83+
Name: operator.Name,
84+
UID: operator.UID,
85+
Controller: pointer.Bool(true),
86+
BlockOwnerDeletion: pointer.Bool(true),
87+
},
88+
},
89+
},
90+
Spec: rukpakv1alpha1.BundleDeploymentSpec{
91+
ProvisionerClassName: "core-rukpak-io-plain",
92+
Template: &rukpakv1alpha1.BundleTemplate{
93+
Spec: rukpakv1alpha1.BundleSpec{
94+
ProvisionerClassName: "core-rukpak-io-registry",
95+
Source: rukpakv1alpha1.BundleSource{
96+
Type: rukpakv1alpha1.SourceTypeImage,
97+
Image: &rukpakv1alpha1.ImageSource{
98+
Ref: "foo.io/packageA/packageA:v2.0.0",
99+
},
100+
},
101+
},
102+
},
103+
},
104+
}
105+
fakeClient := fake.NewClientBuilder().WithScheme(sch).WithObjects(operator, bd).Build()
106+
fakeCatalogClient := testutil.NewFakeCatalogClient(allBundles)
107+
108+
vs := controllers.NewVariableSource(fakeClient, &fakeCatalogClient)
109+
110+
vars, err := vs.GetVariables(context.Background())
111+
require.NoError(t, err)
112+
113+
expectedVars := []deppy.Variable{
114+
olmvariables.NewRequiredPackageVariable("packageA", []*catalogmetadata.Bundle{
115+
bundleSet["packageA.v2.0.0"],
116+
}),
117+
olmvariables.NewInstalledPackageVariable("packageA", []*catalogmetadata.Bundle{
118+
bundleSet["packageA.v2.0.0"],
119+
}),
120+
olmvariables.NewBundleVariable(bundleSet["packageA.v2.0.0"], nil),
121+
olmvariables.NewBundleUniquenessVariable(
122+
"packageA package uniqueness",
123+
deppy.Identifier("fake-catalog-packageA-packageA.v2.0.0"),
124+
),
125+
}
126+
gocmpopts := []cmp.Option{
127+
cmpopts.IgnoreUnexported(catalogmetadata.Bundle{}),
128+
cmp.AllowUnexported(
129+
olmvariables.RequiredPackageVariable{},
130+
olmvariables.InstalledPackageVariable{},
131+
olmvariables.BundleVariable{},
132+
olmvariables.BundleUniquenessVariable{},
133+
input.SimpleVariable{},
134+
constraint.DependencyConstraint{},
135+
constraint.AtMostConstraint{},
136+
),
137+
}
138+
require.Empty(t, cmp.Diff(vars, expectedVars, gocmpopts...))
139+
}

0 commit comments

Comments
 (0)