From 1925f99118df5242728f2b2fc7bb56a7aa591d10 Mon Sep 17 00:00:00 2001 From: Carlos Sanchez Date: Tue, 19 Feb 2019 18:33:46 +0100 Subject: [PATCH 1/2] If generated VirtualService already exists update it Only if spec has changed --- pkg/controller/router.go | 68 +++++++++++++++++++++++----------------- 1 file changed, 39 insertions(+), 29 deletions(-) diff --git a/pkg/controller/router.go b/pkg/controller/router.go index 2c5e130f..9c8cd398 100644 --- a/pkg/controller/router.go +++ b/pkg/controller/router.go @@ -2,6 +2,7 @@ package controller import ( "fmt" + "reflect" istiov1alpha3 "github.com/knative/pkg/apis/istio/v1alpha3" istioclientset "github.com/knative/pkg/client/clientset/versioned" @@ -169,6 +170,35 @@ func (c *CanaryRouter) createVirtualService(cd *flaggerv1.Canary) error { hosts := append(cd.Spec.Service.Hosts, targetName) gateways := append(cd.Spec.Service.Gateways, "mesh") virtualService, err := c.istioClient.NetworkingV1alpha3().VirtualServices(cd.Namespace).Get(targetName, metav1.GetOptions{}) + newSpec := istiov1alpha3.VirtualServiceSpec{ + Hosts: hosts, + Gateways: gateways, + Http: []istiov1alpha3.HTTPRoute{ + { + Route: []istiov1alpha3.DestinationWeight{ + { + Destination: istiov1alpha3.Destination{ + Host: primaryName, + Port: istiov1alpha3.PortSelector{ + Number: uint32(cd.Spec.Service.Port), + }, + }, + Weight: 100, + }, + { + Destination: istiov1alpha3.Destination{ + Host: targetName, + Port: istiov1alpha3.PortSelector{ + Number: uint32(cd.Spec.Service.Port), + }, + }, + Weight: 0, + }, + }, + }, + }, + } + if errors.IsNotFound(err) { c.logger.Debugf("VirtualService %s.%s not found", targetName, cd.Namespace) virtualService = &istiov1alpha3.VirtualService{ @@ -183,42 +213,22 @@ func (c *CanaryRouter) createVirtualService(cd *flaggerv1.Canary) error { }), }, }, - Spec: istiov1alpha3.VirtualServiceSpec{ - Hosts: hosts, - Gateways: gateways, - Http: []istiov1alpha3.HTTPRoute{ - { - Route: []istiov1alpha3.DestinationWeight{ - { - Destination: istiov1alpha3.Destination{ - Host: primaryName, - Port: istiov1alpha3.PortSelector{ - Number: uint32(cd.Spec.Service.Port), - }, - }, - Weight: 100, - }, - { - Destination: istiov1alpha3.Destination{ - Host: targetName, - Port: istiov1alpha3.PortSelector{ - Number: uint32(cd.Spec.Service.Port), - }, - }, - Weight: 0, - }, - }, - }, - }, - }, + Spec: newSpec, } - c.logger.Debugf("Creating VirtualService %s.%s", virtualService.GetName(), cd.Namespace) _, err = c.istioClient.NetworkingV1alpha3().VirtualServices(cd.Namespace).Create(virtualService) if err != nil { return fmt.Errorf("VirtualService %s.%s create error %v", targetName, cd.Namespace, err) } c.logger.With("canary", fmt.Sprintf("%s.%s", cd.Name, cd.Namespace)).Infof("VirtualService %s.%s created", virtualService.GetName(), cd.Namespace) + } else if !reflect.DeepEqual(virtualService.Spec, newSpec) { + virtualService.Spec = newSpec + c.logger.Debugf("Updating VirtualService %s.%s", virtualService.GetName(), cd.Namespace) + _, err = c.istioClient.NetworkingV1alpha3().VirtualServices(cd.Namespace).Update(virtualService) + if err != nil { + return fmt.Errorf("VirtualService %s.%s update error %v", targetName, cd.Namespace, err) + } + c.logger.With("canary", fmt.Sprintf("%s.%s", cd.Name, cd.Namespace)).Infof("VirtualService %s.%s updated", virtualService.GetName(), cd.Namespace) } return nil From 0c0ebaecd5971498ed05f235e7fd40d8153338be Mon Sep 17 00:00:00 2001 From: Carlos Sanchez Date: Tue, 19 Feb 2019 19:54:38 +0100 Subject: [PATCH 2/2] Compare only hosts and gateways --- pkg/controller/router.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pkg/controller/router.go b/pkg/controller/router.go index 9c8cd398..9006065d 100644 --- a/pkg/controller/router.go +++ b/pkg/controller/router.go @@ -221,7 +221,7 @@ func (c *CanaryRouter) createVirtualService(cd *flaggerv1.Canary) error { return fmt.Errorf("VirtualService %s.%s create error %v", targetName, cd.Namespace, err) } c.logger.With("canary", fmt.Sprintf("%s.%s", cd.Name, cd.Namespace)).Infof("VirtualService %s.%s created", virtualService.GetName(), cd.Namespace) - } else if !reflect.DeepEqual(virtualService.Spec, newSpec) { + } else if !reflect.DeepEqual(virtualService.Spec.Hosts, newSpec.Hosts) || !reflect.DeepEqual(virtualService.Spec.Gateways, newSpec.Gateways) { virtualService.Spec = newSpec c.logger.Debugf("Updating VirtualService %s.%s", virtualService.GetName(), cd.Namespace) _, err = c.istioClient.NetworkingV1alpha3().VirtualServices(cd.Namespace).Update(virtualService)