@@ -23,6 +23,7 @@ import (
23
23
"k8s.io/apimachinery/pkg/runtime"
24
24
"k8s.io/apimachinery/pkg/types"
25
25
"k8s.io/client-go/tools/record"
26
+ ctrl "sigs.k8s.io/controller-runtime"
26
27
"sigs.k8s.io/controller-runtime/pkg/client"
27
28
"sigs.k8s.io/controller-runtime/pkg/client/fake"
28
29
"sigs.k8s.io/controller-runtime/pkg/client/interceptor"
@@ -31,6 +32,7 @@ import (
31
32
"github.com/mongodb/mongodb-atlas-kubernetes/v2/api"
32
33
akov2 "github.com/mongodb/mongodb-atlas-kubernetes/v2/api/v1"
33
34
"github.com/mongodb/mongodb-atlas-kubernetes/v2/api/v1/common"
35
+ "github.com/mongodb/mongodb-atlas-kubernetes/v2/api/v1/project"
34
36
atlas_controllers "github.com/mongodb/mongodb-atlas-kubernetes/v2/internal/controller/atlas"
35
37
"github.com/mongodb/mongodb-atlas-kubernetes/v2/internal/controller/customresource"
36
38
"github.com/mongodb/mongodb-atlas-kubernetes/v2/internal/controller/workflow"
@@ -418,9 +420,8 @@ func TestLastSpecFrom(t *testing.T) {
418
420
419
421
"should return nil when there is no last spec" : {},
420
422
"should return error when last spec annotation is wrong" : {
421
- annotations : map [string ]string {"mongodb.com/last-applied-configuration" : "{wrong}" },
422
- expectedError : "error reading AtlasProject Spec from annotation [mongodb.com/last-applied-configuration]:" +
423
- " invalid character 'w' looking for beginning of object key string" ,
423
+ annotations : map [string ]string {"mongodb.com/last-applied-configuration" : "{wrong}" },
424
+ expectedError : "invalid character 'w' looking for beginning of object key string" ,
424
425
},
425
426
"should return last spec" : {
426
427
annotations : map [string ]string {"mongodb.com/last-applied-configuration" : "{\" name\" : \" my-project\" }" },
@@ -442,6 +443,142 @@ func TestLastSpecFrom(t *testing.T) {
442
443
}
443
444
}
444
445
446
+ func TestSkipClearsMigratedResourcesLastConfig (t * testing.T ) {
447
+ ctx := context .Background ()
448
+ prj := akov2.AtlasProject {
449
+ ObjectMeta : metav1.ObjectMeta {
450
+ Name : "test-project" ,
451
+ Namespace : "test-ns" ,
452
+ Annotations : map [string ]string {},
453
+ },
454
+ Spec : akov2.AtlasProjectSpec {
455
+ Name : "test-project" ,
456
+ PrivateEndpoints : []akov2.PrivateEndpoint {{}},
457
+ CloudProviderAccessRoles : []akov2.CloudProviderAccessRole {{}},
458
+ CloudProviderIntegrations : []akov2.CloudProviderIntegration {{}},
459
+ AlertConfigurations : []akov2.AlertConfiguration {{}},
460
+ NetworkPeers : []akov2.NetworkPeer {{}},
461
+ X509CertRef : & common.ResourceRefNamespaced {},
462
+ Integrations : []project.Integration {{}},
463
+ EncryptionAtRest : & akov2.EncryptionAtRest {},
464
+ Auditing : & akov2.Auditing {},
465
+ Settings : & akov2.ProjectSettings {},
466
+ CustomRoles : []akov2.CustomRole {{}},
467
+ Teams : []akov2.Team {{}},
468
+ BackupCompliancePolicyRef : & common.ResourceRefNamespaced {},
469
+ ConnectionSecret : & common.ResourceRefNamespaced {},
470
+ ProjectIPAccessList : []project.IPAccessList {{}},
471
+ MaintenanceWindow : project.MaintenanceWindow {},
472
+ },
473
+ }
474
+ prj .Annotations [customresource .AnnotationLastAppliedConfiguration ] = jsonize (t , prj .Spec )
475
+ prj .Annotations [customresource .ReconciliationPolicyAnnotation ] = customresource .ReconciliationPolicySkip
476
+ testScheme := runtime .NewScheme ()
477
+ require .NoError (t , akov2 .AddToScheme (testScheme ))
478
+ require .NoError (t , corev1 .AddToScheme (testScheme ))
479
+ k8sClient := fake .NewClientBuilder ().
480
+ WithScheme (testScheme ).
481
+ WithObjects (& prj ).
482
+ WithStatusSubresource (& prj ).
483
+ Build ()
484
+
485
+ req := ctrl.Request {NamespacedName : types.NamespacedName {
486
+ Name : prj .Name ,
487
+ Namespace : prj .Namespace ,
488
+ }}
489
+ r := AtlasProjectReconciler {
490
+ Client : k8sClient ,
491
+ Log : zaptest .NewLogger (t ).Sugar (),
492
+ EventRecorder : record .NewFakeRecorder (30 ),
493
+ AtlasProvider : & atlas.TestProvider {
494
+ IsCloudGovFunc : func () bool {
495
+ return false
496
+ },
497
+ IsSupportedFunc : func () bool {
498
+ return true
499
+ },
500
+ },
501
+ }
502
+
503
+ result , err := r .Reconcile (ctx , req )
504
+
505
+ require .Equal (t , reconcile.Result {}, result )
506
+ require .NoError (t , err )
507
+ require .NoError (t , k8sClient .Get (ctx , client .ObjectKeyFromObject (& prj ), & prj ))
508
+ lastApplied , err := customresource.ParseLastConfigApplied [akov2.AtlasProjectSpec ](& prj )
509
+ require .NoError (t , err )
510
+ wantLastApplied := & akov2.AtlasProjectSpec {
511
+ Name : "test-project" ,
512
+ PrivateEndpoints : nil ,
513
+ CustomRoles : nil ,
514
+ NetworkPeers : nil ,
515
+ ProjectIPAccessList : nil ,
516
+ CloudProviderAccessRoles : []akov2.CloudProviderAccessRole {{}},
517
+ CloudProviderIntegrations : []akov2.CloudProviderIntegration {{}},
518
+ AlertConfigurations : []akov2.AlertConfiguration {{}},
519
+ X509CertRef : & common.ResourceRefNamespaced {},
520
+ Integrations : []project.Integration {{}},
521
+ EncryptionAtRest : & akov2.EncryptionAtRest {},
522
+ Auditing : & akov2.Auditing {},
523
+ Settings : & akov2.ProjectSettings {},
524
+ Teams : []akov2.Team {{}},
525
+ BackupCompliancePolicyRef : & common.ResourceRefNamespaced {},
526
+ ConnectionSecret : & common.ResourceRefNamespaced {},
527
+ }
528
+ assert .Equal (t , wantLastApplied , lastApplied )
529
+ }
530
+
531
+ func TestSkipClearsMigratedResourcesLastConfigDoesNotPanic (t * testing.T ) {
532
+ ctx := context .Background ()
533
+ prj := akov2.AtlasProject {
534
+ ObjectMeta : metav1.ObjectMeta {
535
+ Name : "test-project" ,
536
+ Namespace : "test-ns" ,
537
+ Annotations : map [string ]string {},
538
+ },
539
+ Spec : akov2.AtlasProjectSpec {
540
+ Name : "test-project" ,
541
+ },
542
+ }
543
+ prj .Annotations [customresource .ReconciliationPolicyAnnotation ] = customresource .ReconciliationPolicySkip
544
+ testScheme := runtime .NewScheme ()
545
+ require .NoError (t , akov2 .AddToScheme (testScheme ))
546
+ require .NoError (t , corev1 .AddToScheme (testScheme ))
547
+ k8sClient := fake .NewClientBuilder ().
548
+ WithScheme (testScheme ).
549
+ WithObjects (& prj ).
550
+ WithStatusSubresource (& prj ).
551
+ Build ()
552
+
553
+ req := ctrl.Request {NamespacedName : types.NamespacedName {
554
+ Name : prj .Name ,
555
+ Namespace : prj .Namespace ,
556
+ }}
557
+ r := AtlasProjectReconciler {
558
+ Client : k8sClient ,
559
+ Log : zaptest .NewLogger (t ).Sugar (),
560
+ EventRecorder : record .NewFakeRecorder (30 ),
561
+ AtlasProvider : & atlas.TestProvider {
562
+ IsCloudGovFunc : func () bool {
563
+ return false
564
+ },
565
+ IsSupportedFunc : func () bool {
566
+ return true
567
+ },
568
+ },
569
+ }
570
+
571
+ result , err := r .Reconcile (ctx , req )
572
+
573
+ require .Equal (t , reconcile.Result {}, result )
574
+ require .NoError (t , err )
575
+ require .NoError (t , k8sClient .Get (ctx , client .ObjectKeyFromObject (& prj ), & prj ))
576
+ lastApplied , err := customresource.ParseLastConfigApplied [akov2.AtlasProjectSpec ](& prj )
577
+ require .NoError (t , err )
578
+ wantLastApplied := (* akov2 .AtlasProjectSpec )(nil )
579
+ assert .Equal (t , wantLastApplied , lastApplied )
580
+ }
581
+
445
582
func jsonize (t * testing.T , obj any ) string {
446
583
t .Helper ()
447
584
0 commit comments