Skip to content

Commit f392310

Browse files
authored
CLOUDP-146481: Convert OplogMinRetentionHours field properly (#778)
1 parent 530448e commit f392310

File tree

2 files changed

+56
-2
lines changed

2 files changed

+56
-2
lines changed

pkg/api/v1/atlasdeployment_types.go

Lines changed: 32 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,10 @@ limitations under the License.
1717
package v1
1818

1919
import (
20+
"fmt"
2021
"reflect"
22+
"regexp"
23+
"strconv"
2124

2225
"go.mongodb.org/atlas/mongodbatlas"
2326
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
@@ -306,10 +309,28 @@ type ProcessArgs struct {
306309

307310
func (specArgs ProcessArgs) ToAtlas() (*mongodbatlas.ProcessArgs, error) {
308311
result := &mongodbatlas.ProcessArgs{}
312+
if err := convertOplogMinRetentionHours(&specArgs, result); err != nil {
313+
return nil, err
314+
}
315+
309316
err := compat.JSONCopy(result, specArgs)
310317
return result, err
311318
}
312319

320+
func convertOplogMinRetentionHours(specArgs *ProcessArgs, atlasArgs *mongodbatlas.ProcessArgs) error {
321+
if specArgs != nil && specArgs.OplogMinRetentionHours != "" {
322+
OplogMinRetentionHours, err := strconv.ParseFloat(specArgs.OplogMinRetentionHours, 64)
323+
if err != nil {
324+
return err
325+
}
326+
327+
atlasArgs.OplogMinRetentionHours = &OplogMinRetentionHours
328+
specArgs.OplogMinRetentionHours = ""
329+
}
330+
331+
return nil
332+
}
333+
313334
func (specArgs ProcessArgs) IsEqual(newArgs interface{}) bool {
314335
specV := reflect.ValueOf(specArgs)
315336
newV := reflect.Indirect(reflect.ValueOf(newArgs))
@@ -330,21 +351,30 @@ func (specArgs ProcessArgs) IsEqual(newArgs interface{}) bool {
330351
if specValue.IsNil() {
331352
continue
332353
}
354+
specValue = specValue.Elem()
355+
}
356+
357+
if newValue.Kind() == reflect.Ptr {
333358
if newValue.IsNil() {
334359
return false
335360
}
336-
specValue = specValue.Elem()
337361
newValue = newValue.Elem()
338362
}
339363

340-
if specValue.Interface() != newValue.Interface() {
364+
if stringValue(specValue.Interface()) != stringValue(newValue.Interface()) {
341365
return false
342366
}
343367
}
344368

345369
return true
346370
}
347371

372+
var TrailingZerosRegex = regexp.MustCompile(`\.[0]*$`)
373+
374+
func stringValue(v interface{}) string {
375+
return TrailingZerosRegex.ReplaceAllString(fmt.Sprint(v), "")
376+
}
377+
348378
// Check compatibility with library type.
349379
var _ = ComputeSpec(mongodbatlas.Compute{})
350380

pkg/api/v1/atlasdeployment_types_test.go

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -179,4 +179,28 @@ func TestIsEqual(t *testing.T) {
179179

180180
areTheyEqual = operatorArgs.IsEqual(atlasArgs)
181181
assert.False(t, areTheyEqual, "should NOT be equal if Operator has more args")
182+
183+
atlasArgs.OplogSizeMB = toptr.MakePtr[int64](8)
184+
185+
areTheyEqual = operatorArgs.IsEqual(atlasArgs)
186+
assert.True(t, areTheyEqual, "should become equal")
187+
188+
operatorArgs.OplogMinRetentionHours = "2.0"
189+
atlasArgs.OplogMinRetentionHours = toptr.MakePtr[float64](2)
190+
191+
areTheyEqual = operatorArgs.IsEqual(atlasArgs)
192+
assert.True(t, areTheyEqual, "should be equal when OplogMinRetentionHours field is the same")
193+
}
194+
195+
func TestToAtlas(t *testing.T) {
196+
operatorArgs := ProcessArgs{
197+
JavascriptEnabled: toptr.MakePtr(true),
198+
OplogMinRetentionHours: "2.0",
199+
}
200+
201+
atlasArgs, err := operatorArgs.ToAtlas()
202+
assert.NoError(t, err, "no errors should occur")
203+
204+
areTheyEqual := operatorArgs.IsEqual(atlasArgs)
205+
assert.True(t, areTheyEqual, "should be equal after conversion")
182206
}

0 commit comments

Comments
 (0)