Skip to content

feat(gateway-api): add ability to override target on *Route resources #5319

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 9 additions & 2 deletions docs/sources/gateway.md
Original file line number Diff line number Diff line change
Expand Up @@ -106,10 +106,17 @@ Iterates over all listeners for the parent's `parentRef.sectionName`:

The targets of the DNS entries created from a \*Route are sourced from the following places:

1. If a matching parent Gateway has an `external-dns.alpha.kubernetes.io/target` annotation, uses
1. If the route has the route has the `external-dns.alpha.kubernetes.io/target` annotation
with a non-empty value, uses the value from that.

2. If the route has the route has the `external-dns.alpha.kubernetes.io/target: ""` it
will disable the `external-dns.alpha.kubernetes.io/target` on the matching parent
Gateway(s) and continue the regular flow from step 4.

3. If a matching parent Gateway has the `external-dns.alpha.kubernetes.io/target` annotation, uses
the values from that.

2. Otherwise, iterates over that parent Gateway's `status.addresses`,
4. Otherwise, iterates over that parent Gateway's `status.addresses`,
adding each address's `value`.

The targets from each parent Gateway matching the \*Route are then combined and de-duplicated.
Expand Down
10 changes: 9 additions & 1 deletion source/gateway.go
Original file line number Diff line number Diff line change
Expand Up @@ -373,7 +373,15 @@ func (c *gatewayRouteResolver) resolve(rt gatewayRoute) (map[string]endpoint.Tar
if !ok {
continue
}
override := getTargetsFromTargetAnnotation(gw.gateway.Annotations)
var override endpoint.Targets
targetAnnotation, exists := meta.Annotations[targetAnnotationKey]
if exists && targetAnnotation != "" {
override = getTargetsFromTargetAnnotation(meta.Annotations)
hostTargets[host] = append(hostTargets[host], override...)
} else if !exists {
override = getTargetsFromTargetAnnotation(gw.gateway.Annotations)
hostTargets[host] = append(hostTargets[host], override...)
}
hostTargets[host] = append(hostTargets[host], override...)
if len(override) == 0 {
for _, addr := range gw.gateway.Status.Addresses {
Expand Down
138 changes: 138 additions & 0 deletions source/gateway_httproute_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1295,6 +1295,144 @@ func TestGatewayHTTPRouteSourceEndpoints(t *testing.T) {
newTestEndpoint("test.example.internal", "A", "4.3.2.1"),
},
},
{
title: "RouteAnnotationOverride",
config: Config{
GatewayNamespace: "gateway-namespace",
},
namespaces: namespaces("gateway-namespace", "route-namespace"),
gateways: []*v1beta1.Gateway{
{
ObjectMeta: objectMeta("gateway-namespace", "test"),
Spec: v1.GatewaySpec{
Listeners: []v1.Listener{{
Protocol: v1.HTTPProtocolType,
AllowedRoutes: allowAllNamespaces,
}},
},
Status: gatewayStatus("1.2.3.4"),
},
},
routes: []*v1beta1.HTTPRoute{{
ObjectMeta: metav1.ObjectMeta{
Name: "test",
Namespace: "route-namespace",
Annotations: map[string]string{
targetAnnotationKey: "4.3.2.1",
},
},
Spec: v1.HTTPRouteSpec{
Hostnames: hostnames("test.example.internal"),
CommonRouteSpec: v1.CommonRouteSpec{
ParentRefs: []v1.ParentReference{
gwParentRef("gateway-namespace", "test"),
},
},
},
Status: httpRouteStatus(
gwParentRef("gateway-namespace", "test"),
),
}},
endpoints: []*endpoint.Endpoint{
newTestEndpoint("test.example.internal", "A", "4.3.2.1"),
},
},
{
title: "RouteAnnotationGatewayOverride",
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: v1.GatewaySpec{
Listeners: []v1.Listener{{
Protocol: v1.HTTPProtocolType,
AllowedRoutes: allowAllNamespaces,
}},
},
Status: gatewayStatus("1.2.3.4"),
},
},
routes: []*v1beta1.HTTPRoute{{
ObjectMeta: metav1.ObjectMeta{
Name: "test",
Namespace: "route-namespace",
Annotations: map[string]string{
targetAnnotationKey: "2.3.4.5",
},
},
Spec: v1.HTTPRouteSpec{
Hostnames: hostnames("test.example.internal"),
CommonRouteSpec: v1.CommonRouteSpec{
ParentRefs: []v1.ParentReference{
gwParentRef("gateway-namespace", "overriden-gateway"),
},
},
},
Status: httpRouteStatus(
gwParentRef("gateway-namespace", "overriden-gateway"),
),
}},
endpoints: []*endpoint.Endpoint{
newTestEndpoint("test.example.internal", "A", "2.3.4.5"),
},
},
{
title: "RouteAnnotationGatewayOverrideEmpty",
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: v1.GatewaySpec{
Listeners: []v1.Listener{{
Protocol: v1.HTTPProtocolType,
AllowedRoutes: allowAllNamespaces,
}},
},
Status: gatewayStatus("1.2.3.4"),
},
},
routes: []*v1beta1.HTTPRoute{{
ObjectMeta: metav1.ObjectMeta{
Name: "test",
Namespace: "route-namespace",
Annotations: map[string]string{
targetAnnotationKey: "",
},
},
Spec: v1.HTTPRouteSpec{
Hostnames: hostnames("test.example.internal"),
CommonRouteSpec: v1.CommonRouteSpec{
ParentRefs: []v1.ParentReference{
gwParentRef("gateway-namespace", "overriden-gateway"),
},
},
},
Status: httpRouteStatus(
gwParentRef("gateway-namespace", "overriden-gateway"),
),
}},
endpoints: []*endpoint.Endpoint{
newTestEndpoint("test.example.internal", "A", "1.2.3.4"),
},
},
{
title: "MutlipleGatewaysOneAnnotationOverride",
config: Config{
Expand Down
Loading