From d5994ac127ee59213cc5d49879636d960fdbb47f Mon Sep 17 00:00:00 2001 From: Jonny Langefeld Date: Wed, 13 Oct 2021 18:12:32 -0700 Subject: [PATCH] Add metadata to istio VirtualService Some third party software relies on annotations and labels on istios VirtualServices. For instance external-dns makes use of the `external-dns.alpha.kubernetes.io/controller` annotation. Currently there is no way to set labels and annotations on the VirtualService resource. This change takes the metadata from the `canary.Spec.Service.Apex` property to replicate exactly what is already possible for a traefik resource: https://github.com/fluxcd/flagger/blob/c36a13ccffefbda1502bf02e8cac2f1b3ca9d027/pkg/router/traefik.go#L59-L68 Fix #854 Signed-off-by: Jonny Langefeld --- pkg/router/appmesh.go | 17 +++++++++++++++-- pkg/router/appmesh_v1beta2.go | 17 +++++++++++++++-- pkg/router/contour.go | 17 +++++++++++++++-- pkg/router/gloo.go | 17 +++++++++++++++-- pkg/router/istio.go | 17 +++++++++++++++-- 5 files changed, 75 insertions(+), 10 deletions(-) diff --git a/pkg/router/appmesh.go b/pkg/router/appmesh.go index 8f8300ea3..296aaf843 100644 --- a/pkg/router/appmesh.go +++ b/pkg/router/appmesh.go @@ -131,10 +131,23 @@ func (ar *AppMeshRouter) reconcileVirtualNode(canary *flaggerv1.Canary, name str // create virtual node if errors.IsNotFound(err) { + metadata := canary.Spec.Service.Apex + if metadata == nil { + metadata = &flaggerv1.CustomMetadata{} + } + if metadata.Labels == nil { + metadata.Labels = make(map[string]string) + } + if metadata.Annotations == nil { + metadata.Annotations = make(map[string]string) + } + virtualnode = &appmeshv1.VirtualNode{ ObjectMeta: metav1.ObjectMeta{ - Name: name, - Namespace: canary.Namespace, + Name: name, + Namespace: canary.Namespace, + Labels: metadata.Labels, + Annotations: filterMetadata(metadata.Annotations), OwnerReferences: []metav1.OwnerReference{ *metav1.NewControllerRef(canary, schema.GroupVersionKind{ Group: flaggerv1.SchemeGroupVersion.Group, diff --git a/pkg/router/appmesh_v1beta2.go b/pkg/router/appmesh_v1beta2.go index e2b91deca..61e4fb971 100644 --- a/pkg/router/appmesh_v1beta2.go +++ b/pkg/router/appmesh_v1beta2.go @@ -145,10 +145,23 @@ func (ar *AppMeshv1beta2Router) reconcileVirtualNode(canary *flaggerv1.Canary, n // create virtual node if errors.IsNotFound(err) { + metadata := canary.Spec.Service.Apex + if metadata == nil { + metadata = &flaggerv1.CustomMetadata{} + } + if metadata.Labels == nil { + metadata.Labels = make(map[string]string) + } + if metadata.Annotations == nil { + metadata.Annotations = make(map[string]string) + } + virtualnode = &appmeshv1.VirtualNode{ ObjectMeta: metav1.ObjectMeta{ - Name: name, - Namespace: canary.Namespace, + Name: name, + Namespace: canary.Namespace, + Labels: metadata.Labels, + Annotations: filterMetadata(metadata.Annotations), OwnerReferences: []metav1.OwnerReference{ *metav1.NewControllerRef(canary, schema.GroupVersionKind{ Group: flaggerv1.SchemeGroupVersion.Group, diff --git a/pkg/router/contour.go b/pkg/router/contour.go index 18ddabef0..ef34ce088 100644 --- a/pkg/router/contour.go +++ b/pkg/router/contour.go @@ -151,10 +151,23 @@ func (cr *ContourRouter) Reconcile(canary *flaggerv1.Canary) error { proxy, err := cr.contourClient.ProjectcontourV1().HTTPProxies(canary.Namespace).Get(context.TODO(), apexName, metav1.GetOptions{}) if errors.IsNotFound(err) { + metadata := canary.Spec.Service.Apex + if metadata == nil { + metadata = &flaggerv1.CustomMetadata{} + } + if metadata.Labels == nil { + metadata.Labels = make(map[string]string) + } + if metadata.Annotations == nil { + metadata.Annotations = make(map[string]string) + } + proxy = &contourv1.HTTPProxy{ ObjectMeta: metav1.ObjectMeta{ - Name: apexName, - Namespace: canary.Namespace, + Name: apexName, + Namespace: canary.Namespace, + Labels: metadata.Labels, + Annotations: filterMetadata(metadata.Annotations), OwnerReferences: []metav1.OwnerReference{ *metav1.NewControllerRef(canary, schema.GroupVersionKind{ Group: flaggerv1.SchemeGroupVersion.Group, diff --git a/pkg/router/gloo.go b/pkg/router/gloo.go index 823a16b50..214d8addd 100644 --- a/pkg/router/gloo.go +++ b/pkg/router/gloo.go @@ -99,10 +99,23 @@ func (gr *GlooRouter) Reconcile(canary *flaggerv1.Canary) error { routeTable, err := gr.glooClient.GatewayV1().RouteTables(canary.Namespace).Get(context.TODO(), apexName, metav1.GetOptions{}) if errors.IsNotFound(err) { + metadata := canary.Spec.Service.Apex + if metadata == nil { + metadata = &flaggerv1.CustomMetadata{} + } + if metadata.Labels == nil { + metadata.Labels = make(map[string]string) + } + if metadata.Annotations == nil { + metadata.Annotations = make(map[string]string) + } + routeTable = &gatewayv1.RouteTable{ ObjectMeta: metav1.ObjectMeta{ - Name: apexName, - Namespace: canary.Namespace, + Name: apexName, + Namespace: canary.Namespace, + Labels: metadata.Labels, + Annotations: filterMetadata(metadata.Annotations), OwnerReferences: []metav1.OwnerReference{ *metav1.NewControllerRef(canary, schema.GroupVersionKind{ Group: flaggerv1.SchemeGroupVersion.Group, diff --git a/pkg/router/istio.go b/pkg/router/istio.go index dd5ae7d67..d6f7ce799 100644 --- a/pkg/router/istio.go +++ b/pkg/router/istio.go @@ -206,10 +206,23 @@ func (ir *IstioRouter) reconcileVirtualService(canary *flaggerv1.Canary) error { virtualService, err := ir.istioClient.NetworkingV1alpha3().VirtualServices(canary.Namespace).Get(context.TODO(), apexName, metav1.GetOptions{}) // insert if errors.IsNotFound(err) { + metadata := canary.Spec.Service.Apex + if metadata == nil { + metadata = &flaggerv1.CustomMetadata{} + } + if metadata.Labels == nil { + metadata.Labels = make(map[string]string) + } + if metadata.Annotations == nil { + metadata.Annotations = make(map[string]string) + } + virtualService = &istiov1alpha3.VirtualService{ ObjectMeta: metav1.ObjectMeta{ - Name: apexName, - Namespace: canary.Namespace, + Name: apexName, + Namespace: canary.Namespace, + Labels: metadata.Labels, + Annotations: filterMetadata(metadata.Annotations), OwnerReferences: []metav1.OwnerReference{ *metav1.NewControllerRef(canary, schema.GroupVersionKind{ Group: flaggerv1.SchemeGroupVersion.Group,