Skip to content

Commit 2db452e

Browse files
Merge pull request #1899 from awgreene/fix-operatorcondition-generation
Bug 1906134: Don't create OperatorConditions for copied CSVs
2 parents 7062e42 + ff50a2a commit 2db452e

File tree

2 files changed

+82
-2
lines changed

2 files changed

+82
-2
lines changed

pkg/controller/operators/operatorconditiongenerator_controller.go

Lines changed: 30 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,11 @@ import (
99
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
1010
"k8s.io/apimachinery/pkg/runtime"
1111
ctrl "sigs.k8s.io/controller-runtime"
12+
"sigs.k8s.io/controller-runtime/pkg/builder"
1213
"sigs.k8s.io/controller-runtime/pkg/client"
14+
"sigs.k8s.io/controller-runtime/pkg/event"
1315
"sigs.k8s.io/controller-runtime/pkg/handler"
16+
"sigs.k8s.io/controller-runtime/pkg/predicate"
1417
"sigs.k8s.io/controller-runtime/pkg/reconcile"
1518
"sigs.k8s.io/controller-runtime/pkg/source"
1619

@@ -34,9 +37,35 @@ func (r *OperatorConditionGeneratorReconciler) SetupWithManager(mgr ctrl.Manager
3437
IsController: true,
3538
OwnerType: &operatorsv1alpha1.ClusterServiceVersion{},
3639
}
40+
p := predicate.Funcs{
41+
CreateFunc: func(e event.CreateEvent) bool {
42+
if _, ok := e.Meta.GetLabels()[operatorsv1alpha1.CopiedLabelKey]; ok {
43+
return false
44+
}
45+
return true
46+
},
47+
DeleteFunc: func(e event.DeleteEvent) bool {
48+
if _, ok := e.Meta.GetLabels()[operatorsv1alpha1.CopiedLabelKey]; ok {
49+
return false
50+
}
51+
return true
52+
},
53+
UpdateFunc: func(e event.UpdateEvent) bool {
54+
if _, ok := e.MetaOld.GetLabels()[operatorsv1alpha1.CopiedLabelKey]; ok {
55+
return false
56+
}
57+
return true
58+
},
59+
GenericFunc: func(e event.GenericEvent) bool {
60+
if _, ok := e.Meta.GetLabels()[operatorsv1alpha1.CopiedLabelKey]; ok {
61+
return false
62+
}
63+
return true
64+
},
65+
}
3766

3867
return ctrl.NewControllerManagedBy(mgr).
39-
For(&operatorsv1alpha1.ClusterServiceVersion{}).
68+
For(&operatorsv1alpha1.ClusterServiceVersion{}, builder.WithPredicates(p)).
4069
Watches(&source.Kind{Type: &operatorsv1.OperatorCondition{}}, handler).
4170
Complete(r)
4271
}
@@ -61,7 +90,6 @@ var _ reconcile.Reconciler = &OperatorConditionGeneratorReconciler{}
6190
func (r *OperatorConditionGeneratorReconciler) Reconcile(req ctrl.Request) (ctrl.Result, error) {
6291
// Set up a convenient log object so we don't have to type request over and over again
6392
log := r.log.WithValues("request", req)
64-
log.V(2).Info("reconciling ClusterServiceVersion")
6593

6694
in := &operatorsv1alpha1.ClusterServiceVersion{}
6795
err := r.Client.Get(context.TODO(), req.NamespacedName, in)

pkg/controller/operators/operatorconditiongenerator_controller_test.go

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import (
88
appsv1 "k8s.io/api/apps/v1"
99
corev1 "k8s.io/api/core/v1"
1010
rbacv1 "k8s.io/api/rbac/v1"
11+
k8serrors "k8s.io/apimachinery/pkg/api/errors"
1112
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
1213
"k8s.io/apimachinery/pkg/types"
1314
"k8s.io/apiserver/pkg/storage/names"
@@ -99,6 +100,57 @@ var _ = Describe("The OperatorConditionsGenerator Controller", func() {
99100
}, timeout, interval).Should(Succeed())
100101
})
101102

103+
It("does not create an OperatorCondition for a Copied CSV", func() {
104+
depName := genName("dep-")
105+
csv := &operatorsv1alpha1.ClusterServiceVersion{
106+
TypeMeta: metav1.TypeMeta{
107+
Kind: operatorsv1alpha1.ClusterServiceVersionKind,
108+
APIVersion: operatorsv1alpha1.ClusterServiceVersionAPIVersion,
109+
},
110+
ObjectMeta: metav1.ObjectMeta{
111+
Name: genName("csv-"),
112+
Namespace: namespace,
113+
Labels: map[string]string{
114+
operatorsv1alpha1.CopiedLabelKey: "",
115+
},
116+
},
117+
Spec: operatorsv1alpha1.ClusterServiceVersionSpec{
118+
InstallModes: []operatorsv1alpha1.InstallMode{
119+
{
120+
Type: operatorsv1alpha1.InstallModeTypeOwnNamespace,
121+
Supported: true,
122+
},
123+
{
124+
Type: operatorsv1alpha1.InstallModeTypeSingleNamespace,
125+
Supported: true,
126+
},
127+
{
128+
Type: operatorsv1alpha1.InstallModeTypeMultiNamespace,
129+
Supported: true,
130+
},
131+
{
132+
Type: operatorsv1alpha1.InstallModeTypeAllNamespaces,
133+
Supported: true,
134+
},
135+
},
136+
InstallStrategy: newNginxInstallStrategy(depName, nil, nil),
137+
},
138+
}
139+
140+
Expect(k8sClient.Create(ctx, csv)).To(Succeed())
141+
namespacedName := types.NamespacedName{Name: csv.GetName(), Namespace: csv.GetNamespace()}
142+
operatorCondition := &operatorsv1.OperatorCondition{}
143+
144+
// Wait 10 seconds
145+
// Background: This test could pass simply because the controller hasn't reconciled the Copied CSV yet.
146+
// However, this test does sound an alarm if the controller ever reconciles a copied CSV.
147+
// TODO: Improve this test by identifying a way to identify that the controller has not reconciling a resource.
148+
time.Sleep(time.Second * 10)
149+
err := k8sClient.Get(ctx, namespacedName, operatorCondition)
150+
Expect(err).ToNot(BeNil())
151+
Expect(k8serrors.IsNotFound(err)).To(BeTrue())
152+
})
153+
102154
It("creates an OperatorCondition for a CSV with multiple ServiceAccounts and Deployments", func() {
103155
depName := genName("dep-")
104156
csv := &operatorsv1alpha1.ClusterServiceVersion{

0 commit comments

Comments
 (0)