Skip to content

Commit 385eb21

Browse files
author
Per Goncalves da Silva
committed
[CARRY] fixup ns labeler plugin
Signed-off-by: Per Goncalves da Silva <[email protected]>
1 parent 2c4ce67 commit 385eb21

File tree

3 files changed

+42
-78
lines changed

3 files changed

+42
-78
lines changed

staging/operator-lifecycle-manager/pkg/controller/operators/olm/plugins/downstream_csv_namespace_labeler_plugin.go

Lines changed: 12 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,12 @@ package plugins
33
import (
44
"context"
55
"fmt"
6+
"sigs.k8s.io/controller-runtime/pkg/client"
67
"strings"
78

89
"github.com/operator-framework/api/pkg/operators/v1alpha1"
910
"github.com/operator-framework/operator-lifecycle-manager/pkg/api/client/clientset/versioned"
1011
listerv1alpha1 "github.com/operator-framework/operator-lifecycle-manager/pkg/api/client/listers/operators/v1alpha1"
11-
"github.com/operator-framework/operator-lifecycle-manager/pkg/lib/kubestate"
1212
"github.com/operator-framework/operator-lifecycle-manager/pkg/lib/operatorclient"
1313
"github.com/operator-framework/operator-lifecycle-manager/pkg/lib/queueinformer"
1414
"github.com/sirupsen/logrus"
@@ -63,10 +63,11 @@ func NewCsvNamespaceLabelerPluginFunc(ctx context.Context, config OperatorConfig
6363

6464
plugin.namespaceLister = listerv1.NewNamespaceLister(namespaceInformer.GetIndexer())
6565

66-
namespaceQueue := workqueue.NewNamedRateLimitingQueue(
66+
namespaceQueue := workqueue.NewRateLimitingQueueWithConfig(
6767
workqueue.DefaultControllerRateLimiter(),
68-
"csv-ns-labeler-plugin-ns-queue",
69-
)
68+
workqueue.RateLimitingQueueConfig{
69+
Name: "csv-ns-labeler-plugin-ns-queue",
70+
})
7071
namespaceQueueInformer, err := queueinformer.NewQueueInformer(
7172
ctx,
7273
queueinformer.WithInformer(namespaceInformer),
@@ -91,10 +92,12 @@ func NewCsvNamespaceLabelerPluginFunc(ctx context.Context, config OperatorConfig
9192

9293
nonCopiedCsvInformer := hostOperator.Informers()[namespace].CSVInformer.Informer()
9394

94-
nonCopiedCsvQueue := workqueue.NewNamedRateLimitingQueue(
95+
nonCopiedCsvQueue := workqueue.NewRateLimitingQueueWithConfig(
9596
workqueue.DefaultControllerRateLimiter(),
96-
fmt.Sprintf("%s/csv-ns-labeler-plugin-csv-queue", namespace),
97-
)
97+
workqueue.RateLimitingQueueConfig{
98+
Name: fmt.Sprintf("%s/csv-ns-labeler-plugin-csv-queue", namespace),
99+
})
100+
98101
nonCopiedCsvQueueInformer, err := queueinformer.NewQueueInformer(
99102
ctx,
100103
queueinformer.WithInformer(nonCopiedCsvInformer),
@@ -121,17 +124,12 @@ func (p *csvNamespaceLabelerPlugin) Shutdown() error {
121124
return nil
122125
}
123126

124-
func (p *csvNamespaceLabelerPlugin) Sync(ctx context.Context, event kubestate.ResourceEvent) error {
125-
// only act on csv added and updated events
126-
if event.Type() != kubestate.ResourceAdded && event.Type() != kubestate.ResourceUpdated {
127-
return nil
128-
}
129-
127+
func (p *csvNamespaceLabelerPlugin) Sync(ctx context.Context, obj client.Object) error {
130128
var namespace *v1.Namespace
131129
var err error
132130

133131
// get namespace from the event resource
134-
switch eventResource := event.Resource().(type) {
132+
switch eventResource := obj.(type) {
135133

136134
// handle csv events
137135
case *v1alpha1.ClusterServiceVersion:

staging/operator-lifecycle-manager/pkg/controller/operators/olm/plugins/downstream_csv_namespace_labeler_plugin_test.go

Lines changed: 18 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@ import (
1111
"github.com/operator-framework/operator-lifecycle-manager/pkg/api/client/clientset/versioned/fake"
1212
"github.com/operator-framework/operator-lifecycle-manager/pkg/api/client/informers/externalversions"
1313
listerv1alpha1 "github.com/operator-framework/operator-lifecycle-manager/pkg/api/client/listers/operators/v1alpha1"
14-
"github.com/operator-framework/operator-lifecycle-manager/pkg/lib/kubestate"
1514
"github.com/operator-framework/operator-lifecycle-manager/pkg/lib/operatorclient"
1615
"github.com/stretchr/testify/assert"
1716
v1 "k8s.io/api/core/v1"
@@ -139,28 +138,13 @@ func NewLabeledNamespace(name string, labelValue string) *v1.Namespace {
139138
return ns
140139
}
141140

142-
func Test_SyncIgnoresDeletionEvent(t *testing.T) {
143-
// Sync ignores deletion events
144-
namespace := "test-namespace"
145-
plugin, shutdown := NewFakeCSVNamespaceLabelerPlugin(t, withK8sResources(NewNamespace(namespace)))
146-
defer shutdown()
147-
148-
event := kubestate.NewResourceEvent(kubestate.ResourceDeleted, NewCsvInNamespace(namespace))
149-
assert.Nil(t, plugin.Sync(context.Background(), event))
150-
151-
ns, err := plugin.kubeClient.KubernetesInterface().CoreV1().Namespaces().Get(context.Background(), namespace, metav1.GetOptions{})
152-
assert.NoError(t, err)
153-
assert.NotContains(t, ns.GetLabels(), NamespaceLabelSyncerLabelKey)
154-
}
155-
156141
func Test_SyncIgnoresCopiedCsvs(t *testing.T) {
157142
// Sync ignores copied csvs
158143
namespace := "openshift-test"
159144
plugin, shutdown := NewFakeCSVNamespaceLabelerPlugin(t, withK8sResources(NewNamespace(namespace)))
160145
defer shutdown()
161146

162-
event := kubestate.NewResourceEvent(kubestate.ResourceAdded, NewCopiedCsvInNamespace(namespace))
163-
assert.Nil(t, plugin.Sync(context.Background(), event))
147+
assert.Nil(t, plugin.Sync(context.Background(), NewCopiedCsvInNamespace(namespace)))
164148

165149
ns, err := plugin.kubeClient.KubernetesInterface().CoreV1().Namespaces().Get(context.Background(), namespace, metav1.GetOptions{})
166150
assert.NoError(t, err)
@@ -173,8 +157,7 @@ func Test_SyncIgnoresNonOpenshiftNamespaces(t *testing.T) {
173157
plugin, shutdown := NewFakeCSVNamespaceLabelerPlugin(t, withK8sResources(NewNamespace(namespace)))
174158
defer shutdown()
175159

176-
event := kubestate.NewResourceEvent(kubestate.ResourceAdded, NewCopiedCsvInNamespace(namespace))
177-
assert.Nil(t, plugin.Sync(context.Background(), event))
160+
assert.Nil(t, plugin.Sync(context.Background(), NewCopiedCsvInNamespace(namespace)))
178161

179162
ns, err := plugin.kubeClient.KubernetesInterface().CoreV1().Namespaces().Get(context.Background(), namespace, metav1.GetOptions{})
180163
assert.NoError(t, err)
@@ -187,16 +170,14 @@ func Test_SyncIgnoresPayloadOpenshiftNamespacesExceptOperators(t *testing.T) {
187170
plugin, shutdown := NewFakeCSVNamespaceLabelerPlugin(t, withK8sResources(NewNamespace("openshift-monitoring"), NewNamespace("openshift-operators")))
188171
defer shutdown()
189172

190-
event := kubestate.NewResourceEvent(kubestate.ResourceAdded, NewCsvInNamespace("openshift-monitoring"))
191-
assert.Nil(t, plugin.Sync(context.Background(), event))
173+
assert.Nil(t, plugin.Sync(context.Background(), NewCsvInNamespace("openshift-monitoring")))
192174

193175
ns, err := plugin.kubeClient.KubernetesInterface().CoreV1().Namespaces().Get(context.Background(), "openshift-monitoring", metav1.GetOptions{})
194176
assert.NoError(t, err)
195177
assert.NotContains(t, ns.GetLabels(), NamespaceLabelSyncerLabelKey)
196178

197179
// openshift-operators sync -> label added
198-
event = kubestate.NewResourceEvent(kubestate.ResourceAdded, NewCsvInNamespace("openshift-operators"))
199-
assert.Nil(t, plugin.Sync(context.Background(), event))
180+
assert.Nil(t, plugin.Sync(context.Background(), NewCsvInNamespace("openshift-operators")))
200181
ns, err = plugin.kubeClient.KubernetesInterface().CoreV1().Namespaces().Get(context.Background(), "openshift-operators", metav1.GetOptions{})
201182
assert.NoError(t, err)
202183
assert.Equal(t, "true", ns.GetLabels()[NamespaceLabelSyncerLabelKey])
@@ -212,8 +193,7 @@ func Test_SyncIgnoresAlreadyLabeledNonPayloadOpenshiftNamespaces(t *testing.T) {
212193
plugin, shutdown := NewFakeCSVNamespaceLabelerPlugin(t, withK8sResources(NewLabeledNamespace(namespace, labelValue)))
213194
defer shutdown()
214195

215-
event := kubestate.NewResourceEvent(kubestate.ResourceUpdated, NewCsvInNamespace(namespace))
216-
assert.Nil(t, plugin.Sync(context.Background(), event))
196+
assert.Nil(t, plugin.Sync(context.Background(), NewCsvInNamespace(namespace)))
217197

218198
ns, err := plugin.kubeClient.KubernetesInterface().CoreV1().Namespaces().Get(context.Background(), namespace, metav1.GetOptions{})
219199
assert.NoError(t, err)
@@ -223,41 +203,33 @@ func Test_SyncIgnoresAlreadyLabeledNonPayloadOpenshiftNamespaces(t *testing.T) {
223203
}
224204

225205
func Test_SyncLabelsNonPayloadUnlabeledOpenshiftNamespaces(t *testing.T) {
226-
// Sync will label non-labeled non-payload openshift- namespaces independent of event type (except deletion, tested separately)
227-
eventTypes := []kubestate.ResourceEventType{kubestate.ResourceUpdated, kubestate.ResourceAdded}
206+
// Sync will label non-labeled non-payload openshift- namespaces
228207
namespace := "openshift-test"
229208

230-
for _, eventType := range eventTypes {
231-
func() {
232-
plugin, shutdown := NewFakeCSVNamespaceLabelerPlugin(t, withK8sResources(NewNamespace(namespace)))
233-
defer shutdown()
209+
plugin, shutdown := NewFakeCSVNamespaceLabelerPlugin(t, withK8sResources(NewNamespace(namespace)))
210+
defer shutdown()
234211

235-
event := kubestate.NewResourceEvent(eventType, NewCsvInNamespace(namespace))
236-
assert.Nil(t, plugin.Sync(context.Background(), event))
212+
assert.Nil(t, plugin.Sync(context.Background(), NewCsvInNamespace(namespace)))
237213

238-
ns, err := plugin.kubeClient.KubernetesInterface().CoreV1().Namespaces().Get(context.Background(), namespace, metav1.GetOptions{})
239-
assert.NoError(t, err)
240-
assert.Contains(t, ns.GetLabels(), NamespaceLabelSyncerLabelKey)
241-
}()
242-
}
214+
ns, err := plugin.kubeClient.KubernetesInterface().CoreV1().Namespaces().Get(context.Background(), namespace, metav1.GetOptions{})
215+
assert.NoError(t, err)
216+
assert.Contains(t, ns.GetLabels(), NamespaceLabelSyncerLabelKey)
243217
}
244218

245219
func Test_SyncFailsIfEventResourceIsNotCSV(t *testing.T) {
246220
// Sync fails if resource is not a csv\
247221
plugin, shutdown := NewFakeCSVNamespaceLabelerPlugin(t)
248222
defer shutdown()
249223

250-
event := kubestate.NewResourceEvent(kubestate.ResourceAdded, v1.ConfigMap{})
251-
assert.Error(t, plugin.Sync(context.Background(), event))
224+
assert.Error(t, plugin.Sync(context.Background(), &v1.ConfigMap{}))
252225
}
253226

254227
func Test_SyncFailsIfNamespaceNotFound(t *testing.T) {
255228
// Sync fails if the namespace is not found
256229
plugin, shutdown := NewFakeCSVNamespaceLabelerPlugin(t)
257230
defer shutdown()
258231

259-
event := kubestate.NewResourceEvent(kubestate.ResourceAdded, NewCsvInNamespace("openshift-test"))
260-
assert.Error(t, plugin.Sync(context.Background(), event))
232+
assert.Error(t, plugin.Sync(context.Background(), NewCsvInNamespace("openshift-test")))
261233
}
262234

263235
func Test_SyncFailsIfCSVCannotBeUpdated(t *testing.T) {
@@ -266,12 +238,11 @@ func Test_SyncFailsIfCSVCannotBeUpdated(t *testing.T) {
266238
plugin, shutdown := NewFakeCSVNamespaceLabelerPlugin(t, withK8sResources(NewNamespace(namespace)))
267239
defer shutdown()
268240

269-
event := kubestate.NewResourceEvent(kubestate.ResourceAdded, NewCsvInNamespace(namespace))
270241
updateNsError := func(action clienttesting.Action) (handled bool, ret runtime.Object, err error) {
271242
return true, &v1.Namespace{}, errors.New("error updating namespace")
272243
}
273244
plugin.kubeClient.KubernetesInterface().CoreV1().(*v1fake.FakeCoreV1).PrependReactor("update", "namespaces", updateNsError)
274-
assert.Error(t, plugin.Sync(context.Background(), event))
245+
assert.Error(t, plugin.Sync(context.Background(), NewCsvInNamespace(namespace)))
275246
}
276247

277248
func Test_SyncLabelsNamespaceWithCSV(t *testing.T) {
@@ -283,8 +254,7 @@ func Test_SyncLabelsNamespaceWithCSV(t *testing.T) {
283254
plugin, shutdown := NewFakeCSVNamespaceLabelerPlugin(t, withK8sResources(namespace), withExtendedResources(csv))
284255
defer shutdown()
285256

286-
event := kubestate.NewResourceEvent(kubestate.ResourceUpdated, namespace)
287-
assert.NoError(t, plugin.Sync(context.Background(), event))
257+
assert.NoError(t, plugin.Sync(context.Background(), namespace))
288258

289259
ns, err := plugin.kubeClient.KubernetesInterface().CoreV1().Namespaces().Get(context.Background(), namespace.GetName(), metav1.GetOptions{})
290260
assert.NoError(t, err)
@@ -299,8 +269,7 @@ func Test_SyncDoesNotLabelNamespaceWithoutCSVs(t *testing.T) {
299269
plugin, shutdown := NewFakeCSVNamespaceLabelerPlugin(t, withK8sResources(namespace))
300270
defer shutdown()
301271

302-
event := kubestate.NewResourceEvent(kubestate.ResourceUpdated, namespace)
303-
assert.NoError(t, plugin.Sync(context.Background(), event))
272+
assert.NoError(t, plugin.Sync(context.Background(), namespace))
304273

305274
ns, err := plugin.kubeClient.KubernetesInterface().CoreV1().Namespaces().Get(context.Background(), namespace.GetName(), metav1.GetOptions{})
306275
assert.NoError(t, err)
@@ -316,8 +285,7 @@ func Test_SyncDoesNotLabelNamespacesWithCopiedCSVs(t *testing.T) {
316285
plugin, shutdown := NewFakeCSVNamespaceLabelerPlugin(t, withK8sResources(namespace), withExtendedResources(csv))
317286
defer shutdown()
318287

319-
event := kubestate.NewResourceEvent(kubestate.ResourceUpdated, namespace)
320-
assert.NoError(t, plugin.Sync(context.Background(), event))
288+
assert.NoError(t, plugin.Sync(context.Background(), namespace))
321289

322290
ns, err := plugin.kubeClient.KubernetesInterface().CoreV1().Namespaces().Get(context.Background(), namespace.GetName(), metav1.GetOptions{})
323291
assert.NoError(t, err)

vendor/github.com/operator-framework/operator-lifecycle-manager/pkg/controller/operators/olm/plugins/downstream_csv_namespace_labeler_plugin.go

Lines changed: 12 additions & 14 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)