From 56eb581aa5fc569cabeacdb6c57649183aff0fb0 Mon Sep 17 00:00:00 2001 From: Fredrik Larsson Date: Thu, 2 Mar 2023 10:21:27 +0100 Subject: [PATCH 1/5] Allow annotation target override on gateway --- source/gateway.go | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/source/gateway.go b/source/gateway.go index 6ca9b0bfc..40956bd09 100644 --- a/source/gateway.go +++ b/source/gateway.go @@ -352,8 +352,12 @@ func (c *gatewayRouteResolver) resolve(rt gatewayRoute) (map[string]endpoint.Tar if !ok { continue } - for _, addr := range gw.gateway.Status.Addresses { - hostTargets[host] = append(hostTargets[host], addr.Value) + override := getTargetsFromTargetAnnotation(gw.gateway.Annotations) + hostTargets[host] = append(hostTargets[host], override...) + if override == nil { + for _, addr := range gw.gateway.Status.Addresses { + hostTargets[host] = append(hostTargets[host], addr.Value) + } } match = true } From 76f8fbddcd0e5b5c2942b50254ebc5b6954788b6 Mon Sep 17 00:00:00 2001 From: Fredrik Larsson Date: Sun, 5 Mar 2023 15:03:41 +0100 Subject: [PATCH 2/5] Add tests for annotation override (gateway sources) --- source/gateway_httproute_test.go | 122 +++++++++++++++++++++++++++++++ 1 file changed, 122 insertions(+) diff --git a/source/gateway_httproute_test.go b/source/gateway_httproute_test.go index 28926705d..942fec9b6 100644 --- a/source/gateway_httproute_test.go +++ b/source/gateway_httproute_test.go @@ -1033,6 +1033,128 @@ func TestGatewayHTTPRouteSourceEndpoints(t *testing.T) { }}, endpoints: nil, }, + { + title: "AnnotationOverride", + config: Config{ + GatewayNamespace: "gateway-namespace", + }, + namespaces: namespaces("gateway-namespace", "route-namespace"), + gateways: []*v1beta1.Gateway{ + { + ObjectMeta: metav1.ObjectMeta{ + Name: "overriden-gateway", + Namespace: "gateway-namespace", + Annotations: map[string]string{ + targetAnnotationKey: "4.3.2.1", + }, + }, + Spec: v1beta1.GatewaySpec{ + Listeners: []v1beta1.Listener{{ + Protocol: v1beta1.HTTPProtocolType, + AllowedRoutes: allowAllNamespaces, + }}, + }, + Status: gatewayStatus("1.2.3.4"), + }, + }, + routes: []*v1beta1.HTTPRoute{{ + ObjectMeta: objectMeta("route-namespace", "test"), + Spec: v1beta1.HTTPRouteSpec{ + Hostnames: hostnames("test.example.internal"), + }, + Status: httpRouteStatus( // The route is attached to both gateways. + gatewayParentRef("gateway-namespace", "overriden-gateway"), + ), + }}, + endpoints: []*endpoint.Endpoint{ + newTestEndpoint("test.example.internal", "A", "4.3.2.1"), + }, + }, + { + title: "AnnotationOverrideMultipleStatusAddresses", + config: Config{ + GatewayNamespace: "gateway-namespace", + }, + namespaces: namespaces("gateway-namespace", "route-namespace"), + gateways: []*v1beta1.Gateway{ + { + ObjectMeta: metav1.ObjectMeta{ + Name: "overriden-gateway", + Namespace: "gateway-namespace", + Annotations: map[string]string{ + targetAnnotationKey: "4.3.2.1", + }, + }, + Spec: v1beta1.GatewaySpec{ + Listeners: []v1beta1.Listener{{ + Protocol: v1beta1.HTTPProtocolType, + AllowedRoutes: allowAllNamespaces, + }}, + }, + Status: gatewayStatus("1.2.3.4", "2.3.4.5"), + }, + }, + routes: []*v1beta1.HTTPRoute{{ + ObjectMeta: objectMeta("route-namespace", "test"), + Spec: v1beta1.HTTPRouteSpec{ + Hostnames: hostnames("test.example.internal"), + }, + Status: httpRouteStatus( // The route is attached to both gateways. + gatewayParentRef("gateway-namespace", "overriden-gateway"), + ), + }}, + endpoints: []*endpoint.Endpoint{ + newTestEndpoint("test.example.internal", "A", "4.3.2.1"), + }, + }, + { + title: "MutlipleGatewaysOneAnnotationOverride", + config: Config{ + GatewayNamespace: "gateway-namespace", + }, + namespaces: namespaces("gateway-namespace", "route-namespace"), + gateways: []*v1beta1.Gateway{ + { + ObjectMeta: metav1.ObjectMeta{ + Name: "overriden-gateway", + Namespace: "gateway-namespace", + Annotations: map[string]string{ + targetAnnotationKey: "4.3.2.1", + }, + }, + Spec: v1beta1.GatewaySpec{ + Listeners: []v1beta1.Listener{{ + Protocol: v1beta1.HTTPProtocolType, + AllowedRoutes: allowAllNamespaces, + }}, + }, + Status: gatewayStatus("1.2.3.4"), + }, + { + ObjectMeta: objectMeta("gateway-namespace", "test"), + Spec: v1beta1.GatewaySpec{ + Listeners: []v1beta1.Listener{{ + Protocol: v1beta1.HTTPProtocolType, + AllowedRoutes: allowAllNamespaces, + }}, + }, + Status: gatewayStatus("2.3.4.5"), + }, + }, + routes: []*v1beta1.HTTPRoute{{ + ObjectMeta: objectMeta("route-namespace", "test"), + Spec: v1beta1.HTTPRouteSpec{ + Hostnames: hostnames("test.example.internal"), + }, + Status: httpRouteStatus( // The route is attached to both gateways. + gatewayParentRef("gateway-namespace", "overriden-gateway"), + gatewayParentRef("gateway-namespace", "test"), + ), + }}, + endpoints: []*endpoint.Endpoint{ + newTestEndpoint("test.example.internal", "A", "4.3.2.1", "2.3.4.5"), + }, + }, } for _, tt := range tests { tt := tt From bd568e9ba945684d901721273c43d5769bfe138a Mon Sep 17 00:00:00 2001 From: Fredrik Larsson Date: Wed, 5 Jul 2023 09:54:24 +0200 Subject: [PATCH 3/5] Add docs for annotation support on Gateway --- docs/annotations/annotations.md | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/docs/annotations/annotations.md b/docs/annotations/annotations.md index 79957c139..a880674f8 100644 --- a/docs/annotations/annotations.md +++ b/docs/annotations/annotations.md @@ -12,7 +12,7 @@ The following table documents which sources support which annotations: | CloudFoundry | | | | | | | | CRD | | | | | | | | F5 | | | | | Yes | | -| Gateway | Yes | Yes[^1] | | | Yes | Yes | +| Gateway | Yes | Yes[^1] | | Yes[^4] | Yes | Yes | | Gloo | | | | | Yes | Yes | | Ingress | Yes | Yes[^1] | | Yes | Yes | Yes | | Istio | Yes | Yes[^1] | | Yes | Yes | Yes | @@ -27,6 +27,7 @@ The following table documents which sources support which annotations: [^1]: Unless the `--ignore-hostname-annotation` flag is specified. [^2]: Only behaves differently than `hostname` for `Service`s of type `LoadBalancer`. [^3]: Also supported on `Pods` referenced from a headless `Service`'s `Endpoints`. +[^4]: The annotation should be on the `Gateway` ## external-dns.alpha.kubernetes.io/access From f8ae66efab4cf167f25caa70b51e56b2feb2cac8 Mon Sep 17 00:00:00 2001 From: Fredrik Larsson Date: Thu, 6 Jul 2023 11:35:35 +0200 Subject: [PATCH 4/5] Remove unnecessary test --- source/gateway_httproute_test.go | 37 -------------------------------- 1 file changed, 37 deletions(-) diff --git a/source/gateway_httproute_test.go b/source/gateway_httproute_test.go index 942fec9b6..f57969ec4 100644 --- a/source/gateway_httproute_test.go +++ b/source/gateway_httproute_test.go @@ -1070,43 +1070,6 @@ func TestGatewayHTTPRouteSourceEndpoints(t *testing.T) { newTestEndpoint("test.example.internal", "A", "4.3.2.1"), }, }, - { - title: "AnnotationOverrideMultipleStatusAddresses", - config: Config{ - GatewayNamespace: "gateway-namespace", - }, - namespaces: namespaces("gateway-namespace", "route-namespace"), - gateways: []*v1beta1.Gateway{ - { - ObjectMeta: metav1.ObjectMeta{ - Name: "overriden-gateway", - Namespace: "gateway-namespace", - Annotations: map[string]string{ - targetAnnotationKey: "4.3.2.1", - }, - }, - Spec: v1beta1.GatewaySpec{ - Listeners: []v1beta1.Listener{{ - Protocol: v1beta1.HTTPProtocolType, - AllowedRoutes: allowAllNamespaces, - }}, - }, - Status: gatewayStatus("1.2.3.4", "2.3.4.5"), - }, - }, - routes: []*v1beta1.HTTPRoute{{ - ObjectMeta: objectMeta("route-namespace", "test"), - Spec: v1beta1.HTTPRouteSpec{ - Hostnames: hostnames("test.example.internal"), - }, - Status: httpRouteStatus( // The route is attached to both gateways. - gatewayParentRef("gateway-namespace", "overriden-gateway"), - ), - }}, - endpoints: []*endpoint.Endpoint{ - newTestEndpoint("test.example.internal", "A", "4.3.2.1"), - }, - }, { title: "MutlipleGatewaysOneAnnotationOverride", config: Config{ From 712582caad992cd8d9999c92c69ae96ba51d1178 Mon Sep 17 00:00:00 2001 From: Fredrik Larsson Date: Thu, 6 Jul 2023 11:36:34 +0200 Subject: [PATCH 5/5] Check len == 0 instead of nil --- source/gateway.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/source/gateway.go b/source/gateway.go index 40956bd09..f48834f63 100644 --- a/source/gateway.go +++ b/source/gateway.go @@ -354,7 +354,7 @@ func (c *gatewayRouteResolver) resolve(rt gatewayRoute) (map[string]endpoint.Tar } override := getTargetsFromTargetAnnotation(gw.gateway.Annotations) hostTargets[host] = append(hostTargets[host], override...) - if override == nil { + if len(override) == 0 { for _, addr := range gw.gateway.Status.Addresses { hostTargets[host] = append(hostTargets[host], addr.Value) }