Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add summary to msteams #6206

Merged
merged 4 commits into from
Mar 22, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
26 changes: 26 additions & 0 deletions Documentation/api.md

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 4 additions & 0 deletions bundle.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 4 additions & 0 deletions jsonnet/prometheus-operator/alertmanagerconfigs-crd.json
Original file line number Diff line number Diff line change
Expand Up @@ -1252,6 +1252,10 @@
"description": "Whether to notify about resolved alerts.",
"type": "boolean"
},
"summary": {
"description": "Message summary template. It requires Alertmanager >= 0.27.0.",
"type": "string"
},
"text": {
"description": "Message body template.",
"type": "string"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1116,6 +1116,10 @@
description: 'Whether to notify about resolved alerts.',
type: 'boolean',
},
summary: {
description: 'Message summary template. It requires Alertmanager >= 0.27.0.',
type: 'string',
},
text: {
description: 'Message body template.',
type: 'string',
Expand Down
10 changes: 10 additions & 0 deletions pkg/alertmanager/amcfg.go
Original file line number Diff line number Diff line change
Expand Up @@ -1270,6 +1270,10 @@ func (cb *configBuilder) convertMSTeamsConfig(
out.Text = *in.Text
}

if in.Summary != nil {
out.Summary = *in.Summary
}

webHookURL, err := cb.store.GetSecretKey(ctx, crKey.Namespace, in.WebhookURL)
if err != nil {
return nil, err
Expand Down Expand Up @@ -2091,6 +2095,12 @@ func (tc *msTeamsConfig) sanitize(amVersion semver.Version, logger log.Logger) e
return fmt.Errorf("mandatory field %q is empty", "webhook_url")
}

if tc.Summary != "" && amVersion.LT(semver.MustParse("0.27.0")) {
msg := "'summary' supported in Alertmanager >= 0.27.0 only - dropping field `summary` from msteams config"
level.Warn(logger).Log("msg", msg, "current_version", amVersion.String())
tc.Summary = ""
}

return tc.HTTPConfig.sanitize(amVersion, logger)
}

Expand Down
130 changes: 129 additions & 1 deletion pkg/alertmanager/amcfg_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -769,6 +769,11 @@ func TestGenerateConfig(t *testing.T) {
t.Fatal(err)
}

version27, err := semver.ParseTolerant("v0.27.0")
if err != nil {
t.Fatal(err)
}

