Skip to content

Commit

Permalink
Adjust CR for better code scalability
Browse files Browse the repository at this point in the history
Signed-off-by: Bryce-Huang <[email protected]>
  • Loading branch information
Bryce-huang committed Sep 21, 2023
1 parent 88b59b5 commit 7523476
Show file tree
Hide file tree
Showing 9 changed files with 398 additions and 280 deletions.
35 changes: 24 additions & 11 deletions apis/core/v1beta2/function_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -136,12 +136,18 @@ type FunctionSpec struct {
Serving *ServingImpl `json:"serving,omitempty"`
// Canary strategy for a function
// +optional
CanaryStrategy *CanaryStrategy `json:"canaryStrategy,omitempty"`
RolloutStrategy *RolloutStrategy `json:"rolloutStrategy,omitempty"`
}

// RolloutStrategy defines strategy to apply during next rollout
type RolloutStrategy struct {
// +optional
Canary *CanaryStrategy `json:"canary,omitempty"`
}
type CanaryStrategy struct {
// Canary release steps for a function
// +optional
CanarySteps []CanaryStep `json:"canarySteps,omitempty"`
Steps []CanaryStep `json:"steps,omitempty"`
}

type CanaryStep struct {
Expand Down Expand Up @@ -244,25 +250,32 @@ type GitSourceResult struct {

// FunctionStatus defines the observed state of Function
type FunctionStatus struct {
Route *RouteStatus `json:"route,omitempty"`
Build *Condition `json:"build,omitempty"`
Serving *Condition `json:"serving,omitempty"`
StableServing *Condition `json:"stableServing,omitempty"`
CanaryStatus *CanaryStatus `json:"canaryStatus,omitempty"`
Route *RouteStatus `json:"route,omitempty"`
Build *Condition `json:"build,omitempty"`
Serving *Condition `json:"serving,omitempty"`
// Addresses holds the addresses that used to access the Function.
// +optional
Addresses []FunctionAddress `json:"addresses,omitempty"`
Revision *Revision `json:"revision,omitempty"`
StableRevision *Revision `json:"stableRevision,omitempty"`
Addresses []FunctionAddress `json:"addresses,omitempty"`
Revision *Revision `json:"revision,omitempty"`
// Sources holds the results emitted from the step definition
// of different sources
//
// +optional
Sources []SourceResult `json:"sources,omitempty"`
Sources []SourceResult `json:"sources,omitempty"`
RolloutStatus *RolloutStatus `json:"rollout,omitempty"`
}
type RolloutStatus struct {
Canary *CanaryStatus `json:"canary,omitempty"`
}

// CanaryStatus status fields that only pertain to the canary release
type CanaryStatus struct {
Revision *Revision `json:"revision,omitempty"`
CanaryStepStatus *CanaryStepStatus `json:"status,omitempty"`
Serving *Condition `json:"serving,omitempty"`
}

type CanaryStepStatus struct {
CurrentStepIndex int32 `json:"currentStepIndex"`
CurrentStepState CanaryStepState `json:"currentStepState"`
Message string `json:"message,omitempty"`
Expand Down
16 changes: 9 additions & 7 deletions apis/core/v1beta2/function_webhook.go
Original file line number Diff line number Diff line change
Expand Up @@ -231,12 +231,14 @@ func (r *Function) Validate() error {
return field.Required(field.NewPath("spec", "serving"),
"must be specified when `spec.build` is not enabled")
}

if r.Spec.CanaryStrategy != nil && len(r.Spec.CanaryStrategy.CanarySteps) > 0 {
if err := r.ValidCanaryStrategy(field.NewPath("spec", "canaryStrategy")); err != nil {
return err
if r.Spec.RolloutStrategy != nil {
if r.Spec.RolloutStrategy.Canary != nil && len(r.Spec.RolloutStrategy.Canary.Steps) > 0 {
if err := r.ValidCanaryStrategy(field.NewPath("spec", "rolloutStrategy", "canary")); err != nil {
return err
}
}
}

return nil
}

Expand Down Expand Up @@ -293,14 +295,14 @@ func (r *Function) ValidateBuild() error {
return nil
}
func (r *Function) ValidCanaryStrategy(fldPath *field.Path) error {
steps := r.Spec.CanaryStrategy.CanarySteps
steps := r.Spec.RolloutStrategy.Canary.Steps
for i, step := range steps {
weight := step.Weight
if weight == nil {
return field.Invalid(fldPath.Index(i).Child("canarySteps").Child("weight"), steps, `Weight cannot be empty`)
return field.Invalid(fldPath.Index(i).Child("steps").Child("weight"), steps, `Weight cannot be empty`)
}
if *weight < 1 || *weight > 100 {
return field.Invalid(fldPath.Index(i).Child("canarySteps").Child("weight"), steps, `Weight cannot be less than 1 or greater than 100`)
return field.Invalid(fldPath.Index(i).Child("steps").Child("weight"), steps, `Weight cannot be less than 1 or greater than 100`)
}
if step.Pause.Duration != nil {
if *step.Pause.Duration < 1 {
Expand Down
68 changes: 36 additions & 32 deletions apis/core/v1beta2/function_webhook_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -713,25 +713,27 @@ func Test_Validate(t *testing.T) {
},
wantErr: true,
}, {
name: "function.spec.canarySteps",
name: "function.spec.rolloutStrategy.canary.steps",
r: Function{
Spec: FunctionSpec{
Image: "test",
Serving: &ServingImpl{
Triggers: &Triggers{Http: &HttpTrigger{Engine: (*Engine)(utilpointer.String(string(HttpEngineKnative)))}},
},
CanaryStrategy: &CanaryStrategy{
CanarySteps: []CanaryStep{
{
Weight: utilpointer.Int32(10),
Pause: Pause{
Duration: utilpointer.Int32(10000),
RolloutStrategy: &RolloutStrategy{
Canary: &CanaryStrategy{
Steps: []CanaryStep{
{
Weight: utilpointer.Int32(10),
Pause: Pause{
Duration: utilpointer.Int32(10000),
},
},
},
{
Weight: utilpointer.Int32(20),
Pause: Pause{
Duration: utilpointer.Int32(10000),
{
Weight: utilpointer.Int32(20),
Pause: Pause{
Duration: utilpointer.Int32(10000),
},
},
},
},
Expand All @@ -740,15 +742,15 @@ func Test_Validate(t *testing.T) {
},
wantErr: false,
}, {
name: "function.spec.canarySteps.weight",
name: "function.spec.rolloutStrategy.canary.steps.weight",
r: Function{
Spec: FunctionSpec{
Image: "test",
Serving: &ServingImpl{
Triggers: &Triggers{Http: &HttpTrigger{Engine: (*Engine)(utilpointer.String(string(HttpEngineKnative)))}},
},
CanaryStrategy: &CanaryStrategy{
CanarySteps: []CanaryStep{
RolloutStrategy: &RolloutStrategy{Canary: &CanaryStrategy{
Steps: []CanaryStep{
{
Weight: utilpointer.Int32(10),
Pause: Pause{
Expand All @@ -762,20 +764,20 @@ func Test_Validate(t *testing.T) {
},
},
},
},
}},
},
},
wantErr: false,
}, {
name: "function.spec.canarySteps.weight-error",
name: "function.spec.rolloutStrategy.canary.steps.weight-error",
r: Function{
Spec: FunctionSpec{
Image: "test",
Serving: &ServingImpl{
Triggers: &Triggers{Http: &HttpTrigger{Engine: (*Engine)(utilpointer.String(string(HttpEngineKnative)))}},
},
CanaryStrategy: &CanaryStrategy{
CanarySteps: []CanaryStep{
RolloutStrategy: &RolloutStrategy{Canary: &CanaryStrategy{
Steps: []CanaryStep{
{
Weight: utilpointer.Int32(20),
Pause: Pause{
Expand All @@ -789,30 +791,32 @@ func Test_Validate(t *testing.T) {
},
},
},
},
}},
},
},
wantErr: true,
}, {
name: "function.spec.canarySteps.pause",
name: "function.spec.rolloutStrategy.canary.steps.pause",
r: Function{
Spec: FunctionSpec{
Image: "test",
Serving: &ServingImpl{
Triggers: &Triggers{Http: &HttpTrigger{Engine: (*Engine)(utilpointer.String(string(HttpEngineKnative)))}},
},
CanaryStrategy: &CanaryStrategy{
CanarySteps: []CanaryStep{
{
Weight: utilpointer.Int32(10),
Pause: Pause{
Duration: utilpointer.Int32(10000),
RolloutStrategy: &RolloutStrategy{
Canary: &CanaryStrategy{
Steps: []CanaryStep{
{
Weight: utilpointer.Int32(10),
Pause: Pause{
Duration: utilpointer.Int32(10000),
},
},
},
{
Weight: utilpointer.Int32(20),
Pause: Pause{
Duration: utilpointer.Int32(10000),
{
Weight: utilpointer.Int32(20),
Pause: Pause{
Duration: utilpointer.Int32(10000),
},
},
},
},
Expand Down
106 changes: 83 additions & 23 deletions apis/core/v1beta2/zz_generated.deepcopy.go

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

Loading

0 comments on commit 7523476

Please sign in to comment.