Skip to content

Commit

Permalink
Fix test stage
Browse files Browse the repository at this point in the history
  • Loading branch information
wzshiming committed Oct 8, 2024
1 parent 43435cd commit 586a057
Show file tree
Hide file tree
Showing 3 changed files with 72 additions and 4 deletions.
11 changes: 7 additions & 4 deletions pkg/tools/stage/stage.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@ import (
"encoding/json"
"fmt"
"strings"
"time"

metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/runtime"
Expand Down Expand Up @@ -84,15 +83,19 @@ func TestingStages(ctx context.Context, target any, stages []*internalversion.St
return meta, nil
}

var now = time.Date(2006, 1, 2, 15, 4, 5, 0, time.UTC)

func testingStage(ctx context.Context, testTarget Obj, stage *lifecycle.Stage) (any, error) {
meta := map[string]any{
"stage": stage.Name(),
}

delay, ok := stage.DelayRangePossible(ctx, testTarget, now)
delayRange, ok := stage.DelayRange(ctx, testTarget)
if ok {
delay := map[string]string{
"min": delayRange[0],
}
if len(delayRange) > 1 {
delay["max"] = delayRange[1]
}
meta["delay"] = delay
}

Expand Down
38 changes: 38 additions & 0 deletions pkg/utils/expression/value_duration_from.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,16 @@ package expression

import (
"context"
"fmt"
"time"
)

// DurationGetter is a interface that can be used to get a time.Duration value.
type DurationGetter interface {
// Get returns a duration value.
Get(ctx context.Context, v interface{}, now time.Time) (time.Duration, bool)
// Info return a duration information
Info(ctx context.Context, v interface{}) (string, bool)
}

type durationFrom struct {
Expand Down Expand Up @@ -78,15 +81,50 @@ func (d *durationFrom) Get(ctx context.Context, v interface{}, now time.Time) (t
return 0, false
}

func (d *durationFrom) Info(ctx context.Context, v interface{}) (string, bool) {
out, err := d.query.Execute(ctx, v)
if err != nil {
return "", false
}
if len(out) == 0 {
if d.value != nil {
return d.value.String(), true
}
return "", false
}
if t, ok := out[0].(string); ok {
if t == "" {
return "", false
}
_, err := time.Parse(time.RFC3339Nano, t)
if err == nil {
return fmt.Sprintf("(now - %q)", t), true
}
du, err := time.ParseDuration(t)
if err == nil {
return du.String(), true
}
}
return "", false
}

type durationNoop struct {
}

func (durationNoop) Get(ctx context.Context, v interface{}, now time.Time) (time.Duration, bool) {
return 0, false
}

func (durationNoop) Info(ctx context.Context, v interface{}) (string, bool) {
return "", false
}

type duration int64

func (i duration) Get(ctx context.Context, v interface{}, now time.Time) (time.Duration, bool) {
return time.Duration(i), true
}

func (i duration) Info(ctx context.Context, v interface{}) (string, bool) {
return time.Duration(i).String(), false
}
27 changes: 27 additions & 0 deletions pkg/utils/lifecycle/lifecycle.go
Original file line number Diff line number Diff line change
Expand Up @@ -366,6 +366,33 @@ func (s *Stage) DelayRangePossible(ctx context.Context, v interface{}, now time.
return []time.Duration{duration, jitterDuration}, true
}

// DelayRange returns the delay range
func (s *Stage) DelayRange(ctx context.Context, v interface{}) ([]string, bool) {
if s.duration == nil {
return nil, false
}

duration, ok := s.duration.Info(ctx, v)
if !ok {
return nil, false
}

if s.jitterDuration == nil {
return []string{duration}, true
}

jitterDuration, ok := s.jitterDuration.Info(ctx, v)
if !ok {
return []string{duration}, true
}

if jitterDuration < duration {
return []string{jitterDuration}, true
}

return []string{duration, jitterDuration}, true
}

// Next returns the next of the stage.
func (s *Stage) Next() *Next {
return newNext(s.next)
Expand Down

0 comments on commit 586a057

Please sign in to comment.