@@ -6,10 +6,14 @@ import (
66 "fmt"
77 "strings"
88
9+ "github.com/google/go-cmp/cmp"
10+ "github.com/google/go-cmp/cmp/cmpopts"
11+
912 "github.com/mongodb/mongodb-atlas-kubernetes/pkg/api/v1/status"
1013 "github.com/mongodb/mongodb-atlas-kubernetes/pkg/controller/customresource"
1114 "github.com/mongodb/mongodb-atlas-kubernetes/pkg/controller/watch"
1215 "github.com/mongodb/mongodb-atlas-kubernetes/pkg/controller/workflow"
16+ "github.com/mongodb/mongodb-atlas-kubernetes/pkg/util/compat"
1317
1418 "go.mongodb.org/atlas/mongodbatlas"
1519 "golang.org/x/sync/errgroup"
@@ -189,31 +193,43 @@ func (r *AtlasDeploymentReconciler) updateBackupScheduleAndPolicy(
189193
190194 r .Log .Debugf ("updating backup configuration for the atlas deployment: %v" , clusterName )
191195 apiScheduleReq := & mongodbatlas.CloudProviderSnapshotBackupPolicy {
192- ClusterName : clusterName ,
193- ReferenceHourOfDay : & bSchedule .Spec .ReferenceHourOfDay ,
194- ReferenceMinuteOfHour : & bSchedule .Spec .ReferenceMinuteOfHour ,
195- RestoreWindowDays : & bSchedule .Spec .RestoreWindowDays ,
196- UpdateSnapshots : & bSchedule .Spec .UpdateSnapshots ,
197- Policies : []mongodbatlas.Policy {apiPolicy },
196+ ClusterName : clusterName ,
197+ ReferenceHourOfDay : & bSchedule .Spec .ReferenceHourOfDay ,
198+ ReferenceMinuteOfHour : & bSchedule .Spec .ReferenceMinuteOfHour ,
199+ RestoreWindowDays : & bSchedule .Spec .RestoreWindowDays ,
200+ UpdateSnapshots : & bSchedule .Spec .UpdateSnapshots ,
201+ Policies : []mongodbatlas.Policy {apiPolicy },
202+ AutoExportEnabled : & bSchedule .Spec .AutoExportEnabled ,
203+ UseOrgAndGroupNamesInExportPrefix : & bSchedule .Spec .UseOrgAndGroupNamesInExportPrefix ,
198204 }
199205
200- currentSchedule , response , err := service .Client .CloudProviderSnapshotBackupPolicies .Delete (ctx , projectID , clusterName )
206+ currentSchedule , response , err := service .Client .CloudProviderSnapshotBackupPolicies .Get (ctx , projectID , clusterName )
201207 if err != nil {
202- errMessage := "unable to delete current backup configuration for project"
208+ errMessage := "unable to get current backup configuration for project"
203209 r .Log .Debugf ("%s: %s:%s, %v" , errMessage , projectID , clusterName , err )
204210 return fmt .Errorf ("%s: %s:%s, %w" , errMessage , projectID , clusterName , err )
205211 }
206212
207213 if currentSchedule == nil && response != nil {
208- return fmt .Errorf ("can't delete сurrent backup configuration. response status: %s" , response .Status )
214+ return fmt .Errorf ("can not get сurrent backup configuration. response status: %s" , response .Status )
209215 }
210216
211- r .Log .Debugf ("successfully deleted backup configuration. Default schedule received : %v" , currentSchedule )
217+ r .Log .Debugf ("successfully received backup configuration: %v" , currentSchedule )
212218
213219 apiScheduleReq .ClusterID = currentSchedule .ClusterID
214220 // There is only one policy, always
215221 apiScheduleReq .Policies [0 ].ID = currentSchedule .Policies [0 ].ID
216222
223+ equal , err := backupSchedulesAreEqual (currentSchedule , apiScheduleReq )
224+ if err != nil {
225+ return fmt .Errorf ("can not compare BackupSchedule resources: %w" , err )
226+ }
227+
228+ if equal {
229+ r .Log .Debug ("backupschedules are equal, nothing to change" )
230+ return nil
231+ }
232+
217233 r .Log .Debugf ("applying backup configuration: %v" , * bSchedule )
218234 if _ , _ , err := service .Client .CloudProviderSnapshotBackupPolicies .Update (ctx , projectID , clusterName , apiScheduleReq ); err != nil {
219235 return fmt .Errorf ("unable to create backupschedule %s. e: %w" , client .ObjectKeyFromObject (bSchedule ).String (), err )
@@ -222,6 +238,38 @@ func (r *AtlasDeploymentReconciler) updateBackupScheduleAndPolicy(
222238 return nil
223239}
224240
241+ func backupSchedulesAreEqual (currentSchedule * mongodbatlas.CloudProviderSnapshotBackupPolicy , newSchedule * mongodbatlas.CloudProviderSnapshotBackupPolicy ) (bool , error ) {
242+ currentCopy := mongodbatlas.CloudProviderSnapshotBackupPolicy {}
243+ err := compat .JSONCopy (& currentCopy , currentSchedule )
244+ if err != nil {
245+ return false , err
246+ }
247+
248+ newCopy := mongodbatlas.CloudProviderSnapshotBackupPolicy {}
249+ err = compat .JSONCopy (& newCopy , newSchedule )
250+ if err != nil {
251+ return false , err
252+ }
253+
254+ normalizeBackupSchedule (& currentCopy )
255+ normalizeBackupSchedule (& newCopy )
256+ d := cmp .Diff (& currentCopy , & newCopy , cmpopts .EquateEmpty ())
257+ if d != "" {
258+ return false , nil
259+ }
260+ return true , nil
261+ }
262+
263+ func normalizeBackupSchedule (s * mongodbatlas.CloudProviderSnapshotBackupPolicy ) {
264+ s .CopySettings = nil
265+ s .Links = nil
266+ s .NextSnapshot = ""
267+ if len (s .Policies ) > 0 && len (s .Policies [0 ].PolicyItems ) > 0 {
268+ s .Policies [0 ].PolicyItems [0 ].ID = ""
269+ }
270+ s .UpdateSnapshots = nil
271+ }
272+
225273func (r * AtlasDeploymentReconciler ) garbageCollectBackupResource (ctx context.Context , clusterName string ) error {
226274 schedules := & mdbv1.AtlasBackupScheduleList {}
227275
0 commit comments