Skip to content

Commit

Permalink
Merge pull request #1162 from wzshiming/feat/weight-from
Browse files Browse the repository at this point in the history
Add weightFrom to Stages
  • Loading branch information
wzshiming authored Jul 8, 2024
2 parents 057cca9 + 35bdf70 commit 56be4ee
Show file tree
Hide file tree
Showing 30 changed files with 383 additions and 27 deletions.
11 changes: 11 additions & 0 deletions kustomize/crd/bases/kwok.x-k8s.io_stages.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -273,6 +273,17 @@ spec:
a random stage will be matched as the next stage based on the weight.
minimum: 0
type: integer
weightFrom:
description: |-
WeightFrom means is the expression used to get the value.
If it is a number type, convert to int.
If it is a string type, the value get will be parsed by strconv.ParseInt.
properties:
expressionFrom:
description: ExpressionFrom is the expression used to get the
value.
type: string
type: object
required:
- next
- resourceRef
Expand Down
2 changes: 2 additions & 0 deletions kustomize/stage/node/chaos/node-not-ready.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@ spec:
jitterDurationFrom:
expressionFrom: '.metadata.annotations["node-not-ready.stage.kwok.x-k8s.io/jitter-delay"]'
weight: 10000
weightFrom:
expressionFrom: '.metadata.annotations["node-not-ready.stage.kwok.x-k8s.io/weight"]'
next:
statusTemplate: |
{{ $now := Now }}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -68,3 +68,4 @@ stages:
subresource: status
type: application/merge-patch+json
stage: node-initialize
weight: 0
2 changes: 2 additions & 0 deletions kustomize/stage/pod/chaos/pod-container-running-failed.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@ spec:
jitterDurationFrom:
expressionFrom: '.metadata.annotations["pod-container-running-failed.stage.kwok.x-k8s.io/jitter-delay"]'
weight: 10000
weightFrom:
expressionFrom: '.metadata.annotations["pod-container-running-failed.stage.kwok.x-k8s.io/weight"]'
next:
statusTemplate: |
{{ $now := Now }}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@ spec:
jitterDurationFrom:
expressionFrom: '.metadata.annotations["pod-init-container-running-failed.stage.kwok.x-k8s.io/jitter-delay"]'
weight: 10000
weightFrom:
expressionFrom: '.metadata.annotations["pod-init-container-running-failed.stage.kwok.x-k8s.io/weight"]'
next:
statusTemplate: |
{{ $now := Now }}
Expand Down
1 change: 1 addition & 0 deletions kustomize/stage/pod/fast/testdata/pod-pending.output.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -32,3 +32,4 @@ stages:
subresource: status
type: application/merge-patch+json
stage: pod-ready
weight: 0
Original file line number Diff line number Diff line change
Expand Up @@ -22,3 +22,4 @@ stages:
subresource: status
type: application/merge-patch+json
stage: pod-complete
weight: 0
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,4 @@ stages:
- next:
- kind: delete
stage: pod-delete
weight: 0
2 changes: 2 additions & 0 deletions kustomize/stage/pod/general/pod-complete.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@ spec:
values:
- 'Job'
weight: 1
weightFrom:
expressionFrom: '.metadata.annotations["pod-complete.stage.kwok.x-k8s.io/weight"]'
delay:
durationMilliseconds: 1000
durationFrom:
Expand Down
2 changes: 2 additions & 0 deletions kustomize/stage/pod/general/pod-create.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ spec:
- key: '.status.podIP'
operator: 'DoesNotExist'
weight: 1
weightFrom:
expressionFrom: '.metadata.annotations["pod-create.stage.kwok.x-k8s.io/weight"]'
delay:
durationMilliseconds: 1000
durationFrom:
Expand Down
2 changes: 2 additions & 0 deletions kustomize/stage/pod/general/pod-delete.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ spec:
- key: '.metadata.finalizers'
operator: 'DoesNotExist'
weight: 1
weightFrom:
expressionFrom: '.metadata.annotations["pod-delete.stage.kwok.x-k8s.io/weight"]'
delay:
durationMilliseconds: 1000
durationFrom:
Expand Down
2 changes: 2 additions & 0 deletions kustomize/stage/pod/general/pod-init-container-completed.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ spec:
- key: '.status.initContainerStatuses.[].state.running.startedAt'
operator: 'Exists'
weight: 1
weightFrom:
expressionFrom: '.metadata.annotations["pod-init-container-completed.stage.kwok.x-k8s.io/weight"]'
delay:
durationMilliseconds: 1000
durationFrom:
Expand Down
2 changes: 2 additions & 0 deletions kustomize/stage/pod/general/pod-init-container-running.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@ spec:
- key: '.status.initContainerStatuses.[].state.waiting.reason'
operator: 'Exists'
weight: 1
weightFrom:
expressionFrom: '.metadata.annotations["pod-init-container-running.stage.kwok.x-k8s.io/weight"]'
delay:
durationMilliseconds: 1000
durationFrom:
Expand Down
2 changes: 2 additions & 0 deletions kustomize/stage/pod/general/pod-ready.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ spec:
- key: '.status.containerStatuses.[].state.running.startedAt'
operator: 'DoesNotExist'
weight: 1
weightFrom:
expressionFrom: '.metadata.annotations["pod-ready.stage.kwok.x-k8s.io/weight"]'
delay:
durationMilliseconds: 1000
durationFrom:
Expand Down
2 changes: 2 additions & 0 deletions kustomize/stage/pod/general/pod-remove-finalizer.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@ spec:
values:
- 'kwok.x-k8s.io/fake'
weight: 1
weightFrom:
expressionFrom: '.metadata.annotations["pod-remove-finalizer.stage.kwok.x-k8s.io/weight"]'
delay:
durationMilliseconds: 1000
durationFrom:
Expand Down
4 changes: 4 additions & 0 deletions pkg/apis/internalversion/stage_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,10 @@ type StageSpec struct {
// Weight means when multiple stages share the same ResourceRef and Selector,
// a random stage will be matched as the next stage based on the weight.
Weight int
// WeightFrom means is the expression used to get the value.
// If it is a number type, convert to int.
// If it is a string type, the value get will be parsed by strconv.ParseInt.
WeightFrom *ExpressionFromSource
// Delay means there is a delay in this stage.
Delay *StageDelay
// Next indicates that this stage will be moved to.
Expand Down
2 changes: 2 additions & 0 deletions pkg/apis/internalversion/zz_generated.conversion.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 5 additions & 0 deletions pkg/apis/internalversion/zz_generated.deepcopy.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 4 additions & 0 deletions pkg/apis/v1alpha1/stage_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,10 @@ type StageSpec struct {
// +kubebuilder:default=0
// +kubebuilder:validation:Minimum=0
Weight int `json:"weight,omitempty"`
// WeightFrom means is the expression used to get the value.
// If it is a number type, convert to int.
// If it is a string type, the value get will be parsed by strconv.ParseInt.
WeightFrom *ExpressionFromSource `json:"weightFrom,omitempty"`
// Delay means there is a delay in this stage.
Delay *StageDelay `json:"delay,omitempty"`
// Next indicates that this stage will be moved to.
Expand Down
5 changes: 5 additions & 0 deletions pkg/apis/v1alpha1/zz_generated.deepcopy.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion pkg/kwok/controllers/node_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -284,7 +284,7 @@ func (c *NodeController) preprocess(ctx context.Context, node *corev1.Node) erro
}

lc := c.lifecycle.Get()
stage, err := lc.Match(node.Labels, node.Annotations, data)
stage, err := lc.Match(ctx, node.Labels, node.Annotations, data)
if err != nil {
return fmt.Errorf("stage match: %w", err)
}
Expand Down
2 changes: 1 addition & 1 deletion pkg/kwok/controllers/pod_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -219,7 +219,7 @@ func (c *PodController) preprocess(ctx context.Context, pod *corev1.Pod) error {
}

lc := c.lifecycle.Get()
stage, err := lc.Match(pod.Labels, pod.Annotations, data)
stage, err := lc.Match(ctx, pod.Labels, pod.Annotations, data)
if err != nil {
return fmt.Errorf("stage match: %w", err)
}
Expand Down
2 changes: 1 addition & 1 deletion pkg/kwok/controllers/stage_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -196,7 +196,7 @@ func (c *StageController) preprocess(ctx context.Context, resource *unstructured
}

lc := c.lifecycle.Get()
stage, err := lc.Match(resource.GetLabels(), resource.GetAnnotations(), data)
stage, err := lc.Match(ctx, resource.GetLabels(), resource.GetAnnotations(), data)
if err != nil {
return fmt.Errorf("stage match: %w", err)
}
Expand Down
6 changes: 3 additions & 3 deletions pkg/tools/stage/stage.go
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ func TestingStages(ctx context.Context, target any, stages []*internalversion.St
return nil, err
}

lcstages, err := lc.ListAllPossible(testTarget.GetLabels(), testTarget.GetAnnotations(), testTarget)
lcstages, err := lc.ListAllPossible(ctx, testTarget.GetLabels(), testTarget.GetAnnotations(), testTarget)
if err != nil {
return nil, err
}
Expand Down Expand Up @@ -94,8 +94,8 @@ func testingStage(ctx context.Context, testTarget Obj, stage *lifecycle.Stage) (
meta["delay"] = delay
}

weight := stage.Weight()
if weight != 0 {
weight, ok := stage.Weight(ctx, testTarget)
if ok {
meta["weight"] = weight
}

Expand Down
File renamed without changes.
File renamed without changes.
94 changes: 94 additions & 0 deletions pkg/utils/expression/value_int_from.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
/*
Copyright 2024 The Kubernetes Authors.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

package expression

import (
"context"
"strconv"
)

// IntGetter is a interface that can be used to get a int64 value.
type IntGetter interface {
// Get returns a int64 value.
Get(ctx context.Context, v interface{}) (int64, bool)
}

type int64From struct {
value *int64
query *Query
}

// NewIntFrom returns a new IntGetter.
func NewIntFrom(value *int64, src *string) (IntGetter, error) {
if value == nil && src == nil {
return int64Noop{}, nil
}
if src == nil {
return intWrapper(*value), nil
}
query, err := NewQuery(*src)
if err != nil {
return nil, err
}
return &int64From{
value: value,
query: query,
}, nil
}

func (d *int64From) Get(ctx context.Context, v interface{}) (int64, bool) {
out, err := d.query.Execute(ctx, v)
if err != nil {
return 0, false
}
if len(out) == 0 {
if d.value != nil {
return *d.value, true
}
return 0, false
}
switch t := out[0].(type) {
case string:
if t == "" {
return 0, false
}
n, err := strconv.ParseInt(t, 0, 0)
if err == nil {
return n, true
}
return 0, false
case float64: // TODO: don't use float
return int64(t), true
}
if d.value != nil {
return *d.value, true
}
return 0, false
}

type int64Noop struct {
}

func (int64Noop) Get(ctx context.Context, v interface{}) (int64, bool) {
return 0, false
}

type intWrapper int64

func (i intWrapper) Get(ctx context.Context, v interface{}) (int64, bool) {
return int64(i), true
}
Loading

0 comments on commit 56be4ee

Please sign in to comment.