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

[WIP] Add test for general stage #1109

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
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
15 changes: 15 additions & 0 deletions kustomize/stage/pod/general/testdata/pod-pending.input.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
# @Stage: ../pod-create.yaml
# @Stage: ../pod-init-container-running.yaml
# @Stage: ../pod-init-container-completed.yaml
# @Stage: ../pod-ready.yaml
# @Stage: ../pod-complete.yaml
# @Stage: ../pod-remove-finalizer.yaml
# @Stage: ../pod-delete.yaml
apiVersion: v1
kind: Pod
metadata:
name: example-pod
spec:
containers:
- name: container
image: image
50 changes: 50 additions & 0 deletions kustomize/stage/pod/general/testdata/pod-pending.output.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
apiGroup: v1
kind: Pod
name: example-pod
stages:
- delay:
max: 5s
min: 1s
next:
- data:
- op: add
path: /metadata/finalizers
value:
- kwok.x-k8s.io/fake
kind: patch
type: application/json-patch+json
- data:
status:
conditions:
- lastProbeTime: null
lastTransitionTime: <Now>
status: "True"
type: Initialized
- lastProbeTime: null
lastTransitionTime: <Now>
message: 'containers with unready status: [ container ]'
reason: ContainersNotReady
status: "False"
type: Ready
- lastProbeTime: null
lastTransitionTime: <Now>
message: 'containers with unready status: [ container ]'
reason: ContainersNotReady
status: "False"
type: ContainersReady
containerStatuses:
- image: image
name: container
ready: false
restartCount: 0
started: false
state:
waiting:
reason: ContainerCreating
hostIP: <NodeIPWith(<nil>)>
phase: Pending
podIP: <PodIPWith(<nil>, false, "", "example-pod", "")>
kind: patch
subresource: status
type: application/merge-patch+json
stage: pod-create
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
Loading