Skip to content

Commit

Permalink
Fix handling of the global smtp_require_tls field in Alertmanager con…
Browse files Browse the repository at this point in the history
…figuration (prometheus-operator#3960)

* Added test for smtp_require_tls global flag

* pkg/alertmanager: omit smtp_require_tls when unspecified

Signed-off-by: Simon Pasquier <spasquie@redhat.com>

* pkg/alertmanager: omit global resolve_timeout if empty

Signed-off-by: Simon Pasquier <spasquie@redhat.com>

Co-authored-by: Martin Dostal <dostal@utu.unassigned-domain>
  • Loading branch information
simonpasquier and Martin Dostal authored Apr 8, 2021
1 parent b18d542 commit a739cab
Show file tree
Hide file tree
Showing 2 changed files with 64 additions and 3 deletions.
63 changes: 62 additions & 1 deletion pkg/alertmanager/amcfg_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,12 @@ import (
"context"
"net/url"
"testing"
"time"

"github.com/google/go-cmp/cmp"
"github.com/prometheus-operator/prometheus-operator/pkg/assets"
"github.com/prometheus/alertmanager/config"
"github.com/prometheus/common/model"
corev1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/client-go/kubernetes"
Expand Down Expand Up @@ -58,6 +60,66 @@ func TestGenerateConfig(t *testing.T) {
receivers:
- name: "null"
templates: []
`,
},
{
name: "skeleton base with global send_revolved, no CRs",
kclient: fake.NewSimpleClientset(),
baseConfig: alertmanagerConfig{
Global: &globalConfig{
ResolveTimeout: func(d model.Duration) *model.Duration { return &d }(model.Duration(time.Minute)),
},
Route: &route{Receiver: "null"},
Receivers: []*receiver{{Name: "null"}},
},
amConfigs: map[string]*monitoringv1alpha1.AlertmanagerConfig{},
expected: `global:
resolve_timeout: 1m
route:
receiver: "null"
receivers:
- name: "null"
templates: []
`,
},
{
name: "skeleton base with global smtp_require_tls set to false, no CRs",
kclient: fake.NewSimpleClientset(),
baseConfig: alertmanagerConfig{
Global: &globalConfig{
SMTPRequireTLS: func(b bool) *bool { return &b }(false),
},
Route: &route{Receiver: "null"},
Receivers: []*receiver{{Name: "null"}},
},
amConfigs: map[string]*monitoringv1alpha1.AlertmanagerConfig{},
expected: `global:
smtp_require_tls: false
route:
receiver: "null"
receivers:
- name: "null"
templates: []
`,
},
{
name: "skeleton base with global smtp_require_tls set to true, no CRs",
kclient: fake.NewSimpleClientset(),
baseConfig: alertmanagerConfig{
Global: &globalConfig{
SMTPRequireTLS: func(b bool) *bool { return &b }(true),
},
Route: &route{Receiver: "null"},
Receivers: []*receiver{{Name: "null"}},
},
amConfigs: map[string]*monitoringv1alpha1.AlertmanagerConfig{},
expected: `global:
smtp_require_tls: true
route:
receiver: "null"
receivers:
- name: "null"
templates: []
`,
},
{
Expand Down Expand Up @@ -542,7 +604,6 @@ templates: []
},
},
expected: `global:
resolve_timeout: 0s
slack_api_url: http://slack.example.com
route:
receiver: "null"
Expand Down
4 changes: 2 additions & 2 deletions pkg/alertmanager/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ type alertmanagerConfig struct {
type globalConfig struct {
// ResolveTimeout is the time after which an alert is declared resolved
// if it has not been updated.
ResolveTimeout model.Duration `yaml:"resolve_timeout" json:"resolve_timeout"`
ResolveTimeout *model.Duration `yaml:"resolve_timeout,omitempty" json:"resolve_timeout,omitempty"`

HTTPConfig *commoncfg.HTTPClientConfig `yaml:"http_config,omitempty" json:"http_config,omitempty"`

Expand All @@ -50,7 +50,7 @@ type globalConfig struct {
SMTPAuthPassword string `yaml:"smtp_auth_password,omitempty" json:"smtp_auth_password,omitempty"`
SMTPAuthSecret string `yaml:"smtp_auth_secret,omitempty" json:"smtp_auth_secret,omitempty"`
SMTPAuthIdentity string `yaml:"smtp_auth_identity,omitempty" json:"smtp_auth_identity,omitempty"`
SMTPRequireTLS bool `yaml:"smtp_require_tls,omitempty" json:"smtp_require_tls,omitempty"`
SMTPRequireTLS *bool `yaml:"smtp_require_tls,omitempty" json:"smtp_require_tls,omitempty"`
SlackAPIURL *config.URL `yaml:"slack_api_url,omitempty" json:"slack_api_url,omitempty"`
PagerdutyURL *config.URL `yaml:"pagerduty_url,omitempty" json:"pagerduty_url,omitempty"`
HipchatAPIURL *config.URL `yaml:"hipchat_api_url,omitempty" json:"hipchat_api_url,omitempty"`
Expand Down

0 comments on commit a739cab

Please sign in to comment.