generated from kubernetes/kubernetes-template-project
-
Notifications
You must be signed in to change notification settings - Fork 582
Add GRPCRoute weighted backendRefs test #3962
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
sarthyparty
wants to merge
11
commits into
kubernetes-sigs:main
Choose a base branch
from
sarthyparty:grpcroute-weight-test
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+506
−227
Open
Changes from all commits
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
a880d85
Add GRPCRoute weighted backendRefs test
sarthyparty 0641e88
use default grpc client
sarthyparty bcb34aa
reorganize imports
sarthyparty 56a9015
refactor weight test into shared function and fix nits
sarthyparty cc54768
fix lint
sarthyparty bef25ff
add grpc entropy
sarthyparty 493804e
use mesh sender
sarthyparty 9496c74
fix import cycle
sarthyparty 5c8a990
add grpc mesh test
sarthyparty 2310926
fix format and mesh test
sarthyparty a311380
add newline
sarthyparty 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,98 @@ | ||
/* | ||
Copyright 2025 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 ( | ||
"fmt" | ||
"testing" | ||
|
||
"google.golang.org/grpc/codes" | ||
"k8s.io/apimachinery/pkg/types" | ||
|
||
v1 "sigs.k8s.io/gateway-api/apis/v1" | ||
pb "sigs.k8s.io/gateway-api/conformance/echo-basic/grpcechoserver" | ||
"sigs.k8s.io/gateway-api/conformance/utils/grpc" | ||
"sigs.k8s.io/gateway-api/conformance/utils/kubernetes" | ||
"sigs.k8s.io/gateway-api/conformance/utils/suite" | ||
"sigs.k8s.io/gateway-api/conformance/utils/weight" | ||
"sigs.k8s.io/gateway-api/pkg/features" | ||
) | ||
|
||
func init() { | ||
ConformanceTests = append(ConformanceTests, GRPCRouteWeight) | ||
} | ||
|
||
var GRPCRouteWeight = suite.ConformanceTest{ | ||
ShortName: "GRPCRouteWeight", | ||
Description: "An GRPCRoute with weighted backends", | ||
Manifests: []string{"tests/grpcroute-weight.yaml"}, | ||
Features: []features.FeatureName{ | ||
features.SupportGateway, | ||
features.SupportGRPCRoute, | ||
}, | ||
Test: func(t *testing.T, suite *suite.ConformanceTestSuite) { | ||
var ( | ||
ns = "gateway-conformance-infra" | ||
routeNN = types.NamespacedName{Name: "weighted-backends", Namespace: ns} | ||
gwNN = types.NamespacedName{Name: "same-namespace", Namespace: ns} | ||
gwAddr = kubernetes.GatewayAndRoutesMustBeAccepted(t, suite.Client, suite.TimeoutConfig, suite.ControllerName, kubernetes.NewGatewayRef(gwNN), &v1.GRPCRoute{}, true, routeNN) | ||
) | ||
|
||
t.Run("Requests should have a distribution that matches the weight", func(t *testing.T) { | ||
expected := grpc.ExpectedResponse{ | ||
EchoRequest: &pb.EchoRequest{}, | ||
Response: grpc.Response{Code: codes.OK}, | ||
Namespace: "gateway-conformance-infra", | ||
} | ||
|
||
// Assert request succeeds before doing our distribution check | ||
grpc.MakeRequestAndExpectEventuallyConsistentResponse(t, suite.GRPCClient, suite.TimeoutConfig, gwAddr, expected) | ||
|
||
expectedWeights := map[string]float64{ | ||
"grpc-infra-backend-v1": 0.7, | ||
"grpc-infra-backend-v2": 0.3, | ||
"grpc-infra-backend-v3": 0.0, | ||
} | ||
|
||
sender := weight.NewFunctionBasedSender(func() (string, error) { | ||
uniqueExpected := expected | ||
if err := grpc.AddEntropy(&uniqueExpected); err != nil { | ||
return "", fmt.Errorf("error adding entropy: %w", err) | ||
} | ||
client := &grpc.DefaultClient{} | ||
defer client.Close() | ||
resp, err := client.SendRPC(t, gwAddr, uniqueExpected, suite.TimeoutConfig.MaxTimeToConsistency) | ||
if err != nil { | ||
return "", fmt.Errorf("failed to send gRPC request: %w", err) | ||
} | ||
if resp.Code != codes.OK { | ||
return "", fmt.Errorf("expected OK response, got %v", resp.Code) | ||
} | ||
return resp.Response.GetAssertions().GetContext().GetPod(), nil | ||
}) | ||
|
||
for i := 0; i < 10; i++ { | ||
if err := weight.TestWeightedDistribution(sender, expectedWeights); err != nil { | ||
t.Logf("Traffic distribution test failed (%d/10): %s", i+1, err) | ||
} else { | ||
return | ||
} | ||
} | ||
t.Fatal("Weighted distribution tests failed") | ||
}) | ||
}, | ||
} |
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,19 @@ | ||
apiVersion: gateway.networking.k8s.io/v1 | ||
kind: GRPCRoute | ||
metadata: | ||
name: weighted-backends | ||
namespace: gateway-conformance-infra | ||
spec: | ||
parentRefs: | ||
- name: same-namespace | ||
rules: | ||
- backendRefs: | ||
- name: grpc-infra-backend-v1 | ||
port: 8080 | ||
weight: 70 | ||
- name: grpc-infra-backend-v2 | ||
port: 8080 | ||
weight: 30 | ||
- name: grpc-infra-backend-v3 | ||
port: 8080 | ||
weight: 0 |
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,83 @@ | ||
/* | ||
Copyright 2025 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 meshtests | ||
|
||
import ( | ||
"fmt" | ||
"testing" | ||
|
||
"sigs.k8s.io/gateway-api/conformance/utils/echo" | ||
"sigs.k8s.io/gateway-api/conformance/utils/http" | ||
"sigs.k8s.io/gateway-api/conformance/utils/suite" | ||
"sigs.k8s.io/gateway-api/conformance/utils/weight" | ||
"sigs.k8s.io/gateway-api/pkg/features" | ||
) | ||
|
||
func init() { | ||
MeshConformanceTests = append(MeshConformanceTests, MeshGRPCRouteWeight) | ||
} | ||
|
||
var MeshGRPCRouteWeight = suite.ConformanceTest{ | ||
ShortName: "MeshGRPCRouteWeight", | ||
Description: "A GRPCRoute with weighted backends in mesh mode", | ||
Manifests: []string{"tests/mesh/grpcroute-weight.yaml"}, | ||
Features: []features.FeatureName{ | ||
features.SupportMesh, | ||
features.SupportGRPCRoute, | ||
}, | ||
Test: func(t *testing.T, s *suite.ConformanceTestSuite) { | ||
client := echo.ConnectToApp(t, s, echo.MeshAppEchoV1) | ||
|
||
t.Run("Requests should have a distribution that matches the weight", func(t *testing.T) { | ||
// Create a gRPC request using the mesh client framework | ||
expected := http.ExpectedResponse{ | ||
Request: http.Request{Protocol: "grpc", Path: "", Host: "echo:7070"}, | ||
Response: http.Response{StatusCode: 200}, | ||
Namespace: "gateway-conformance-mesh", | ||
} | ||
|
||
// Assert request succeeds before doing our distribution check | ||
client.MakeRequestAndExpectEventuallyConsistentResponse(t, expected, s.TimeoutConfig) | ||
|
||
expectedWeights := map[string]float64{ | ||
"echo-v1": 0.7, | ||
"echo-v2": 0.3, | ||
} | ||
|
||
sender := weight.NewFunctionBasedSender(func() (string, error) { | ||
uniqueExpected := expected | ||
if err := http.AddEntropy(&uniqueExpected); err != nil { | ||
return "", fmt.Errorf("error adding entropy: %w", err) | ||
} | ||
_, cRes, err := client.CaptureRequestResponseAndCompare(t, uniqueExpected) | ||
if err != nil { | ||
return "", fmt.Errorf("failed gRPC mesh request: %w", err) | ||
} | ||
return cRes.Hostname, nil | ||
}) | ||
|
||
for i := 0; i < 10; i++ { | ||
if err := weight.TestWeightedDistribution(sender, expectedWeights); err != nil { | ||
t.Logf("Traffic distribution test failed (%d/10): %s", i+1, err) | ||
} else { | ||
return | ||
} | ||
} | ||
t.Fatal("Weighted distribution tests failed") | ||
}) | ||
}, | ||
} |
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,19 @@ | ||
apiVersion: gateway.networking.k8s.io/v1 | ||
kind: GRPCRoute | ||
metadata: | ||
name: mesh-grpc-weighted-backends | ||
namespace: gateway-conformance-mesh | ||
spec: | ||
parentRefs: | ||
- group: "" | ||
kind: Service | ||
name: echo | ||
port: 7070 | ||
rules: | ||
- backendRefs: | ||
- name: echo-v1 | ||
port: 7070 | ||
weight: 70 | ||
- name: echo-v2 | ||
port: 7070 | ||
weight: 30 |
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
it looks like it is now doing http..? there is no sendGRPC stuff
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
From my understand the way that the mesh http route weight test is working is by execing into the pod and running a command
client http://echo:7070
. Using the grpc package doesn't work, since I need to run the request from the pod I think?I was able to configure this by just setting the protocol to grpc in the http Request structure. I think the correct way to do this is probably to refactor the echo client code to accept a structure that is independent of protocol.
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This code here utils/echo/pod.go L168, with args being ['client', 'http://echo:7070'] for http. By setting the protocol to grpc it becomes ['client', 'grpc://echo:7070'] which makes a grpc request I believe
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think I will refactor the echo pod function
CaptureRequestResponseAndCompare
to accept a structure that is protocol independent since it doesn't need to be http specific.Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Actually its a lot more code change than I thought to refactor that, what do you think
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
this is istio client code
/cc @howardjohn can you relate to this?