generated from kubernetes/kubernetes-template-project
-
Notifications
You must be signed in to change notification settings - Fork 621
[conformance] Gateway Dynamic Listener Ports #1859
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
Closed
dprotaso
wants to merge
15
commits into
kubernetes-sigs:main
from
dprotaso:dynamic-multiple-listeners
Closed
Changes from all commits
Commits
Show all changes
15 commits
Select commit
Hold shift + click to select a range
dd08f3d
Gateway Dynamic Listener Ports
dprotaso ceb8dfd
ignore security warnings when picking random ports
dprotaso d0e5ceb
allow configuration of ephemeral ports
dprotaso caedb88
fix boilerplate
dprotaso cc4f484
add godoc
dprotaso 18d038f
add SupportsGateway
dprotaso 6e103de
add some validation to the test input
dprotaso 6a9a433
assert listener resolved refs condition
dprotaso f1bb3c1
fix linter and gofmt
dprotaso f64cae3
change feature name and tweak conditions to check for Programmed=True
dprotaso a6e3183
Use GetTimeout duration when fetching and patching the Gateway
dprotaso 225fc80
don't override global timeout when making HTTP requests
dprotaso c1e0249
test dialing to a listener port stops working when listeners are removed
dprotaso 9934124
fix govet warnings
dprotaso 1dd084a
tweak test structure
dprotaso File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,260 @@ | ||
| /* | ||
| Copyright 2023 The Kubernetes Authors. | ||
|
|
||
| Licensed under the Apache License, Version 2.0 (the "License"); | ||
| you may not use this file except in compliance with the License. | ||
| You may obtain a copy of the License at | ||
|
|
||
| http://www.apache.org/licenses/LICENSE-2.0 | ||
|
|
||
| Unless required by applicable law or agreed to in writing, software | ||
| distributed under the License is distributed on an "AS IS" BASIS, | ||
| WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| See the License for the specific language governing permissions and | ||
| limitations under the License. | ||
| */ | ||
|
|
||
| package tests | ||
|
|
||
| import ( | ||
| "context" | ||
| "fmt" | ||
| "math/rand" | ||
| "net" | ||
| "strconv" | ||
| "strings" | ||
| "testing" | ||
| "time" | ||
|
|
||
| "github.com/stretchr/testify/require" | ||
| metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" | ||
| "k8s.io/apimachinery/pkg/types" | ||
| "k8s.io/apimachinery/pkg/util/sets" | ||
| "sigs.k8s.io/controller-runtime/pkg/client" | ||
|
|
||
| "sigs.k8s.io/gateway-api/apis/v1beta1" | ||
| "sigs.k8s.io/gateway-api/conformance/utils/http" | ||
| "sigs.k8s.io/gateway-api/conformance/utils/kubernetes" | ||
| "sigs.k8s.io/gateway-api/conformance/utils/suite" | ||
| "sigs.k8s.io/gateway-api/conformance/utils/tls" | ||
| ) | ||
|
|
||
| func init() { | ||
| ConformanceTests = append(ConformanceTests, GatewayListenerHTTPRouteDynamicPorts) | ||
| } | ||
|
|
||
| var GatewayListenerHTTPRouteDynamicPorts = suite.ConformanceTest{ | ||
| ShortName: "GatewayListenerHTTPRouteDynamicPorts", | ||
| Features: []suite.SupportedFeature{ | ||
| suite.SupportGateway, | ||
| suite.SupportHTTPRoute, | ||
| suite.SupportGatewayListenerHTTPRouteDynamicPorts, | ||
| }, | ||
| Description: "A Gateway and an HTTPRoute in the gateway-conformance-infra namespace should support adding and removing listeners with arbitrary ports", | ||
| Manifests: []string{"tests/gateway-dynamic-listeners.yaml"}, | ||
| Test: func(t *testing.T, s *suite.ConformanceTestSuite) { | ||
| // Ephemeral port range | ||
| const ( | ||
| portCount = 10 | ||
| tlsPortCount = 5 | ||
| ) | ||
| var ( | ||
| portStart = s.ListenerConfig.DynamicPortRange.Start | ||
| portEnd = s.ListenerConfig.DynamicPortRange.End | ||
|
|
||
| gwNN = types.NamespacedName{Name: "gateway-dynamic-listener", Namespace: "gateway-conformance-infra"} | ||
| namespaces = []string{"gateway-conformance-infra"} | ||
| certNN = types.NamespacedName{Name: "tls-wildcard-hostname", Namespace: gwNN.Namespace} | ||
| ports = sets.New[int]() | ||
| listeners = make([]v1beta1.Listener, 0, portCount) | ||
| same = v1beta1.NamespacesFromSame | ||
|
|
||
| expectedConditions = []metav1.Condition{{ | ||
| Type: string(v1beta1.ListenerConditionAccepted), | ||
| Status: metav1.ConditionTrue, | ||
| Reason: "", // any reason | ||
| }, { | ||
| Type: string(v1beta1.ListenerConditionProgrammed), | ||
| Status: metav1.ConditionTrue, | ||
| Reason: "", // any reason | ||
| }} | ||
|
|
||
| expectedListeners = []v1beta1.ListenerStatus{{ | ||
| Name: "http", | ||
| SupportedKinds: []v1beta1.RouteGroupKind{{ | ||
| Group: (*v1beta1.Group)(&v1beta1.GroupVersion.Group), | ||
| Kind: v1beta1.Kind("HTTPRoute"), | ||
| }}, | ||
| Conditions: expectedConditions, | ||
| AttachedRoutes: 1, | ||
| }} | ||
| ) | ||
| if portEnd < portStart { | ||
| t.Fatal("DynamicPortRange.Start must be less than DynamicPortRange.End") | ||
| } | ||
|
|
||
| if portEnd-portStart < portCount { | ||
| t.Fatal("DynamicPortRange input requires at least 10 ports") | ||
| } | ||
|
|
||
| for i := 0; i < portCount; i++ { | ||
| port := nextPort(portStart, portEnd, ports) | ||
|
|
||
| listeners = append(listeners, v1beta1.Listener{ | ||
| Name: v1beta1.SectionName(strconv.Itoa(port)), | ||
| Port: v1beta1.PortNumber(port), | ||
| Protocol: v1beta1.HTTPProtocolType, | ||
| AllowedRoutes: &v1beta1.AllowedRoutes{ | ||
| Namespaces: &v1beta1.RouteNamespaces{From: &same}, | ||
| }, | ||
| }) | ||
|
|
||
| expectedListeners = append(expectedListeners, v1beta1.ListenerStatus{ | ||
| Name: v1beta1.SectionName(strconv.Itoa(port)), | ||
| SupportedKinds: []v1beta1.RouteGroupKind{{ | ||
| Group: (*v1beta1.Group)(&v1beta1.GroupVersion.Group), | ||
| Kind: v1beta1.Kind("HTTPRoute"), | ||
| }}, | ||
| Conditions: expectedConditions, | ||
| AttachedRoutes: 1, | ||
| }) | ||
| } | ||
|
|
||
| for i := 0; i < tlsPortCount; i++ { | ||
| port := nextPort(portStart, portEnd, ports) | ||
| hostname := v1beta1.Hostname(fmt.Sprintf("%v.example.com", port)) | ||
|
|
||
| listeners = append(listeners, v1beta1.Listener{ | ||
| Name: v1beta1.SectionName(strconv.Itoa(port)), | ||
| Port: v1beta1.PortNumber(port), | ||
| Hostname: &hostname, | ||
| Protocol: v1beta1.HTTPSProtocolType, | ||
| AllowedRoutes: &v1beta1.AllowedRoutes{ | ||
| Namespaces: &v1beta1.RouteNamespaces{From: &same}, | ||
| }, | ||
| TLS: &v1beta1.GatewayTLSConfig{ | ||
| CertificateRefs: []v1beta1.SecretObjectReference{{ | ||
| Name: v1beta1.ObjectName(certNN.Name), | ||
| }}, | ||
| }, | ||
| }) | ||
|
|
||
| expectedListeners = append(expectedListeners, v1beta1.ListenerStatus{ | ||
| Name: v1beta1.SectionName(strconv.Itoa(port)), | ||
| SupportedKinds: []v1beta1.RouteGroupKind{{ | ||
| Group: (*v1beta1.Group)(&v1beta1.GroupVersion.Group), | ||
| Kind: v1beta1.Kind("HTTPRoute"), | ||
dprotaso marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| }}, | ||
| Conditions: append([]metav1.Condition{{ | ||
| Type: string(v1beta1.ListenerConditionResolvedRefs), | ||
| Status: metav1.ConditionTrue, | ||
| Reason: "", // any reason | ||
| }}, expectedConditions...), | ||
| AttachedRoutes: 1, | ||
| }) | ||
| } | ||
|
|
||
| gwAddr, err := kubernetes.WaitForGatewayAddress(t, s.Client, s.TimeoutConfig, gwNN) | ||
| require.NoErrorf(t, err, "timed out waiting for Gateway address to be assigned") | ||
| host, _, err := net.SplitHostPort(gwAddr) | ||
| require.NoErrorf(t, err, "unable to split gateway address %q", gwAddr) | ||
|
|
||
| kubernetes.NamespacesMustBeReady(t, s.Client, s.TimeoutConfig, namespaces) | ||
| certBytes, keyBytes, err := GetTLSSecret(s.Client, certNN) | ||
| require.NoErrorf(t, err, "error getting certificate: %v", err) | ||
|
|
||
| sendRequestToEachListener := func(t *testing.T, expectedResponse http.ExpectedResponse, listeners []v1beta1.Listener) { | ||
| for _, listener := range listeners { | ||
| addr := net.JoinHostPort(host, strconv.Itoa(int(listener.Port))) | ||
|
|
||
| if listener.TLS != nil { | ||
| listenerHost := string(*listener.Hostname) | ||
| expectedResponse.Request.Host = listenerHost | ||
| tls.MakeTLSRequestAndExpectEventuallyConsistentResponse(t, s.RoundTripper, s.TimeoutConfig, addr, certBytes, keyBytes, listenerHost, expectedResponse) | ||
| } else { | ||
| http.MakeRequestAndExpectEventuallyConsistentResponse(t, s.RoundTripper, s.TimeoutConfig, addr, expectedResponse) | ||
| } | ||
| } | ||
| } | ||
|
|
||
| original := &v1beta1.Gateway{} | ||
|
|
||
| t.Log("should be able to add multiple HTTP listeners with dynamic ports that then become available for routing traffic") | ||
| ctx, cancel := context.WithTimeout(context.Background(), s.TimeoutConfig.GetTimeout) | ||
| defer cancel() | ||
|
|
||
| err = s.Client.Get(ctx, gwNN, original) | ||
| require.NoErrorf(t, err, "error getting Gateway: %v", err) | ||
|
|
||
| // verify that the implementation is tracking the most recent resource changes | ||
| kubernetes.GatewayMustHaveLatestConditions(t, s.TimeoutConfig, original) | ||
| mutate := original.DeepCopy() | ||
| mutate.Spec.Listeners = append(mutate.Spec.Listeners, listeners...) | ||
|
|
||
| err = s.Client.Patch(ctx, mutate, client.MergeFrom(original)) | ||
| require.NoErrorf(t, err, "error patching the Gateway: %v", err) | ||
| kubernetes.GatewayStatusMustHaveListeners(t, s.Client, s.TimeoutConfig, gwNN, expectedListeners) | ||
|
|
||
| successResponse := http.ExpectedResponse{ | ||
| Namespace: gwNN.Namespace, | ||
| Request: http.Request{Path: "/"}, | ||
| Response: http.Response{StatusCode: 200}, | ||
| } | ||
|
|
||
| sendRequestToEachListener(t, successResponse, mutate.Spec.Listeners) | ||
|
|
||
| t.Log("should be able to remove multiple HTTP listeners with dynamic ports") | ||
| ctx, cancel = context.WithTimeout(context.Background(), s.TimeoutConfig.GetTimeout) | ||
| defer cancel() | ||
|
|
||
| err = s.Client.Get(ctx, gwNN, mutate) | ||
| require.NoErrorf(t, err, "error getting Gateway: %v", err) | ||
|
|
||
| mutate.Spec.Listeners = original.Spec.Listeners | ||
| err = s.Client.Update(ctx, mutate) | ||
| require.NoErrorf(t, err, "error patching the Gateway: %v", err) | ||
|
|
||
| expectedListeners = []v1beta1.ListenerStatus{{ | ||
| Name: "http", | ||
| SupportedKinds: []v1beta1.RouteGroupKind{{ | ||
| Group: (*v1beta1.Group)(&v1beta1.GroupVersion.Group), | ||
| Kind: v1beta1.Kind("HTTPRoute"), | ||
| }}, | ||
| Conditions: expectedConditions, | ||
| AttachedRoutes: 1, | ||
| }} | ||
|
|
||
| kubernetes.GatewayStatusMustHaveListeners(t, s.Client, s.TimeoutConfig, gwNN, expectedListeners) | ||
|
|
||
| // Original listener should work | ||
| sendRequestToEachListener(t, successResponse, original.Spec.Listeners) | ||
|
|
||
| for _, listener := range listeners { | ||
| addr := net.JoinHostPort(host, strconv.Itoa(int(listener.Port))) | ||
|
|
||
| // Listeners that were removed should stop working | ||
| dial := func(elapsed time.Duration) bool { | ||
| conn, err := net.DialTimeout("tcp", addr, time.Second) | ||
| if conn != nil { | ||
| conn.Close() | ||
| return false | ||
| } | ||
| if err != nil && strings.Contains(err.Error(), "connection refused") { | ||
| return true | ||
| } | ||
| return false | ||
| } | ||
| http.AwaitConvergence(t, s.TimeoutConfig.RequiredConsecutiveSuccesses, s.TimeoutConfig.MaxTimeToConsistency, dial) | ||
| } | ||
| }, | ||
| } | ||
|
|
||
| func nextPort(start, end int, ports sets.Set[int]) int { | ||
| port := start + rand.Intn(end-start) //nolint:gosec | ||
dprotaso marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| // We want a unique port | ||
| for ports.Has(port) { | ||
| port = start + rand.Intn(end-start) //nolint:gosec | ||
| } | ||
| ports.Insert(port) | ||
| return port | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,29 @@ | ||
| apiVersion: gateway.networking.k8s.io/v1beta1 | ||
| kind: Gateway | ||
| metadata: | ||
| name: gateway-dynamic-listener | ||
| namespace: gateway-conformance-infra | ||
| spec: | ||
| gatewayClassName: "{GATEWAY_CLASS_NAME}" | ||
| listeners: | ||
| - name: http | ||
| port: 80 | ||
| protocol: HTTP | ||
| allowedRoutes: | ||
| namespaces: | ||
| from: Same | ||
| --- | ||
| apiVersion: gateway.networking.k8s.io/v1beta1 | ||
| kind: HTTPRoute | ||
| metadata: | ||
| name: http-route-dynamic-listener | ||
| namespace: gateway-conformance-infra | ||
| spec: | ||
| parentRefs: | ||
| - kind: Gateway | ||
| name: gateway-dynamic-listener | ||
| namespace: gateway-conformance-infra | ||
| rules: | ||
| - backendRefs: | ||
| - name: infra-backend-v1 | ||
| port: 8080 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,54 @@ | ||
| /* | ||
| Copyright 2023 The Kubernetes Authors. | ||
|
|
||
| Licensed under the Apache License, Version 2.0 (the "License"); | ||
| you may not use this file except in compliance with the License. | ||
| You may obtain a copy of the License at | ||
|
|
||
| http://www.apache.org/licenses/LICENSE-2.0 | ||
|
|
||
| Unless required by applicable law or agreed to in writing, software | ||
| distributed under the License is distributed on an "AS IS" BASIS, | ||
| WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| See the License for the specific language governing permissions and | ||
| limitations under the License. | ||
| */ | ||
|
|
||
| package config | ||
|
|
||
| // DynamicPortRange specifies the starting and ending port of a | ||
| // range | ||
| type DynamicPortRange struct { | ||
| Start int | ||
| End int | ||
| } | ||
|
|
||
| // ListenerConfig allow conformance test runners to configure | ||
| type ListenerConfig struct { | ||
| DynamicPortRange DynamicPortRange | ||
dprotaso marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| } | ||
|
|
||
| // DefaultListenerConfig returns a [ListenerConfig] where the [DynamicPortRange] | ||
| // defaults to the suggested IANA [dynamic port range] (49152-65535) | ||
| // | ||
| // [dynamic port range]: https://www.iana.org/assignments/service-names-port-numbers/service-names-port-numbers.xhtml | ||
| func DefaultListenerConfig() ListenerConfig { | ||
| return ListenerConfig{ | ||
| DynamicPortRange: DynamicPortRange{ | ||
| Start: 49152, | ||
| End: 65535, | ||
| }, | ||
| } | ||
| } | ||
|
|
||
| // SetupListenerConfig will apply defaults to the passed [ListenerConfig] | ||
| func SetupListenerConfig(c *ListenerConfig) { | ||
| defaultConfig := DefaultListenerConfig() | ||
|
|
||
| if c.DynamicPortRange.Start == 0 { | ||
| c.DynamicPortRange.Start = defaultConfig.DynamicPortRange.Start | ||
| } | ||
| if c.DynamicPortRange.End == 0 { | ||
| c.DynamicPortRange.End = defaultConfig.DynamicPortRange.End | ||
| } | ||
dprotaso marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.