Skip to content

Commit 724b86b

Browse files
authored
feat(source): fitler by gateway name (#5160)
* feat: filter by gateway name Signed-off-by: Steven Kreitzer <[email protected]> * address comments * address comments * address comments --------- Signed-off-by: Steven Kreitzer <[email protected]>
1 parent c72c499 commit 724b86b

File tree

7 files changed

+92
-0
lines changed

7 files changed

+92
-0
lines changed

Diff for: docs/flags.md

+1
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828
| `--[no-]ignore-hostname-annotation` | Ignore hostname annotation when generating DNS names, valid only when --fqdn-template is set (default: false) |
2929
| `--[no-]ignore-non-host-network-pods` | Ignore pods not running on host network when using pod source (default: true) |
3030
| `--[no-]ignore-ingress-tls-spec` | Ignore the spec.tls section in Ingress resources (default: false) |
31+
| `--gateway-name=GATEWAY-NAME` | Limit Gateways of Route endpoints to a specific name (default: all names) |
3132
| `--gateway-namespace=GATEWAY-NAMESPACE` | Limit Gateways of Route endpoints to a specific namespace (default: all namespaces) |
3233
| `--gateway-label-filter=GATEWAY-LABEL-FILTER` | Filter Gateways of Route endpoints via label selector (default: all gateways) |
3334
| `--compatibility=` | Process annotation semantics from legacy implementations (optional, options: mate, molecule, kops-dns-controller) |

Diff for: docs/sources/gateway-api.md

+2
Original file line numberDiff line numberDiff line change
@@ -100,6 +100,8 @@ spec:
100100
- --namespace=my-route-namespace
101101
# Optionally, limit Routes to those matching the given label selector.
102102
- --label-filter=my-route-label==my-route-value
103+
# Optionally, limit Route endpoints to those Gateways with the given name.
104+
- --gateway-name=my-gateway-name
103105
# Optionally, limit Route endpoints to those Gateways in the given namespace.
104106
- --gateway-namespace=my-gateway-namespace
105107
# Optionally, limit Route endpoints to those Gateways matching the given label selector.

Diff for: docs/sources/gateway.md

+33
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,39 @@ Matching Gateways are discovered by iterating over the \*Route's `status.parents
4343
- Ignores parents with a `parentRef.group` other than
4444
`gateway.networking.k8s.io` or a `parentRef.kind` other than `Gateway`.
4545

46+
- If the `--gateway-name` flag was specified, ignores parents with a `parentRef.name` other than the
47+
specified value.
48+
49+
For example, given the following HTTPRoute:
50+
51+
```yaml
52+
apiVersion: gateway.networking.k8s.io/v1
53+
kind: HTTPRoute
54+
metadata:
55+
name: echo
56+
spec:
57+
hostnames:
58+
- echoserver.example.org
59+
parentRefs:
60+
- group: networking.k8s.io
61+
kind: Gateway
62+
name: internal
63+
---
64+
apiVersion: gateway.networking.k8s.io/v1
65+
kind: HTTPRoute
66+
metadata:
67+
name: echo2
68+
spec:
69+
hostnames:
70+
- echoserver2.example.org
71+
parentRefs:
72+
- group: networking.k8s.io
73+
kind: Gateway
74+
name: external
75+
```
76+
77+
And using the `--gateway-name=external` flag, only the `echo2` HTTPRoute will be considered for DNS entries.
78+
4679
- If the `--gateway-namespace` flag was specified, ignores parents with a `parentRef.namespace` other
4780
than the specified value.
4881

Diff for: pkg/apis/externaldns/types.go

+3
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,7 @@ type Config struct {
6161
IgnoreIngressTLSSpec bool
6262
IgnoreIngressRulesSpec bool
6363
ListenEndpointEvents bool
64+
GatewayName string
6465
GatewayNamespace string
6566
GatewayLabelFilter string
6667
Compatibility string
@@ -230,6 +231,7 @@ var defaultConfig = &Config{
230231
IgnoreHostnameAnnotation: false,
231232
IgnoreIngressTLSSpec: false,
232233
IgnoreIngressRulesSpec: false,
234+
GatewayName: "",
233235
GatewayNamespace: "",
234236
GatewayLabelFilter: "",
235237
Compatibility: "",
@@ -453,6 +455,7 @@ func App(cfg *Config) *kingpin.Application {
453455
app.Flag("ignore-hostname-annotation", "Ignore hostname annotation when generating DNS names, valid only when --fqdn-template is set (default: false)").BoolVar(&cfg.IgnoreHostnameAnnotation)
454456
app.Flag("ignore-non-host-network-pods", "Ignore pods not running on host network when using pod source (default: true)").BoolVar(&cfg.IgnoreNonHostNetworkPods)
455457
app.Flag("ignore-ingress-tls-spec", "Ignore the spec.tls section in Ingress resources (default: false)").BoolVar(&cfg.IgnoreIngressTLSSpec)
458+
app.Flag("gateway-name", "Limit Gateways of Route endpoints to a specific name (default: all names)").StringVar(&cfg.GatewayName)
456459
app.Flag("gateway-namespace", "Limit Gateways of Route endpoints to a specific namespace (default: all namespaces)").StringVar(&cfg.GatewayNamespace)
457460
app.Flag("gateway-label-filter", "Filter Gateways of Route endpoints via label selector (default: all gateways)").StringVar(&cfg.GatewayLabelFilter)
458461
app.Flag("compatibility", "Process annotation semantics from legacy implementations (optional, options: mate, molecule, kops-dns-controller)").Default(defaultConfig.Compatibility).EnumVar(&cfg.Compatibility, "", "mate", "molecule", "kops-dns-controller")

Diff for: source/gateway.go

+7
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,7 @@ func newGatewayInformerFactory(client gateway.Interface, namespace string, label
8282
}
8383

8484
type gatewayRouteSource struct {
85+
gwName string
8586
gwNamespace string
8687
gwLabels labels.Selector
8788
gwInformer informers_v1beta1.GatewayInformer
@@ -161,6 +162,7 @@ func newGatewayRouteSource(clients ClientGenerator, config *Config, kind string,
161162
}
162163

163164
src := &gatewayRouteSource{
165+
gwName: config.GatewayName,
164166
gwNamespace: config.GatewayNamespace,
165167
gwLabels: gwLabels,
166168
gwInformer: gwInformer,
@@ -309,6 +311,11 @@ func (c *gatewayRouteResolver) resolve(rt gatewayRoute) (map[string]endpoint.Tar
309311
log.Debugf("Gateway %s/%s not found for %s %s/%s", namespace, ref.Name, c.src.rtKind, meta.Namespace, meta.Name)
310312
continue
311313
}
314+
// Confirm the Gateway has the correct name, if specified.
315+
if c.src.gwName != "" && c.src.gwName != gw.gateway.Name {
316+
log.Debugf("Gateway %s/%s does not match %s %s/%s", namespace, ref.Name, c.src.gwName, meta.Namespace, meta.Name)
317+
continue
318+
}
312319
// Confirm the Gateway has accepted the Route.
313320
if !gwRouteIsAccepted(rps.Conditions) {
314321
log.Debugf("Gateway %s/%s has not accepted %s %s/%s", namespace, ref.Name, c.src.rtKind, meta.Namespace, meta.Name)

Diff for: source/gateway_httproute_test.go

+45
Original file line numberDiff line numberDiff line change
@@ -142,6 +142,51 @@ func TestGatewayHTTPRouteSourceEndpoints(t *testing.T) {
142142
endpoints []*endpoint.Endpoint
143143
logExpectations []string
144144
}{
145+
{
146+
title: "GatewayName",
147+
config: Config{
148+
GatewayName: "gateway-name",
149+
},
150+
namespaces: namespaces("gateway-namespace", "route-namespace"),
151+
gateways: []*v1beta1.Gateway{
152+
{
153+
ObjectMeta: objectMeta("gateway-namespace", "gateway-name"),
154+
Spec: v1.GatewaySpec{
155+
Listeners: []v1.Listener{{
156+
Protocol: v1.HTTPProtocolType,
157+
AllowedRoutes: allowAllNamespaces,
158+
}},
159+
},
160+
Status: gatewayStatus("1.2.3.4"),
161+
},
162+
{
163+
ObjectMeta: objectMeta("gateway-namespace", "not-gateway-name"),
164+
Spec: v1.GatewaySpec{
165+
Listeners: []v1.Listener{{
166+
Protocol: v1.HTTPProtocolType,
167+
AllowedRoutes: allowAllNamespaces,
168+
}},
169+
},
170+
Status: gatewayStatus("2.3.4.5"),
171+
},
172+
},
173+
routes: []*v1beta1.HTTPRoute{{
174+
ObjectMeta: objectMeta("route-namespace", "test"),
175+
Spec: v1.HTTPRouteSpec{
176+
Hostnames: hostnames("test.example.internal"),
177+
},
178+
Status: httpRouteStatus( // The route is attached to both gateways.
179+
gwParentRef("gateway-namespace", "gateway-name"),
180+
gwParentRef("gateway-namespace", "not-gateway-name"),
181+
),
182+
}},
183+
endpoints: []*endpoint.Endpoint{
184+
newTestEndpoint("test.example.internal", "A", "1.2.3.4"),
185+
},
186+
logExpectations: []string{
187+
"level=debug msg=\"Gateway gateway-namespace/not-gateway-name does not match gateway-name route-namespace/test\"",
188+
},
189+
},
145190
{
146191
title: "GatewayNamespace",
147192
config: Config{

Diff for: source/store.go

+1
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,7 @@ type Config struct {
5454
IgnoreIngressTLSSpec bool
5555
IgnoreIngressRulesSpec bool
5656
ListenEndpointEvents bool
57+
GatewayName string
5758
GatewayNamespace string
5859
GatewayLabelFilter string
5960
Compatibility string

0 commit comments

Comments
 (0)