Skip to content

Commit

Permalink
feat: add bot_token_file in TelegramConfig
Browse files Browse the repository at this point in the history
Fixes #5858

Signed-off-by: Jayapriya Pai <slashpai9@gmail.com>
  • Loading branch information
slashpai committed Sep 4, 2023
1 parent eae06bc commit 49e9147
Show file tree
Hide file tree
Showing 19 changed files with 265 additions and 22 deletions.
36 changes: 34 additions & 2 deletions Documentation/api.md

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

14 changes: 11 additions & 3 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.

6 changes: 5 additions & 1 deletion jsonnet/prometheus-operator/alertmanagerconfigs-crd.json
Original file line number Diff line number Diff line change
Expand Up @@ -3331,7 +3331,7 @@
"type": "string"
},
"botToken": {
"description": "Telegram bot token The secret needs to be in the same namespace as the AlertmanagerConfig object and accessible by the Prometheus Operator.",
"description": "Telegram bot token. It is mutually exclusive with `botTokenFile`. The secret needs to be in the same namespace as the AlertmanagerConfig object and accessible by the Prometheus Operator. \n Either `botToken` or `botTokenFile` is required.",
"properties": {
"key": {
"description": "The key of the secret to select from. Must be a valid secret key.",
Expand All @@ -3351,6 +3351,10 @@
],
"type": "object"
},
"botTokenFile": {
"description": "File to read the Telegram bot token from. It is mutually exclusive with `botToken`. Either `botToken` or `botTokenFile` is required. \n It requires Alertmanager >= v0.26.0.",
"type": "string"
},
"chatID": {
"description": "The Telegram chat ID.",
"format": "int64",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3186,7 +3186,7 @@
type: 'string',
},
botToken: {
description: 'Telegram bot token The secret needs to be in the same namespace as the AlertmanagerConfig object and accessible by the Prometheus Operator.',
description: 'Telegram bot token. It is mutually exclusive with `botTokenFile`. The secret needs to be in the same namespace as the AlertmanagerConfig object and accessible by the Prometheus Operator. \n Either `botToken` or `botTokenFile` is required.',
properties: {
key: {
description: 'The key of the secret to select from. Must be a valid secret key.',
Expand All @@ -3205,6 +3205,10 @@
],
type: 'object',
},
botTokenFile: {
description: 'File to read the Telegram bot token from. It is mutually exclusive with `botToken`. Either `botToken` or `botTokenFile` is required. \n It requires Alertmanager >= v0.26.0.',
type: 'string',
},
chatID: {
description: 'The Telegram chat ID.',
format: 'int64',
Expand Down
16 changes: 14 additions & 2 deletions pkg/alertmanager/amcfg.go
Original file line number Diff line number Diff line change
Expand Up @@ -1979,17 +1979,29 @@ func (sc *snsConfig) sanitize(amVersion semver.Version, logger log.Logger) error
}

func (tc *telegramConfig) sanitize(amVersion semver.Version, logger log.Logger) error {
lessThanV0_26 := amVersion.LT(semver.MustParse("0.26.0"))
telegramAllowed := amVersion.GTE(semver.MustParse("0.24.0"))

if !telegramAllowed {
return fmt.Errorf(`invalid syntax in receivers config; telegram integration is available in Alertmanager >= 0.24.0`)
}

if tc.BotTokenFile != "" && lessThanV0_26 {
msg := "'bot_token_file' supported in Alertmanager >= 0.26.0 only - dropping field from provided config"
level.Warn(logger).Log("msg", msg, "current_version", amVersion.String())
tc.BotTokenFile = ""
}

if tc.ChatID == 0 {
return errors.Errorf("mandatory field %q is empty", "chatID")
}

if tc.BotToken == "" {
return fmt.Errorf("mandatory field %q is empty", "botToken")
if tc.BotToken == "" && tc.BotTokenFile == "" {
return fmt.Errorf("missing mandatory field botToken or botTokenFile")
}

if tc.BotToken != "" && tc.BotTokenFile != "" {
return fmt.Errorf("at most one of botToken or botTokenFile must be configured")
}

return tc.HTTPConfig.sanitize(amVersion, logger)
Expand Down
81 changes: 81 additions & 0 deletions pkg/alertmanager/amcfg_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1964,6 +1964,9 @@ func TestSanitizeConfig(t *testing.T) {
versionWebexAllowed := semver.Version{Major: 0, Minor: 25}
versionWebexNotAllowed := semver.Version{Major: 0, Minor: 24}

versionTelegramBotTokenFileAllowed := semver.Version{Major: 0, Minor: 26}
versionTelegramBotTokenFileNotAllowed := semver.Version{Major: 0, Minor: 25}

for _, tc := range []struct {
name string
againstVersion semver.Version
Expand Down Expand Up @@ -2364,6 +2367,84 @@ func TestSanitizeConfig(t *testing.T) {
},
expectErr: true,
},
{
name: "bot_token_file field for Telegram config",
againstVersion: versionTelegramBotTokenFileAllowed,
in: &alertmanagerConfig{
Receivers: []*receiver{
{
Name: "telegram",
TelegramConfigs: []*telegramConfig{
{
ChatID: 12345,
BotTokenFile: "/test",
},
},
},
},
},
expect: alertmanagerConfig{
Receivers: []*receiver{
{
Name: "telegram",
TelegramConfigs: []*telegramConfig{
{
ChatID: 12345,
BotTokenFile: "/test",
},
},
},
},
},
},
{
name: "bot_token_file and bot_token fields for Telegram config",
againstVersion: versionTelegramBotTokenFileAllowed,
in: &alertmanagerConfig{
Receivers: []*receiver{
{
Name: "telegram",
TelegramConfigs: []*telegramConfig{
{
ChatID: 12345,
BotToken: "test",
BotTokenFile: "/test",
},
},
},
},
},
expectErr: true,
},
{
name: "bot_token_file is dropped for unsupported versions",
againstVersion: versionTelegramBotTokenFileNotAllowed,
in: &alertmanagerConfig{
Receivers: []*receiver{
{
Name: "telegram",
TelegramConfigs: []*telegramConfig{
{
ChatID: 12345,
BotToken: "test",
BotTokenFile: "/test",
},
},
},
},
},
expect: alertmanagerConfig{
Receivers: []*receiver{
{
Name: "telegram",
TelegramConfigs: []*telegramConfig{{
ChatID: 12345,
BotToken: "test",
}},
},
},
},
},
} {
t.Run(tc.name, func(t *testing.T) {
err := tc.in.sanitize(tc.againstVersion, logger)
Expand Down
1 change: 1 addition & 0 deletions pkg/alertmanager/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -337,6 +337,7 @@ type telegramConfig struct {
VSendResolved *bool `yaml:"send_resolved,omitempty" json:"send_resolved,omitempty"`
APIUrl string `yaml:"api_url,omitempty" json:"api_url,omitempty"`
BotToken string `yaml:"bot_token,omitempty" json:"bot_token,omitempty"`
BotTokenFile string `yaml:"bot_token_file,omitempty" json:"bot_token_file,omitempty"`
ChatID int64 `yaml:"chat_id,omitempty" json:"chat_id,omitempty"`
Message string `yaml:"message,omitempty" json:"message,omitempty"`
DisableNotifications bool `yaml:"disable_notifications,omitempty" json:"disable_notifications,omitempty"`
Expand Down
4 changes: 2 additions & 2 deletions pkg/alertmanager/validation/v1alpha1/validation.go
Original file line number Diff line number Diff line change
Expand Up @@ -300,8 +300,8 @@ func validateSnsConfigs(configs []monitoringv1alpha1.SNSConfig) error {
func validateTelegramConfigs(configs []monitoringv1alpha1.TelegramConfig) error {
for _, config := range configs {

if config.BotToken == nil {
return fmt.Errorf("mandatory field %q is empty", "botToken")
if config.BotToken == nil && config.BotTokenFile == nil {
return fmt.Errorf("mandatory field botToken or botTokenfile is empty")
}

if config.ChatID == 0 {
Expand Down
23 changes: 23 additions & 0 deletions pkg/alertmanager/validation/v1beta1/validation.go
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,10 @@ func validateReceivers(receivers []monitoringv1beta1.Receiver) (map[string]struc
return nil, errors.Wrapf(err, "failed to validate 'snsConfig' - receiver %s", receiver.Name)
}

if err := validateTelegramConfigs(receiver.TelegramConfigs); err != nil {
return nil, errors.Wrapf(err, "failed to validate 'telegramConfig' - receiver %s", receiver.Name)
}

if err := validateDiscordConfigs(receiver.DiscordConfigs); err != nil {
return nil, errors.Wrapf(err, "failed to validate 'discordConfig' - receiver %s", receiver.Name)
}
Expand Down Expand Up @@ -284,6 +288,25 @@ func validateSnsConfigs(configs []monitoringv1beta1.SNSConfig) error {
return nil
}

func validateTelegramConfigs(configs []monitoringv1beta1.TelegramConfig) error {
for _, config := range configs {

if config.BotToken == nil && config.BotTokenFile == nil {
return fmt.Errorf("mandatory field botToken or botTokenfile is empty")
}

if config.ChatID == 0 {
return fmt.Errorf("mandatory field %q is empty", "chatID")
}

if err := config.HTTPConfig.Validate(); err != nil {
return err
}
}

return nil
}

func validateWebexConfigs(configs []monitoringv1beta1.WebexConfig) error {
for _, config := range configs {
if *config.APIURL != "" {
Expand Down

0 comments on commit 49e9147

Please sign in to comment.