Skip to content

Commit

Permalink
Router improvements
Browse files Browse the repository at this point in the history
- 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
  • Loading branch information
stefanprodan committed Feb 25, 2019
1 parent 6750f10 commit eabef3d
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 10 deletions.
20 changes: 12 additions & 8 deletions pkg/controller/router.go
Original file line number Diff line number Diff line change
Expand Up @@ -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),
},
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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
Expand All @@ -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},
},
}

Expand Down
4 changes: 2 additions & 2 deletions pkg/controller/router_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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())
}
Expand Down Expand Up @@ -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
}
}
Expand Down

0 comments on commit eabef3d

Please sign in to comment.