From 3c1c6ee6f8ff432d25f2b1d09e784c2c252dbf01 Mon Sep 17 00:00:00 2001 From: Kuromesi Date: Wed, 12 Jul 2023 17:59:51 +0800 Subject: [PATCH] custom trafficRouting Signed-off-by: Kuromesi --- pkg/trafficrouting/network/custom/custom.go | 5 +- .../network/custom/custom_test.go | 7 ++- .../trafficrouting_ingress/aliyun-alb.lua | 36 --------------- .../trafficrouting_ingress/higress.lua | 46 ------------------- .../trafficrouting_ingress/mse.lua | 45 ------------------ .../trafficrouting_ingress/nginx.lua | 46 ------------------- test/e2e/rollout_test.go | 4 ++ 7 files changed, 9 insertions(+), 180 deletions(-) delete mode 100644 pkg/trafficrouting/network/custom/lua_configuration/trafficrouting_ingress/aliyun-alb.lua delete mode 100644 pkg/trafficrouting/network/custom/lua_configuration/trafficrouting_ingress/higress.lua delete mode 100644 pkg/trafficrouting/network/custom/lua_configuration/trafficrouting_ingress/mse.lua delete mode 100644 pkg/trafficrouting/network/custom/lua_configuration/trafficrouting_ingress/nginx.lua diff --git a/pkg/trafficrouting/network/custom/custom.go b/pkg/trafficrouting/network/custom/custom.go index 3e56eb5..1f87309 100644 --- a/pkg/trafficrouting/network/custom/custom.go +++ b/pkg/trafficrouting/network/custom/custom.go @@ -25,7 +25,6 @@ import ( "k8s.io/apimachinery/pkg/api/errors" "k8s.io/klog/v2" - "github.com/openkruise/rollouts/api/v1alpha1" rolloutv1alpha1 "github.com/openkruise/rollouts/api/v1alpha1" "github.com/openkruise/rollouts/pkg/trafficrouting/network" "github.com/openkruise/rollouts/pkg/util" @@ -59,7 +58,7 @@ type Config struct { CanaryService string StableService string // network providers need to be created - TrafficConf []v1alpha1.NetworkRef + TrafficConf []rolloutv1alpha1.NetworkRef OwnerRef metav1.OwnerReference } @@ -238,7 +237,7 @@ func (r *customController) executeLuaForCanary(spec interface{}, strategy *rollo return nil, fmt.Errorf("expect table output from Lua script, not %s", returnValue.Type().String()) } -func (r *customController) getLuascript(ctx context.Context, ref v1alpha1.NetworkRef) string { +func (r *customController) getLuascript(ctx context.Context, ref rolloutv1alpha1.NetworkRef) string { // get local lua script // luaScript.Provider: CRDGroupt/Kind group := strings.Split(ref.APIVersion, "/")[0] diff --git a/pkg/trafficrouting/network/custom/custom_test.go b/pkg/trafficrouting/network/custom/custom_test.go index aa8375b..fd78633 100644 --- a/pkg/trafficrouting/network/custom/custom_test.go +++ b/pkg/trafficrouting/network/custom/custom_test.go @@ -23,7 +23,6 @@ import ( "reflect" "testing" - "github.com/openkruise/rollouts/api/v1alpha1" rolloutsv1alpha1 "github.com/openkruise/rollouts/api/v1alpha1" "github.com/openkruise/rollouts/pkg/util" "k8s.io/apimachinery/pkg/apis/meta/v1/unstructured" @@ -155,7 +154,7 @@ func TestInitialize(t *testing.T) { return Config{ StableService: "echoserver", CanaryService: "echoserver-canary", - TrafficConf: []v1alpha1.NetworkRef{ + TrafficConf: []rolloutsv1alpha1.NetworkRef{ { APIVersion: "networking.istio.io/v1alpha3", Kind: "VirtualService", @@ -346,7 +345,7 @@ func TestEnsureRoutes(t *testing.T) { RolloutName: "rollout-demo", StableService: "echoserver", CanaryService: "echoserver-canary", - TrafficConf: []v1alpha1.NetworkRef{ + TrafficConf: []rolloutsv1alpha1.NetworkRef{ { APIVersion: "networking.istio.io/v1alpha3", Kind: "VirtualService", @@ -404,7 +403,7 @@ func TestFinalise(t *testing.T) { return Config{ StableService: "echoserver", CanaryService: "echoserver-canary", - TrafficConf: []v1alpha1.NetworkRef{ + TrafficConf: []rolloutsv1alpha1.NetworkRef{ { APIVersion: "networking.istio.io/v1alpha3", Kind: "VirtualService", diff --git a/pkg/trafficrouting/network/custom/lua_configuration/trafficrouting_ingress/aliyun-alb.lua b/pkg/trafficrouting/network/custom/lua_configuration/trafficrouting_ingress/aliyun-alb.lua deleted file mode 100644 index 4ec4412..0000000 --- a/pkg/trafficrouting/network/custom/lua_configuration/trafficrouting_ingress/aliyun-alb.lua +++ /dev/null @@ -1,36 +0,0 @@ -annotations = {} -if ( obj.annotations ) -then - annotations = obj.annotations -end -annotations["alb.ingress.kubernetes.io/canary"] = "true" -annotations["alb.ingress.kubernetes.io/canary-by-cookie"] = nil -annotations["alb.ingress.kubernetes.io/canary-by-header"] = nil -annotations["alb.ingress.kubernetes.io/canary-by-header-pattern"] = nil -annotations["alb.ingress.kubernetes.io/canary-by-header-value"] = nil -annotations["alb.ingress.kubernetes.io/canary-weight"] = nil -annotations["alb.ingress.kubernetes.io/order"] = "1" -if ( obj.weight ~= "-1" ) -then - annotations["alb.ingress.kubernetes.io/canary-weight"] = obj.weight -end -if ( not obj.matches ) -then - return annotations -end -for _,match in ipairs(obj.matches) do - local header = match.headers[1] - if ( header.name == "canary-by-cookie" ) - then - annotations["alb.ingress.kubernetes.io/canary-by-cookie"] = header.value - else - annotations["alb.ingress.kubernetes.io/canary-by-header"] = header.name - if ( header.type == "RegularExpression" ) - then - annotations["alb.ingress.kubernetes.io/canary-by-header-pattern"] = header.value - else - annotations["alb.ingress.kubernetes.io/canary-by-header-value"] = header.value - end - end -end -return annotations diff --git a/pkg/trafficrouting/network/custom/lua_configuration/trafficrouting_ingress/higress.lua b/pkg/trafficrouting/network/custom/lua_configuration/trafficrouting_ingress/higress.lua deleted file mode 100644 index 7ce0210..0000000 --- a/pkg/trafficrouting/network/custom/lua_configuration/trafficrouting_ingress/higress.lua +++ /dev/null @@ -1,46 +0,0 @@ -annotations = {} --- obj.annotations is ingress annotations, it is recommended not to remove the part of the lua script, it must be kept -if ( obj.annotations ) -then - annotations = obj.annotations -end --- indicates the ingress is nginx canary api -annotations["nginx.ingress.kubernetes.io/canary"] = "true" --- First, set all canary api to nil -annotations["nginx.ingress.kubernetes.io/canary-by-cookie"] = nil -annotations["nginx.ingress.kubernetes.io/canary-by-header"] = nil -annotations["nginx.ingress.kubernetes.io/canary-by-header-pattern"] = nil -annotations["nginx.ingress.kubernetes.io/canary-by-header-value"] = nil -annotations["nginx.ingress.kubernetes.io/canary-weight"] = nil --- if rollout.spec.strategy.canary.steps.weight is nil, obj.weight will be -1, --- then we need remove the canary-weight annotation -if ( obj.weight ~= "-1" ) -then - annotations["nginx.ingress.kubernetes.io/canary-weight"] = obj.weight -end --- if don't contains headers, immediate return annotations -if ( not obj.matches ) -then - return annotations -end --- headers & cookie apis --- traverse matches -for _,match in ipairs(obj.matches) do - local header = match.headers[1] - -- cookie - if ( header.name == "canary-by-cookie" ) - then - annotations["nginx.ingress.kubernetes.io/canary-by-cookie"] = header.value - else - annotations["nginx.ingress.kubernetes.io/canary-by-header"] = header.name - -- if regular expression - if ( header.type == "RegularExpression" ) - then - annotations["nginx.ingress.kubernetes.io/canary-by-header-pattern"] = header.value - else - annotations["nginx.ingress.kubernetes.io/canary-by-header-value"] = header.value - end - end -end --- must be return annotations -return annotations \ No newline at end of file diff --git a/pkg/trafficrouting/network/custom/lua_configuration/trafficrouting_ingress/mse.lua b/pkg/trafficrouting/network/custom/lua_configuration/trafficrouting_ingress/mse.lua deleted file mode 100644 index ecaf3a0..0000000 --- a/pkg/trafficrouting/network/custom/lua_configuration/trafficrouting_ingress/mse.lua +++ /dev/null @@ -1,45 +0,0 @@ -function split(input, delimiter) - local arr = {} - string.gsub(input, '[^' .. delimiter ..']+', function(w) table.insert(arr, w) end) - return arr -end - -annotations = obj.annotations -annotations["nginx.ingress.kubernetes.io/canary"] = "true" -annotations["nginx.ingress.kubernetes.io/canary-by-cookie"] = nil -annotations["nginx.ingress.kubernetes.io/canary-by-header"] = nil -annotations["nginx.ingress.kubernetes.io/canary-by-header-pattern"] = nil -annotations["nginx.ingress.kubernetes.io/canary-by-header-value"] = nil -annotations["nginx.ingress.kubernetes.io/canary-weight"] = nil -if ( obj.weight ~= "-1" ) -then - annotations["nginx.ingress.kubernetes.io/canary-weight"] = obj.weight -end -if ( obj.requestHeaderModifier ) -then - local str = '' - for _,header in ipairs(obj.requestHeaderModifier.set) do - str = str..string.format("%s %s", header.name, header.value) - end - annotations["mse.ingress.kubernetes.io/request-header-control-update"] = str -end -if ( not obj.matches ) -then - return annotations -end -for _,match in ipairs(obj.matches) do - header = match.headers[1] - if ( header.name == "canary-by-cookie" ) - then - annotations["nginx.ingress.kubernetes.io/canary-by-cookie"] = header.value - else - annotations["nginx.ingress.kubernetes.io/canary-by-header"] = header.name - if ( header.type == "RegularExpression" ) - then - annotations["nginx.ingress.kubernetes.io/canary-by-header-pattern"] = header.value - else - annotations["nginx.ingress.kubernetes.io/canary-by-header-value"] = header.value - end - end -end -return annotations diff --git a/pkg/trafficrouting/network/custom/lua_configuration/trafficrouting_ingress/nginx.lua b/pkg/trafficrouting/network/custom/lua_configuration/trafficrouting_ingress/nginx.lua deleted file mode 100644 index 16ec4c6..0000000 --- a/pkg/trafficrouting/network/custom/lua_configuration/trafficrouting_ingress/nginx.lua +++ /dev/null @@ -1,46 +0,0 @@ -annotations = {} --- obj.annotations is ingress annotations, it is recommended not to remove the part of the lua script, it must be kept -if ( obj.annotations ) -then - annotations = obj.annotations -end --- indicates the ingress is nginx canary api -annotations["nginx.ingress.kubernetes.io/canary"] = "true" --- First, set all canary api to nil -annotations["nginx.ingress.kubernetes.io/canary-by-cookie"] = nil -annotations["nginx.ingress.kubernetes.io/canary-by-header"] = nil -annotations["nginx.ingress.kubernetes.io/canary-by-header-pattern"] = nil -annotations["nginx.ingress.kubernetes.io/canary-by-header-value"] = nil -annotations["nginx.ingress.kubernetes.io/canary-weight"] = nil --- if rollout.spec.strategy.canary.steps.weight is nil, obj.weight will be -1, --- then we need remove the canary-weight annotation -if ( obj.weight ~= "-1" ) -then - annotations["nginx.ingress.kubernetes.io/canary-weight"] = obj.weight -end --- if don't contains headers, immediate return annotations -if ( not obj.matches ) -then - return annotations -end --- headers & cookie apis --- traverse matches -for _,match in ipairs(obj.matches) do - local header = match.headers[1] - -- cookie - if ( header.name == "canary-by-cookie" ) - then - annotations["nginx.ingress.kubernetes.io/canary-by-cookie"] = header.value - else - annotations["nginx.ingress.kubernetes.io/canary-by-header"] = header.name - -- if regular expression - if ( header.type == "RegularExpression" ) - then - annotations["nginx.ingress.kubernetes.io/canary-by-header-pattern"] = header.value - else - annotations["nginx.ingress.kubernetes.io/canary-by-header-value"] = header.value - end - end -end --- must be return annotations -return annotations diff --git a/test/e2e/rollout_test.go b/test/e2e/rollout_test.go index 7581fd5..762a558 100644 --- a/test/e2e/rollout_test.go +++ b/test/e2e/rollout_test.go @@ -5551,6 +5551,10 @@ var _ = SIGDescribe("Rollout", func() { }) }) + + KruiseDescribe("Custom network provider tests", func() { + + }) }) func mergeEnvVar(original []v1.EnvVar, add v1.EnvVar) []v1.EnvVar {