99
1010 "github.com/golang/mock/gomock"
1111 api "github.com/open-feature/open-feature-operator/apis/core/v1beta1"
12+ "github.com/open-feature/open-feature-operator/internal/common"
1213 commonfake "github.com/open-feature/open-feature-operator/internal/common/flagdinjector/fake"
1314 resources "github.com/open-feature/open-feature-operator/internal/controller/core/flagd/common"
1415 "github.com/stretchr/testify/require"
@@ -251,7 +252,7 @@ func Test_areDeploymentsEqual(t *testing.T) {
251252 want bool
252253 }{
253254 {
254- name : "has changed" ,
255+ name : "has spec changed" ,
255256 args : args {
256257 old : & appsv1.Deployment {
257258 Spec : appsv1.DeploymentSpec {
@@ -266,6 +267,46 @@ func Test_areDeploymentsEqual(t *testing.T) {
266267 },
267268 want : false ,
268269 },
270+ {
271+ name : "has labels changed" ,
272+ args : args {
273+ old : & appsv1.Deployment {
274+ ObjectMeta : metav1.ObjectMeta {
275+ Labels : map [string ]string {
276+ "key" : "old" ,
277+ },
278+ },
279+ },
280+ new : & appsv1.Deployment {
281+ ObjectMeta : metav1.ObjectMeta {
282+ Labels : map [string ]string {
283+ "key" : "new" ,
284+ },
285+ },
286+ },
287+ },
288+ want : false ,
289+ },
290+ {
291+ name : "has annotations changed" ,
292+ args : args {
293+ old : & appsv1.Deployment {
294+ ObjectMeta : metav1.ObjectMeta {
295+ Annotations : map [string ]string {
296+ "key" : "old" ,
297+ },
298+ },
299+ },
300+ new : & appsv1.Deployment {
301+ ObjectMeta : metav1.ObjectMeta {
302+ Annotations : map [string ]string {
303+ "key" : "new" ,
304+ },
305+ },
306+ },
307+ },
308+ want : false ,
309+ },
269310 {
270311 name : "has not changed" ,
271312 args : args {
@@ -309,7 +350,6 @@ func Test_areDeploymentsEqual(t *testing.T) {
309350 }
310351 for _ , tt := range tests {
311352 t .Run (tt .name , func (t * testing.T ) {
312-
313353 d := & FlagdDeployment {}
314354 got := d .AreObjectsEqual (tt .args .old , tt .args .new )
315355
@@ -318,6 +358,188 @@ func Test_areDeploymentsEqual(t *testing.T) {
318358 }
319359}
320360
361+ func Test_getLabels (t * testing.T ) {
362+ const (
363+ flagdConfigTag = "latest"
364+ flagdName = "test-flagd"
365+ )
366+ type args struct {
367+ flagdConfigLabels map [string ]string
368+ flagdLabels map [string ]string
369+ }
370+ tests := []struct {
371+ name string
372+ args args
373+ want map [string ]string
374+ }{
375+ {
376+ name : "no config labels, no flagd labels" ,
377+ args : args {
378+ flagdConfigLabels : nil ,
379+ flagdLabels : nil ,
380+ },
381+ want : map [string ]string {
382+ "app" : flagdName ,
383+ "app.kubernetes.io/name" : flagdName ,
384+ "app.kubernetes.io/managed-by" : common .ManagedByAnnotationValue ,
385+ "app.kubernetes.io/version" : flagdConfigTag ,
386+ },
387+ },
388+ {
389+ name : "unique config and flagd labels" ,
390+ args : args {
391+ flagdConfigLabels : map [string ]string {
392+ "config-label" : "config-value" ,
393+ },
394+ flagdLabels : map [string ]string {
395+ "flagd-label" : "flagd-value" ,
396+ },
397+ },
398+ want : map [string ]string {
399+ "app" : flagdName ,
400+ "app.kubernetes.io/name" : flagdName ,
401+ "app.kubernetes.io/managed-by" : common .ManagedByAnnotationValue ,
402+ "app.kubernetes.io/version" : flagdConfigTag ,
403+ "config-label" : "config-value" ,
404+ "flagd-label" : "flagd-value" ,
405+ },
406+ },
407+ {
408+ name : "overlapping config and flagd labels" ,
409+ args : args {
410+ flagdConfigLabels : map [string ]string {
411+ "overlapping" : "config-value" ,
412+ },
413+ flagdLabels : map [string ]string {
414+ "overlapping" : "flagd-value" ,
415+ },
416+ },
417+ want : map [string ]string {
418+ "app" : flagdName ,
419+ "app.kubernetes.io/name" : flagdName ,
420+ "app.kubernetes.io/managed-by" : common .ManagedByAnnotationValue ,
421+ "app.kubernetes.io/version" : flagdConfigTag ,
422+ "overlapping" : "flagd-value" ,
423+ },
424+ },
425+ {
426+ name : "overlapping default labels" ,
427+ args : args {
428+ flagdConfigLabels : map [string ]string {
429+ "app.kubernetes.io/name" : "config-value" ,
430+ },
431+ flagdLabels : map [string ]string {
432+ "app.kubernetes.io/name" : "flagd-value" ,
433+ },
434+ },
435+ want : map [string ]string {
436+ "app" : flagdName ,
437+ "app.kubernetes.io/name" : flagdName ,
438+ "app.kubernetes.io/managed-by" : common .ManagedByAnnotationValue ,
439+ "app.kubernetes.io/version" : flagdConfigTag ,
440+ },
441+ },
442+ }
443+
444+ for _ , tt := range tests {
445+ t .Run (tt .name , func (t * testing.T ) {
446+ r := & FlagdDeployment {
447+ FlagdConfig : resources.FlagdConfiguration {
448+ Labels : tt .args .flagdConfigLabels ,
449+ Tag : flagdConfigTag ,
450+ },
451+ }
452+ flagd := & api.Flagd {
453+ ObjectMeta : metav1.ObjectMeta {
454+ Name : flagdName ,
455+ },
456+ Spec : api.FlagdSpec {
457+ PodLabels : tt .args .flagdLabels ,
458+ },
459+ }
460+
461+ got := r .getLabels (flagd )
462+
463+ require .Equal (t , tt .want , got )
464+ })
465+ }
466+ }
467+
468+ func Test_getAnnotations (t * testing.T ) {
469+ const (
470+ flagdName = "test-flagd"
471+ )
472+ type args struct {
473+ flagdConfigAnnotations map [string ]string
474+ flagdAnnotations map [string ]string
475+ }
476+ tests := []struct {
477+ name string
478+ args args
479+ want map [string ]string
480+ }{
481+ {
482+ name : "no config annotations, no flagd annotations" ,
483+ args : args {
484+ flagdConfigAnnotations : nil ,
485+ flagdAnnotations : nil ,
486+ },
487+ want : map [string ]string {},
488+ },
489+ {
490+ name : "unique annotations and flagd annotations" ,
491+ args : args {
492+ flagdConfigAnnotations : map [string ]string {
493+ "config-annotation" : "config-value" ,
494+ },
495+ flagdAnnotations : map [string ]string {
496+ "flagd-annotation" : "flagd-value" ,
497+ },
498+ },
499+ want : map [string ]string {
500+ "config-annotation" : "config-value" ,
501+ "flagd-annotation" : "flagd-value" ,
502+ },
503+ },
504+ {
505+ name : "overlapping config and flagd labels" ,
506+ args : args {
507+ flagdConfigAnnotations : map [string ]string {
508+ "overlapping" : "config-value" ,
509+ },
510+ flagdAnnotations : map [string ]string {
511+ "overlapping" : "flagd-value" ,
512+ },
513+ },
514+ want : map [string ]string {
515+ "overlapping" : "flagd-value" ,
516+ },
517+ },
518+ }
519+
520+ for _ , tt := range tests {
521+ t .Run (tt .name , func (t * testing.T ) {
522+ r := & FlagdDeployment {
523+ FlagdConfig : resources.FlagdConfiguration {
524+ Annotations : tt .args .flagdConfigAnnotations ,
525+ },
526+ }
527+ flagd := & api.Flagd {
528+ ObjectMeta : metav1.ObjectMeta {
529+ Name : flagdName ,
530+ },
531+ Spec : api.FlagdSpec {
532+ PodAnnotations : tt .args .flagdAnnotations ,
533+ },
534+ }
535+
536+ got := r .getAnnotations (flagd )
537+
538+ require .Equal (t , tt .want , got )
539+ })
540+ }
541+ }
542+
321543func intPtr (i int32 ) * int32 {
322544 return & i
323545}
0 commit comments