@@ -59,7 +59,10 @@ func (r *AtlasDeploymentReconciler) ensureAdvancedDeploymentState(ctx *workflow.
5959}
6060
6161func advancedDeploymentIdle (ctx * workflow.Context , project * mdbv1.AtlasProject , deployment * mdbv1.AtlasDeployment , atlasDeploymentAsAtlas * mongodbatlas.AdvancedCluster ) (* mongodbatlas.AdvancedCluster , workflow.Result ) {
62- handleAutoscaling (deployment .Spec .AdvancedDeploymentSpec )
62+ err := handleAutoscaling (ctx , deployment .Spec .AdvancedDeploymentSpec , atlasDeploymentAsAtlas )
63+ if err != nil {
64+ return atlasDeploymentAsAtlas , workflow .Terminate (workflow .Internal , err .Error ())
65+ }
6366
6467 specDeployment , atlasDeployment , err := MergedAdvancedDeployment (* atlasDeploymentAsAtlas , * deployment .Spec .AdvancedDeploymentSpec )
6568 if err != nil {
@@ -108,14 +111,25 @@ func cleanupTheSpec(deployment *mdbv1.AdvancedDeploymentSpec) {
108111// It will also prevent from setting ANY of (electable | analytics | readonly) specs
109112//
110113// if region config has enabled compute autoscaling
111- func handleAutoscaling (kubeDeployment * mdbv1.AdvancedDeploymentSpec ) {
114+ func handleAutoscaling (ctx * workflow. Context , desiredDeployment * mdbv1.AdvancedDeploymentSpec , currentDeployment * mongodbatlas. AdvancedCluster ) error {
112115 isDiskAutoScaled := false
113- cleanupInstanceSize := func (s * mdbv1.Specs ) {
116+ syncInstanceSize := func (s * mdbv1.Specs , as * mdbv1. AdvancedAutoScalingSpec ) error {
114117 if s != nil {
115- s .InstanceSize = ""
118+ size , err := normalizeInstanceSize (ctx , s .InstanceSize , as )
119+ if err != nil {
120+ return err
121+ }
122+
123+ if isInstanceSizeTheSame (currentDeployment , size ) {
124+ size = ""
125+ }
126+
127+ s .InstanceSize = size
116128 }
129+
130+ return nil
117131 }
118- for _ , repSpec := range kubeDeployment .ReplicationSpecs {
132+ for _ , repSpec := range desiredDeployment .ReplicationSpecs {
119133 for _ , regConfig := range repSpec .RegionConfigs {
120134 if regConfig .AutoScaling != nil {
121135 if regConfig .AutoScaling .DiskGB != nil &&
@@ -127,17 +141,30 @@ func handleAutoscaling(kubeDeployment *mdbv1.AdvancedDeploymentSpec) {
127141 if regConfig .AutoScaling .Compute != nil &&
128142 regConfig .AutoScaling .Compute .Enabled != nil &&
129143 * regConfig .AutoScaling .Compute .Enabled {
130- cleanupInstanceSize (regConfig .ElectableSpecs )
131- cleanupInstanceSize (regConfig .AnalyticsSpecs )
132- cleanupInstanceSize (regConfig .ReadOnlySpecs )
144+ err := syncInstanceSize (regConfig .ElectableSpecs , regConfig .AutoScaling )
145+ if err != nil {
146+ return err
147+ }
148+
149+ err = syncInstanceSize (regConfig .AnalyticsSpecs , regConfig .AutoScaling )
150+ if err != nil {
151+ return err
152+ }
153+
154+ err = syncInstanceSize (regConfig .ReadOnlySpecs , regConfig .AutoScaling )
155+ if err != nil {
156+ return err
157+ }
133158 }
134159 }
135160 }
136161 }
137162
138163 if isDiskAutoScaled {
139- kubeDeployment .DiskSizeGB = nil
164+ desiredDeployment .DiskSizeGB = nil
140165 }
166+
167+ return nil
141168}
142169
143170// MergedAdvancedDeployment will return the result of merging AtlasDeploymentSpec with Atlas Advanced Deployment
@@ -222,3 +249,36 @@ func GetAllDeploymentNames(client mongodbatlas.Client, projectID string) ([]stri
222249 }
223250 return deploymentNames , nil
224251}
252+
253+ func normalizeInstanceSize (ctx * workflow.Context , currentInstanceSize string , autoscaling * mdbv1.AdvancedAutoScalingSpec ) (string , error ) {
254+ currentSize , err := NewFromInstanceSizeName (currentInstanceSize )
255+ if err != nil {
256+ return "" , err
257+ }
258+
259+ minSize , err := NewFromInstanceSizeName (autoscaling .Compute .MinInstanceSize )
260+ if err != nil {
261+ return "" , err
262+ }
263+
264+ maxSize , err := NewFromInstanceSizeName (autoscaling .Compute .MaxInstanceSize )
265+ if err != nil {
266+ return "" , err
267+ }
268+
269+ if CompareInstanceSizes (currentSize , minSize ) == - 1 {
270+ ctx .Log .Warnf ("The instance size is below the minimum autoscaling configuration. Setting it to %s. Consider update your CRD" , autoscaling .Compute .MinInstanceSize )
271+ return autoscaling .Compute .MinInstanceSize , nil
272+ }
273+
274+ if CompareInstanceSizes (currentSize , maxSize ) == 1 {
275+ ctx .Log .Warnf ("The instance size is above the maximum autoscaling configuration. Setting it to %s. Consider update your CRD" , autoscaling .Compute .MaxInstanceSize )
276+ return autoscaling .Compute .MaxInstanceSize , nil
277+ }
278+
279+ return currentInstanceSize , nil
280+ }
281+
282+ func isInstanceSizeTheSame (currentDeployment * mongodbatlas.AdvancedCluster , desiredInstanceSize string ) bool {
283+ return desiredInstanceSize == currentDeployment .ReplicationSpecs [0 ].RegionConfigs [0 ].ElectableSpecs .InstanceSize
284+ }
0 commit comments