globalSlackAPIURL, err := url.Parse("http://slack.example.com")
if err != nil {
t.Fatal("Could not parse slack API URL")
Expand Down Expand Up @@ -1965,6 +1970,59 @@ func TestGenerateConfig(t *testing.T) {
},
golden: "CR_with_MSTeams_Receiver.golden",
},
{
name: "CR with MSTeams Receiver with Summary",
amVersion: &version27,
kclient: fake.NewSimpleClientset(
&corev1.Secret{
ObjectMeta: metav1.ObjectMeta{
Name: "ms-teams-secret",
Namespace: "mynamespace",
},
Data: map[string][]byte{
"url": []byte("https://webhook.office.com/webhookb2/id/IncomingWebhook/id"),
},
},
),
baseConfig: alertmanagerConfig{
Route: &route{
Receiver: "null",
},
Receivers: []*receiver{{Name: "null"}},
},
amConfigs: map[string]*monitoringv1alpha1.AlertmanagerConfig{
"mynamespace": {
ObjectMeta: metav1.ObjectMeta{
Name: "myamc",
Namespace: "mynamespace",
},
Spec: monitoringv1alpha1.AlertmanagerConfigSpec{
Route: &monitoringv1alpha1.Route{
Receiver: "test",
},
Receivers: []monitoringv1alpha1.Receiver{
{
Name: "test",
MSTeamsConfigs: []monitoringv1alpha1.MSTeamsConfig{
{
WebhookURL: corev1.SecretKeySelector{
Key: "url",
LocalObjectReference: corev1.LocalObjectReference{
Name: "ms-teams-secret",
},
},
Title: ptr.To("test title"),
Summary: ptr.To("test summary"),
Text: ptr.To("test text"),
},
},
},
},
},
},
},
golden: "CR_with_MSTeams_Receiver_Summary.golden",
},
{
name: "CR with MSTeams Receiver with Partial Conf",
amVersion: &version26,
Expand Down Expand Up @@ -2074,6 +2132,9 @@ func TestSanitizeConfig(t *testing.T) {
versionTelegramBotTokenFileAllowed := semver.Version{Major: 0, Minor: 26}
versionTelegramBotTokenFileNotAllowed := semver.Version{Major: 0, Minor: 25}

versionMSTeamsSummaryAllowed := semver.Version{Major: 0, Minor: 27}
versionMSTeamsSummaryNotAllowed := semver.Version{Major: 0, Minor: 26}

for _, tc := range []struct {
name string
againstVersion semver.Version
Expand Down Expand Up @@ -2597,6 +2658,73 @@ func TestSanitizeConfig(t *testing.T) {
},
expectErr: true,
},
{
name: "summary is dropped for unsupported versions for MSTeams config",
againstVersion: versionMSTeamsSummaryNotAllowed,
in: &alertmanagerConfig{
Receivers: []*receiver{
{
Name: "msteams",
MSTeamsConfigs: []*msTeamsConfig{
{
WebhookURL: "http://example.com",
Title: "test title",
Summary: "test summary",
Text: "test text",
},
},
},
},
},
expect: alertmanagerConfig{
Receivers: []*receiver{
{
Name: "msteams",
MSTeamsConfigs: []*msTeamsConfig{
{
WebhookURL: "http://example.com",
Title: "test title",
Text: "test text",
},
},
},
},
},
},
{
name: "summary add in supported versions for MSTeams config",
againstVersion: versionMSTeamsSummaryAllowed,
in: &alertmanagerConfig{
Receivers: []*receiver{
{
Name: "msteams",
MSTeamsConfigs: []*msTeamsConfig{
{
WebhookURL: "http://example.com",
Title: "test title",
Summary: "test summary",
Text: "test text",
},
},
},
},
},
expect: alertmanagerConfig{
Receivers: []*receiver{
{
Name: "msteams",
MSTeamsConfigs: []*msTeamsConfig{
{
WebhookURL: "http://example.com",
Title: "test title",
Summary: "test summary",
Text: "test text",
},
},
},
},
},
},
} {
t.Run(tc.name, func(t *testing.T) {
err := tc.in.sanitize(tc.againstVersion, logger)
Expand Down Expand Up @@ -3837,7 +3965,7 @@ func TestSanitizeRoute(t *testing.T) {

// We want to ensure that the imported types from config.MuteTimeInterval
// and any others with custom marshalling/unmarshalling are parsed
// into the internal struct as expected
// into the internal struct as expected.
func TestLoadConfig(t *testing.T) {
testCase := []struct {
name string
Expand Down

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions pkg/alertmanager/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -406,6 +406,7 @@ type msTeamsConfig struct {
SendResolved *bool `yaml:"send_resolved,omitempty"`
WebhookURL string `yaml:"webhook_url"`
Title string `yaml:"title,omitempty"`
Summary string `yaml:"summary,omitempty"`
Text string `yaml:"text,omitempty"`
HTTPConfig *httpClientConfig `yaml:"http_config,omitempty"`
}
Expand Down
4 changes: 4 additions & 0 deletions pkg/apis/monitoring/v1alpha1/alertmanager_config_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -913,6 +913,10 @@ type MSTeamsConfig struct {
// Message title template.
// +optional
Title *string `json:"title,omitempty"`
// Message summary template.
// It requires Alertmanager >= 0.27.0.
// +optional
Summary *string `json:"summary,omitempty"`
// Message body template.
// +optional
Text *string `json:"text,omitempty"`
Expand Down
5 changes: 5 additions & 0 deletions pkg/apis/monitoring/v1alpha1/zz_generated.deepcopy.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 4 additions & 0 deletions pkg/apis/monitoring/v1beta1/alertmanager_config_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -905,6 +905,10 @@ type MSTeamsConfig struct {
// Message title template.
// +optional
Title *string `json:"title,omitempty"`
// Message summary template.
// It requires Alertmanager >= 0.27.0.
// +optional
Summary *string `json:"summary,omitempty"`
nicolastakashi marked this conversation as resolved.
Show resolved Hide resolved
// Message body template.
// +optional
Text *string `json:"text,omitempty"`
Expand Down
1 change: 1 addition & 0 deletions pkg/apis/monitoring/v1beta1/conversion_from.go
Original file line number Diff line number Diff line change
Expand Up @@ -458,6 +458,7 @@ func convertMSTeamsConfigFrom(in v1alpha1.MSTeamsConfig) MSTeamsConfig {
SendResolved: in.SendResolved,
WebhookURL: in.WebhookURL,
Title: in.Title,
Summary: in.Summary,
Text: in.Text,
HTTPConfig: convertHTTPConfigFrom(in.HTTPConfig),
}
Expand Down
1 change: 1 addition & 0 deletions pkg/apis/monitoring/v1beta1/conversion_to.go
Original file line number Diff line number Diff line change
Expand Up @@ -455,6 +455,7 @@ func convertMSTeamsConfigTo(in MSTeamsConfig) v1alpha1.MSTeamsConfig {
WebhookURL: in.WebhookURL,
Title: in.Title,
Text: in.Text,
Summary: in.Summary,
HTTPConfig: convertHTTPConfigTo(in.HTTPConfig),
}
}
Expand Down
5 changes: 5 additions & 0 deletions pkg/apis/monitoring/v1beta1/zz_generated.deepcopy.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.