generated from kubernetes/kubernetes-template-project
-
Notifications
You must be signed in to change notification settings - Fork 583
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 9 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,100 @@ | ||
/* | ||
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" | ||
|
||
"google.golang.org/grpc/codes" | ||
|
||
pb "sigs.k8s.io/gateway-api/conformance/echo-basic/grpcechoserver" | ||
"sigs.k8s.io/gateway-api/conformance/utils/echo" | ||
"sigs.k8s.io/gateway-api/conformance/utils/grpc" | ||
"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) { | ||
// Connect to mesh app to get the service address | ||
client := echo.ConnectToApp(t, s, echo.MeshAppEchoV1) | ||
|
||
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-mesh", | ||
} | ||
|
||
// Assert request succeeds before doing our distribution check | ||
grpcClient := &grpc.DefaultClient{} | ||
defer grpcClient.Close() | ||
resp, err := grpcClient.SendRPC(t, client.Address+":9000", expected, s.TimeoutConfig.MaxTimeToConsistency) | ||
if err != nil { | ||
t.Skipf("gRPC mesh test requires gRPC support on mesh services: %v", err) | ||
} | ||
if resp.Code != codes.OK { | ||
t.Skipf("gRPC mesh test requires working gRPC endpoints: got %v", resp.Code) | ||
} | ||
|
||
expectedWeights := map[string]float64{ | ||
"echo-v1": 0.7, | ||
"echo-v2": 0.3, | ||
} | ||
|
||
sender := weight.NewFunctionBasedSender(func() (string, error) { | ||
uniqueExpected := expected | ||
if err := grpc.AddEntropy(&uniqueExpected); err != nil { | ||
return "", fmt.Errorf("error adding entropy: %w", err) | ||
} | ||
|
||
grpcClient := &grpc.DefaultClient{} | ||
defer grpcClient.Close() | ||
resp, err := grpcClient.SendRPC(t, client.Address+":9000", uniqueExpected, s.TimeoutConfig.MaxTimeToConsistency) | ||
if err != nil { | ||
return "", fmt.Errorf("failed to send gRPC mesh 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: mesh-grpc-weighted-backends | ||
namespace: gateway-conformance-mesh | ||
spec: | ||
parentRefs: | ||
- group: "" | ||
kind: Service | ||
name: echo | ||
port: 9000 | ||
rules: | ||
- backendRefs: | ||
- name: echo-v1 | ||
port: 9000 | ||
weight: 70 | ||
- name: echo-v2 | ||
port: 9000 | ||
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.
Uh oh!
There was an error while loading. Please reload this page.