Skip to content

Commit a75dfa6

Browse files
authored
xds: change the DumpResources API to return proto message containing the resource dump (#7240)
1 parent 48b6b11 commit a75dfa6

File tree

6 files changed

+428
-306
lines changed

6 files changed

+428
-306
lines changed

xds/csds/csds.go

Lines changed: 5 additions & 59 deletions
Original file line numberDiff line numberDiff line change
@@ -34,10 +34,7 @@ import (
3434
internalgrpclog "google.golang.org/grpc/internal/grpclog"
3535
"google.golang.org/grpc/status"
3636
"google.golang.org/grpc/xds/internal/xdsclient"
37-
"google.golang.org/grpc/xds/internal/xdsclient/xdsresource"
38-
"google.golang.org/protobuf/types/known/timestamppb"
3937

40-
v3adminpb "github.com/envoyproxy/go-control-plane/envoy/admin/v3"
4138
v3statusgrpc "github.com/envoyproxy/go-control-plane/envoy/service/status/v3"
4239
v3statuspb "github.com/envoyproxy/go-control-plane/envoy/service/status/v3"
4340
)
@@ -77,7 +74,7 @@ func NewClientStatusDiscoveryServer() (*ClientStatusDiscoveryServer, error) {
7774
return s, nil
7875
}
7976

80-
// StreamClientStatus implementations interface ClientStatusDiscoveryServiceServer.
77+
// StreamClientStatus implements interface ClientStatusDiscoveryServiceServer.
8178
func (s *ClientStatusDiscoveryServer) StreamClientStatus(stream v3statusgrpc.ClientStatusDiscoveryService_StreamClientStatusServer) error {
8279
for {
8380
req, err := stream.Recv()
@@ -97,13 +94,13 @@ func (s *ClientStatusDiscoveryServer) StreamClientStatus(stream v3statusgrpc.Cli
9794
}
9895
}
9996

100-
// FetchClientStatus implementations interface ClientStatusDiscoveryServiceServer.
97+
// FetchClientStatus implements interface ClientStatusDiscoveryServiceServer.
10198
func (s *ClientStatusDiscoveryServer) FetchClientStatus(_ context.Context, req *v3statuspb.ClientStatusRequest) (*v3statuspb.ClientStatusResponse, error) {
10299
return s.buildClientStatusRespForReq(req)
103100
}
104101

105-
// buildClientStatusRespForReq fetches the status from the client, and returns
106-
// the response to be sent back to xdsclient.
102+
// buildClientStatusRespForReq fetches the status of xDS resources from the
103+
// xdsclient, and returns the response to be sent back to the csds client.
107104
//
108105
// If it returns an error, the error is a status error.
109106
func (s *ClientStatusDiscoveryServer) buildClientStatusRespForReq(req *v3statuspb.ClientStatusRequest) (*v3statuspb.ClientStatusResponse, error) {
@@ -119,16 +116,7 @@ func (s *ClientStatusDiscoveryServer) buildClientStatusRespForReq(req *v3statusp
119116
return nil, status.Errorf(codes.InvalidArgument, "node_matchers are not supported, request contains node_matchers: %v", req.NodeMatchers)
120117
}
121118

122-
dump := s.xdsClient.DumpResources()
123-
ret := &v3statuspb.ClientStatusResponse{
124-
Config: []*v3statuspb.ClientConfig{
125-
{
126-
Node: s.xdsClient.BootstrapConfig().NodeProto,
127-
GenericXdsConfigs: dumpToGenericXdsConfig(dump),
128-
},
129-
},
130-
}
131-
return ret, nil
119+
return s.xdsClient.DumpResources()
132120
}
133121

134122
// Close cleans up the resources.
@@ -137,45 +125,3 @@ func (s *ClientStatusDiscoveryServer) Close() {
137125
s.xdsClientClose()
138126
}
139127
}
140-
141-
func dumpToGenericXdsConfig(dump map[string]map[string]xdsresource.UpdateWithMD) []*v3statuspb.ClientConfig_GenericXdsConfig {
142-
var ret []*v3statuspb.ClientConfig_GenericXdsConfig
143-
for typeURL, updates := range dump {
144-
for name, update := range updates {
145-
config := &v3statuspb.ClientConfig_GenericXdsConfig{
146-
TypeUrl: typeURL,
147-
Name: name,
148-
VersionInfo: update.MD.Version,
149-
XdsConfig: update.Raw,
150-
LastUpdated: timestamppb.New(update.MD.Timestamp),
151-
ClientStatus: serviceStatusToProto(update.MD.Status),
152-
}
153-
if errState := update.MD.ErrState; errState != nil {
154-
config.ErrorState = &v3adminpb.UpdateFailureState{
155-
LastUpdateAttempt: timestamppb.New(errState.Timestamp),
156-
Details: errState.Err.Error(),
157-
VersionInfo: errState.Version,
158-
}
159-
}
160-
ret = append(ret, config)
161-
}
162-
}
163-
return ret
164-
}
165-
166-
func serviceStatusToProto(serviceStatus xdsresource.ServiceStatus) v3adminpb.ClientResourceStatus {
167-
switch serviceStatus {
168-
case xdsresource.ServiceStatusUnknown:
169-
return v3adminpb.ClientResourceStatus_UNKNOWN
170-
case xdsresource.ServiceStatusRequested:
171-
return v3adminpb.ClientResourceStatus_REQUESTED
172-
case xdsresource.ServiceStatusNotExist:
173-
return v3adminpb.ClientResourceStatus_DOES_NOT_EXIST
174-
case xdsresource.ServiceStatusACKed:
175-
return v3adminpb.ClientResourceStatus_ACKED
176-
case xdsresource.ServiceStatusNACKed:
177-
return v3adminpb.ClientResourceStatus_NACKED
178-
default:
179-
return v3adminpb.ClientResourceStatus_UNKNOWN
180-
}
181-
}

xds/internal/xdsclient/authority.go

Lines changed: 40 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,10 @@ import (
3232
"google.golang.org/grpc/xds/internal/xdsclient/transport"
3333
"google.golang.org/grpc/xds/internal/xdsclient/xdsresource"
3434
"google.golang.org/protobuf/types/known/anypb"
35+
"google.golang.org/protobuf/types/known/timestamppb"
36+
37+
v3adminpb "github.com/envoyproxy/go-control-plane/envoy/admin/v3"
38+
v3statuspb "github.com/envoyproxy/go-control-plane/envoy/service/status/v3"
3539
)
3640

3741
type watchState int
@@ -586,26 +590,54 @@ func (a *authority) reportLoad() (*load.Store, func()) {
586590
return a.transport.ReportLoad()
587591
}
588592

589-
func (a *authority) dumpResources() map[string]map[string]xdsresource.UpdateWithMD {
593+
func (a *authority) dumpResources() ([]*v3statuspb.ClientConfig_GenericXdsConfig, error) {
590594
a.resourcesMu.Lock()
591595
defer a.resourcesMu.Unlock()
592596

593-
dump := make(map[string]map[string]xdsresource.UpdateWithMD)
597+
var ret []*v3statuspb.ClientConfig_GenericXdsConfig
594598
for rType, resourceStates := range a.resources {
595-
states := make(map[string]xdsresource.UpdateWithMD)
599+
typeURL := rType.TypeURL()
596600
for name, state := range resourceStates {
597601
var raw *anypb.Any
598602
if state.cache != nil {
599603
raw = state.cache.Raw()
600604
}
601-
states[name] = xdsresource.UpdateWithMD{
602-
MD: state.md,
603-
Raw: raw,
605+
config := &v3statuspb.ClientConfig_GenericXdsConfig{
606+
TypeUrl: typeURL,
607+
Name: name,
608+
VersionInfo: state.md.Version,
609+
XdsConfig: raw,
610+
LastUpdated: timestamppb.New(state.md.Timestamp),
611+
ClientStatus: serviceStatusToProto(state.md.Status),
612+
}
613+
if errState := state.md.ErrState; errState != nil {
614+
config.ErrorState = &v3adminpb.UpdateFailureState{
615+
LastUpdateAttempt: timestamppb.New(errState.Timestamp),
616+
Details: errState.Err.Error(),
617+
VersionInfo: errState.Version,
618+
}
604619
}
620+
ret = append(ret, config)
605621
}
606-
dump[rType.TypeURL()] = states
607622
}
608-
return dump
623+
return ret, nil
624+
}
625+
626+
func serviceStatusToProto(serviceStatus xdsresource.ServiceStatus) v3adminpb.ClientResourceStatus {
627+
switch serviceStatus {
628+
case xdsresource.ServiceStatusUnknown:
629+
return v3adminpb.ClientResourceStatus_UNKNOWN
630+
case xdsresource.ServiceStatusRequested:
631+
return v3adminpb.ClientResourceStatus_REQUESTED
632+
case xdsresource.ServiceStatusNotExist:
633+
return v3adminpb.ClientResourceStatus_DOES_NOT_EXIST
634+
case xdsresource.ServiceStatusACKed:
635+
return v3adminpb.ClientResourceStatus_ACKED
636+
case xdsresource.ServiceStatusNACKed:
637+
return v3adminpb.ClientResourceStatus_NACKED
638+
default:
639+
return v3adminpb.ClientResourceStatus_UNKNOWN
640+
}
609641
}
610642

611643
func combineErrors(rType string, topLevelErrors []error, perResourceErrors map[string]error) error {

xds/internal/xdsclient/client.go

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,8 @@ import (
2424
"google.golang.org/grpc/internal/xds/bootstrap"
2525
"google.golang.org/grpc/xds/internal/xdsclient/load"
2626
"google.golang.org/grpc/xds/internal/xdsclient/xdsresource"
27+
28+
v3statuspb "github.com/envoyproxy/go-control-plane/envoy/service/status/v3"
2729
)
2830

2931
// XDSClient is a full fledged gRPC client which queries a set of discovery APIs
@@ -48,7 +50,7 @@ type XDSClient interface {
4850

4951
// DumpResources returns the status of the xDS resources. Returns a map of
5052
// resource type URLs to a map of resource names to resource state.
51-
DumpResources() map[string]map[string]xdsresource.UpdateWithMD
53+
DumpResources() (*v3statuspb.ClientStatusResponse, error)
5254

5355
ReportLoad(*bootstrap.ServerConfig) (*load.Store, func())
5456

xds/internal/xdsclient/clientimpl_dump.go

Lines changed: 19 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -19,35 +19,30 @@
1919
package xdsclient
2020

2121
import (
22-
"google.golang.org/grpc/xds/internal/xdsclient/xdsresource"
22+
v3statuspb "github.com/envoyproxy/go-control-plane/envoy/service/status/v3"
2323
)
2424

25-
func appendMaps(dst, src map[string]map[string]xdsresource.UpdateWithMD) {
26-
// Iterate through the resource types.
27-
for rType, srcResources := range src {
28-
// Lookup/create the resource type specific map in the destination.
29-
dstResources := dst[rType]
30-
if dstResources == nil {
31-
dstResources = make(map[string]xdsresource.UpdateWithMD)
32-
dst[rType] = dstResources
33-
}
34-
35-
// Iterate through the resources within the resource type in the source,
36-
// and copy them over to the destination.
37-
for name, update := range srcResources {
38-
dstResources[name] = update
39-
}
40-
}
41-
}
42-
4325
// DumpResources returns the status and contents of all xDS resources.
44-
func (c *clientImpl) DumpResources() map[string]map[string]xdsresource.UpdateWithMD {
26+
func (c *clientImpl) DumpResources() (*v3statuspb.ClientStatusResponse, error) {
4527
c.authorityMu.Lock()
4628
defer c.authorityMu.Unlock()
47-
dumps := make(map[string]map[string]xdsresource.UpdateWithMD)
29+
30+
var retCfg []*v3statuspb.ClientConfig_GenericXdsConfig
4831
for _, a := range c.authorities {
49-
dump := a.dumpResources()
50-
appendMaps(dumps, dump)
32+
cfg, err := a.dumpResources()
33+
if err != nil {
34+
return nil, err
35+
}
36+
retCfg = append(retCfg, cfg...)
5137
}
52-
return dumps
38+
39+
return &v3statuspb.ClientStatusResponse{
40+
Config: []*v3statuspb.ClientConfig{
41+
{
42+
// TODO: Populate ClientScope. Need to update go-control-plane dependency.
43+
Node: c.config.NodeProto,
44+
GenericXdsConfigs: retCfg,
45+
},
46+
},
47+
}, nil
5348
}

0 commit comments

Comments
 (0)