Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Merged
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 16 additions & 16 deletions cli/silence_add.go
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,17 @@ func (c *silenceAddCmd) add(ctx context.Context, _ *kingpin.ParseContext) error
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
if c.end != "" {
endsAt, err = time.Parse(time.RFC3339, c.end)
Expand All @@ -122,28 +133,17 @@ func (c *silenceAddCmd) add(ctx context.Context, _ *kingpin.ParseContext) error
if d == 0 {
return fmt.Errorf("silence duration must be greater than 0")
}
endsAt = time.Now().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()
endsAt = startsAt.UTC().Add(time.Duration(d))
}

if startsAt.After(endsAt) {
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)
end := strfmt.DateTime(endsAt)
ps := &models.PostableSilence{
Expand Down