Skip to content

Commit b282b12

Browse files
authored
Change global var GetInstalledBundle to interface (#899)
Signed-off-by: Brett Tofel <[email protected]>
1 parent 5b64db2 commit b282b12

File tree

5 files changed

+149
-115
lines changed

5 files changed

+149
-115
lines changed

cmd/manager/main.go

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -181,13 +181,14 @@ func main() {
181181
}
182182

183183
if err = (&controllers.ClusterExtensionReconciler{
184-
Client: cl,
185-
ReleaseNamespace: systemNamespace,
186-
BundleProvider: catalogClient,
187-
ActionClientGetter: acg,
188-
Unpacker: unpacker,
189-
Storage: localStorage,
190-
Handler: handler.HandlerFunc(handler.HandleClusterExtension),
184+
Client: cl,
185+
ReleaseNamespace: systemNamespace,
186+
BundleProvider: catalogClient,
187+
ActionClientGetter: acg,
188+
Unpacker: unpacker,
189+
Storage: localStorage,
190+
Handler: handler.HandlerFunc(handler.HandleClusterExtension),
191+
InstalledBundleGetter: &controllers.DefaultInstalledBundleGetter{},
191192
}).SetupWithManager(mgr); err != nil {
192193
setupLog.Error(err, "unable to create controller", "controller", "ClusterExtension")
193194
os.Exit(1)

internal/controllers/clusterextension_controller.go

Lines changed: 19 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -83,16 +83,21 @@ import (
8383
// ClusterExtensionReconciler reconciles a ClusterExtension object
8484
type ClusterExtensionReconciler struct {
8585
client.Client
86-
ReleaseNamespace string
87-
BundleProvider BundleProvider
88-
Unpacker rukpaksource.Unpacker
89-
ActionClientGetter helmclient.ActionClientGetter
90-
Storage storage.Storage
91-
Handler handler.Handler
92-
dynamicWatchMutex sync.RWMutex
93-
dynamicWatchGVKs map[schema.GroupVersionKind]struct{}
94-
controller crcontroller.Controller
95-
cache cache.Cache
86+
ReleaseNamespace string
87+
BundleProvider BundleProvider
88+
Unpacker rukpaksource.Unpacker
89+
ActionClientGetter helmclient.ActionClientGetter
90+
Storage storage.Storage
91+
Handler handler.Handler
92+
dynamicWatchMutex sync.RWMutex
93+
dynamicWatchGVKs map[schema.GroupVersionKind]struct{}
94+
controller crcontroller.Controller
95+
cache cache.Cache
96+
InstalledBundleGetter InstalledBundleGetter
97+
}
98+
99+
type InstalledBundleGetter interface {
100+
GetInstalledBundle(ctx context.Context, acg helmclient.ActionClientGetter, allBundles []*catalogmetadata.Bundle, ext *ocv1alpha1.ClusterExtension) (*catalogmetadata.Bundle, error)
96101
}
97102

98103
//+kubebuilder:rbac:groups=olm.operatorframework.io,resources=clusterextensions,verbs=get;list;watch
@@ -416,7 +421,7 @@ func (r *ClusterExtensionReconciler) resolve(ctx context.Context, ext ocv1alpha1
416421
channelName := ext.Spec.Channel
417422
versionRange := ext.Spec.Version
418423

419-
installedBundle, err := GetInstalledbundle(ctx, r.ActionClientGetter, allBundles, &ext)
424+
installedBundle, err := r.InstalledBundleGetter.GetInstalledBundle(ctx, r.ActionClientGetter, allBundles, &ext)
420425
if err != nil {
421426
return nil, err
422427
}
@@ -709,7 +714,9 @@ func (r *ClusterExtensionReconciler) getReleaseState(cl helmclient.ActionInterfa
709714
return currentRelease, stateUnchanged, nil
710715
}
711716

712-
var GetInstalledbundle = func(ctx context.Context, acg helmclient.ActionClientGetter, allBundles []*catalogmetadata.Bundle, ext *ocv1alpha1.ClusterExtension) (*catalogmetadata.Bundle, error) {
717+
type DefaultInstalledBundleGetter struct{}
718+
719+
func (d *DefaultInstalledBundleGetter) GetInstalledBundle(ctx context.Context, acg helmclient.ActionClientGetter, allBundles []*catalogmetadata.Bundle, ext *ocv1alpha1.ClusterExtension) (*catalogmetadata.Bundle, error) {
713720
cl, err := acg.ActionClientFor(ctx, ext)
714721
if err != nil {
715722
return nil, err

internal/controllers/clusterextension_controller_test.go

Lines changed: 89 additions & 86 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,6 @@ import (
1919
ctrl "sigs.k8s.io/controller-runtime"
2020
"sigs.k8s.io/controller-runtime/pkg/client"
2121

22-
helmclient "github.com/operator-framework/helm-operator-plugins/pkg/client"
2322
"github.com/operator-framework/operator-registry/alpha/declcfg"
2423
"github.com/operator-framework/operator-registry/alpha/property"
2524
rukpakv1alpha2 "github.com/operator-framework/rukpak/api/v1alpha2"
@@ -34,7 +33,7 @@ import (
3433

3534
// Describe: ClusterExtension Controller Test
3635
func TestClusterExtensionDoesNotExist(t *testing.T) {
37-
_, reconciler := newClientAndReconciler(t)
36+
_, reconciler := newClientAndReconciler(t, nil)
3837

3938
t.Log("When the cluster extension does not exist")
4039
t.Log("It returns no error")
@@ -44,7 +43,7 @@ func TestClusterExtensionDoesNotExist(t *testing.T) {
4443
}
4544

4645
func TestClusterExtensionNonExistentPackage(t *testing.T) {
47-
cl, reconciler := newClientAndReconciler(t)
46+
cl, reconciler := newClientAndReconciler(t, nil)
4847
ctx := context.Background()
4948
extKey := types.NamespacedName{Name: fmt.Sprintf("cluster-extension-test-%s", rand.String(8))}
5049

@@ -85,7 +84,7 @@ func TestClusterExtensionNonExistentPackage(t *testing.T) {
8584
}
8685

8786
func TestClusterExtensionNonExistentVersion(t *testing.T) {
88-
cl, reconciler := newClientAndReconciler(t)
87+
cl, reconciler := newClientAndReconciler(t, nil)
8988
ctx := context.Background()
9089
extKey := types.NamespacedName{Name: fmt.Sprintf("cluster-extension-test-%s", rand.String(8))}
9190

@@ -130,7 +129,7 @@ func TestClusterExtensionNonExistentVersion(t *testing.T) {
130129
}
131130

132131
func TestClusterExtensionChannelVersionExists(t *testing.T) {
133-
cl, reconciler := newClientAndReconciler(t)
132+
cl, reconciler := newClientAndReconciler(t, nil)
134133
mockUnpacker := unpacker.(*MockUnpacker)
135134
// Set up the Unpack method to return a result with StateUnpacked
136135
mockUnpacker.On("Unpack", mock.Anything, mock.AnythingOfType("*v1alpha2.BundleDeployment")).Return(&source.Result{
@@ -189,7 +188,7 @@ func TestClusterExtensionChannelVersionExists(t *testing.T) {
189188
}
190189

191190
func TestClusterExtensionChannelExistsNoVersion(t *testing.T) {
192-
cl, reconciler := newClientAndReconciler(t)
191+
cl, reconciler := newClientAndReconciler(t, nil)
193192
mockUnpacker := unpacker.(*MockUnpacker)
194193
// Set up the Unpack method to return a result with StateUnpacked
195194
mockUnpacker.On("Unpack", mock.Anything, mock.AnythingOfType("*v1alpha2.BundleDeployment")).Return(&source.Result{
@@ -248,7 +247,7 @@ func TestClusterExtensionChannelExistsNoVersion(t *testing.T) {
248247
}
249248

250249
func TestClusterExtensionVersionNoChannel(t *testing.T) {
251-
cl, reconciler := newClientAndReconciler(t)
250+
cl, reconciler := newClientAndReconciler(t, nil)
252251
ctx := context.Background()
253252
extKey := types.NamespacedName{Name: fmt.Sprintf("cluster-extension-test-%s", rand.String(8))}
254253

@@ -298,7 +297,7 @@ func TestClusterExtensionVersionNoChannel(t *testing.T) {
298297
}
299298

300299
func TestClusterExtensionNoChannel(t *testing.T) {
301-
cl, reconciler := newClientAndReconciler(t)
300+
cl, reconciler := newClientAndReconciler(t, nil)
302301
ctx := context.Background()
303302
extKey := types.NamespacedName{Name: fmt.Sprintf("cluster-extension-test-%s", rand.String(8))}
304303

@@ -345,7 +344,7 @@ func TestClusterExtensionNoChannel(t *testing.T) {
345344
}
346345

347346
func TestClusterExtensionNoVersion(t *testing.T) {
348-
cl, reconciler := newClientAndReconciler(t)
347+
cl, reconciler := newClientAndReconciler(t, nil)
349348
ctx := context.Background()
350349
extKey := types.NamespacedName{Name: fmt.Sprintf("cluster-extension-test-%s", rand.String(8))}
351350

@@ -416,18 +415,28 @@ func verifyConditionsInvariants(t *testing.T, ext *ocv1alpha1.ClusterExtension)
416415
}
417416

418417
func TestClusterExtensionUpgrade(t *testing.T) {
419-
cl, reconciler := newClientAndReconciler(t)
418+
bundle := &catalogmetadata.Bundle{
419+
Bundle: declcfg.Bundle{
420+
Name: "operatorhub/prometheus/beta/1.0.0",
421+
Package: "prometheus",
422+
Image: "quay.io/operatorhubio/[email protected]",
423+
Properties: []property.Property{
424+
{Type: property.TypePackage, Value: json.RawMessage(`{"packageName":"prometheus","version":"1.0.0"}`)},
425+
{Type: property.TypeGVK, Value: json.RawMessage(`[]`)},
426+
},
427+
},
428+
CatalogName: "fake-catalog",
429+
InChannels: []*catalogmetadata.Channel{&prometheusBetaChannel},
430+
}
431+
432+
cl, reconciler := newClientAndReconciler(t, bundle)
433+
420434
mockUnpacker := unpacker.(*MockUnpacker)
421435
// Set up the Unpack method to return a result with StateUnpackPending
422436
mockUnpacker.On("Unpack", mock.Anything, mock.AnythingOfType("*v1alpha2.BundleDeployment")).Return(&source.Result{
423437
State: source.StatePending,
424438
}, nil)
425439
ctx := context.Background()
426-
defer func() {
427-
controllers.GetInstalledbundle = func(ctx context.Context, acg helmclient.ActionClientGetter, allBundles []*catalogmetadata.Bundle, ext *ocv1alpha1.ClusterExtension) (*catalogmetadata.Bundle, error) {
428-
return nil, nil
429-
}
430-
}()
431440

432441
t.Run("semver upgrade constraints enforcement of upgrades within major version", func(t *testing.T) {
433442
defer featuregatetesting.SetFeatureGateDuringTest(t, features.OperatorControllerFeatureGate, features.ForceSemverUpgradeConstraints, true)()
@@ -477,22 +486,21 @@ func TestClusterExtensionUpgrade(t *testing.T) {
477486
err = cl.Update(ctx, clusterExtension)
478487
require.NoError(t, err)
479488

480-
controllers.GetInstalledbundle = func(ctx context.Context, acg helmclient.ActionClientGetter, allBundles []*catalogmetadata.Bundle, ext *ocv1alpha1.ClusterExtension) (*catalogmetadata.Bundle, error) {
481-
return &catalogmetadata.Bundle{
482-
Bundle: declcfg.Bundle{
483-
Name: "operatorhub/prometheus/beta/1.0.0",
484-
Package: "prometheus",
485-
Image: "quay.io/operatorhubio/[email protected]",
486-
Properties: []property.Property{
487-
{Type: property.TypePackage, Value: json.RawMessage(`{"packageName":"prometheus","version":"1.0.0"}`)},
488-
{Type: property.TypeGVK, Value: json.RawMessage(`[]`)},
489-
},
489+
bundle := &catalogmetadata.Bundle{
490+
Bundle: declcfg.Bundle{
491+
Name: "operatorhub/prometheus/beta/1.0.0",
492+
Package: "prometheus",
493+
Image: "quay.io/operatorhubio/[email protected]",
494+
Properties: []property.Property{
495+
{Type: property.TypePackage, Value: json.RawMessage(`{"packageName":"prometheus","version":"1.0.0"}`)},
496+
{Type: property.TypeGVK, Value: json.RawMessage(`[]`)},
490497
},
491-
CatalogName: "fake-catalog",
492-
InChannels: []*catalogmetadata.Channel{&prometheusBetaChannel},
493-
}, nil
498+
},
499+
CatalogName: "fake-catalog",
500+
InChannels: []*catalogmetadata.Channel{&prometheusBetaChannel},
494501
}
495502

503+
cl, reconciler := newClientAndReconciler(t, bundle)
496504
// Run reconcile again
497505
res, err = reconciler.Reconcile(ctx, ctrl.Request{NamespacedName: extKey})
498506
require.Error(t, err)
@@ -586,22 +594,22 @@ func TestClusterExtensionUpgrade(t *testing.T) {
586594
err = cl.Update(ctx, clusterExtension)
587595
require.NoError(t, err)
588596

589-
controllers.GetInstalledbundle = func(ctx context.Context, acg helmclient.ActionClientGetter, allBundles []*catalogmetadata.Bundle, ext *ocv1alpha1.ClusterExtension) (*catalogmetadata.Bundle, error) {
590-
return &catalogmetadata.Bundle{
591-
Bundle: declcfg.Bundle{
592-
Name: "operatorhub/prometheus/beta/1.0.0",
593-
Package: "prometheus",
594-
Image: "quay.io/operatorhubio/[email protected]",
595-
Properties: []property.Property{
596-
{Type: property.TypePackage, Value: json.RawMessage(`{"packageName":"prometheus","version":"1.0.0"}`)},
597-
{Type: property.TypeGVK, Value: json.RawMessage(`[]`)},
598-
},
597+
bundle := &catalogmetadata.Bundle{
598+
Bundle: declcfg.Bundle{
599+
Name: "operatorhub/prometheus/beta/1.0.0",
600+
Package: "prometheus",
601+
Image: "quay.io/operatorhubio/[email protected]",
602+
Properties: []property.Property{
603+
{Type: property.TypePackage, Value: json.RawMessage(`{"packageName":"prometheus","version":"1.0.0"}`)},
604+
{Type: property.TypeGVK, Value: json.RawMessage(`[]`)},
599605
},
600-
CatalogName: "fake-catalog",
601-
InChannels: []*catalogmetadata.Channel{&prometheusBetaChannel},
602-
}, nil
606+
},
607+
CatalogName: "fake-catalog",
608+
InChannels: []*catalogmetadata.Channel{&prometheusBetaChannel},
603609
}
604610

611+
cl, reconciler := newClientAndReconciler(t, bundle)
612+
605613
// Run reconcile again
606614
res, err = reconciler.Reconcile(ctx, ctrl.Request{NamespacedName: extKey})
607615
require.Error(t, err)
@@ -709,22 +717,22 @@ func TestClusterExtensionUpgrade(t *testing.T) {
709717
err = cl.Update(ctx, clusterExtension)
710718
require.NoError(t, err)
711719

712-
controllers.GetInstalledbundle = func(ctx context.Context, acg helmclient.ActionClientGetter, allBundles []*catalogmetadata.Bundle, ext *ocv1alpha1.ClusterExtension) (*catalogmetadata.Bundle, error) {
713-
return &catalogmetadata.Bundle{
714-
Bundle: declcfg.Bundle{
715-
Name: "operatorhub/prometheus/beta/1.0.0",
716-
Package: "prometheus",
717-
Image: "quay.io/operatorhubio/[email protected]",
718-
Properties: []property.Property{
719-
{Type: property.TypePackage, Value: json.RawMessage(`{"packageName":"prometheus","version":"1.0.0"}`)},
720-
{Type: property.TypeGVK, Value: json.RawMessage(`[]`)},
721-
},
720+
bundle := &catalogmetadata.Bundle{
721+
Bundle: declcfg.Bundle{
722+
Name: "operatorhub/prometheus/beta/1.0.0",
723+
Package: "prometheus",
724+
Image: "quay.io/operatorhubio/[email protected]",
725+
Properties: []property.Property{
726+
{Type: property.TypePackage, Value: json.RawMessage(`{"packageName":"prometheus","version":"1.0.0"}`)},
727+
{Type: property.TypeGVK, Value: json.RawMessage(`[]`)},
722728
},
723-
CatalogName: "fake-catalog",
724-
InChannels: []*catalogmetadata.Channel{&prometheusBetaChannel},
725-
}, nil
729+
},
730+
CatalogName: "fake-catalog",
731+
InChannels: []*catalogmetadata.Channel{&prometheusBetaChannel},
726732
}
727733

734+
cl, reconciler := newClientAndReconciler(t, bundle)
735+
728736
// Run reconcile again
729737
res, err = reconciler.Reconcile(ctx, ctrl.Request{NamespacedName: extKey})
730738
require.NoError(t, err)
@@ -749,18 +757,13 @@ func TestClusterExtensionUpgrade(t *testing.T) {
749757
}
750758

751759
func TestClusterExtensionDowngrade(t *testing.T) {
752-
cl, reconciler := newClientAndReconciler(t)
760+
cl, reconciler := newClientAndReconciler(t, nil)
753761
mockUnpacker := unpacker.(*MockUnpacker)
754762
// Set up the Unpack method to return a result with StateUnpacked
755763
mockUnpacker.On("Unpack", mock.Anything, mock.AnythingOfType("*v1alpha2.BundleDeployment")).Return(&source.Result{
756764
State: source.StatePending,
757765
}, nil)
758766
ctx := context.Background()
759-
defer func() {
760-
controllers.GetInstalledbundle = func(ctx context.Context, acg helmclient.ActionClientGetter, allBundles []*catalogmetadata.Bundle, ext *ocv1alpha1.ClusterExtension) (*catalogmetadata.Bundle, error) {
761-
return nil, nil
762-
}
763-
}()
764767

765768
t.Run("enforce upgrade constraints", func(t *testing.T) {
766769
for _, tt := range []struct {
@@ -821,22 +824,22 @@ func TestClusterExtensionDowngrade(t *testing.T) {
821824
err = cl.Update(ctx, clusterExtension)
822825
require.NoError(t, err)
823826

824-
controllers.GetInstalledbundle = func(ctx context.Context, acg helmclient.ActionClientGetter, allBundles []*catalogmetadata.Bundle, ext *ocv1alpha1.ClusterExtension) (*catalogmetadata.Bundle, error) {
825-
return &catalogmetadata.Bundle{
826-
Bundle: declcfg.Bundle{
827-
Name: "operatorhub/prometheus/beta/1.0.1",
828-
Package: "prometheus",
829-
Image: "quay.io/operatorhubio/[email protected]",
830-
Properties: []property.Property{
831-
{Type: property.TypePackage, Value: json.RawMessage(`{"packageName":"prometheus","version":"1.0.1"}`)},
832-
{Type: property.TypeGVK, Value: json.RawMessage(`[]`)},
833-
},
827+
bundle := &catalogmetadata.Bundle{
828+
Bundle: declcfg.Bundle{
829+
Name: "operatorhub/prometheus/beta/1.0.1",
830+
Package: "prometheus",
831+
Image: "quay.io/operatorhubio/[email protected]",
832+
Properties: []property.Property{
833+
{Type: property.TypePackage, Value: json.RawMessage(`{"packageName":"prometheus","version":"1.0.1"}`)},
834+
{Type: property.TypeGVK, Value: json.RawMessage(`[]`)},
834835
},
835-
CatalogName: "fake-catalog",
836-
InChannels: []*catalogmetadata.Channel{&prometheusBetaChannel},
837-
}, nil
836+
},
837+
CatalogName: "fake-catalog",
838+
InChannels: []*catalogmetadata.Channel{&prometheusBetaChannel},
838839
}
839840

841+
cl, reconciler := newClientAndReconciler(t, bundle)
842+
840843
// Run reconcile again
841844
res, err = reconciler.Reconcile(ctx, ctrl.Request{NamespacedName: extKey})
842845
require.Error(t, err)
@@ -920,22 +923,22 @@ func TestClusterExtensionDowngrade(t *testing.T) {
920923
err = cl.Update(ctx, clusterExtension)
921924
require.NoError(t, err)
922925

923-
controllers.GetInstalledbundle = func(ctx context.Context, acg helmclient.ActionClientGetter, allBundles []*catalogmetadata.Bundle, ext *ocv1alpha1.ClusterExtension) (*catalogmetadata.Bundle, error) {
924-
return &catalogmetadata.Bundle{
925-
Bundle: declcfg.Bundle{
926-
Name: "operatorhub/prometheus/beta/2.0.0",
927-
Package: "prometheus",
928-
Image: "quay.io/operatorhubio/[email protected]",
929-
Properties: []property.Property{
930-
{Type: property.TypePackage, Value: json.RawMessage(`{"packageName":"prometheus","version":"2.0.0"}`)},
931-
{Type: property.TypeGVK, Value: json.RawMessage(`[]`)},
932-
},
926+
bundle := &catalogmetadata.Bundle{
927+
Bundle: declcfg.Bundle{
928+
Name: "operatorhub/prometheus/beta/2.0.0",
929+
Package: "prometheus",
930+
Image: "quay.io/operatorhubio/[email protected]",
931+
Properties: []property.Property{
932+
{Type: property.TypePackage, Value: json.RawMessage(`{"packageName":"prometheus","version":"2.0.0"}`)},
933+
{Type: property.TypeGVK, Value: json.RawMessage(`[]`)},
933934
},
934-
CatalogName: "fake-catalog",
935-
InChannels: []*catalogmetadata.Channel{&prometheusBetaChannel},
936-
}, nil
935+
},
936+
CatalogName: "fake-catalog",
937+
InChannels: []*catalogmetadata.Channel{&prometheusBetaChannel},
937938
}
938939

940+
cl, reconciler := newClientAndReconciler(t, bundle)
941+
939942
// Run reconcile again
940943
res, err = reconciler.Reconcile(ctx, ctrl.Request{NamespacedName: extKey})
941944
require.NoError(t, err)

0 commit comments

Comments
 (0)