@@ -75,6 +75,139 @@ func (l *testLogger) WithName(name string) logr.LogSink {
75
75
return l
76
76
}
77
77
78
+ func doStripMetadataTestWithOptions (mgr manager.Manager , suffix string , opts ... * metadataFilterInjectorFunc ) {
79
+ statefulSetMaps := make (chan * metav1.PartialObjectMetadata )
80
+ forOpts := []ForOption {OnlyMetadata }
81
+ ownOpts := []OwnsOption {OnlyMetadata }
82
+ watchOpts := []WatchesOption {OnlyMetadata }
83
+
84
+ expectManagedFields := true
85
+ expectAnnotations := true
86
+
87
+ for _ , v := range opts {
88
+ forOpts = append (forOpts , * v )
89
+ ownOpts = append (ownOpts , * v )
90
+ watchOpts = append (watchOpts , * v )
91
+
92
+ expectManagedFields = expectManagedFields && v != & WithoutManagedFields
93
+ expectAnnotations = expectAnnotations && v != & WithoutAnnotations
94
+ }
95
+
96
+ testObject := func (o metav1.Object ) bool {
97
+ // Validate that ManagedFields is nil
98
+ if expectAnnotations {
99
+ Expect (o .GetAnnotations ()).ToNot (BeNil ())
100
+ } else {
101
+ Expect ((o .GetAnnotations ())).To (BeNil ())
102
+ }
103
+ if expectManagedFields {
104
+ Expect ((o .GetManagedFields ())).ToNot (BeNil ())
105
+ } else {
106
+ Expect ((o .GetManagedFields ())).To (BeNil ())
107
+ }
108
+ return true
109
+ }
110
+
111
+ set := & appsv1.StatefulSet {
112
+ ObjectMeta : metav1.ObjectMeta {
113
+ Namespace : "default" ,
114
+ Name : "test1-" + suffix ,
115
+ Labels : map [string ]string {
116
+ "foo" : "bar" ,
117
+ },
118
+ Annotations : map [string ]string {
119
+ "foo" : "bar" ,
120
+ },
121
+ },
122
+ Spec : appsv1.StatefulSetSpec {
123
+ Selector : & metav1.LabelSelector {
124
+ MatchLabels : map [string ]string {"foo" : "bar" },
125
+ },
126
+ Template : corev1.PodTemplateSpec {
127
+ ObjectMeta : metav1.ObjectMeta {Labels : map [string ]string {"foo" : "bar" }},
128
+ Spec : corev1.PodSpec {
129
+ Containers : []corev1.Container {
130
+ {
131
+ Name : "nginx" ,
132
+ Image : "nginx" ,
133
+ },
134
+ },
135
+ },
136
+ },
137
+ },
138
+ }
139
+
140
+ bldr := ControllerManagedBy (mgr ).
141
+ For (& appsv1.Deployment {}, forOpts ... ).
142
+ Owns (& appsv1.ReplicaSet {}, ownOpts ... ).
143
+ Watches (& source.Kind {Type : & appsv1.StatefulSet {}},
144
+ handler .EnqueueRequestsFromMapFunc (func (o client.Object ) []reconcile.Request {
145
+ defer GinkgoRecover ()
146
+
147
+ if o .GetName () != set .Name {
148
+ // I suppose the objects in the test cluster are not reset
149
+ // in-between tests?
150
+ return nil
151
+ }
152
+
153
+ ometa := o .(* metav1.PartialObjectMetadata )
154
+ Expect (ometa .Name ).To (Equal (set .Name ))
155
+ Expect (ometa .Namespace ).To (Equal (set .Namespace ))
156
+ Expect (ometa .Labels ).To (Equal (set .Labels ))
157
+ statefulSetMaps <- ometa
158
+
159
+ // Validate that the GVK is not empty when dealing with PartialObjectMetadata objects.
160
+ Expect (o .GetObjectKind ().GroupVersionKind ()).To (Equal (schema.GroupVersionKind {
161
+ Group : "apps" ,
162
+ Version : "v1" ,
163
+ Kind : "StatefulSet" ,
164
+ }))
165
+
166
+ testObject (o )
167
+ return nil
168
+ }),
169
+ watchOpts ... )
170
+
171
+ ctx , cancel := context .WithCancel (context .Background ())
172
+ defer cancel ()
173
+
174
+ doReconcileTest (ctx , suffix , mgr , true , bldr )
175
+
176
+ By ("Creating a new stateful set" )
177
+
178
+ err := mgr .GetClient ().Create (context .TODO (), set )
179
+ Expect (err ).NotTo (HaveOccurred ())
180
+
181
+ By ("Checking that 'Owns' Deployments have correct projection" )
182
+ deployment , err := bldr .project (& appsv1.Deployment {}, projectAsMetadata )
183
+ Expect (err ).NotTo (HaveOccurred ())
184
+ err = mgr .GetCache ().Get (ctx , types.NamespacedName {
185
+ Namespace : "default" ,
186
+ Name : "deploy-name-" + suffix ,
187
+ }, deployment )
188
+ Expect (err ).NotTo (HaveOccurred ())
189
+ testObject (deployment )
190
+
191
+ By ("Checking that 'For' ReplicaSets have correct projection" )
192
+ rs , err := bldr .project (& appsv1.ReplicaSet {}, projectAsMetadata )
193
+ Expect (err ).NotTo (HaveOccurred ())
194
+ err = mgr .GetCache ().Get (ctx , types.NamespacedName {
195
+ Namespace : "default" ,
196
+ Name : "rs-name-" + suffix ,
197
+ }, rs )
198
+ Expect (err ).NotTo (HaveOccurred ())
199
+ testObject (rs )
200
+
201
+ By ("Checking that the mapping function has been called" )
202
+ Eventually (func () bool {
203
+ metaSet := <- statefulSetMaps
204
+ Expect (metaSet .Name ).To (Equal (set .Name ))
205
+ Expect (metaSet .Namespace ).To (Equal (set .Namespace ))
206
+ Expect (metaSet .Labels ).To (Equal (set .Labels ))
207
+ return true
208
+ }).Should (BeTrue ())
209
+ }
210
+
78
211
var _ = Describe ("application" , func () {
79
212
BeforeEach (func () {
80
213
newController = controller .New
@@ -417,6 +550,38 @@ var _ = Describe("application", func() {
417
550
doReconcileTest (ctx , "6" , mgr , true , bldr1 , bldr2 )
418
551
})
419
552
553
+ It ("should support keeping metadata fields if not all controllers agree to strip them" , func () {
554
+ bldr1 := ControllerManagedBy (mgr ).For (& appsv1.Deployment {}, OnlyMetadata , WithoutManagedFields )
555
+ bldr2 := ControllerManagedBy (mgr ).For (& appsv1.Deployment {}, OnlyMetadata )
556
+
557
+ ctx , cancel := context .WithCancel (context .Background ())
558
+ defer cancel ()
559
+
560
+ doReconcileTest (ctx , "7-0" , mgr , true , bldr1 , bldr2 )
561
+
562
+ By ("Checking that Deployments metadata still has managed fields" )
563
+ deployment , err := bldr1 .project (& appsv1.Deployment {}, projectAsMetadata )
564
+ Expect (err ).NotTo (HaveOccurred ())
565
+ err = mgr .GetCache ().Get (ctx , types.NamespacedName {
566
+ Namespace : "default" ,
567
+ Name : "deploy-name-" + "7-0" ,
568
+ }, deployment )
569
+ Expect (err ).NotTo (HaveOccurred ())
570
+ Expect (deployment .GetManagedFields ()).ToNot (BeNil ())
571
+ })
572
+
573
+ It ("should support stripping annoatations for metadata projections" , func () {
574
+ doStripMetadataTestWithOptions (mgr , "7-1" , & WithoutAnnotations )
575
+ })
576
+
577
+ It ("should support stripping managed fields options for metadata projections" , func () {
578
+ doStripMetadataTestWithOptions (mgr , "7-2" , & WithoutManagedFields )
579
+ })
580
+
581
+ It ("should support all stripping options for metadata projections" , func () {
582
+ doStripMetadataTestWithOptions (mgr , "7-3" , & WithoutAnnotations , & WithoutManagedFields )
583
+ })
584
+
420
585
It ("should support watching For, Owns, and Watch as metadata" , func () {
421
586
statefulSetMaps := make (chan * metav1.PartialObjectMetadata )
422
587
@@ -427,15 +592,17 @@ var _ = Describe("application", func() {
427
592
handler .EnqueueRequestsFromMapFunc (func (o client.Object ) []reconcile.Request {
428
593
defer GinkgoRecover ()
429
594
430
- ometa := o .(* metav1.PartialObjectMetadata )
431
- statefulSetMaps <- ometa
432
-
433
- // Validate that the GVK is not empty when dealing with PartialObjectMetadata objects.
434
- Expect (o .GetObjectKind ().GroupVersionKind ()).To (Equal (schema.GroupVersionKind {
435
- Group : "apps" ,
436
- Version : "v1" ,
437
- Kind : "StatefulSet" ,
438
- }))
595
+ if o .GetName () == "test1" {
596
+ ometa := o .(* metav1.PartialObjectMetadata )
597
+ statefulSetMaps <- ometa
598
+
599
+ // Validate that the GVK is not empty when dealing with PartialObjectMetadata objects.
600
+ Expect (o .GetObjectKind ().GroupVersionKind ()).To (Equal (schema.GroupVersionKind {
601
+ Group : "apps" ,
602
+ Version : "v1" ,
603
+ Kind : "StatefulSet" ,
604
+ }))
605
+ }
439
606
return nil
440
607
}),
441
608
OnlyMetadata )
@@ -559,6 +726,9 @@ func doReconcileTest(ctx context.Context, nameSuffix string, mgr manager.Manager
559
726
ObjectMeta : metav1.ObjectMeta {
560
727
Namespace : "default" ,
561
728
Name : deployName ,
729
+ Annotations : map [string ]string {
730
+ "foo" : "bar" ,
731
+ },
562
732
},
563
733
Spec : appsv1.DeploymentSpec {
564
734
Selector : & metav1.LabelSelector {
@@ -601,6 +771,9 @@ func doReconcileTest(ctx context.Context, nameSuffix string, mgr manager.Manager
601
771
UID : dep .UID ,
602
772
},
603
773
},
774
+ Annotations : map [string ]string {
775
+ "foo" : "bar" ,
776
+ },
604
777
},
605
778
Spec : appsv1.ReplicaSetSpec {
606
779
Selector : dep .Spec .Selector ,
0 commit comments