-
Notifications
You must be signed in to change notification settings - Fork 4.4k
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
xds: add test-only injection of xds config to client and server #4476
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -52,7 +52,7 @@ func init() { | |
balancer.Register(clusterImplBB{}) | ||
} | ||
|
||
var newXDSClient = func() (xdsClientInterface, error) { return xdsclient.New() } | ||
var newXDSClient func() (xdsClientInterface, error) | ||
|
||
type clusterImplBB struct{} | ||
|
||
|
@@ -61,18 +61,22 @@ func (clusterImplBB) Build(cc balancer.ClientConn, bOpts balancer.BuildOptions) | |
ClientConn: cc, | ||
bOpts: bOpts, | ||
closed: grpcsync.NewEvent(), | ||
done: grpcsync.NewEvent(), | ||
loadWrapper: loadstore.NewWrapper(), | ||
pickerUpdateCh: buffer.NewUnbounded(), | ||
requestCountMax: defaultRequestCountMax, | ||
} | ||
b.logger = prefixLogger(b) | ||
|
||
client, err := newXDSClient() | ||
if err != nil { | ||
b.logger.Errorf("failed to create xds-client: %v", err) | ||
return nil | ||
if newXDSClient != nil { | ||
// For tests | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Complete the comment? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I thought this was sufficient, actually. Tests will set this; it will be |
||
client, err := newXDSClient() | ||
if err != nil { | ||
b.logger.Errorf("failed to create xds-client: %v", err) | ||
return nil | ||
} | ||
b.xdsC = client | ||
} | ||
b.xdsC = client | ||
go b.run() | ||
|
||
b.logger.Infof("Created") | ||
|
@@ -107,6 +111,7 @@ type clusterImplBalancer struct { | |
// synchronized with Close(). | ||
mu sync.Mutex | ||
closed *grpcsync.Event | ||
done *grpcsync.Event | ||
|
||
bOpts balancer.BuildOptions | ||
logger *grpclog.PrefixLogger | ||
|
@@ -204,6 +209,14 @@ func (cib *clusterImplBalancer) UpdateClientConnState(s balancer.ClientConnState | |
return fmt.Errorf("balancer %q not registered", newConfig.ChildPolicy.Name) | ||
} | ||
|
||
if cib.xdsC == nil { | ||
c := xdsclient.FromResolverState(s.ResolverState) | ||
if c == nil { | ||
return balancer.ErrBadResolverState | ||
} | ||
cib.xdsC = c | ||
} | ||
|
||
// Update load reporting config. This needs to be done before updating the | ||
// child policy because we need the loadStore from the updated client to be | ||
// passed to the ccWrapper, so that the next picker from the child policy | ||
|
@@ -315,7 +328,10 @@ func (cib *clusterImplBalancer) Close() { | |
cib.childLB.Close() | ||
cib.childLB = nil | ||
} | ||
cib.xdsC.Close() | ||
if newXDSClient != nil { | ||
cib.xdsC.Close() | ||
} | ||
<-cib.done.Done() | ||
cib.logger.Infof("Shutdown") | ||
} | ||
|
||
|
@@ -363,6 +379,7 @@ type dropConfigs struct { | |
} | ||
|
||
func (cib *clusterImplBalancer) run() { | ||
defer cib.done.Fire() | ||
for { | ||
select { | ||
case update := <-cib.pickerUpdateCh.Get(): | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -53,7 +53,7 @@ var ( | |
newEDSBalancer = func(cc balancer.ClientConn, opts balancer.BuildOptions, enqueueState func(priorityType, balancer.State), lw load.PerClusterReporter, logger *grpclog.PrefixLogger) edsBalancerImplInterface { | ||
return newEDSBalancerImpl(cc, opts, enqueueState, lw, logger) | ||
} | ||
newXDSClient = func() (xdsClientInterface, error) { return xdsclient.New() } | ||
newXDSClient func() (xdsClientInterface, error) | ||
) | ||
|
||
func init() { | ||
|
@@ -76,13 +76,16 @@ func (b *edsBalancerBuilder) Build(cc balancer.ClientConn, opts balancer.BuildOp | |
} | ||
x.logger = prefixLogger(x) | ||
|
||
client, err := newXDSClient() | ||
if err != nil { | ||
x.logger.Errorf("xds: failed to create xds-client: %v", err) | ||
return nil | ||
if newXDSClient != nil { | ||
// For tests | ||
client, err := newXDSClient() | ||
if err != nil { | ||
x.logger.Errorf("xds: failed to create xds-client: %v", err) | ||
return nil | ||
} | ||
x.xdsClient = client | ||
} | ||
|
||
x.xdsClient = client | ||
x.edsImpl = newEDSBalancer(x.cc, opts, x.enqueueChildBalancerState, x.loadWrapper, x.logger) | ||
x.logger.Infof("Created") | ||
go x.run() | ||
|
@@ -172,7 +175,9 @@ func (x *edsBalancer) run() { | |
x.edsImpl.updateState(u.priority, u.s) | ||
case <-x.closed.Done(): | ||
x.cancelWatch() | ||
x.xdsClient.Close() | ||
if newXDSClient != nil { | ||
x.xdsClient.Close() | ||
} | ||
x.edsImpl.close() | ||
x.logger.Infof("Shutdown") | ||
x.done.Fire() | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I liked the There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Can we delay this as another cleanup? It will conflict with #4479 |
||
|
@@ -380,6 +385,14 @@ func (x *edsBalancer) ResolverError(err error) { | |
} | ||
|
||
func (x *edsBalancer) UpdateClientConnState(s balancer.ClientConnState) error { | ||
if x.xdsClient == nil { | ||
c := xdsclient.FromResolverState(s.ResolverState) | ||
if c == nil { | ||
return balancer.ErrBadResolverState | ||
} | ||
x.xdsClient = c | ||
} | ||
|
||
select { | ||
case x.grpcUpdate <- &s: | ||
case <-x.closed.Done(): | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
/* | ||
* Copyright 2021 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 client | ||
|
||
import "google.golang.org/grpc/resolver" | ||
|
||
type clientKeyType string | ||
|
||
const clientKey = clientKeyType("grpc.xds.internal.client.Client") | ||
|
||
// FromResolverState returns the Client from state, or nil if not present. | ||
func FromResolverState(state resolver.State) *Client { | ||
cs, _ := state.Attributes.Value(clientKey).(*Client) | ||
return cs | ||
} | ||
|
||
// SetClient sets c in state and returns the new state. | ||
func SetClient(state resolver.State, c *Client) resolver.State { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Would we ever have the need to pass the xdsClient in attributes which are not part of the There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This is the convention we've been using for everything else in attributes... This makes it more convenient to use (set & get) than operating on attributes. I wouldn't be opposed to a change, but for this PR I'd rather follow the established convention. |
||
state.Attributes = state.Attributes.WithValues(clientKey, c) | ||
return state | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -160,13 +160,19 @@ func bootstrapConfigFromEnvVariable() ([]byte, error) { | |
// fields left unspecified, in which case the caller should use some sane | ||
// defaults. | ||
func NewConfig() (*Config, error) { | ||
config := &Config{} | ||
|
||
data, err := bootstrapConfigFromEnvVariable() | ||
if err != nil { | ||
return nil, fmt.Errorf("xds: Failed to read bootstrap config: %v", err) | ||
} | ||
logger.Debugf("Bootstrap content: %s", data) | ||
return NewConfigFromContents(data) | ||
} | ||
|
||
// NewConfigFromContents returns a new Config using the specified bootstrap | ||
// file contents instead of reading the environment variable. This is only | ||
// suitable for testing purposes. | ||
func NewConfigFromContents(data []byte) (*Config, error) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Same here: There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It doesn't seem worthwhile to use this naming given:
|
||
config := &Config{} | ||
|
||
var jsonData map[string]json.RawMessage | ||
if err := json.Unmarshal(data, &jsonData); err != nil { | ||
|
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 see that the contents of this file were moved from
xds/internal/testutils/e2e/bootstrap.go
to here in an earlier PR implementing admin services. What is the reason for this code to node live in a package which is explicitly for testing? The change need not be done in this PR, but I'm trying to understand why this package was moved from atestutils
kind of package to simply aninternal
package.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 assume it was moved because the admin service/CSDS is not a test-only thing. That seems reasonable to me. Or are you saying more was moved than needed to be?
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.
Yes, it seems like all uses of funcs in this file are from tests. And the newly added
BootstrapContents
as well.