-
Notifications
You must be signed in to change notification settings - Fork 2.8k
feat(source): gateway api hostname source annotation #5959
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
base: master
Are you sure you want to change the base?
Changes from 2 commits
73df5d0
477f480
0379521
7825cf1
ebeb384
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -46,8 +46,10 @@ import ( | |
| ) | ||
|
|
||
| const ( | ||
| gatewayGroup = "gateway.networking.k8s.io" | ||
| gatewayKind = "Gateway" | ||
| gatewayGroup = "gateway.networking.k8s.io" | ||
| gatewayKind = "Gateway" | ||
| gatewayHostnameSourceAnnotationOnlyValue = "annotation-only" | ||
| gatewayHostnameSourceDefinedHostsOnlyValue = "defined-hosts-only" | ||
| ) | ||
|
|
||
| type gatewayRoute interface { | ||
|
|
@@ -401,13 +403,14 @@ func (c *gatewayRouteResolver) resolve(rt gatewayRoute) (map[string]endpoint.Tar | |
|
|
||
| func (c *gatewayRouteResolver) hosts(rt gatewayRoute) ([]string, error) { | ||
| var hostnames []string | ||
| var annotationHostnames []string | ||
| for _, name := range rt.Hostnames() { | ||
| hostnames = append(hostnames, string(name)) | ||
| } | ||
| // TODO: The ignore-hostname-annotation flag help says "valid only when using fqdn-template" | ||
| // but other sources don't check if fqdn-template is set. Which should it be? | ||
| if !c.src.ignoreHostnameAnnotation { | ||
| hostnames = append(hostnames, annotations.HostnamesFromAnnotations(rt.Metadata().Annotations)...) | ||
| annotationHostnames = append(annotationHostnames, annotations.HostnamesFromAnnotations(rt.Metadata().Annotations)...) | ||
| } | ||
| // TODO: The combine-fqdn-annotation flag is similarly vague. | ||
| if c.src.fqdnTemplate != nil && (len(hostnames) == 0 || c.src.combineFQDNAnnotation) { | ||
|
|
@@ -417,12 +420,23 @@ func (c *gatewayRouteResolver) hosts(rt gatewayRoute) ([]string, error) { | |
| } | ||
| hostnames = append(hostnames, hosts...) | ||
| } | ||
| // This means that the route doesn't specify a hostname and should use any provided by | ||
| // attached Gateway Listeners. This is only useful for {HTTP,TLS}Routes, but it doesn't | ||
| // break {TCP,UDP}Routes. | ||
| if len(rt.Hostnames()) == 0 { | ||
| hostnames = append(hostnames, "") | ||
|
|
||
| hostNameAnnotation, hostNameAnnotationExists := rt.Metadata().Annotations[annotations.GatewayHostnameSourceKey] | ||
| if !hostNameAnnotationExists { | ||
| // This means that the route doesn't specify a hostname and should use any provided by | ||
| // attached Gateway Listeners. This is only useful for {HTTP,TLS}Routes, but it doesn't | ||
| // break {TCP,UDP}Routes. | ||
| if len(rt.Hostnames()) == 0 { | ||
| hostnames = append(hostnames, "") | ||
| } | ||
|
|
||
| hostnames = append(hostnames, annotationHostnames...) | ||
| } | ||
|
|
||
| if strings.ToLower(hostNameAnnotation) == gatewayHostnameSourceAnnotationOnlyValue { | ||
| return annotationHostnames, nil | ||
| } | ||
|
|
||
| return hostnames, nil | ||
|
||
| } | ||
|
|
||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -1540,6 +1540,86 @@ func TestGatewayHTTPRouteSourceEndpoints(t *testing.T) { | |
| "Parent reference gateway-namespace/other-gateway not found in routeParentRefs for HTTPRoute route-namespace/test", | ||
| }, | ||
| }, | ||
| { | ||
| title: "SourceAnnotation", | ||
| 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: "route-test", | ||
| Namespace: "test", | ||
| Annotations: map[string]string{annotations.GatewayHostnameSourceKey: "defined-hosts-only", annotations.HostnameKey: "test.org.internal"}, | ||
| }, | ||
| 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", "1.2.3.4"), | ||
| }, | ||
| }, | ||
| { | ||
| title: "OnlyAnnotationHost", | ||
| 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: "route-test", | ||
| Namespace: "test", | ||
| Annotations: map[string]string{annotations.GatewayHostnameSourceKey: "annotation-only", annotations.HostnameKey: "test.org.internal"}, | ||
| }, | ||
| 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.org.internal", "A", "1.2.3.4"), | ||
| }, | ||
| }, | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Could you add a test case with an invalid annotation value?
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Added a test case with an invalid annotation value |
||
| } | ||
| for _, tt := range tests { | ||
| t.Run(tt.title, func(t *testing.T) { | ||
|
|
||
Uh oh!
There was an error while loading. Please reload this page.