From 5ddf9e24eaddc4e1ee2188761aef04b7a52eee6e Mon Sep 17 00:00:00 2001 From: Takuma Ishikawa Date: Mon, 18 Oct 2021 22:13:42 +0900 Subject: [PATCH] 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 --- cli/silence_add.go | 32 ++++++++++++++++---------------- 1 file changed, 16 insertions(+), 16 deletions(-) diff --git a/cli/silence_add.go b/cli/silence_add.go index c7c57c31..30d4b535 100644 --- a/cli/silence_add.go +++ b/cli/silence_add.go @@ -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) @@ -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{