Skip to content

Commit fb176f7

Browse files
Merge pull request #375 from benluddy/backport-46-1908431
Bug 1915905: Preserve custom catsrc w/ default catsrc name
2 parents 116e656 + 6ec19a8 commit fb176f7

File tree

6 files changed

+26
-71
lines changed

6 files changed

+26
-71
lines changed

pkg/controller/catalogsource/catalogsource_controller.go

Lines changed: 2 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,13 @@ package catalogsource
22

33
import (
44
"context"
5-
"time"
65

76
olm "github.com/operator-framework/operator-marketplace/pkg/apis/olm/v1alpha1"
7+
v1 "github.com/operator-framework/operator-marketplace/pkg/apis/operators/v1"
88
"github.com/operator-framework/operator-marketplace/pkg/controller/options"
99
"github.com/operator-framework/operator-marketplace/pkg/defaults"
1010
"github.com/operator-framework/operator-marketplace/pkg/operatorhub"
1111
log "github.com/sirupsen/logrus"
12-
"k8s.io/apimachinery/pkg/api/errors"
1312
"sigs.k8s.io/controller-runtime/pkg/client"
1413
"sigs.k8s.io/controller-runtime/pkg/controller"
1514
"sigs.k8s.io/controller-runtime/pkg/event"
@@ -89,39 +88,8 @@ type ReconcileCatalogSource struct {
8988
}
9089

9190
func (r *ReconcileCatalogSource) Reconcile(request reconcile.Request) (reconcile.Result, error) {
92-
9391
_, defaultCatalogsources := defaults.GetGlobalDefinitions()
94-
defaultCatsrcDef := defaultCatalogsources[request.Name]
95-
96-
if operatorhub.GetSingleton().Get()[defaultCatsrcDef.Name] {
97-
log.Info("%s disabled. Not taking any action", defaultCatsrcDef.Name)
98-
return reconcile.Result{}, nil
99-
}
100-
log.Infof("Reconciling default CatalogSource %s", request.Name)
101-
102-
// Fetch the CatalogSource instance
103-
instance := &olm.CatalogSource{}
104-
err := r.client.Get(context.TODO(), request.NamespacedName, instance)
105-
if err != nil {
106-
if errors.IsNotFound(err) {
107-
createNewCatsrcInstance(r.client, defaultCatsrcDef)
108-
return reconcile.Result{}, nil
109-
}
110-
// Error reading the object - requeue the request.
111-
return reconcile.Result{Requeue: true, RequeueAfter: time.Second * 5}, err
112-
}
113-
114-
if instance.DeletionTimestamp != nil {
115-
return reconcile.Result{Requeue: true, RequeueAfter: time.Second * 5}, nil
116-
}
117-
118-
if !defaults.AreCatsrcSpecsEqual(&defaultCatsrcDef.Spec, &instance.Spec) {
119-
if err := r.client.Delete(context.TODO(), instance); err != nil {
120-
log.Warnf("Could not set default CatalogSource %s's spec back to desired default state. Error in deleting updated CatalogSource: %s", defaultCatsrcDef.GetName(), err.Error())
121-
}
122-
return reconcile.Result{Requeue: true, RequeueAfter: time.Second * 5}, nil
123-
}
124-
return reconcile.Result{}, nil
92+
return reconcile.Result{}, defaults.New(map[string]v1.OperatorSource{}, defaultCatalogsources, operatorhub.GetSingleton().Get()).Ensure(r.client, request.Name)
12593
}
12694

12795
func createNewCatsrcInstance(client client.Client, catsrc olm.CatalogSource) error {

pkg/defaults/catsrcHelpers.go

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,9 @@ func processCatsrc(client wrapper.Client, def olm.CatalogSource, disable bool) e
6464
}
6565

6666
if disable {
67-
err = ensureCatsrcAbsent(client, def, cluster)
67+
if cluster.Annotations[defaultCatsrcAnnotationKey] == defaultCatsrcAnnotationValue {
68+
err = ensureCatsrcAbsent(client, def, cluster)
69+
}
6870
} else {
6971
err = ensureCatsrcPresent(client, def, cluster)
7072
}
@@ -96,6 +98,12 @@ func ensureCatsrcAbsent(client wrapper.Client, def olm.CatalogSource, cluster *o
9698

9799
// ensureCatsrcPresent ensure that that the default CatalogSource is present on the cluster
98100
func ensureCatsrcPresent(client wrapper.Client, def olm.CatalogSource, cluster *olm.CatalogSource) error {
101+
def = *def.DeepCopy()
102+
if def.Annotations == nil {
103+
def.Annotations = make(map[string]string)
104+
}
105+
def.Annotations[defaultCatsrcAnnotationKey] = defaultCatsrcAnnotationValue
106+
99107
// Create if not present or is deleted
100108
if cluster.Name == "" || (!cluster.ObjectMeta.DeletionTimestamp.IsZero() && len(cluster.Finalizers) == 0) {
101109
err := client.Create(context.TODO(), &def)
@@ -106,13 +114,17 @@ func ensureCatsrcPresent(client wrapper.Client, def olm.CatalogSource, cluster *
106114
return nil
107115
}
108116

109-
if AreCatsrcSpecsEqual(&def.Spec, &cluster.Spec) {
110-
logrus.Infof("[defaults] CatalogSource %s default and on cluster specs are same", def.Name)
117+
if cluster.Annotations[defaultCatsrcAnnotationKey] == defaultCatsrcAnnotationValue && AreCatsrcSpecsEqual(&def.Spec, &cluster.Spec) {
118+
logrus.Infof("[defaults] CatalogSource %s is annotated and its spec is the same as the default spec", def.Name)
111119
return nil
112120
}
113121

114122
// Update if the spec has changed
115123
cluster.Spec = def.Spec
124+
if cluster.Annotations == nil {
125+
cluster.Annotations = make(map[string]string)
126+
}
127+
cluster.Annotations[defaultCatsrcAnnotationKey] = defaultCatsrcAnnotationValue
116128
err := client.Update(context.TODO(), cluster)
117129
if err != nil {
118130
return err

pkg/defaults/defaults.go

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ import (
66
"os"
77

88
olm "github.com/operator-framework/operator-marketplace/pkg/apis/olm/v1alpha1"
9-
"github.com/operator-framework/operator-marketplace/pkg/apis/operators/v1"
9+
v1 "github.com/operator-framework/operator-marketplace/pkg/apis/operators/v1"
1010
wrapper "github.com/operator-framework/operator-marketplace/pkg/client"
1111
utilerrors "k8s.io/apimachinery/pkg/util/errors"
1212
"sigs.k8s.io/controller-runtime/pkg/client"
@@ -38,6 +38,11 @@ var (
3838
defaultConfig = make(map[string]bool)
3939
)
4040

41+
const (
42+
defaultCatsrcAnnotationKey string = "operatorframework.io/managed-by"
43+
defaultCatsrcAnnotationValue string = "marketplace-operator"
44+
)
45+
4146
// Defaults is the interface that can be used to ensure default OperatorSources
4247
// are always present on the cluster.
4348
type Defaults interface {

test/helpers/helpers.go

Lines changed: 0 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -391,31 +391,6 @@ func WaitForDeploymentScaled(client test.FrameworkClient, name, namespace string
391391
return nil
392392
}
393393

394-
// RestartMarketplace scales the marketplace deployment down to zero and then scales
395-
// it back up to it's original number of replicas, and waits for a successful deployment.
396-
func RestartMarketplace(client test.FrameworkClient, namespace string) error {
397-
marketplace := &apps.Deployment{}
398-
err := client.Get(context.TODO(), types.NamespacedName{Name: "marketplace-operator", Namespace: namespace}, marketplace)
399-
if err != nil {
400-
return err
401-
}
402-
initialReplicas := marketplace.Spec.Replicas
403-
404-
// Scale down deployment
405-
err = ScaleMarketplace(client, namespace, int32(0))
406-
if err != nil {
407-
return err
408-
}
409-
410-
// Now scale it back up
411-
ScaleMarketplace(client, namespace, *initialReplicas)
412-
if err != nil {
413-
return err
414-
}
415-
416-
return nil
417-
}
418-
419394
// ScaleMarketplace scales the marketplace deployment to the specified replica scale size
420395
func ScaleMarketplace(client test.FrameworkClient, namespace string, scale int32) error {
421396
marketplace := &apps.Deployment{}

test/testsuites/defaultcatsrctests.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -171,6 +171,9 @@ func testDefaultCatsrcWhileDisabled(t *testing.T) {
171171
})
172172
require.NoError(t, err, "The update on the custom CatalogSource was reverted back")
173173

174+
customCatsrc, err = checkForCatsrc("redhat-operators", namespace)
175+
require.NoError(t, err, "Custom CatalogSource redhat-operators was removed from the cluster after marketplace was restarted")
176+
174177
err = toggle(t, 4, false, false) //Re-enable all default CatalogSources
175178
require.NoError(t, err, "Could not enable default CatalogSources")
176179

test/testsuites/operatorhubtests.go

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -243,10 +243,6 @@ func testClusterStatusDefaultsDisabled(t *testing.T) {
243243
err = checkClusterOperatorHub(t, 4)
244244
assert.NoError(t, err, "Incorrect cluster OperatorHub")
245245

246-
// Restart marketplace operator
247-
err = helpers.RestartMarketplace(test.Global.Client, namespace)
248-
require.NoError(t, err, "Could not restart marketplace operator")
249-
250246
// Check that the ClusterOperator resource has the correct status
251247
clusterOperatorName := "marketplace"
252248
expectedTypeStatus := map[apiconfigv1.ClusterStatusConditionType]apiconfigv1.ConditionStatus{
@@ -301,10 +297,6 @@ func testSomeClusterStatusDefaultsDisabled(t *testing.T) {
301297
err = checkClusterOperatorHub(t, 2)
302298
assert.NoError(t, err, "Incorrect cluster OperatorHub")
303299

304-
// Restart marketplace operator
305-
err = helpers.RestartMarketplace(test.Global.Client, namespace)
306-
require.NoError(t, err, "Could not restart marketplace operator")
307-
308300
// Check that the ClusterOperator resource has the correct status
309301
clusterOperatorName := "marketplace"
310302
expectedTypeStatus := map[apiconfigv1.ClusterStatusConditionType]apiconfigv1.ConditionStatus{

0 commit comments

Comments
 (0)