Skip to content

Commit e15c4f8

Browse files
s-urbaniakhelderjs
andauthored
CLOUDP-242961: lowercase field names for matchers in ToAtlas (#1505)
* lowercase field names for matchers in ToAtlas * fix alert config conversion to Atlas * add test to validate matcher usage * use JSONCopy instead of custom similar func * fix imports --------- Co-authored-by: Helder Santana <[email protected]>
1 parent d9963bc commit e15c4f8

File tree

5 files changed

+86
-34
lines changed

5 files changed

+86
-34
lines changed

internal/unstructured/unstructured.go

Lines changed: 0 additions & 20 deletions
This file was deleted.

pkg/api/v1/alert_configurations.go

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -41,9 +41,9 @@ func (in *AlertConfiguration) ToAtlas() (*admin.GroupAlertsConfig, error) {
4141
matchers = append(
4242
matchers,
4343
map[string]interface{}{
44-
"FieldName": m.FieldName,
45-
"Operator": m.Operator,
46-
"Value": m.Value,
44+
"fieldName": m.FieldName,
45+
"operator": m.Operator,
46+
"value": m.Value,
4747
},
4848
)
4949
}
@@ -63,13 +63,13 @@ func (in *AlertConfiguration) ToAtlas() (*admin.GroupAlertsConfig, error) {
6363
if err != nil {
6464
return nil, err
6565
}
66-
result.SetThreshold(*tr)
66+
result.Threshold = tr
6767

6868
metricThreshold, err := in.MetricThreshold.ToAtlas()
6969
if err != nil {
7070
return nil, err
7171
}
72-
result.SetMetricThreshold(*metricThreshold)
72+
result.MetricThreshold = metricThreshold
7373

7474
return result, err
7575
}
@@ -130,7 +130,7 @@ func (in *Threshold) IsEqual(threshold *admin.GreaterThanRawThreshold) bool {
130130

131131
func (in *Threshold) ToAtlas() (*admin.GreaterThanRawThreshold, error) {
132132
if in == nil {
133-
return &admin.GreaterThanRawThreshold{}, nil
133+
return nil, nil
134134
}
135135

136136
tr64, err := strconv.ParseInt(in.Threshold, 10, 64)
@@ -317,7 +317,7 @@ func (in *MetricThreshold) IsEqual(threshold *admin.ServerlessMetricThreshold) b
317317

318318
func (in *MetricThreshold) ToAtlas() (*admin.ServerlessMetricThreshold, error) {
319319
if in == nil {
320-
return &admin.ServerlessMetricThreshold{}, nil
320+
return nil, nil
321321
}
322322

323323
tr, err := strconv.ParseFloat(in.Threshold, 64)

pkg/api/v1/status/alert_configurations.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,10 @@ import (
55
"fmt"
66
"strconv"
77

8-
"github.com/mongodb/mongodb-atlas-kubernetes/v2/internal/unstructured"
9-
108
"go.mongodb.org/atlas-sdk/v20231115008/admin"
119
"go.uber.org/zap"
1210

11+
"github.com/mongodb/mongodb-atlas-kubernetes/v2/internal/compat"
1312
"github.com/mongodb/mongodb-atlas-kubernetes/v2/internal/timeutil"
1413
)
1514

@@ -207,11 +206,12 @@ func ParseAlertConfiguration(alertConfiguration admin.GroupAlertsConfig, logger
207206
}
208207

209208
if unstructuredMatchers, ok := alertConfiguration.GetMatchersOk(); ok {
210-
matchers, err := unstructured.TypedFromUnstructured[[]map[string]interface{}, []Matcher](*unstructuredMatchers)
209+
var matchers []Matcher
210+
err := compat.JSONCopy(matchers, *unstructuredMatchers)
211211
if err != nil {
212212
logger.Errorf("unable to convert matchers to structured type: %s", err)
213213
}
214-
status.Matchers = *matchers
214+
status.Matchers = matchers
215215
}
216216

217217
mThreshold := alertConfiguration.GetMetricThreshold()

pkg/controller/atlasproject/alert_configurations.go

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ import (
1010
v1 "k8s.io/api/core/v1"
1111
"sigs.k8s.io/controller-runtime/pkg/client"
1212

13-
"github.com/mongodb/mongodb-atlas-kubernetes/v2/internal/unstructured"
13+
"github.com/mongodb/mongodb-atlas-kubernetes/v2/internal/compat"
1414
akov2 "github.com/mongodb/mongodb-atlas-kubernetes/v2/pkg/api/v1"
1515
"github.com/mongodb/mongodb-atlas-kubernetes/v2/pkg/api/v1/common"
1616
"github.com/mongodb/mongodb-atlas-kubernetes/v2/pkg/api/v1/status"
@@ -314,14 +314,16 @@ func isAlertConfigSpecEqualToAtlas(logger *zap.SugaredLogger, alertConfigSpec ak
314314
logger.Debugf("len(alertConfigSpec.Matchers) %v != len(atlasAlertConfig.Matchers) %v", len(alertConfigSpec.Matchers), len(atlasAlertConfig.GetMatchers()))
315315
return false
316316
}
317-
atlasMatchers, err := unstructured.TypedFromUnstructured[[]map[string]interface{}, []akov2.Matcher](atlasAlertConfig.GetMatchers())
317+
318+
var atlasMatchers []akov2.Matcher
319+
err := compat.JSONCopy(atlasMatchers, atlasAlertConfig.GetMatchers())
318320
if err != nil {
319321
logger.Errorf("unable to convert matchers to structured type: %s", err)
320322
return false
321323
}
322324
for _, matcher := range alertConfigSpec.Matchers {
323325
found := false
324-
for _, atlasMatcher := range *atlasMatchers {
326+
for _, atlasMatcher := range atlasMatchers {
325327
if matcher.IsEqual(atlasMatcher) {
326328
found = true
327329
}

test/e2e/alert_config_test.go

Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@ import (
66

77
. "github.com/onsi/ginkgo/v2"
88
. "github.com/onsi/gomega"
9+
10+
"go.mongodb.org/atlas-sdk/v20231115008/admin"
911
corev1 "k8s.io/api/core/v1"
1012
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
1113
"k8s.io/apimachinery/pkg/types"
@@ -142,6 +144,44 @@ var _ = Describe("Alert configuration tests", Label("alert-config"), func() {
142144
},
143145
},
144146
),
147+
Entry("Test[alert-configs-3]: Project with an alert config containing a matcher", Label("alert-configs-3"),
148+
model.DataProvider(
149+
"alert-configs-3",
150+
model.NewEmptyAtlasKeyType().UseDefaultFullAccess(),
151+
40000,
152+
[]func(*model.TestDataProvider){},
153+
).WithProject(data.DefaultProject()),
154+
[]akov2.AlertConfiguration{
155+
{
156+
EventTypeName: "REPLICATION_OPLOG_WINDOW_RUNNING_OUT",
157+
Enabled: true,
158+
Threshold: &akov2.Threshold{
159+
Operator: "LESS_THAN",
160+
Threshold: "1",
161+
Units: "HOURS",
162+
},
163+
Matchers: []akov2.Matcher{
164+
{
165+
FieldName: "CLUSTER_NAME",
166+
Operator: "STARTS_WITH",
167+
Value: "ako_e2e_test_",
168+
},
169+
},
170+
Notifications: []akov2.Notification{
171+
{
172+
IntervalMin: 5,
173+
DelayMin: pointer.MakePtr(5),
174+
EmailEnabled: pointer.MakePtr(true),
175+
SMSEnabled: pointer.MakePtr(false),
176+
Roles: []string{
177+
"GROUP_OWNER",
178+
},
179+
TypeName: "GROUP",
180+
},
181+
},
182+
},
183+
},
184+
),
145185
)
146186

147187
})
@@ -172,6 +212,36 @@ func alertConfigFlow(userData *model.TestDataProvider, alertConfigs []akov2.Aler
172212
statusIDList = append(statusIDList, alertConfig.ID)
173213
}
174214
Expect(compare.IsEqualWithoutOrder(statusIDList, atlasIDList)).Should(BeTrue())
215+
216+
atlasConfigs := alertConfigurations.GetResults()
217+
for i := range atlasConfigs {
218+
atlasConfig := normalizeAtlasAlertConfig(atlasConfigs[i])
219+
akoConfig, err := alertConfigs[i].ToAtlas()
220+
Expect(err).ToNot(HaveOccurred())
221+
Expect(atlasConfig).Should(Equal(*akoConfig))
222+
}
223+
}
224+
225+
func normalizeAtlasAlertConfig(atlasConfig admin.GroupAlertsConfig) admin.GroupAlertsConfig {
226+
atlasConfig.Id = nil
227+
atlasConfig.GroupId = nil
228+
atlasConfig.Created = nil
229+
atlasConfig.Updated = nil
230+
atlasConfig.Links = nil
231+
232+
notifications := atlasConfig.GetNotifications()
233+
for j := range notifications {
234+
notifications[j].NotifierId = nil
235+
notifications[j].DatadogApiKey = pointer.MakePtr("")
236+
notifications[j].OpsGenieApiKey = pointer.MakePtr("")
237+
notifications[j].ServiceKey = pointer.MakePtr("")
238+
notifications[j].ApiToken = pointer.MakePtr("")
239+
notifications[j].VictorOpsApiKey = pointer.MakePtr("")
240+
notifications[j].VictorOpsRoutingKey = pointer.MakePtr("")
241+
}
242+
atlasConfig.SetNotifications(notifications)
243+
244+
return atlasConfig
175245
}
176246

177247
var _ = Describe("Alert configuration with secrets test", Label("alert-config"), func() {

0 commit comments

Comments
 (0)