Skip to content

Commit

Permalink
CLOUDP-242961: lowercase field names for matchers in ToAtlas (#1505)
Browse files Browse the repository at this point in the history
* 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]>
  • Loading branch information
s-urbaniak and helderjs authored Apr 12, 2024
1 parent d9963bc commit e15c4f8
Show file tree
Hide file tree
Showing 5 changed files with 86 additions and 34 deletions.
20 changes: 0 additions & 20 deletions internal/unstructured/unstructured.go

This file was deleted.

14 changes: 7 additions & 7 deletions pkg/api/v1/alert_configurations.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,9 +41,9 @@ func (in *AlertConfiguration) ToAtlas() (*admin.GroupAlertsConfig, error) {
matchers = append(
matchers,
map[string]interface{}{
"FieldName": m.FieldName,
"Operator": m.Operator,
"Value": m.Value,
"fieldName": m.FieldName,
"operator": m.Operator,
"value": m.Value,
},
)
}
Expand All @@ -63,13 +63,13 @@ func (in *AlertConfiguration) ToAtlas() (*admin.GroupAlertsConfig, error) {
if err != nil {
return nil, err
}
result.SetThreshold(*tr)
result.Threshold = tr

metricThreshold, err := in.MetricThreshold.ToAtlas()
if err != nil {
return nil, err
}
result.SetMetricThreshold(*metricThreshold)
result.MetricThreshold = metricThreshold

return result, err
}
Expand Down Expand Up @@ -130,7 +130,7 @@ func (in *Threshold) IsEqual(threshold *admin.GreaterThanRawThreshold) bool {

func (in *Threshold) ToAtlas() (*admin.GreaterThanRawThreshold, error) {
if in == nil {
return &admin.GreaterThanRawThreshold{}, nil
return nil, nil
}

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

func (in *MetricThreshold) ToAtlas() (*admin.ServerlessMetricThreshold, error) {
if in == nil {
return &admin.ServerlessMetricThreshold{}, nil
return nil, nil
}

tr, err := strconv.ParseFloat(in.Threshold, 64)
Expand Down
8 changes: 4 additions & 4 deletions pkg/api/v1/status/alert_configurations.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,10 @@ import (
"fmt"
"strconv"

"github.com/mongodb/mongodb-atlas-kubernetes/v2/internal/unstructured"

"go.mongodb.org/atlas-sdk/v20231115008/admin"
"go.uber.org/zap"

"github.com/mongodb/mongodb-atlas-kubernetes/v2/internal/compat"
"github.com/mongodb/mongodb-atlas-kubernetes/v2/internal/timeutil"
)

Expand Down Expand Up @@ -207,11 +206,12 @@ func ParseAlertConfiguration(alertConfiguration admin.GroupAlertsConfig, logger
}

if unstructuredMatchers, ok := alertConfiguration.GetMatchersOk(); ok {
matchers, err := unstructured.TypedFromUnstructured[[]map[string]interface{}, []Matcher](*unstructuredMatchers)
var matchers []Matcher
err := compat.JSONCopy(matchers, *unstructuredMatchers)
if err != nil {
logger.Errorf("unable to convert matchers to structured type: %s", err)
}
status.Matchers = *matchers
status.Matchers = matchers
}

mThreshold := alertConfiguration.GetMetricThreshold()
Expand Down
8 changes: 5 additions & 3 deletions pkg/controller/atlasproject/alert_configurations.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import (
v1 "k8s.io/api/core/v1"
"sigs.k8s.io/controller-runtime/pkg/client"

"github.com/mongodb/mongodb-atlas-kubernetes/v2/internal/unstructured"
"github.com/mongodb/mongodb-atlas-kubernetes/v2/internal/compat"
akov2 "github.com/mongodb/mongodb-atlas-kubernetes/v2/pkg/api/v1"
"github.com/mongodb/mongodb-atlas-kubernetes/v2/pkg/api/v1/common"
"github.com/mongodb/mongodb-atlas-kubernetes/v2/pkg/api/v1/status"
Expand Down Expand Up @@ -314,14 +314,16 @@ func isAlertConfigSpecEqualToAtlas(logger *zap.SugaredLogger, alertConfigSpec ak
logger.Debugf("len(alertConfigSpec.Matchers) %v != len(atlasAlertConfig.Matchers) %v", len(alertConfigSpec.Matchers), len(atlasAlertConfig.GetMatchers()))
return false
}
atlasMatchers, err := unstructured.TypedFromUnstructured[[]map[string]interface{}, []akov2.Matcher](atlasAlertConfig.GetMatchers())

var atlasMatchers []akov2.Matcher
err := compat.JSONCopy(atlasMatchers, atlasAlertConfig.GetMatchers())
if err != nil {
logger.Errorf("unable to convert matchers to structured type: %s", err)
return false
}
for _, matcher := range alertConfigSpec.Matchers {
found := false
for _, atlasMatcher := range *atlasMatchers {
for _, atlasMatcher := range atlasMatchers {
if matcher.IsEqual(atlasMatcher) {
found = true
}
Expand Down
70 changes: 70 additions & 0 deletions test/e2e/alert_config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ import (

. "github.com/onsi/ginkgo/v2"
. "github.com/onsi/gomega"

"go.mongodb.org/atlas-sdk/v20231115008/admin"
corev1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/types"
Expand Down Expand Up @@ -142,6 +144,44 @@ var _ = Describe("Alert configuration tests", Label("alert-config"), func() {
},
},
),
Entry("Test[alert-configs-3]: Project with an alert config containing a matcher", Label("alert-configs-3"),
model.DataProvider(
"alert-configs-3",
model.NewEmptyAtlasKeyType().UseDefaultFullAccess(),
40000,
[]func(*model.TestDataProvider){},
).WithProject(data.DefaultProject()),
[]akov2.AlertConfiguration{
{
EventTypeName: "REPLICATION_OPLOG_WINDOW_RUNNING_OUT",
Enabled: true,
Threshold: &akov2.Threshold{
Operator: "LESS_THAN",
Threshold: "1",
Units: "HOURS",
},
Matchers: []akov2.Matcher{
{
FieldName: "CLUSTER_NAME",
Operator: "STARTS_WITH",
Value: "ako_e2e_test_",
},
},
Notifications: []akov2.Notification{
{
IntervalMin: 5,
DelayMin: pointer.MakePtr(5),
EmailEnabled: pointer.MakePtr(true),
SMSEnabled: pointer.MakePtr(false),
Roles: []string{
"GROUP_OWNER",
},
TypeName: "GROUP",
},
},
},
},
),
)

})
Expand Down Expand Up @@ -172,6 +212,36 @@ func alertConfigFlow(userData *model.TestDataProvider, alertConfigs []akov2.Aler
statusIDList = append(statusIDList, alertConfig.ID)
}
Expect(compare.IsEqualWithoutOrder(statusIDList, atlasIDList)).Should(BeTrue())

atlasConfigs := alertConfigurations.GetResults()
for i := range atlasConfigs {
atlasConfig := normalizeAtlasAlertConfig(atlasConfigs[i])
akoConfig, err := alertConfigs[i].ToAtlas()
Expect(err).ToNot(HaveOccurred())
Expect(atlasConfig).Should(Equal(*akoConfig))
}
}

func normalizeAtlasAlertConfig(atlasConfig admin.GroupAlertsConfig) admin.GroupAlertsConfig {
atlasConfig.Id = nil
atlasConfig.GroupId = nil
atlasConfig.Created = nil
atlasConfig.Updated = nil
atlasConfig.Links = nil

notifications := atlasConfig.GetNotifications()
for j := range notifications {
notifications[j].NotifierId = nil
notifications[j].DatadogApiKey = pointer.MakePtr("")
notifications[j].OpsGenieApiKey = pointer.MakePtr("")
notifications[j].ServiceKey = pointer.MakePtr("")
notifications[j].ApiToken = pointer.MakePtr("")
notifications[j].VictorOpsApiKey = pointer.MakePtr("")
notifications[j].VictorOpsRoutingKey = pointer.MakePtr("")
}
atlasConfig.SetNotifications(notifications)

return atlasConfig
}

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

0 comments on commit e15c4f8

Please sign in to comment.