@@ -12,24 +12,24 @@ import (
1212 "k8s.io/apimachinery/pkg/api/errors"
1313 metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
1414 "k8s.io/apimachinery/pkg/labels"
15- "k8s.io/client-go/metadata/metadatalister"
1615 ktesting "k8s.io/client-go/testing"
1716
1817 "github.com/operator-framework/api/pkg/operators/v1alpha1"
1918
2019 "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"
2121 "github.com/operator-framework/operator-lifecycle-manager/pkg/controller/registry/resolver/cache"
2222 "github.com/operator-framework/operator-registry/pkg/registry"
2323)
2424
2525// fakeCSVNamespaceLister implements metadatalister.NamespaceLister
2626type fakeCSVNamespaceLister struct {
2727 namespace string
28- items []* metav1. PartialObjectMetadata
28+ items []* v1alpha1. ClusterServiceVersion
2929}
3030
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
3333 for _ , item := range n .items {
3434 if item != nil && item .Namespace == n .namespace {
3535 result = append (result , item )
@@ -38,77 +38,7 @@ func (n *fakeCSVNamespaceLister) List(selector labels.Selector) ([]*metav1.Parti
3838 return result , nil
3939}
4040
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 ) {
11242 for _ , item := range n .items {
11343 if item != nil && item .Namespace == n .namespace && item .Name == name {
11444 return item , nil
@@ -117,19 +47,19 @@ func (n *fakeCSVNamespaceLister) Get(name string) (*metav1.PartialObjectMetadata
11747 return nil , errors .NewNotFound (v1alpha1 .Resource ("clusterserviceversion" ), name )
11848}
11949
120- // fakeCSVLister implements the full metadatalister.Lister interface
50+ // fakeCSVLister implements the full listersv1alpha1.ClusterServiceVersionLister interface
12151// so that Operator.copiedCSVLister = &fakeCSVLister{...} works.
12252type fakeCSVLister struct {
123- items []* metav1. PartialObjectMetadata
53+ items []* v1alpha1. ClusterServiceVersion
12454}
12555
12656// 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 ) {
12858 return f .items , nil
12959}
13060
13161// 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 ) {
13363 for _ , item := range f .items {
13464 if item != nil && item .Name == name {
13565 return item , nil
@@ -139,7 +69,7 @@ func (f *fakeCSVLister) Get(name string) (*metav1.PartialObjectMetadata, error)
13969}
14070
14171// 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 {
14373 return & fakeCSVNamespaceLister {
14474 namespace : ns ,
14575 items : f .items ,
@@ -156,7 +86,7 @@ func TestCopyToNamespace(t *testing.T) {
15686 Hash string
15787 StatusHash string
15888 Prototype v1alpha1.ClusterServiceVersion
159- ExistingCopy * metav1. PartialObjectMetadata
89+ ExistingCopy * v1alpha1. ClusterServiceVersion
16090 ExpectedResult * v1alpha1.ClusterServiceVersion
16191 ExpectedError error
16292 ExpectedActions []ktesting.Action
@@ -264,15 +194,15 @@ func TestCopyToNamespace(t *testing.T) {
264194 Phase : "waxing gibbous" ,
265195 },
266196 },
267- ExistingCopy : & metav1. PartialObjectMetadata {
197+ ExistingCopy : & v1alpha1. ClusterServiceVersion {
268198 ObjectMeta : metav1.ObjectMeta {
269199 Name : "name" ,
270200 Namespace : "to" ,
271201 UID : "uid" ,
272202 ResourceVersion : "42" ,
273203 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
276206 },
277207 },
278208 },
@@ -286,7 +216,7 @@ func TestCopyToNamespace(t *testing.T) {
286216 ResourceVersion : "42" ,
287217 // We'll set the new nonStatusCopyHashAnnotation = "hn-1"
288218 Annotations : map [string ]string {
289- nonStatusCopyHashAnnotation : "hn-1" ,
219+ copyCSVSpecHash : "hn-1" ,
290220 },
291221 },
292222 Spec : v1alpha1.ClusterServiceVersionSpec {
@@ -323,17 +253,17 @@ func TestCopyToNamespace(t *testing.T) {
323253 Phase : "waxing gibbous" ,
324254 },
325255 },
326- ExistingCopy : & metav1. PartialObjectMetadata {
256+ ExistingCopy : & v1alpha1. ClusterServiceVersion {
327257 ObjectMeta : metav1.ObjectMeta {
328258 Name : "name" ,
329259 Namespace : "to" ,
330260 UID : "uid" ,
331261 ResourceVersion : "42" ,
332262 Annotations : map [string ]string {
333263 // non-status matches => no normal update
334- nonStatusCopyHashAnnotation : "hn" ,
264+ copyCSVSpecHash : "hn" ,
335265 // status differs => subresource + normal update
336- statusCopyHashAnnotation : "hs-2" ,
266+ copyCSVStatusHash : "hs-2" ,
337267 },
338268 },
339269 },
@@ -346,7 +276,7 @@ func TestCopyToNamespace(t *testing.T) {
346276 UID : "uid" ,
347277 ResourceVersion : "42" ,
348278 Annotations : map [string ]string {
349- nonStatusCopyHashAnnotation : "hn" ,
279+ copyCSVSpecHash : "hn" ,
350280 },
351281 },
352282 Spec : v1alpha1.ClusterServiceVersionSpec {
@@ -364,8 +294,8 @@ func TestCopyToNamespace(t *testing.T) {
364294 UID : "uid" ,
365295 ResourceVersion : "42" ,
366296 Annotations : map [string ]string {
367- nonStatusCopyHashAnnotation : "hn" ,
368- statusCopyHashAnnotation : "hs-1" ,
297+ copyCSVSpecHash : "hn" ,
298+ copyCSVStatusHash : "hs-1" ,
369299 },
370300 },
371301 Spec : v1alpha1.ClusterServiceVersionSpec {
@@ -402,16 +332,16 @@ func TestCopyToNamespace(t *testing.T) {
402332 Phase : "waxing gibbous" ,
403333 },
404334 },
405- ExistingCopy : & metav1. PartialObjectMetadata {
335+ ExistingCopy : & v1alpha1. ClusterServiceVersion {
406336 ObjectMeta : metav1.ObjectMeta {
407337 Name : "name" ,
408338 Namespace : "to" ,
409339 UID : "uid" ,
410340 ResourceVersion : "42" ,
411341 Annotations : map [string ]string {
412342 // Both nonStatus and status mismatch
413- nonStatusCopyHashAnnotation : "hn-2" ,
414- statusCopyHashAnnotation : "hs-2" ,
343+ copyCSVSpecHash : "hn-2" ,
344+ copyCSVStatusHash : "hs-2" ,
415345 },
416346 },
417347 },
@@ -424,7 +354,7 @@ func TestCopyToNamespace(t *testing.T) {
424354 UID : "uid" ,
425355 ResourceVersion : "42" ,
426356 Annotations : map [string ]string {
427- nonStatusCopyHashAnnotation : "hn-1" ,
357+ copyCSVSpecHash : "hn-1" ,
428358 },
429359 },
430360 Spec : v1alpha1.ClusterServiceVersionSpec {
@@ -442,7 +372,7 @@ func TestCopyToNamespace(t *testing.T) {
442372 UID : "uid" ,
443373 ResourceVersion : "42" ,
444374 Annotations : map [string ]string {
445- nonStatusCopyHashAnnotation : "hn-1" ,
375+ copyCSVSpecHash : "hn-1" ,
446376 },
447377 },
448378 Spec : v1alpha1.ClusterServiceVersionSpec {
@@ -460,8 +390,8 @@ func TestCopyToNamespace(t *testing.T) {
460390 UID : "uid" ,
461391 ResourceVersion : "42" ,
462392 Annotations : map [string ]string {
463- nonStatusCopyHashAnnotation : "hn-1" ,
464- statusCopyHashAnnotation : "hs-1" ,
393+ copyCSVSpecHash : "hn-1" ,
394+ copyCSVStatusHash : "hs-1" ,
465395 },
466396 },
467397 Spec : v1alpha1.ClusterServiceVersionSpec {
@@ -492,14 +422,14 @@ func TestCopyToNamespace(t *testing.T) {
492422 Annotations : map [string ]string {},
493423 },
494424 },
495- ExistingCopy : & metav1. PartialObjectMetadata {
425+ ExistingCopy : & v1alpha1. ClusterServiceVersion {
496426 ObjectMeta : metav1.ObjectMeta {
497427 Name : "name" ,
498428 Namespace : "to" ,
499429 UID : "uid" ,
500430 Annotations : map [string ]string {
501- nonStatusCopyHashAnnotation : "hn" ,
502- statusCopyHashAnnotation : "hs" ,
431+ copyCSVSpecHash : "hn" ,
432+ copyCSVStatusHash : "hs" ,
503433 },
504434 },
505435 },
@@ -516,17 +446,17 @@ func TestCopyToNamespace(t *testing.T) {
516446 t .Run (tc .Name , func (t * testing.T ) {
517447 // Create a new fake clientset populated with the "existing copy" if any
518448 client := fake .NewSimpleClientset ()
519- var lister metadatalister. Lister
449+ var lister listersv1alpha1. ClusterServiceVersionLister
520450
521451 // If we have an existing CSV in that target namespace, add it to the slice
522- items := []* metav1. PartialObjectMetadata {}
452+ items := []* v1alpha1. ClusterServiceVersion {}
523453 if tc .ExistingCopy != nil {
524454 existingObj := & v1alpha1.ClusterServiceVersion {
525455 ObjectMeta : tc .ExistingCopy .ObjectMeta ,
526456 // ... if you want to set Spec/Status in the client, you can
527457 }
528458 client = fake .NewSimpleClientset (existingObj )
529- items = []* metav1. PartialObjectMetadata {tc .ExistingCopy }
459+ items = []* v1alpha1. ClusterServiceVersion {tc .ExistingCopy }
530460 }
531461
532462 // Create the full Lister
@@ -549,9 +479,9 @@ func TestCopyToNamespace(t *testing.T) {
549479
550480 // Ensure the in-memory 'proto' has the correct final annotations
551481 annotations := proto .GetObjectMeta ().GetAnnotations ()
552- require .Equal (t , tc .Hash , annotations [nonStatusCopyHashAnnotation ],
482+ require .Equal (t , tc .Hash , annotations [copyCSVSpecHash ],
553483 "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 ],
555485 "proto should have the status hash annotation set" )
556486 } else {
557487 require .EqualError (t , err , tc .ExpectedError .Error ())
@@ -572,10 +502,10 @@ func TestCopyToNamespace(t *testing.T) {
572502 }
573503}
574504
575- type FakeClusterServiceVersionLister []* metav1. PartialObjectMetadata
505+ type FakeClusterServiceVersionLister []* v1alpha1. ClusterServiceVersion
576506
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
579509 for _ , csv := range l {
580510 if ! selector .Matches (labels .Set (csv .GetLabels ())) {
581511 continue
@@ -585,8 +515,8 @@ func (l FakeClusterServiceVersionLister) List(selector labels.Selector) ([]*meta
585515 return result , nil
586516}
587517
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
590520 for _ , csv := range l {
591521 if csv .GetNamespace () != namespace {
592522 continue
@@ -596,7 +526,7 @@ func (l FakeClusterServiceVersionLister) Namespace(namespace string) metadatalis
596526 return FakeClusterServiceVersionLister (filtered )
597527}
598528
599- func (l FakeClusterServiceVersionLister ) Get (name string ) (* metav1. PartialObjectMetadata , error ) {
529+ func (l FakeClusterServiceVersionLister ) Get (name string ) (* v1alpha1. ClusterServiceVersion , error ) {
600530 for _ , csv := range l {
601531 if csv .GetName () == name {
602532 return csv , nil
@@ -606,8 +536,8 @@ func (l FakeClusterServiceVersionLister) Get(name string) (*metav1.PartialObject
606536}
607537
608538var (
609- _ metadatalister. Lister = FakeClusterServiceVersionLister {}
610- _ metadatalister. NamespaceLister = FakeClusterServiceVersionLister {}
539+ _ listersv1alpha1. ClusterServiceVersionLister = FakeClusterServiceVersionLister {}
540+ _ listersv1alpha1. ClusterServiceVersionNamespaceLister = FakeClusterServiceVersionLister {}
611541)
612542
613543func TestCSVCopyPrototype (t * testing.T ) {
0 commit comments