From eabef3db30c5b5594c2d8298a69f4b6117bde55d Mon Sep 17 00:00:00 2001 From: stefanprodan Date: Mon, 25 Feb 2019 03:14:45 +0200 Subject: [PATCH] Router improvements - change virtual service route to canary service - keep the existing destination weights on virtual service updates - set the match conditions and URI rewrite when changing the traffic weight --- pkg/controller/router.go | 20 ++++++++++++-------- pkg/controller/router_test.go | 4 ++-- 2 files changed, 14 insertions(+), 10 deletions(-) diff --git a/pkg/controller/router.go b/pkg/controller/router.go index dd99873a1..869f2c071 100644 --- a/pkg/controller/router.go +++ b/pkg/controller/router.go @@ -180,7 +180,7 @@ func (c *CanaryRouter) syncVirtualService(cd *flaggerv1.Canary) error { }, { Destination: istiov1alpha3.Destination{ - Host: targetName, + Host: fmt.Sprintf("%s-canary", targetName), Port: istiov1alpha3.PortSelector{ Number: uint32(cd.Spec.Service.Port), }, @@ -230,13 +230,15 @@ func (c *CanaryRouter) syncVirtualService(cd *flaggerv1.Canary) error { return fmt.Errorf("VirtualService %s.%s query error %v", targetName, cd.Namespace, err) } - // update service + // update service but keep the original destination weights if virtualService != nil { if diff := cmp.Diff(newSpec, virtualService.Spec, cmpopts.IgnoreTypes(istiov1alpha3.DestinationWeight{})); diff != "" { + //fmt.Println(diff) vtClone := virtualService.DeepCopy() vtClone.Spec = newSpec - //TODO: keep original destination weights - //vtClone.Spec.Http = virtualService.Spec.Http + if len(virtualService.Spec.Http) > 0 { + vtClone.Spec.Http[0].Route = virtualService.Spec.Http[0].Route + } _, err = c.istioClient.NetworkingV1alpha3().VirtualServices(cd.Namespace).Update(vtClone) if err != nil { return fmt.Errorf("VirtualService %s.%s update error %v", targetName, cd.Namespace, err) @@ -272,15 +274,15 @@ func (c *CanaryRouter) GetRoutes(cd *flaggerv1.Canary) ( if route.Destination.Host == fmt.Sprintf("%s-primary", targetName) { primary = route } - if route.Destination.Host == targetName { + if route.Destination.Host == fmt.Sprintf("%s-canary", targetName) { canary = route } } } if primary.Weight == 0 && canary.Weight == 0 { - err = fmt.Errorf("VirtualService %s.%s does not contain routes for %s and %s", - targetName, cd.Namespace, fmt.Sprintf("%s-primary", targetName), targetName) + err = fmt.Errorf("VirtualService %s.%s does not contain routes for %s-primary and %s-canary", + targetName, cd.Namespace, targetName, targetName) } return @@ -305,7 +307,9 @@ func (c *CanaryRouter) SetRoutes( vsCopy := vs.DeepCopy() vsCopy.Spec.Http = []istiov1alpha3.HTTPRoute{ { - Route: []istiov1alpha3.DestinationWeight{primary, canary}, + Match: cd.Spec.Service.Match, + Rewrite: cd.Spec.Service.Rewrite, + Route: []istiov1alpha3.DestinationWeight{primary, canary}, }, } diff --git a/pkg/controller/router_test.go b/pkg/controller/router_test.go index 415279244..64059b945 100644 --- a/pkg/controller/router_test.go +++ b/pkg/controller/router_test.go @@ -15,7 +15,7 @@ func TestCanaryRouter_SyncClusterIPServices(t *testing.T) { t.Fatal(err.Error()) } - canarySvc, err := mocks.kubeClient.CoreV1().Services("default").Get("podinfo", metav1.GetOptions{}) + canarySvc, err := mocks.kubeClient.CoreV1().Services("default").Get("podinfo-canary", metav1.GetOptions{}) if err != nil { t.Fatal(err.Error()) } @@ -177,7 +177,7 @@ func TestCanaryRouter_SetRoutes(t *testing.T) { if route.Destination.Host == fmt.Sprintf("%s-primary", mocks.canary.Spec.TargetRef.Name) { pRoute = route } - if route.Destination.Host == mocks.canary.Spec.TargetRef.Name { + if route.Destination.Host == fmt.Sprintf("%s-canary", mocks.canary.Spec.TargetRef.Name) { cRoute = route } }