generated from kubernetes/kubernetes-template-project
-
Notifications
You must be signed in to change notification settings - Fork 620
Request mirroring conformance #1912
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
Merged
k8s-ci-robot
merged 9 commits into
kubernetes-sigs:main
from
LiorLieberman:request-mirroring-conformance
May 17, 2023
Merged
Changes from all commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
90783dc
add request mirror conformance tests
LiorLieberman 6c2c40c
add more files
LiorLieberman eb542a3
fix 2023
LiorLieberman 5e6a4ca
added mirrored backend to the base manifests + added logs to Expected…
LiorLieberman e1c3944
add newline
LiorLieberman db4dc30
uncomment Suite Features
LiorLieberman 720934d
fix imports
LiorLieberman 315e1e1
remove mirrored-backend from base manifests and reuse existing infra-…
LiorLieberman a44dfe3
add comment on controller-runtime
LiorLieberman 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
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,74 @@ | ||
| /* | ||
| 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 ( | ||
| "testing" | ||
|
|
||
| "k8s.io/apimachinery/pkg/types" | ||
|
|
||
| "sigs.k8s.io/gateway-api/conformance/utils/http" | ||
| "sigs.k8s.io/gateway-api/conformance/utils/kubernetes" | ||
| "sigs.k8s.io/gateway-api/conformance/utils/suite" | ||
| ) | ||
|
|
||
| func init() { | ||
| ConformanceTests = append(ConformanceTests, HTTPRouteRequestMirror) | ||
| } | ||
|
|
||
| var HTTPRouteRequestMirror = suite.ConformanceTest{ | ||
| ShortName: "HTTPRouteRequestMirror", | ||
| Description: "An HTTPRoute with request mirror filter", | ||
| Manifests: []string{"tests/httproute-request-mirror.yaml"}, | ||
| Features: []suite.SupportedFeature{ | ||
| suite.SupportGateway, | ||
| suite.SupportHTTPRoute, | ||
| suite.SupportHTTPRouteRequestMirror, | ||
| }, | ||
| Test: func(t *testing.T, suite *suite.ConformanceTestSuite) { | ||
| ns := "gateway-conformance-infra" | ||
| routeNN := types.NamespacedName{Name: "request-mirror", Namespace: ns} | ||
| gwNN := types.NamespacedName{Name: "same-namespace", Namespace: ns} | ||
| gwAddr := kubernetes.GatewayAndHTTPRoutesMustBeAccepted(t, suite.Client, suite.TimeoutConfig, suite.ControllerName, kubernetes.NewGatewayRef(gwNN), routeNN) | ||
|
|
||
| testCases := []http.ExpectedResponse{ | ||
| { | ||
| Request: http.Request{ | ||
| Path: "/mirror", | ||
| }, | ||
| ExpectedRequest: &http.ExpectedRequest{ | ||
| Request: http.Request{ | ||
| Path: "/mirror", | ||
| }, | ||
| }, | ||
| Backend: "infra-backend-v1", | ||
| MirroredTo: "infra-backend-v2", | ||
| Namespace: ns, | ||
| }, | ||
| } | ||
| for i := range testCases { | ||
| // Declare tc here to avoid loop variable | ||
| // reuse issues across parallel tests. | ||
| tc := testCases[i] | ||
| t.Run(tc.GetTestCaseName(i), func(t *testing.T) { | ||
| t.Parallel() | ||
| http.MakeRequestAndExpectEventuallyConsistentResponse(t, suite.RoundTripper, suite.TimeoutConfig, gwAddr, tc) | ||
| http.ExpectMirroredRequest(t, suite.Client, suite.Clientset, ns, tc.MirroredTo, tc.Request.Path) | ||
| }) | ||
| } | ||
| }, | ||
| } |
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,24 @@ | ||
| apiVersion: gateway.networking.k8s.io/v1beta1 | ||
| kind: HTTPRoute | ||
| metadata: | ||
| name: request-mirror | ||
| namespace: gateway-conformance-infra | ||
| spec: | ||
| parentRefs: | ||
| - name: same-namespace | ||
| rules: | ||
| - matches: | ||
| - path: | ||
| type: PathPrefix | ||
| value: /mirror | ||
| filters: | ||
| - type: RequestMirror | ||
| requestMirror: | ||
| backendRef: | ||
| name: infra-backend-v2 | ||
| namespace: gateway-conformance-infra | ||
| port: 8080 | ||
| backendRefs: | ||
| - name: infra-backend-v1 | ||
| port: 8080 | ||
| namespace: gateway-conformance-infra |
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
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,60 @@ | ||
| /* | ||
| 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 http | ||
|
|
||
| import ( | ||
| "fmt" | ||
| "regexp" | ||
| "testing" | ||
| "time" | ||
|
|
||
| "github.com/stretchr/testify/require" | ||
| clientset "k8s.io/client-go/kubernetes" | ||
| "sigs.k8s.io/controller-runtime/pkg/client" | ||
|
|
||
| "sigs.k8s.io/gateway-api/conformance/utils/kubernetes" | ||
| ) | ||
|
|
||
| func ExpectMirroredRequest(t *testing.T, client client.Client, clientset *clientset.Clientset, ns, mirrorPod, path string) { | ||
| if mirrorPod == "" { | ||
| t.Fatalf("MirroredTo wasn't provided in the testcase, this test should only check http request mirror.") | ||
| } | ||
|
|
||
| require.Eventually(t, func() bool { | ||
| var mirrored bool | ||
| mirrorLogRegexp := regexp.MustCompile(fmt.Sprintf("Echoing back request made to \\%s to client", path)) | ||
|
|
||
| t.Log("Searching for the mirrored request log") | ||
| t.Logf("Reading \"%s/%s\" logs", ns, mirrorPod) | ||
| logs, err := kubernetes.DumpEchoLogs(ns, mirrorPod, client, clientset) | ||
| if err != nil { | ||
| t.Logf("could not read \"%s/%s\" logs: %v", ns, mirrorPod, err) | ||
| return false | ||
| } | ||
|
|
||
| for _, log := range logs { | ||
| if mirrorLogRegexp.MatchString(string(log)) { | ||
| mirrored = true | ||
| break | ||
| } | ||
| } | ||
| return mirrored | ||
|
|
||
| }, 60*time.Second, time.Second, "Mirrored request log wasn't found") | ||
|
|
||
| t.Log("Mirrored request log found") | ||
| } |
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,64 @@ | ||
| /* | ||
| 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 kubernetes | ||
|
|
||
| import ( | ||
| "context" | ||
| "io" | ||
|
|
||
| corev1 "k8s.io/api/core/v1" | ||
| "k8s.io/apimachinery/pkg/labels" | ||
| clientset "k8s.io/client-go/kubernetes" | ||
| "sigs.k8s.io/controller-runtime/pkg/client" | ||
| ) | ||
|
|
||
| // DumpEchoLogs returns logs of the echoserver pod in | ||
| // in the given namespace and with the given name. | ||
| func DumpEchoLogs(ns, name string, c client.Client, cs *clientset.Clientset) ([][]byte, error) { | ||
| var logs [][]byte | ||
|
|
||
| pods := new(corev1.PodList) | ||
| podListOptions := &client.ListOptions{ | ||
| LabelSelector: labels.SelectorFromSet(map[string]string{"app": name}), | ||
| Namespace: ns, | ||
| } | ||
| if err := c.List(context.TODO(), pods, podListOptions); err != nil { | ||
| return nil, err | ||
| } | ||
|
|
||
| podLogOptions := &corev1.PodLogOptions{ | ||
| Container: name, | ||
LiorLieberman marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| } | ||
| for _, pod := range pods.Items { | ||
| if pod.Status.Phase == corev1.PodFailed { | ||
| continue | ||
| } | ||
| req := cs.CoreV1().Pods(ns).GetLogs(pod.Name, podLogOptions) | ||
| logStream, err := req.Stream(context.TODO()) | ||
| if err != nil { | ||
| continue | ||
| } | ||
| defer logStream.Close() | ||
| logBytes, err := io.ReadAll(logStream) | ||
| if err != nil { | ||
| continue | ||
| } | ||
| logs = append(logs, logBytes) | ||
| } | ||
|
|
||
| return logs, nil | ||
| } | ||
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
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
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.