-
Notifications
You must be signed in to change notification settings - Fork 4.5k
xds: add xDS transport custom dial options support #7997
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
Merged
Changes from all commits
Commits
Show all changes
18 commits
Select commit
Hold shift + click to select a range
404a3a6
xds: add xDS transport custom dial options support
yousukseung 499d283
removed unused const
yousukseung 466fb00
trigger test again
yousukseung 6e7639d
comments addressed
yousukseung 5b9e9d7
don't use join
yousukseung 4288b8b
vet.sh fix
yousukseung 5767b77
retest
yousukseung 1b41034
Merge branch 'master' into xdsclient-dialopts
yousukseung 3768fef
update comment
yousukseung 21cb7c9
test with mock dialopts
yousukseung dbc1062
Merge branch 'master' into xdsclient-dialopts
yousukseung cda2ca1
vet.sh fix
yousukseung 21e4e0f
vet fix, renamed
yousukseung 681038c
more vet fix
yousukseung 5b5c6dd
comments addressed
yousukseung 3189c43
vet fix
yousukseung 3e21df2
comment updated
yousukseung a637cab
test message capitalization
yousukseung 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
165 changes: 165 additions & 0 deletions
165
xds/internal/xdsclient/tests/client_custom_dialopts_test.go
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,165 @@ | ||
/* | ||
* | ||
* Copyright 2024 gRPC 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 xdsclient_test | ||
|
||
import ( | ||
"context" | ||
"encoding/json" | ||
"fmt" | ||
"testing" | ||
|
||
"github.com/google/go-cmp/cmp" | ||
"github.com/google/uuid" | ||
"google.golang.org/grpc" | ||
"google.golang.org/grpc/credentials" | ||
"google.golang.org/grpc/credentials/insecure" | ||
"google.golang.org/grpc/internal" | ||
"google.golang.org/grpc/internal/stubserver" | ||
"google.golang.org/grpc/internal/testutils" | ||
"google.golang.org/grpc/internal/testutils/xds/e2e" | ||
internalbootstrap "google.golang.org/grpc/internal/xds/bootstrap" | ||
testgrpc "google.golang.org/grpc/interop/grpc_testing" | ||
testpb "google.golang.org/grpc/interop/grpc_testing" | ||
"google.golang.org/grpc/resolver" | ||
"google.golang.org/grpc/xds/bootstrap" | ||
xci "google.golang.org/grpc/xds/internal/xdsclient/internal" | ||
) | ||
|
||
// nopDialOption is a no-op grpc.DialOption with a name. | ||
type nopDialOption struct { | ||
grpc.EmptyDialOption | ||
name string | ||
} | ||
|
||
// testCredsBundle implements `credentials.Bundle` and `extraDialOptions`. | ||
type testCredsBundle struct { | ||
credentials.Bundle | ||
testDialOptNames []string | ||
} | ||
|
||
func (t *testCredsBundle) DialOptions() []grpc.DialOption { | ||
var opts []grpc.DialOption | ||
for _, name := range t.testDialOptNames { | ||
opts = append(opts, &nopDialOption{name: name}) | ||
} | ||
return opts | ||
} | ||
|
||
type testCredsBuilder struct { | ||
testDialOptNames []string | ||
} | ||
|
||
func (t *testCredsBuilder) Build(config json.RawMessage) (credentials.Bundle, func(), error) { | ||
return &testCredsBundle{ | ||
Bundle: insecure.NewBundle(), | ||
testDialOptNames: t.testDialOptNames, | ||
}, func() {}, nil | ||
} | ||
|
||
func (t *testCredsBuilder) Name() string { | ||
return "test_dialer_creds" | ||
} | ||
|
||
func (s) TestClientCustomDialOptsFromCredentialsBundle(t *testing.T) { | ||
// Create and register the credentials bundle builder. | ||
credsBuilder := &testCredsBuilder{ | ||
testDialOptNames: []string{"opt1", "opt2", "opt3"}, | ||
} | ||
bootstrap.RegisterCredentials(credsBuilder) | ||
|
||
// Start an xDS management server. | ||
mgmtServer := e2e.StartManagementServer(t, e2e.ManagementServerOptions{}) | ||
|
||
// Create bootstrap configuration pointing to the above management server. | ||
nodeID := uuid.New().String() | ||
bc, err := internalbootstrap.NewContentsForTesting(internalbootstrap.ConfigOptionsForTesting{ | ||
Servers: []byte(fmt.Sprintf(`[{ | ||
"server_uri": %q, | ||
"channel_creds": [{ | ||
"type": %q, | ||
"config": {"mgmt_server_address": %q} | ||
}] | ||
}]`, mgmtServer.Address, credsBuilder.Name(), mgmtServer.Address)), | ||
Node: []byte(fmt.Sprintf(`{"id": "%s"}`, nodeID)), | ||
}) | ||
if err != nil { | ||
t.Fatalf("Failed to create bootstrap configuration: %v", err) | ||
} | ||
|
||
// Create an xDS resolver with the above bootstrap configuration. | ||
var resolverBuilder resolver.Builder | ||
if newResolver := internal.NewXDSResolverWithConfigForTesting; newResolver != nil { | ||
resolverBuilder, err = newResolver.(func([]byte) (resolver.Builder, error))(bc) | ||
if err != nil { | ||
t.Fatalf("Failed to create xDS resolver for testing: %v", err) | ||
} | ||
} | ||
|
||
// Spin up a test backend. | ||
server := stubserver.StartTestService(t, nil) | ||
defer server.Stop() | ||
|
||
// Configure client side xDS resources on the management server. | ||
const serviceName = "my-service-client-side-xds" | ||
resources := e2e.DefaultClientResources(e2e.ResourceParams{ | ||
DialTarget: serviceName, | ||
NodeID: nodeID, | ||
Host: "localhost", | ||
Port: testutils.ParsePort(t, server.Address), | ||
SecLevel: e2e.SecurityLevelNone, | ||
}) | ||
ctx, cancel := context.WithTimeout(context.Background(), defaultTestTimeout) | ||
defer cancel() | ||
if err := mgmtServer.Update(ctx, resources); err != nil { | ||
t.Fatal(err) | ||
} | ||
|
||
// Intercept a grpc.NewClient call from the xds client to validate DialOptions. | ||
xci.GRPCNewClient = func(target string, opts ...grpc.DialOption) (conn *grpc.ClientConn, err error) { | ||
got := map[string]int{} | ||
for _, opt := range opts { | ||
if mo, ok := opt.(*nopDialOption); ok { | ||
got[mo.name]++ | ||
} | ||
} | ||
want := map[string]int{} | ||
for _, name := range credsBuilder.testDialOptNames { | ||
want[name]++ | ||
} | ||
if !cmp.Equal(got, want) { | ||
t.Errorf("grpc.NewClient() was called with unexpected DialOptions: got %v, want %v", got, want) | ||
} | ||
return grpc.NewClient(target, opts...) | ||
} | ||
defer func() { xci.GRPCNewClient = grpc.NewClient }() | ||
|
||
// Create a ClientConn and make a successful RPC. The insecure transport | ||
// credentials passed into the gRPC.NewClient is the credentials for the | ||
// data plane communication with the test backend. | ||
cc, err := grpc.NewClient(fmt.Sprintf("xds:///%s", serviceName), grpc.WithTransportCredentials(insecure.NewCredentials()), grpc.WithResolvers(resolverBuilder)) | ||
if err != nil { | ||
t.Fatalf("Failed to dial local test server: %v", err) | ||
} | ||
|
||
client := testgrpc.NewTestServiceClient(cc) | ||
if _, err := client.EmptyCall(ctx, &testpb.Empty{}); err != nil { | ||
t.Fatalf("EmptyCall() failed: %v", err) | ||
} | ||
cc.Close() | ||
} |
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.