amtool: Fix behavior of adding silence with duration option (#2741)

endsAt should be calculated from startsAt, not from the current time

Signed-off-by: nekketsuuu <nekketsuuu@users.noreply.github.com>
This commit is contained in:
Takuma Ishikawa 2021-10-18 22:13:42 +09:00 committed by GitHub
parent 78fdd6f56b
commit 5ddf9e24ea
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 16 additions and 16 deletions

View File

@ -108,6 +108,17 @@ func (c *silenceAddCmd) add(ctx context.Context, _ *kingpin.ParseContext) error
return fmt.Errorf("no matchers specified") return fmt.Errorf("no matchers specified")
} }
var startsAt time.Time
if c.start != "" {
startsAt, err = time.Parse(time.RFC3339, c.start)
if err != nil {
return err
}
} else {
startsAt = time.Now().UTC()
}
var endsAt time.Time var endsAt time.Time
if c.end != "" { if c.end != "" {
endsAt, err = time.Parse(time.RFC3339, c.end) endsAt, err = time.Parse(time.RFC3339, c.end)
@ -122,28 +133,17 @@ func (c *silenceAddCmd) add(ctx context.Context, _ *kingpin.ParseContext) error
if d == 0 { if d == 0 {
return fmt.Errorf("silence duration must be greater than 0") return fmt.Errorf("silence duration must be greater than 0")
} }
endsAt = time.Now().UTC().Add(time.Duration(d)) endsAt = startsAt.UTC().Add(time.Duration(d))
}
if c.requireComment && c.comment == "" {
return errors.New("comment required by config")
}
var startsAt time.Time
if c.start != "" {
startsAt, err = time.Parse(time.RFC3339, c.start)
if err != nil {
return err
}
} else {
startsAt = time.Now().UTC()
} }
if startsAt.After(endsAt) { if startsAt.After(endsAt) {
return errors.New("silence cannot start after it ends") return errors.New("silence cannot start after it ends")
} }
if c.requireComment && c.comment == "" {
return errors.New("comment required by config")
}
start := strfmt.DateTime(startsAt) start := strfmt.DateTime(startsAt)
end := strfmt.DateTime(endsAt) end := strfmt.DateTime(endsAt)
ps := &models.PostableSilence{ ps := &models.PostableSilence{