Skip to content

Commit aadbee7

Browse files
CLOUDP-153756: Fixed BackupSchedule reconciliation (#854)
Fixed BackupSchedule reconciliation
1 parent 49eaa70 commit aadbee7

File tree

5 files changed

+61
-14
lines changed

5 files changed

+61
-14
lines changed

.golangci.yml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,6 @@ linters:
2222
- gofmt
2323
- ineffassign
2424
- misspell
25-
- nolintlint
2625
- noctx
2726
- prealloc
2827
- rowserrcheck

config/crd/bases/atlas.mongodb.com_atlasbackupschedules.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ spec:
3737
description: AtlasBackupScheduleSpec defines the desired state of AtlasBackupSchedule
3838
properties:
3939
autoExportEnabled:
40-
default: true
40+
default: false
4141
description: Specify true to enable automatic export of cloud backup
4242
snapshots to the AWS bucket. You must also define the export policy
4343
using export. If omitted, defaults to false.

pkg/api/v1/atlasbackupschedule_types.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ import (
2020
type AtlasBackupScheduleSpec struct {
2121
// Specify true to enable automatic export of cloud backup snapshots to the AWS bucket. You must also define the export policy using export. If omitted, defaults to false.
2222
// +optional
23-
// +kubebuilder:default:=true
23+
// +kubebuilder:default:=false
2424
AutoExportEnabled bool `json:"autoExportEnabled,omitempty"`
2525

2626
// Export policy for automatically exporting cloud backup snapshots to AWS bucket.

pkg/controller/atlasdeployment/backup.go

Lines changed: 58 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -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+
225273
func (r *AtlasDeploymentReconciler) garbageCollectBackupResource(ctx context.Context, clusterName string) error {
226274
schedules := &mdbv1.AtlasBackupScheduleList{}
227275

test/int/dbuser_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -827,7 +827,7 @@ func validateDatabaseUserUpdatingFunc(g Gomega) func(a mdbv1.AtlasCustomResource
827827
}
828828
}
829829

830-
//nolint
830+
// nolint
831831
func validateDatabaseUserWaitingForCluster() func(a mdbv1.AtlasCustomResource) {
832832
return func(a mdbv1.AtlasCustomResource) {
833833
d := a.(*mdbv1.AtlasDatabaseUser)

0 commit comments

Comments
 (0)