Skip to content

Commit

Permalink
Change global var GetInstalledBundle to interface
Browse files Browse the repository at this point in the history
Signed-off-by: Brett Tofel <btofel@redhat.com>
  • Loading branch information
bentito committed May 30, 2024
1 parent 0058054 commit 93b05e0
Show file tree
Hide file tree
Showing 5 changed files with 149 additions and 115 deletions.
15 changes: 8 additions & 7 deletions cmd/manager/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -181,13 +181,14 @@ func main() {
}

if err = (&controllers.ClusterExtensionReconciler{
Client: cl,
ReleaseNamespace: systemNamespace,
BundleProvider: catalogClient,
ActionClientGetter: acg,
Unpacker: unpacker,
Storage: localStorage,
Handler: handler.HandlerFunc(handler.HandleClusterExtension),
Client: cl,
ReleaseNamespace: systemNamespace,
BundleProvider: catalogClient,
ActionClientGetter: acg,
Unpacker: unpacker,
Storage: localStorage,
Handler: handler.HandlerFunc(handler.HandleClusterExtension),
InstalledBundleGetter: &controllers.DefaultInstalledBundleGetter{},
}).SetupWithManager(mgr); err != nil {
setupLog.Error(err, "unable to create controller", "controller", "ClusterExtension")
os.Exit(1)
Expand Down
31 changes: 19 additions & 12 deletions internal/controllers/clusterextension_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -83,16 +83,21 @@ import (
// ClusterExtensionReconciler reconciles a ClusterExtension object
type ClusterExtensionReconciler struct {
client.Client
ReleaseNamespace string
BundleProvider BundleProvider
Unpacker rukpaksource.Unpacker
ActionClientGetter helmclient.ActionClientGetter
Storage storage.Storage
Handler handler.Handler
dynamicWatchMutex sync.RWMutex
dynamicWatchGVKs map[schema.GroupVersionKind]struct{}
controller crcontroller.Controller
cache cache.Cache
ReleaseNamespace string
BundleProvider BundleProvider
Unpacker rukpaksource.Unpacker
ActionClientGetter helmclient.ActionClientGetter
Storage storage.Storage
Handler handler.Handler
dynamicWatchMutex sync.RWMutex
dynamicWatchGVKs map[schema.GroupVersionKind]struct{}
controller crcontroller.Controller
cache cache.Cache
InstalledBundleGetter InstalledBundleGetter
}

type InstalledBundleGetter interface {
GetInstalledBundle(ctx context.Context, acg helmclient.ActionClientGetter, allBundles []*catalogmetadata.Bundle, ext *ocv1alpha1.ClusterExtension) (*catalogmetadata.Bundle, error)
}

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

installedBundle, err := GetInstalledbundle(ctx, r.ActionClientGetter, allBundles, &ext)
installedBundle, err := r.InstalledBundleGetter.GetInstalledBundle(ctx, r.ActionClientGetter, allBundles, &ext)
if err != nil {
return nil, err
}
Expand Down Expand Up @@ -709,7 +714,9 @@ func (r *ClusterExtensionReconciler) getReleaseState(cl helmclient.ActionInterfa
return currentRelease, stateUnchanged, nil
}

var GetInstalledbundle = func(ctx context.Context, acg helmclient.ActionClientGetter, allBundles []*catalogmetadata.Bundle, ext *ocv1alpha1.ClusterExtension) (*catalogmetadata.Bundle, error) {
type DefaultInstalledBundleGetter struct{}

func (d *DefaultInstalledBundleGetter) GetInstalledBundle(ctx context.Context, acg helmclient.ActionClientGetter, allBundles []*catalogmetadata.Bundle, ext *ocv1alpha1.ClusterExtension) (*catalogmetadata.Bundle, error) {
cl, err := acg.ActionClientFor(ctx, ext)
if err != nil {
return nil, err
Expand Down
175 changes: 89 additions & 86 deletions internal/controllers/clusterextension_controller_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ import (
ctrl "sigs.k8s.io/controller-runtime"
"sigs.k8s.io/controller-runtime/pkg/client"

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

// Describe: ClusterExtension Controller Test
func TestClusterExtensionDoesNotExist(t *testing.T) {
_, reconciler := newClientAndReconciler(t)
_, reconciler := newClientAndReconciler(t, nil)

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

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

Expand Down Expand Up @@ -85,7 +84,7 @@ func TestClusterExtensionNonExistentPackage(t *testing.T) {
}

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

Expand Down Expand Up @@ -130,7 +129,7 @@ func TestClusterExtensionNonExistentVersion(t *testing.T) {
}

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

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

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

Expand Down Expand Up @@ -298,7 +297,7 @@ func TestClusterExtensionVersionNoChannel(t *testing.T) {
}

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

Expand Down Expand Up @@ -345,7 +344,7 @@ func TestClusterExtensionNoChannel(t *testing.T) {
}

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

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

func TestClusterExtensionUpgrade(t *testing.T) {
cl, reconciler := newClientAndReconciler(t)
bundle := &catalogmetadata.Bundle{
Bundle: declcfg.Bundle{
Name: "operatorhub/prometheus/beta/1.0.0",
Package: "prometheus",
Image: "quay.io/operatorhubio/prometheus@fake1.0.0",
Properties: []property.Property{
{Type: property.TypePackage, Value: json.RawMessage(`{"packageName":"prometheus","version":"1.0.0"}`)},
{Type: property.TypeGVK, Value: json.RawMessage(`[]`)},
},
},
CatalogName: "fake-catalog",
InChannels: []*catalogmetadata.Channel{&prometheusBetaChannel},
}

cl, reconciler := newClientAndReconciler(t, bundle)

mockUnpacker := unpacker.(*MockUnpacker)
// Set up the Unpack method to return a result with StateUnpackPending
mockUnpacker.On("Unpack", mock.Anything, mock.AnythingOfType("*v1alpha2.BundleDeployment")).Return(&source.Result{
State: source.StatePending,
}, nil)
ctx := context.Background()
defer func() {
controllers.GetInstalledbundle = func(ctx context.Context, acg helmclient.ActionClientGetter, allBundles []*catalogmetadata.Bundle, ext *ocv1alpha1.ClusterExtension) (*catalogmetadata.Bundle, error) {
return nil, nil
}
}()

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

controllers.GetInstalledbundle = func(ctx context.Context, acg helmclient.ActionClientGetter, allBundles []*catalogmetadata.Bundle, ext *ocv1alpha1.ClusterExtension) (*catalogmetadata.Bundle, error) {
return &catalogmetadata.Bundle{
Bundle: declcfg.Bundle{
Name: "operatorhub/prometheus/beta/1.0.0",
Package: "prometheus",
Image: "quay.io/operatorhubio/prometheus@fake1.0.0",
Properties: []property.Property{
{Type: property.TypePackage, Value: json.RawMessage(`{"packageName":"prometheus","version":"1.0.0"}`)},
{Type: property.TypeGVK, Value: json.RawMessage(`[]`)},
},
bundle := &catalogmetadata.Bundle{
Bundle: declcfg.Bundle{
Name: "operatorhub/prometheus/beta/1.0.0",
Package: "prometheus",
Image: "quay.io/operatorhubio/prometheus@fake1.0.0",
Properties: []property.Property{
{Type: property.TypePackage, Value: json.RawMessage(`{"packageName":"prometheus","version":"1.0.0"}`)},
{Type: property.TypeGVK, Value: json.RawMessage(`[]`)},
},
CatalogName: "fake-catalog",
InChannels: []*catalogmetadata.Channel{&prometheusBetaChannel},
}, nil
},
CatalogName: "fake-catalog",
InChannels: []*catalogmetadata.Channel{&prometheusBetaChannel},
}

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

controllers.GetInstalledbundle = func(ctx context.Context, acg helmclient.ActionClientGetter, allBundles []*catalogmetadata.Bundle, ext *ocv1alpha1.ClusterExtension) (*catalogmetadata.Bundle, error) {
return &catalogmetadata.Bundle{
Bundle: declcfg.Bundle{
Name: "operatorhub/prometheus/beta/1.0.0",
Package: "prometheus",
Image: "quay.io/operatorhubio/prometheus@fake1.0.0",
Properties: []property.Property{
{Type: property.TypePackage, Value: json.RawMessage(`{"packageName":"prometheus","version":"1.0.0"}`)},
{Type: property.TypeGVK, Value: json.RawMessage(`[]`)},
},
bundle := &catalogmetadata.Bundle{
Bundle: declcfg.Bundle{
Name: "operatorhub/prometheus/beta/1.0.0",
Package: "prometheus",
Image: "quay.io/operatorhubio/prometheus@fake1.0.0",
Properties: []property.Property{
{Type: property.TypePackage, Value: json.RawMessage(`{"packageName":"prometheus","version":"1.0.0"}`)},
{Type: property.TypeGVK, Value: json.RawMessage(`[]`)},
},
CatalogName: "fake-catalog",
InChannels: []*catalogmetadata.Channel{&prometheusBetaChannel},
}, nil
},
CatalogName: "fake-catalog",
InChannels: []*catalogmetadata.Channel{&prometheusBetaChannel},
}

cl, reconciler := newClientAndReconciler(t, bundle)

// Run reconcile again
res, err = reconciler.Reconcile(ctx, ctrl.Request{NamespacedName: extKey})
require.Error(t, err)
Expand Down Expand Up @@ -709,22 +717,22 @@ func TestClusterExtensionUpgrade(t *testing.T) {
err = cl.Update(ctx, clusterExtension)
require.NoError(t, err)

controllers.GetInstalledbundle = func(ctx context.Context, acg helmclient.ActionClientGetter, allBundles []*catalogmetadata.Bundle, ext *ocv1alpha1.ClusterExtension) (*catalogmetadata.Bundle, error) {
return &catalogmetadata.Bundle{
Bundle: declcfg.Bundle{
Name: "operatorhub/prometheus/beta/1.0.0",
Package: "prometheus",
Image: "quay.io/operatorhubio/prometheus@fake1.0.0",
Properties: []property.Property{
{Type: property.TypePackage, Value: json.RawMessage(`{"packageName":"prometheus","version":"1.0.0"}`)},
{Type: property.TypeGVK, Value: json.RawMessage(`[]`)},
},
bundle := &catalogmetadata.Bundle{
Bundle: declcfg.Bundle{
Name: "operatorhub/prometheus/beta/1.0.0",
Package: "prometheus",
Image: "quay.io/operatorhubio/prometheus@fake1.0.0",
Properties: []property.Property{
{Type: property.TypePackage, Value: json.RawMessage(`{"packageName":"prometheus","version":"1.0.0"}`)},
{Type: property.TypeGVK, Value: json.RawMessage(`[]`)},
},
CatalogName: "fake-catalog",
InChannels: []*catalogmetadata.Channel{&prometheusBetaChannel},
}, nil
},
CatalogName: "fake-catalog",
InChannels: []*catalogmetadata.Channel{&prometheusBetaChannel},
}

cl, reconciler := newClientAndReconciler(t, bundle)

// Run reconcile again
res, err = reconciler.Reconcile(ctx, ctrl.Request{NamespacedName: extKey})
require.NoError(t, err)
Expand All @@ -749,18 +757,13 @@ func TestClusterExtensionUpgrade(t *testing.T) {
}

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

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

controllers.GetInstalledbundle = func(ctx context.Context, acg helmclient.ActionClientGetter, allBundles []*catalogmetadata.Bundle, ext *ocv1alpha1.ClusterExtension) (*catalogmetadata.Bundle, error) {
return &catalogmetadata.Bundle{
Bundle: declcfg.Bundle{
Name: "operatorhub/prometheus/beta/1.0.1",
Package: "prometheus",
Image: "quay.io/operatorhubio/prometheus@fake1.0.1",
Properties: []property.Property{
{Type: property.TypePackage, Value: json.RawMessage(`{"packageName":"prometheus","version":"1.0.1"}`)},
{Type: property.TypeGVK, Value: json.RawMessage(`[]`)},
},
bundle := &catalogmetadata.Bundle{
Bundle: declcfg.Bundle{
Name: "operatorhub/prometheus/beta/1.0.1",
Package: "prometheus",
Image: "quay.io/operatorhubio/prometheus@fake1.0.1",
Properties: []property.Property{
{Type: property.TypePackage, Value: json.RawMessage(`{"packageName":"prometheus","version":"1.0.1"}`)},
{Type: property.TypeGVK, Value: json.RawMessage(`[]`)},
},
CatalogName: "fake-catalog",
InChannels: []*catalogmetadata.Channel{&prometheusBetaChannel},
}, nil
},
CatalogName: "fake-catalog",
InChannels: []*catalogmetadata.Channel{&prometheusBetaChannel},
}

cl, reconciler := newClientAndReconciler(t, bundle)

// Run reconcile again
res, err = reconciler.Reconcile(ctx, ctrl.Request{NamespacedName: extKey})
require.Error(t, err)
Expand Down Expand Up @@ -920,22 +923,22 @@ func TestClusterExtensionDowngrade(t *testing.T) {
err = cl.Update(ctx, clusterExtension)
require.NoError(t, err)

controllers.GetInstalledbundle = func(ctx context.Context, acg helmclient.ActionClientGetter, allBundles []*catalogmetadata.Bundle, ext *ocv1alpha1.ClusterExtension) (*catalogmetadata.Bundle, error) {
return &catalogmetadata.Bundle{
Bundle: declcfg.Bundle{
Name: "operatorhub/prometheus/beta/2.0.0",
Package: "prometheus",
Image: "quay.io/operatorhubio/prometheus@fake2.0.0",
Properties: []property.Property{
{Type: property.TypePackage, Value: json.RawMessage(`{"packageName":"prometheus","version":"2.0.0"}`)},
{Type: property.TypeGVK, Value: json.RawMessage(`[]`)},
},
bundle := &catalogmetadata.Bundle{
Bundle: declcfg.Bundle{
Name: "operatorhub/prometheus/beta/2.0.0",
Package: "prometheus",
Image: "quay.io/operatorhubio/prometheus@fake2.0.0",
Properties: []property.Property{
{Type: property.TypePackage, Value: json.RawMessage(`{"packageName":"prometheus","version":"2.0.0"}`)},
{Type: property.TypeGVK, Value: json.RawMessage(`[]`)},
},
CatalogName: "fake-catalog",
InChannels: []*catalogmetadata.Channel{&prometheusBetaChannel},
}, nil
},
CatalogName: "fake-catalog",
InChannels: []*catalogmetadata.Channel{&prometheusBetaChannel},
}

cl, reconciler := newClientAndReconciler(t, bundle)

// Run reconcile again
res, err = reconciler.Reconcile(ctx, ctrl.Request{NamespacedName: extKey})
require.NoError(t, err)
Expand Down
Loading

0 comments on commit 93b05e0

Please sign in to comment.