Skip to content

Commit

Permalink
Rename override to provider-override
Browse files Browse the repository at this point in the history
  • Loading branch information
TwiN committed Dec 16, 2024
1 parent 127e7cd commit 66e921e
Show file tree
Hide file tree
Showing 50 changed files with 245 additions and 98 deletions.
6 changes: 3 additions & 3 deletions alerting/alert/alert.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,9 +47,9 @@ type Alert struct {
// or not for provider.ParseWithDefaultAlert to work. Use Alert.IsSendingOnResolved() for a non-pointer
SendOnResolved *bool `yaml:"send-on-resolved,omitempty"`

// Override is an optional field that can be used to override the provider's configuration
// ProviderOverride is an optional field that can be used to override the provider's configuration
// It is freeform so that it can be used for any provider-specific configuration.
Override map[string]any `yaml:"override,omitempty"`
ProviderOverride map[string]any `yaml:"provider-override,omitempty"`

// ResolveKey is an optional field that is used by some providers (i.e. PagerDuty's dedup_key) to resolve
// ongoing/triggered incidents
Expand Down Expand Up @@ -120,7 +120,7 @@ func (alert *Alert) Checksum() string {
}

func (alert *Alert) OverrideAsBytes() []byte {
yamlBytes, err := yaml.Marshal(alert.Override)
yamlBytes, err := yaml.Marshal(alert.ProviderOverride)
if err != nil {
logr.Warnf("[alert.OverrideAsBytes] Failed to marshal alert override of type=%s as bytes: %v", alert.Type, err)
}
Expand Down
8 changes: 7 additions & 1 deletion alerting/provider/awsses/awsses.go
Original file line number Diff line number Diff line change
Expand Up @@ -209,7 +209,7 @@ func (provider *AlertProvider) GetConfig(group string, alert *alert.Alert) (*Con
}
}
// Handle alert overrides
if len(alert.Override) != 0 {
if len(alert.ProviderOverride) != 0 {
overrideConfig := Config{}
if err := yaml.Unmarshal(alert.OverrideAsBytes(), &overrideConfig); err != nil {
return nil, err
Expand All @@ -222,3 +222,9 @@ func (provider *AlertProvider) GetConfig(group string, alert *alert.Alert) (*Con
}
return &cfg, nil
}

// ValidateOverrides validates the alert's provider override and, if present, the group override
func (provider *AlertProvider) ValidateOverrides(group string, alert *alert.Alert) error {
_, err := provider.GetConfig(group, alert)
return err
}
2 changes: 1 addition & 1 deletion alerting/provider/awsses/awsses_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -212,7 +212,7 @@ func TestAlertProvider_getConfigWithOverrides(t *testing.T) {
},
InputGroup: "group",
InputAlert: alert.Alert{
Override: map[string]any{
ProviderOverride: map[string]any{
"to": "[email protected]",
"access-key-id": 123,
},
Expand Down
8 changes: 7 additions & 1 deletion alerting/provider/custom/custom.go
Original file line number Diff line number Diff line change
Expand Up @@ -160,7 +160,7 @@ func (provider *AlertProvider) GetConfig(group string, alert *alert.Alert) (*Con
}
}
// Handle alert overrides
if len(alert.Override) != 0 {
if len(alert.ProviderOverride) != 0 {
overrideConfig := Config{}
if err := yaml.Unmarshal(alert.OverrideAsBytes(), &overrideConfig); err != nil {
return nil, err
Expand All @@ -173,3 +173,9 @@ func (provider *AlertProvider) GetConfig(group string, alert *alert.Alert) (*Con
}
return &cfg, nil
}

// ValidateOverrides validates the alert's provider override and, if present, the group override
func (provider *AlertProvider) ValidateOverrides(group string, alert *alert.Alert) error {
_, err := provider.GetConfig(group, alert)
return err
}
4 changes: 2 additions & 2 deletions alerting/provider/custom/custom_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -348,7 +348,7 @@ func TestAlertProvider_GetConfig(t *testing.T) {
},
},
InputGroup: "group",
InputAlert: alert.Alert{Override: map[string]any{"url": "http://alert-example.com", "body": "alert-body"}},
InputAlert: alert.Alert{ProviderOverride: map[string]any{"url": "http://alert-example.com", "body": "alert-body"}},
ExpectedOutput: Config{URL: "http://alert-example.com", Body: "alert-body"},
},
{
Expand All @@ -363,7 +363,7 @@ func TestAlertProvider_GetConfig(t *testing.T) {
},
},
InputGroup: "group",
InputAlert: alert.Alert{Override: map[string]any{"body": "alert-body"}},
InputAlert: alert.Alert{ProviderOverride: map[string]any{"body": "alert-body"}},
ExpectedOutput: Config{URL: "http://example.com", Body: "alert-body", Method: "POST"},
},
}
Expand Down
8 changes: 7 additions & 1 deletion alerting/provider/discord/discord.go
Original file line number Diff line number Diff line change
Expand Up @@ -180,7 +180,7 @@ func (provider *AlertProvider) GetConfig(group string, alert *alert.Alert) (*Con
}
}
// Handle alert overrides
if len(alert.Override) != 0 {
if len(alert.ProviderOverride) != 0 {
overrideConfig := Config{}
if err := yaml.Unmarshal(alert.OverrideAsBytes(), &overrideConfig); err != nil {
return nil, err
Expand All @@ -193,3 +193,9 @@ func (provider *AlertProvider) GetConfig(group string, alert *alert.Alert) (*Con
}
return &cfg, nil
}

// ValidateOverrides validates the alert's provider override and, if present, the group override
func (provider *AlertProvider) ValidateOverrides(group string, alert *alert.Alert) error {
_, err := provider.GetConfig(group, alert)
return err
}
4 changes: 2 additions & 2 deletions alerting/provider/discord/discord_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,7 @@ func TestAlertProvider_Send(t *testing.T) {
{
Name: "triggered-with-webhook-override",
Provider: AlertProvider{DefaultConfig: Config{WebhookURL: "http://example.com"}},
Alert: alert.Alert{Description: &firstDescription, SuccessThreshold: 5, FailureThreshold: 3, Override: map[string]any{"webhook-url": "http://example01.com"}},
Alert: alert.Alert{Description: &firstDescription, SuccessThreshold: 5, FailureThreshold: 3, ProviderOverride: map[string]any{"webhook-url": "http://example01.com"}},
Resolved: false,
MockRoundTripper: test.MockRoundTripper(func(r *http.Request) *http.Response {
return &http.Response{StatusCode: http.StatusOK, Body: http.NoBody}
Expand Down Expand Up @@ -310,7 +310,7 @@ func TestAlertProvider_GetConfig(t *testing.T) {
},
},
InputGroup: "group",
InputAlert: alert.Alert{Override: map[string]any{"webhook-url": "http://alert-example.com"}},
InputAlert: alert.Alert{ProviderOverride: map[string]any{"webhook-url": "http://alert-example.com"}},
ExpectedOutput: Config{WebhookURL: "http://alert-example.com"},
},
}
Expand Down
8 changes: 7 additions & 1 deletion alerting/provider/email/email.go
Original file line number Diff line number Diff line change
Expand Up @@ -187,7 +187,7 @@ func (provider *AlertProvider) GetConfig(group string, alert *alert.Alert) (*Con
}
}
// Handle alert overrides
if len(alert.Override) != 0 {
if len(alert.ProviderOverride) != 0 {
overrideConfig := Config{}
if err := yaml.Unmarshal(alert.OverrideAsBytes(), &overrideConfig); err != nil {
return nil, err
Expand All @@ -200,3 +200,9 @@ func (provider *AlertProvider) GetConfig(group string, alert *alert.Alert) (*Con
}
return &cfg, nil
}

// ValidateOverrides validates the alert's provider override and, if present, the group override
func (provider *AlertProvider) ValidateOverrides(group string, alert *alert.Alert) error {
_, err := provider.GetConfig(group, alert)
return err
}
2 changes: 1 addition & 1 deletion alerting/provider/email/email_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -198,7 +198,7 @@ func TestAlertProvider_GetConfig(t *testing.T) {
},
},
InputGroup: "group",
InputAlert: alert.Alert{Override: map[string]any{"to": "[email protected]"}},
InputAlert: alert.Alert{ProviderOverride: map[string]any{"to": "[email protected]"}},
ExpectedOutput: Config{From: "[email protected]", To: "[email protected]", Host: "smtp.gmail.com", Port: 587, Password: "password"},
},
}
Expand Down
16 changes: 13 additions & 3 deletions alerting/provider/gitea/gitea.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,10 @@ func (cfg *Config) Validate() error {
if len(pathParts) != 3 {
return ErrInvalidRepositoryURL
}
if cfg.repositoryOwner == pathParts[1] && cfg.repositoryName == pathParts[2] && cfg.giteaClient != nil {
// Already validated, let's skip the rest of the validation to avoid unnecessary API calls
return nil
}
cfg.repositoryOwner = pathParts[1]
cfg.repositoryName = pathParts[2]
opts := []gitea.ClientOption{
Expand Down Expand Up @@ -109,7 +113,7 @@ func (provider *AlertProvider) Validate() error {
// Send creates an issue in the designed RepositoryURL if the resolved parameter passed is false,
// or closes the relevant issue(s) if the resolved parameter passed is true.
func (provider *AlertProvider) Send(ep *endpoint.Endpoint, alert *alert.Alert, result *endpoint.Result, resolved bool) error {
cfg, err := provider.GetConfig(alert)
cfg, err := provider.GetConfig(ep.Group, alert)
if err != nil {
return err
}
Expand Down Expand Up @@ -191,10 +195,10 @@ func (provider *AlertProvider) GetDefaultAlert() *alert.Alert {
}

// GetConfig returns the configuration for the provider with the overrides applied
func (provider *AlertProvider) GetConfig(alert *alert.Alert) (*Config, error) {
func (provider *AlertProvider) GetConfig(group string, alert *alert.Alert) (*Config, error) {
cfg := provider.DefaultConfig
// Handle alert overrides
if len(alert.Override) != 0 {
if len(alert.ProviderOverride) != 0 {
overrideConfig := Config{}
if err := yaml.Unmarshal(alert.OverrideAsBytes(), &overrideConfig); err != nil {
return nil, err
Expand All @@ -205,3 +209,9 @@ func (provider *AlertProvider) GetConfig(alert *alert.Alert) (*Config, error) {
err := cfg.Validate()
return &cfg, err
}

// ValidateOverrides validates the alert's provider override and, if present, the group override
func (provider *AlertProvider) ValidateOverrides(group string, alert *alert.Alert) error {
_, err := provider.GetConfig(group, alert)
return err
}
9 changes: 3 additions & 6 deletions alerting/provider/gitea/gitea_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ func TestAlertProvider_Send(t *testing.T) {
}
for _, scenario := range scenarios {
t.Run(scenario.Name, func(t *testing.T) {
cfg, err := scenario.Provider.GetConfig(&scenario.Alert)
cfg, err := scenario.Provider.GetConfig("", &scenario.Alert)
if err != nil && !strings.Contains(err.Error(), "user does not exist") && !strings.Contains(err.Error(), "no such host") {
t.Error("expected no error, got", err.Error())
}
Expand Down Expand Up @@ -180,7 +180,6 @@ func TestAlertProvider_GetConfig(t *testing.T) {
scenarios := []struct {
Name string
Provider AlertProvider
InputGroup string
InputAlert alert.Alert
ExpectedOutput Config
}{
Expand All @@ -189,7 +188,6 @@ func TestAlertProvider_GetConfig(t *testing.T) {
Provider: AlertProvider{
DefaultConfig: Config{RepositoryURL: "https://gitea.com/TwiN/test", Token: "12345"},
},
InputGroup: "",
InputAlert: alert.Alert{},
ExpectedOutput: Config{RepositoryURL: "https://gitea.com/TwiN/test", Token: "12345"},
},
Expand All @@ -198,14 +196,13 @@ func TestAlertProvider_GetConfig(t *testing.T) {
Provider: AlertProvider{
DefaultConfig: Config{RepositoryURL: "https://gitea.com/TwiN/test", Token: "12345"},
},
InputGroup: "group",
InputAlert: alert.Alert{Override: map[string]any{"repository-url": "https://gitea.com/TwiN/alert-test", "token": "54321", "assignees": []string{"TwiN"}}},
InputAlert: alert.Alert{ProviderOverride: map[string]any{"repository-url": "https://gitea.com/TwiN/alert-test", "token": "54321", "assignees": []string{"TwiN"}}},
ExpectedOutput: Config{RepositoryURL: "https://gitea.com/TwiN/alert-test", Token: "54321", Assignees: []string{"TwiN"}},
},
}
for _, scenario := range scenarios {
t.Run(scenario.Name, func(t *testing.T) {
got, err := scenario.Provider.GetConfig(&scenario.InputAlert)
got, err := scenario.Provider.GetConfig("", &scenario.InputAlert)
if err != nil && !strings.Contains(err.Error(), "user does not exist") && !strings.Contains(err.Error(), "no such host") {
t.Fatalf("unexpected error: %s", err)
}
Expand Down
16 changes: 13 additions & 3 deletions alerting/provider/github/github.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,10 @@ func (cfg *Config) Validate() error {
if len(pathParts) != 3 {
return ErrInvalidRepositoryURL
}
if cfg.repositoryOwner == pathParts[1] && cfg.repositoryName == pathParts[2] && cfg.githubClient != nil {
// Already validated, let's skip the rest of the validation to avoid unnecessary API calls
return nil
}
cfg.repositoryOwner = pathParts[1]
cfg.repositoryName = pathParts[2]
// Create oauth2 HTTP client with GitHub token
Expand Down Expand Up @@ -99,7 +103,7 @@ func (provider *AlertProvider) Validate() error {
// Send creates an issue in the designed RepositoryURL if the resolved parameter passed is false,
// or closes the relevant issue(s) if the resolved parameter passed is true.
func (provider *AlertProvider) Send(ep *endpoint.Endpoint, alert *alert.Alert, result *endpoint.Result, resolved bool) error {
cfg, err := provider.GetConfig(alert)
cfg, err := provider.GetConfig(ep.Group, alert)
if err != nil {
return err
}
Expand Down Expand Up @@ -164,10 +168,10 @@ func (provider *AlertProvider) GetDefaultAlert() *alert.Alert {
}

// GetConfig returns the configuration for the provider with the overrides applied
func (provider *AlertProvider) GetConfig(alert *alert.Alert) (*Config, error) {
func (provider *AlertProvider) GetConfig(group string, alert *alert.Alert) (*Config, error) {
cfg := provider.DefaultConfig
// Handle alert overrides
if len(alert.Override) != 0 {
if len(alert.ProviderOverride) != 0 {
overrideConfig := Config{}
if err := yaml.Unmarshal(alert.OverrideAsBytes(), &overrideConfig); err != nil {
return nil, err
Expand All @@ -178,3 +182,9 @@ func (provider *AlertProvider) GetConfig(alert *alert.Alert) (*Config, error) {
err := cfg.Validate()
return &cfg, err
}

// ValidateOverrides validates the alert's provider override and, if present, the group override
func (provider *AlertProvider) ValidateOverrides(group string, alert *alert.Alert) error {
_, err := provider.GetConfig(group, alert)
return err
}
9 changes: 3 additions & 6 deletions alerting/provider/github/github_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ func TestAlertProvider_Send(t *testing.T) {
}
for _, scenario := range scenarios {
t.Run(scenario.Name, func(t *testing.T) {
cfg, err := scenario.Provider.GetConfig(&scenario.Alert)
cfg, err := scenario.Provider.GetConfig("", &scenario.Alert)
if err != nil && !strings.Contains(err.Error(), "failed to retrieve GitHub user") && !strings.Contains(err.Error(), "no such host") {
t.Error("expected no error, got", err.Error())
}
Expand Down Expand Up @@ -180,7 +180,6 @@ func TestAlertProvider_GetConfig(t *testing.T) {
scenarios := []struct {
Name string
Provider AlertProvider
InputGroup string
InputAlert alert.Alert
ExpectedOutput Config
}{
Expand All @@ -189,7 +188,6 @@ func TestAlertProvider_GetConfig(t *testing.T) {
Provider: AlertProvider{
DefaultConfig: Config{RepositoryURL: "https://github.com/TwiN/test", Token: "12345"},
},
InputGroup: "",
InputAlert: alert.Alert{},
ExpectedOutput: Config{RepositoryURL: "https://github.com/TwiN/test", Token: "12345"},
},
Expand All @@ -198,14 +196,13 @@ func TestAlertProvider_GetConfig(t *testing.T) {
Provider: AlertProvider{
DefaultConfig: Config{RepositoryURL: "https://github.com/TwiN/test", Token: "12345"},
},
InputGroup: "group",
InputAlert: alert.Alert{Override: map[string]any{"repository-url": "https://github.com/TwiN/alert-test", "token": "54321"}},
InputAlert: alert.Alert{ProviderOverride: map[string]any{"repository-url": "https://github.com/TwiN/alert-test", "token": "54321"}},
ExpectedOutput: Config{RepositoryURL: "https://github.com/TwiN/alert-test", Token: "54321"},
},
}
for _, scenario := range scenarios {
t.Run(scenario.Name, func(t *testing.T) {
got, err := scenario.Provider.GetConfig(&scenario.InputAlert)
got, err := scenario.Provider.GetConfig("", &scenario.InputAlert)
if err != nil && !strings.Contains(err.Error(), "failed to retrieve GitHub user") && !strings.Contains(err.Error(), "no such host") {
t.Fatalf("unexpected error: %s", err)
}
Expand Down
12 changes: 9 additions & 3 deletions alerting/provider/gitlab/gitlab.go
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ func (provider *AlertProvider) Validate() error {
// Send creates an issue in the designed RepositoryURL if the resolved parameter passed is false,
// or closes the relevant issue(s) if the resolved parameter passed is true.
func (provider *AlertProvider) Send(ep *endpoint.Endpoint, alert *alert.Alert, result *endpoint.Result, resolved bool) error {
cfg, err := provider.GetConfig(alert)
cfg, err := provider.GetConfig(ep.Group, alert)
if err != nil {
return err
}
Expand Down Expand Up @@ -182,10 +182,10 @@ func (provider *AlertProvider) GetDefaultAlert() *alert.Alert {
}

// GetConfig returns the configuration for the provider with the overrides applied
func (provider *AlertProvider) GetConfig(alert *alert.Alert) (*Config, error) {
func (provider *AlertProvider) GetConfig(group string, alert *alert.Alert) (*Config, error) {
cfg := provider.DefaultConfig
// Handle alert overrides
if len(alert.Override) != 0 {
if len(alert.ProviderOverride) != 0 {
overrideConfig := Config{}
if err := yaml.Unmarshal(alert.OverrideAsBytes(), &overrideConfig); err != nil {
return nil, err
Expand All @@ -196,3 +196,9 @@ func (provider *AlertProvider) GetConfig(alert *alert.Alert) (*Config, error) {
err := cfg.Validate()
return &cfg, err
}

// ValidateOverrides validates the alert's provider override and, if present, the group override
func (provider *AlertProvider) ValidateOverrides(group string, alert *alert.Alert) error {
_, err := provider.GetConfig(group, alert)
return err
}
9 changes: 3 additions & 6 deletions alerting/provider/gitlab/gitlab_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,7 @@ func TestAlertProvider_buildAlertBody(t *testing.T) {
}
for _, scenario := range scenarios {
t.Run(scenario.Name, func(t *testing.T) {
cfg, err := scenario.Provider.GetConfig(&scenario.Alert)
cfg, err := scenario.Provider.GetConfig("", &scenario.Alert)
if err != nil {
t.Error("expected no error, got", err.Error())
}
Expand Down Expand Up @@ -170,7 +170,6 @@ func TestAlertProvider_GetConfig(t *testing.T) {
scenarios := []struct {
Name string
Provider AlertProvider
InputGroup string
InputAlert alert.Alert
ExpectedOutput Config
}{
Expand All @@ -179,7 +178,6 @@ func TestAlertProvider_GetConfig(t *testing.T) {
Provider: AlertProvider{
DefaultConfig: Config{WebhookURL: "https://github.com/TwiN/test", AuthorizationKey: "12345"},
},
InputGroup: "",
InputAlert: alert.Alert{},
ExpectedOutput: Config{WebhookURL: "https://github.com/TwiN/test", AuthorizationKey: "12345", Severity: DefaultSeverity, MonitoringTool: DefaultMonitoringTool},
},
Expand All @@ -188,14 +186,13 @@ func TestAlertProvider_GetConfig(t *testing.T) {
Provider: AlertProvider{
DefaultConfig: Config{WebhookURL: "https://github.com/TwiN/test", AuthorizationKey: "12345"},
},
InputGroup: "group",
InputAlert: alert.Alert{Override: map[string]any{"repository-url": "https://github.com/TwiN/alert-test", "authorization-key": "54321", "severity": "info", "monitoring-tool": "not-gatus", "environment-name": "prod", "service": "example"}},
InputAlert: alert.Alert{ProviderOverride: map[string]any{"repository-url": "https://github.com/TwiN/alert-test", "authorization-key": "54321", "severity": "info", "monitoring-tool": "not-gatus", "environment-name": "prod", "service": "example"}},
ExpectedOutput: Config{WebhookURL: "https://github.com/TwiN/test", AuthorizationKey: "54321", Severity: "info", MonitoringTool: "not-gatus", EnvironmentName: "prod", Service: "example"},
},
}
for _, scenario := range scenarios {
t.Run(scenario.Name, func(t *testing.T) {
got, err := scenario.Provider.GetConfig(&scenario.InputAlert)
got, err := scenario.Provider.GetConfig("", &scenario.InputAlert)
if err != nil && !strings.Contains(err.Error(), "user does not exist") && !strings.Contains(err.Error(), "no such host") {
t.Fatalf("unexpected error: %s", err)
}
Expand Down
Loading

0 comments on commit 66e921e

Please sign in to comment.