@@ -12,24 +12,24 @@ import (
12
12
"k8s.io/apimachinery/pkg/api/errors"
13
13
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
14
14
"k8s.io/apimachinery/pkg/labels"
15
- "k8s.io/client-go/metadata/metadatalister"
16
15
ktesting "k8s.io/client-go/testing"
17
16
18
17
"github.com/operator-framework/api/pkg/operators/v1alpha1"
19
18
20
19
"github.com/operator-framework/operator-lifecycle-manager/pkg/api/client/clientset/versioned/fake"
20
+ listersv1alpha1 "github.com/operator-framework/operator-lifecycle-manager/pkg/api/client/listers/operators/v1alpha1"
21
21
"github.com/operator-framework/operator-lifecycle-manager/pkg/controller/registry/resolver/cache"
22
22
"github.com/operator-framework/operator-registry/pkg/registry"
23
23
)
24
24
25
25
// fakeCSVNamespaceLister implements metadatalister.NamespaceLister
26
26
type fakeCSVNamespaceLister struct {
27
27
namespace string
28
- items []* metav1. PartialObjectMetadata
28
+ items []* v1alpha1. ClusterServiceVersion
29
29
}
30
30
31
- func (n * fakeCSVNamespaceLister ) List (selector labels.Selector ) ([]* metav1. PartialObjectMetadata , error ) {
32
- var result []* metav1. PartialObjectMetadata
31
+ func (n * fakeCSVNamespaceLister ) List (selector labels.Selector ) ([]* v1alpha1. ClusterServiceVersion , error ) {
32
+ var result []* v1alpha1. ClusterServiceVersion
33
33
for _ , item := range n .items {
34
34
if item != nil && item .Namespace == n .namespace {
35
35
result = append (result , item )
@@ -38,77 +38,7 @@ func (n *fakeCSVNamespaceLister) List(selector labels.Selector) ([]*metav1.Parti
38
38
return result , nil
39
39
}
40
40
41
- // Test full resync via metadata drift guard when observedGeneration mismatches
42
- func TestCopyToNamespace_MetadataDriftGuard (t * testing.T ) {
43
- // Prepare prototype CSV and compute hashes
44
- prototype := v1alpha1.ClusterServiceVersion {
45
- ObjectMeta : metav1.ObjectMeta {
46
- Name : "name" ,
47
- Annotations : map [string ]string {},
48
- },
49
- Spec : v1alpha1.ClusterServiceVersionSpec {Replaces : "replacee" },
50
- Status : v1alpha1.ClusterServiceVersionStatus {Phase : "waxing gibbous" },
51
- }
52
- specHash , statusHash , err := copyableCSVHash (& prototype )
53
- require .NoError (t , err )
54
-
55
- // Existing partial copy with observedGeneration mismatched
56
- existingCopy := & metav1.PartialObjectMetadata {
57
- ObjectMeta : metav1.ObjectMeta {
58
- Name : "name" ,
59
- Namespace : "to" ,
60
- UID : "uid" ,
61
- ResourceVersion : "42" ,
62
- Generation : 2 ,
63
- Annotations : map [string ]string {
64
- nonStatusCopyHashAnnotation : specHash ,
65
- statusCopyHashAnnotation : statusHash ,
66
- observedGenerationAnnotation : "1" ,
67
- observedResourceVersionAnnotation : "42" ,
68
- },
69
- },
70
- }
71
- // Full CSV for fake client
72
- full := & v1alpha1.ClusterServiceVersion {
73
- ObjectMeta : metav1.ObjectMeta {
74
- Name : existingCopy .Name ,
75
- Namespace : existingCopy .Namespace ,
76
- UID : existingCopy .UID ,
77
- ResourceVersion : existingCopy .ResourceVersion ,
78
- Generation : existingCopy .Generation ,
79
- Annotations : existingCopy .Annotations ,
80
- },
81
- Spec : prototype .Spec ,
82
- Status : prototype .Status ,
83
- }
84
-
85
- client := fake .NewSimpleClientset (full )
86
- lister := & fakeCSVLister {items : []* metav1.PartialObjectMetadata {existingCopy }}
87
- logger , _ := test .NewNullLogger ()
88
- o := & Operator {copiedCSVLister : lister , client : client , logger : logger }
89
-
90
- protoCopy := prototype .DeepCopy ()
91
- result , err := o .copyToNamespace (protoCopy , "from" , "to" , specHash , statusHash )
92
- require .NoError (t , err )
93
- require .Equal (t , "name" , result .GetName ())
94
- require .Equal (t , "to" , result .GetNamespace ())
95
- require .Equal (t , "uid" , string (result .GetUID ()))
96
-
97
- actions := client .Actions ()
98
- // Expect: update(spec), updateStatus, update(guard annotations)
99
- require .Len (t , actions , 3 )
100
- // First action: spec update
101
- ua0 := actions [0 ].(ktesting.UpdateAction )
102
- require .Equal (t , "" , ua0 .GetSubresource ())
103
- // Second action: status subresource
104
- ua1 := actions [1 ].(ktesting.UpdateAction )
105
- require .Equal (t , "status" , ua1 .GetSubresource ())
106
- // Third action: metadata guard update
107
- ua2 := actions [2 ].(ktesting.UpdateAction )
108
- require .Equal (t , "" , ua2 .GetSubresource ())
109
- }
110
-
111
- func (n * fakeCSVNamespaceLister ) Get (name string ) (* metav1.PartialObjectMetadata , error ) {
41
+ func (n * fakeCSVNamespaceLister ) Get (name string ) (* v1alpha1.ClusterServiceVersion , error ) {
112
42
for _ , item := range n .items {
113
43
if item != nil && item .Namespace == n .namespace && item .Name == name {
114
44
return item , nil
@@ -117,19 +47,19 @@ func (n *fakeCSVNamespaceLister) Get(name string) (*metav1.PartialObjectMetadata
117
47
return nil , errors .NewNotFound (v1alpha1 .Resource ("clusterserviceversion" ), name )
118
48
}
119
49
120
- // fakeCSVLister implements the full metadatalister.Lister interface
50
+ // fakeCSVLister implements the full listersv1alpha1.ClusterServiceVersionLister interface
121
51
// so that Operator.copiedCSVLister = &fakeCSVLister{...} works.
122
52
type fakeCSVLister struct {
123
- items []* metav1. PartialObjectMetadata
53
+ items []* v1alpha1. ClusterServiceVersion
124
54
}
125
55
126
56
// List returns all CSV metadata items, ignoring namespaces.
127
- func (f * fakeCSVLister ) List (selector labels.Selector ) ([]* metav1. PartialObjectMetadata , error ) {
57
+ func (f * fakeCSVLister ) List (selector labels.Selector ) ([]* v1alpha1. ClusterServiceVersion , error ) {
128
58
return f .items , nil
129
59
}
130
60
131
61
// Get returns the CSV by name, ignoring namespaces.
132
- func (f * fakeCSVLister ) Get (name string ) (* metav1. PartialObjectMetadata , error ) {
62
+ func (f * fakeCSVLister ) Get (name string ) (* v1alpha1. ClusterServiceVersion , error ) {
133
63
for _ , item := range f .items {
134
64
if item != nil && item .Name == name {
135
65
return item , nil
@@ -139,7 +69,7 @@ func (f *fakeCSVLister) Get(name string) (*metav1.PartialObjectMetadata, error)
139
69
}
140
70
141
71
// Namespace returns a namespace-scoped lister wrapper.
142
- func (f * fakeCSVLister ) Namespace (ns string ) metadatalister. NamespaceLister {
72
+ func (f * fakeCSVLister ) ClusterServiceVersions (ns string ) listersv1alpha1. ClusterServiceVersionNamespaceLister {
143
73
return & fakeCSVNamespaceLister {
144
74
namespace : ns ,
145
75
items : f .items ,
@@ -156,7 +86,7 @@ func TestCopyToNamespace(t *testing.T) {
156
86
Hash string
157
87
StatusHash string
158
88
Prototype v1alpha1.ClusterServiceVersion
159
- ExistingCopy * metav1. PartialObjectMetadata
89
+ ExistingCopy * v1alpha1. ClusterServiceVersion
160
90
ExpectedResult * v1alpha1.ClusterServiceVersion
161
91
ExpectedError error
162
92
ExpectedActions []ktesting.Action
@@ -264,15 +194,15 @@ func TestCopyToNamespace(t *testing.T) {
264
194
Phase : "waxing gibbous" ,
265
195
},
266
196
},
267
- ExistingCopy : & metav1. PartialObjectMetadata {
197
+ ExistingCopy : & v1alpha1. ClusterServiceVersion {
268
198
ObjectMeta : metav1.ObjectMeta {
269
199
Name : "name" ,
270
200
Namespace : "to" ,
271
201
UID : "uid" ,
272
202
ResourceVersion : "42" ,
273
203
Annotations : map [string ]string {
274
- nonStatusCopyHashAnnotation : "hn-2" , // differs => triggers normal Update
275
- statusCopyHashAnnotation : "hs" , // same => no status update
204
+ copyCSVSpecHash : "hn-2" , // differs => triggers normal Update
205
+ copyCSVStatusHash : "hs" , // same => no status update
276
206
},
277
207
},
278
208
},
@@ -286,7 +216,7 @@ func TestCopyToNamespace(t *testing.T) {
286
216
ResourceVersion : "42" ,
287
217
// We'll set the new nonStatusCopyHashAnnotation = "hn-1"
288
218
Annotations : map [string ]string {
289
- nonStatusCopyHashAnnotation : "hn-1" ,
219
+ copyCSVSpecHash : "hn-1" ,
290
220
},
291
221
},
292
222
Spec : v1alpha1.ClusterServiceVersionSpec {
@@ -323,17 +253,17 @@ func TestCopyToNamespace(t *testing.T) {
323
253
Phase : "waxing gibbous" ,
324
254
},
325
255
},
326
- ExistingCopy : & metav1. PartialObjectMetadata {
256
+ ExistingCopy : & v1alpha1. ClusterServiceVersion {
327
257
ObjectMeta : metav1.ObjectMeta {
328
258
Name : "name" ,
329
259
Namespace : "to" ,
330
260
UID : "uid" ,
331
261
ResourceVersion : "42" ,
332
262
Annotations : map [string ]string {
333
263
// non-status matches => no normal update
334
- nonStatusCopyHashAnnotation : "hn" ,
264
+ copyCSVSpecHash : "hn" ,
335
265
// status differs => subresource + normal update
336
- statusCopyHashAnnotation : "hs-2" ,
266
+ copyCSVStatusHash : "hs-2" ,
337
267
},
338
268
},
339
269
},
@@ -346,7 +276,7 @@ func TestCopyToNamespace(t *testing.T) {
346
276
UID : "uid" ,
347
277
ResourceVersion : "42" ,
348
278
Annotations : map [string ]string {
349
- nonStatusCopyHashAnnotation : "hn" ,
279
+ copyCSVSpecHash : "hn" ,
350
280
},
351
281
},
352
282
Spec : v1alpha1.ClusterServiceVersionSpec {
@@ -364,8 +294,8 @@ func TestCopyToNamespace(t *testing.T) {
364
294
UID : "uid" ,
365
295
ResourceVersion : "42" ,
366
296
Annotations : map [string ]string {
367
- nonStatusCopyHashAnnotation : "hn" ,
368
- statusCopyHashAnnotation : "hs-1" ,
297
+ copyCSVSpecHash : "hn" ,
298
+ copyCSVStatusHash : "hs-1" ,
369
299
},
370
300
},
371
301
Spec : v1alpha1.ClusterServiceVersionSpec {
@@ -402,16 +332,16 @@ func TestCopyToNamespace(t *testing.T) {
402
332
Phase : "waxing gibbous" ,
403
333
},
404
334
},
405
- ExistingCopy : & metav1. PartialObjectMetadata {
335
+ ExistingCopy : & v1alpha1. ClusterServiceVersion {
406
336
ObjectMeta : metav1.ObjectMeta {
407
337
Name : "name" ,
408
338
Namespace : "to" ,
409
339
UID : "uid" ,
410
340
ResourceVersion : "42" ,
411
341
Annotations : map [string ]string {
412
342
// Both nonStatus and status mismatch
413
- nonStatusCopyHashAnnotation : "hn-2" ,
414
- statusCopyHashAnnotation : "hs-2" ,
343
+ copyCSVSpecHash : "hn-2" ,
344
+ copyCSVStatusHash : "hs-2" ,
415
345
},
416
346
},
417
347
},
@@ -424,7 +354,7 @@ func TestCopyToNamespace(t *testing.T) {
424
354
UID : "uid" ,
425
355
ResourceVersion : "42" ,
426
356
Annotations : map [string ]string {
427
- nonStatusCopyHashAnnotation : "hn-1" ,
357
+ copyCSVSpecHash : "hn-1" ,
428
358
},
429
359
},
430
360
Spec : v1alpha1.ClusterServiceVersionSpec {
@@ -442,7 +372,7 @@ func TestCopyToNamespace(t *testing.T) {
442
372
UID : "uid" ,
443
373
ResourceVersion : "42" ,
444
374
Annotations : map [string ]string {
445
- nonStatusCopyHashAnnotation : "hn-1" ,
375
+ copyCSVSpecHash : "hn-1" ,
446
376
},
447
377
},
448
378
Spec : v1alpha1.ClusterServiceVersionSpec {
@@ -460,8 +390,8 @@ func TestCopyToNamespace(t *testing.T) {
460
390
UID : "uid" ,
461
391
ResourceVersion : "42" ,
462
392
Annotations : map [string ]string {
463
- nonStatusCopyHashAnnotation : "hn-1" ,
464
- statusCopyHashAnnotation : "hs-1" ,
393
+ copyCSVSpecHash : "hn-1" ,
394
+ copyCSVStatusHash : "hs-1" ,
465
395
},
466
396
},
467
397
Spec : v1alpha1.ClusterServiceVersionSpec {
@@ -492,14 +422,14 @@ func TestCopyToNamespace(t *testing.T) {
492
422
Annotations : map [string ]string {},
493
423
},
494
424
},
495
- ExistingCopy : & metav1. PartialObjectMetadata {
425
+ ExistingCopy : & v1alpha1. ClusterServiceVersion {
496
426
ObjectMeta : metav1.ObjectMeta {
497
427
Name : "name" ,
498
428
Namespace : "to" ,
499
429
UID : "uid" ,
500
430
Annotations : map [string ]string {
501
- nonStatusCopyHashAnnotation : "hn" ,
502
- statusCopyHashAnnotation : "hs" ,
431
+ copyCSVSpecHash : "hn" ,
432
+ copyCSVStatusHash : "hs" ,
503
433
},
504
434
},
505
435
},
@@ -516,17 +446,17 @@ func TestCopyToNamespace(t *testing.T) {
516
446
t .Run (tc .Name , func (t * testing.T ) {
517
447
// Create a new fake clientset populated with the "existing copy" if any
518
448
client := fake .NewSimpleClientset ()
519
- var lister metadatalister. Lister
449
+ var lister listersv1alpha1. ClusterServiceVersionLister
520
450
521
451
// If we have an existing CSV in that target namespace, add it to the slice
522
- items := []* metav1. PartialObjectMetadata {}
452
+ items := []* v1alpha1. ClusterServiceVersion {}
523
453
if tc .ExistingCopy != nil {
524
454
existingObj := & v1alpha1.ClusterServiceVersion {
525
455
ObjectMeta : tc .ExistingCopy .ObjectMeta ,
526
456
// ... if you want to set Spec/Status in the client, you can
527
457
}
528
458
client = fake .NewSimpleClientset (existingObj )
529
- items = []* metav1. PartialObjectMetadata {tc .ExistingCopy }
459
+ items = []* v1alpha1. ClusterServiceVersion {tc .ExistingCopy }
530
460
}
531
461
532
462
// Create the full Lister
@@ -549,9 +479,9 @@ func TestCopyToNamespace(t *testing.T) {
549
479
550
480
// Ensure the in-memory 'proto' has the correct final annotations
551
481
annotations := proto .GetObjectMeta ().GetAnnotations ()
552
- require .Equal (t , tc .Hash , annotations [nonStatusCopyHashAnnotation ],
482
+ require .Equal (t , tc .Hash , annotations [copyCSVSpecHash ],
553
483
"proto should have the non-status hash annotation set" )
554
- require .Equal (t , tc .StatusHash , annotations [statusCopyHashAnnotation ],
484
+ require .Equal (t , tc .StatusHash , annotations [copyCSVStatusHash ],
555
485
"proto should have the status hash annotation set" )
556
486
} else {
557
487
require .EqualError (t , err , tc .ExpectedError .Error ())
@@ -572,10 +502,10 @@ func TestCopyToNamespace(t *testing.T) {
572
502
}
573
503
}
574
504
575
- type FakeClusterServiceVersionLister []* metav1. PartialObjectMetadata
505
+ type FakeClusterServiceVersionLister []* v1alpha1. ClusterServiceVersion
576
506
577
- func (l FakeClusterServiceVersionLister ) List (selector labels.Selector ) ([]* metav1. PartialObjectMetadata , error ) {
578
- var result []* metav1. PartialObjectMetadata
507
+ func (l FakeClusterServiceVersionLister ) List (selector labels.Selector ) ([]* v1alpha1. ClusterServiceVersion , error ) {
508
+ var result []* v1alpha1. ClusterServiceVersion
579
509
for _ , csv := range l {
580
510
if ! selector .Matches (labels .Set (csv .GetLabels ())) {
581
511
continue
@@ -585,8 +515,8 @@ func (l FakeClusterServiceVersionLister) List(selector labels.Selector) ([]*meta
585
515
return result , nil
586
516
}
587
517
588
- func (l FakeClusterServiceVersionLister ) Namespace (namespace string ) metadatalister. NamespaceLister {
589
- var filtered []* metav1. PartialObjectMetadata
518
+ func (l FakeClusterServiceVersionLister ) ClusterServiceVersions (namespace string ) listersv1alpha1. ClusterServiceVersionNamespaceLister {
519
+ var filtered []* v1alpha1. ClusterServiceVersion
590
520
for _ , csv := range l {
591
521
if csv .GetNamespace () != namespace {
592
522
continue
@@ -596,7 +526,7 @@ func (l FakeClusterServiceVersionLister) Namespace(namespace string) metadatalis
596
526
return FakeClusterServiceVersionLister (filtered )
597
527
}
598
528
599
- func (l FakeClusterServiceVersionLister ) Get (name string ) (* metav1. PartialObjectMetadata , error ) {
529
+ func (l FakeClusterServiceVersionLister ) Get (name string ) (* v1alpha1. ClusterServiceVersion , error ) {
600
530
for _ , csv := range l {
601
531
if csv .GetName () == name {
602
532
return csv , nil
@@ -606,8 +536,8 @@ func (l FakeClusterServiceVersionLister) Get(name string) (*metav1.PartialObject
606
536
}
607
537
608
538
var (
609
- _ metadatalister. Lister = FakeClusterServiceVersionLister {}
610
- _ metadatalister. NamespaceLister = FakeClusterServiceVersionLister {}
539
+ _ listersv1alpha1. ClusterServiceVersionLister = FakeClusterServiceVersionLister {}
540
+ _ listersv1alpha1. ClusterServiceVersionNamespaceLister = FakeClusterServiceVersionLister {}
611
541
)
612
542
613
543
func TestCSVCopyPrototype (t * testing.T ) {
0 commit comments