From 7d4c3c581420824cb30a9fd952ec4ee4a91f941f Mon Sep 17 00:00:00 2001 From: stefanprodan Date: Mon, 14 Oct 2019 20:27:48 +0300 Subject: [PATCH] Implement App Mesh HTTP retry policy --- pkg/router/appmesh.go | 34 +++++++++++++++++++++++++++++++++- 1 file changed, 33 insertions(+), 1 deletion(-) diff --git a/pkg/router/appmesh.go b/pkg/router/appmesh.go index 67e86771..f087f242 100644 --- a/pkg/router/appmesh.go +++ b/pkg/router/appmesh.go @@ -3,6 +3,7 @@ package router import ( "fmt" "strings" + "time" "github.com/google/go-cmp/cmp" "github.com/google/go-cmp/cmp/cmpopts" @@ -181,7 +182,7 @@ func (ar *AppMeshRouter) reconcileVirtualService(canary *flaggerv1.Canary, name MeshName: canary.Spec.Service.MeshName, VirtualRouter: &appmeshv1.VirtualRouter{ Name: routerName, - Listeners: []appmeshv1.Listener{ + Listeners: []appmeshv1.VirtualRouterListener{ { PortMapping: appmeshv1.PortMapping{ Port: int64(canary.Spec.Service.Port), @@ -214,6 +215,33 @@ func (ar *AppMeshRouter) reconcileVirtualService(canary *flaggerv1.Canary, name }, } + // add retry policy (default: one retry on gateway error with a 250ms timeout) + if canary.Spec.Service.Retries != nil { + timeout := int64(250) + if d, err := time.ParseDuration(canary.Spec.Service.Retries.PerTryTimeout); err == nil { + timeout = d.Milliseconds() + } + + attempts := int64(1) + if canary.Spec.Service.Retries.Attempts > 0 { + attempts = int64(canary.Spec.Service.Retries.Attempts) + } + + retryPolicy := &appmeshv1.HttpRetryPolicy{ + PerRetryTimeoutMillis: int64p(timeout), + MaxRetries: int64p(attempts), + } + + events := []string{"gateway-error"} + if len(canary.Spec.Service.Retries.RetryOn) > 0 { + events = strings.Split(canary.Spec.Service.Retries.RetryOn, ",") + } + for _, value := range events { + retryPolicy.HttpRetryPolicyEvents = append(retryPolicy.HttpRetryPolicyEvents, appmeshv1.HttpRetryPolicyEvent(value)) + } + vsSpec.Routes[0].Http.RetryPolicy = retryPolicy + } + virtualService, err := ar.appmeshClient.AppmeshV1beta1().VirtualServices(canary.Namespace).Get(name, metav1.GetOptions{}) // create virtual service @@ -353,3 +381,7 @@ func getProtocol(canary *flaggerv1.Canary) string { } return "http" } + +func int64p(i int64) *int64 { + return &i +}