From 358a1e8277811884008a98f6192b92982e3040b2 Mon Sep 17 00:00:00 2001 From: Ubuntu Date: Sat, 22 Jul 2023 11:19:57 +0000 Subject: [PATCH] chore: leave the validation of setHeaderRoute to the plugin if plugins not empty. Signed-off-by: Liming Liu --- pkg/apis/rollouts/validation/validation.go | 2 +- .../rollouts/validation/validation_test.go | 37 +++++++++++++++++++ 2 files changed, 38 insertions(+), 1 deletion(-) diff --git a/pkg/apis/rollouts/validation/validation.go b/pkg/apis/rollouts/validation/validation.go index 34c4e3cbf9..94ba58f59b 100644 --- a/pkg/apis/rollouts/validation/validation.go +++ b/pkg/apis/rollouts/validation/validation.go @@ -305,7 +305,7 @@ func ValidateRolloutStrategyCanary(rollout *v1alpha1.Rollout, fldPath *field.Pat if step.SetHeaderRoute != nil { trafficRouting := rollout.Spec.Strategy.Canary.TrafficRouting - if trafficRouting == nil || (trafficRouting.Istio == nil && trafficRouting.ALB == nil && trafficRouting.Apisix == nil) { + if trafficRouting == nil || (trafficRouting.Istio == nil && trafficRouting.ALB == nil && trafficRouting.Apisix == nil && len(trafficRouting.Plugins) == 0) { allErrs = append(allErrs, field.Invalid(stepFldPath.Child("setHeaderRoute"), step.SetHeaderRoute, InvalidSetHeaderRouteTrafficPolicy)) } else if step.SetHeaderRoute.Match != nil && len(step.SetHeaderRoute.Match) > 0 { for j, match := range step.SetHeaderRoute.Match { diff --git a/pkg/apis/rollouts/validation/validation_test.go b/pkg/apis/rollouts/validation/validation_test.go index 9722d0d093..58a4571ae7 100644 --- a/pkg/apis/rollouts/validation/validation_test.go +++ b/pkg/apis/rollouts/validation/validation_test.go @@ -1,6 +1,7 @@ package validation import ( + "encoding/json" "fmt" "testing" @@ -305,6 +306,42 @@ func TestValidateRolloutStrategyCanarySetHeaderRoute(t *testing.T) { }) } +func TestValidateRolloutStrategyCanarySetHeaderRoutePlugins(t *testing.T) { + ro := &v1alpha1.Rollout{} + ro.Spec.Strategy.Canary = &v1alpha1.CanaryStrategy{ + CanaryService: "canary", + StableService: "stable", + } + + t.Run("using SetHeaderRoute step with plugins", func(t *testing.T) { + invalidRo := ro.DeepCopy() + routeName := "test" + invalidRo.Spec.Strategy.Canary.Steps = []v1alpha1.CanaryStep{{ + SetHeaderRoute: &v1alpha1.SetHeaderRoute{ + Name: routeName, + Match: []v1alpha1.HeaderRoutingMatch{ + { + HeaderName: "agent", + HeaderValue: &v1alpha1.StringMatch{Exact: "chrome"}, + }, + }, + }, + }} + invalidRo.Spec.Strategy.Canary.TrafficRouting = &v1alpha1.RolloutTrafficRouting{ + ManagedRoutes: []v1alpha1.MangedRoutes{ + { + Name: routeName, + }, + }, + Plugins: map[string]json.RawMessage{ + "anyplugin": []byte(`{"key": "value"}`), + }, + } + allErrs := ValidateRolloutStrategyCanary(invalidRo, field.NewPath("")) + assert.Equal(t, 0, len(allErrs)) + }) +} + func TestValidateRolloutStrategyCanarySetHeaderRouteIstio(t *testing.T) { ro := &v1alpha1.Rollout{} ro.Spec.Strategy.Canary = &v1alpha1.CanaryStrategy{