mirror of
https://github.com/prometheus-operator/prometheus-operator.git
synced 2025-04-21 03:38:43 +00:00
Fix: fix time.ParseDuration bug (#7249)
* fix time.ParseDuration bug Signed-off-by: dongjiang <dongjiang1989@126.com>
This commit is contained in:
parent
e61abf1b84
commit
4ddd256878
5 changed files with 103 additions and 23 deletions
pkg/alertmanager
|
@ -24,7 +24,6 @@ import (
|
|||
"net/url"
|
||||
"path"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"github.com/blang/semver/v4"
|
||||
"github.com/prometheus/alertmanager/config"
|
||||
|
@ -1195,13 +1194,19 @@ func (cb *configBuilder) convertPushoverConfig(ctx context.Context, in monitorin
|
|||
|
||||
{
|
||||
if in.Retry != "" {
|
||||
retry, _ := time.ParseDuration(in.Retry)
|
||||
out.Retry = duration(retry)
|
||||
retry, err := model.ParseDuration(in.Retry)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("parse resolve retry: %w", err)
|
||||
}
|
||||
out.Retry = &retry
|
||||
}
|
||||
|
||||
if in.Expire != "" {
|
||||
expire, _ := time.ParseDuration(in.Expire)
|
||||
out.Expire = duration(expire)
|
||||
expire, err := model.ParseDuration(in.Expire)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("parse resolve expire: %w", err)
|
||||
}
|
||||
out.Expire = &expire
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -1596,6 +1596,68 @@ func TestGenerateConfig(t *testing.T) {
|
|||
},
|
||||
golden: "CR_with_WeChat_Receiver.golden",
|
||||
},
|
||||
{
|
||||
name: "CR with Pushover Receiver",
|
||||
kclient: fake.NewSimpleClientset(
|
||||
&corev1.Secret{
|
||||
ObjectMeta: metav1.ObjectMeta{
|
||||
Name: "am-pushover-test-receiver",
|
||||
Namespace: "mynamespace",
|
||||
},
|
||||
Data: map[string][]byte{
|
||||
"userkey": []byte("userkeySecret"),
|
||||
},
|
||||
},
|
||||
&corev1.Secret{
|
||||
ObjectMeta: metav1.ObjectMeta{
|
||||
Name: "am-pushover-token-receiver",
|
||||
Namespace: "mynamespace",
|
||||
},
|
||||
Data: map[string][]byte{
|
||||
"token": []byte("tokenSecret"),
|
||||
},
|
||||
},
|
||||
),
|
||||
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",
|
||||
PushoverConfigs: []monitoringv1alpha1.PushoverConfig{{
|
||||
UserKey: &corev1.SecretKeySelector{
|
||||
LocalObjectReference: corev1.LocalObjectReference{
|
||||
Name: "am-pushover-test-receiver",
|
||||
},
|
||||
Key: "userkey",
|
||||
},
|
||||
Token: &corev1.SecretKeySelector{
|
||||
LocalObjectReference: corev1.LocalObjectReference{
|
||||
Name: "am-pushover-token-receiver",
|
||||
},
|
||||
Key: "token",
|
||||
},
|
||||
Retry: "5m",
|
||||
Expire: "30s",
|
||||
}},
|
||||
}},
|
||||
},
|
||||
},
|
||||
},
|
||||
golden: "CR_with_Pushover_Receiver.golden",
|
||||
},
|
||||
{
|
||||
name: "CR with Telegram Receiver",
|
||||
amVersion: &version24,
|
||||
|
|
|
@ -27,6 +27,7 @@ import (
|
|||
"github.com/blang/semver/v4"
|
||||
"github.com/mitchellh/hashstructure"
|
||||
"github.com/prometheus/client_golang/prometheus"
|
||||
"github.com/prometheus/common/model"
|
||||
appsv1 "k8s.io/api/apps/v1"
|
||||
v1 "k8s.io/api/core/v1"
|
||||
apierrors "k8s.io/apimachinery/pkg/api/errors"
|
||||
|
@ -1492,6 +1493,18 @@ func checkPushoverConfigs(
|
|||
return err
|
||||
}
|
||||
|
||||
if config.Expire != "" {
|
||||
if _, err := model.ParseDuration(config.Expire); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
|
||||
if config.Retry != "" {
|
||||
if _, err := model.ParseDuration(config.Retry); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
|
||||
if err := configureHTTPConfigInStore(ctx, config.HTTPConfig, namespace, store); err != nil {
|
||||
return err
|
||||
}
|
||||
|
|
16
pkg/alertmanager/testdata/CR_with_Pushover_Receiver.golden
generated
vendored
Normal file
16
pkg/alertmanager/testdata/CR_with_Pushover_Receiver.golden
generated
vendored
Normal file
|
@ -0,0 +1,16 @@
|
|||
route:
|
||||
receiver: "null"
|
||||
routes:
|
||||
- receiver: mynamespace/myamc/test
|
||||
matchers:
|
||||
- namespace="mynamespace"
|
||||
continue: true
|
||||
receivers:
|
||||
- name: "null"
|
||||
- name: mynamespace/myamc/test
|
||||
pushover_configs:
|
||||
- user_key: userkeySecret
|
||||
token: tokenSecret
|
||||
retry: 5m
|
||||
expire: 30s
|
||||
templates: []
|
|
@ -15,8 +15,6 @@
|
|||
package alertmanager
|
||||
|
||||
import (
|
||||
"time"
|
||||
|
||||
"github.com/prometheus/alertmanager/config"
|
||||
"github.com/prometheus/common/model"
|
||||
)
|
||||
|
@ -334,8 +332,8 @@ type pushoverConfig struct {
|
|||
Device string `yaml:"device,omitempty" json:"device,omitempty"`
|
||||
Sound string `yaml:"sound,omitempty" json:"sound,omitempty"`
|
||||
Priority string `yaml:"priority,omitempty" json:"priority,omitempty"`
|
||||
Retry duration `yaml:"retry,omitempty" json:"retry,omitempty"`
|
||||
Expire duration `yaml:"expire,omitempty" json:"expire,omitempty"`
|
||||
Retry *model.Duration `yaml:"retry,omitempty" json:"retry,omitempty"`
|
||||
Expire *model.Duration `yaml:"expire,omitempty" json:"expire,omitempty"`
|
||||
HTML bool `yaml:"html,omitempty" json:"html,omitempty"`
|
||||
}
|
||||
|
||||
|
@ -392,20 +390,6 @@ type sigV4Config struct {
|
|||
RoleARN string `yaml:"role_arn,omitempty" json:"role_arn,omitempty"`
|
||||
}
|
||||
|
||||
type duration time.Duration
|
||||
|
||||
func (d *duration) UnmarshalText(text []byte) error {
|
||||
parsed, err := time.ParseDuration(string(text))
|
||||
if err == nil {
|
||||
*d = duration(parsed)
|
||||
}
|
||||
return err
|
||||
}
|
||||
|
||||
func (d *duration) MarshalText() ([]byte, error) {
|
||||
return []byte(time.Duration(*d).String()), nil
|
||||
}
|
||||
|
||||
type victorOpsConfig struct {
|
||||
VSendResolved *bool `yaml:"send_resolved,omitempty" json:"send_resolved,omitempty"`
|
||||
HTTPConfig *httpClientConfig `yaml:"http_config,omitempty" json:"http_config,omitempty"`
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue