google.golang.org/grpc@v1.72.2/xds/csds/csds.go (about)

     1  /*
     2   *
     3   * Copyright 2021 gRPC authors.
     4   *
     5   * Licensed under the Apache License, Version 2.0 (the "License");
     6   * you may not use this file except in compliance with the License.
     7   * You may obtain a copy of the License at
     8   *
     9   *     http://www.apache.org/licenses/LICENSE-2.0
    10   *
    11   * Unless required by applicable law or agreed to in writing, software
    12   * distributed under the License is distributed on an "AS IS" BASIS,
    13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    14   * See the License for the specific language governing permissions and
    15   * limitations under the License.
    16   *
    17   */
    18  
    19  // Package csds implements features to dump the status (xDS responses) the
    20  // xds_client is using.
    21  //
    22  // Notice: This package is EXPERIMENTAL and may be changed or removed in a later
    23  // release.
    24  package csds
    25  
    26  import (
    27  	"context"
    28  	"fmt"
    29  	"io"
    30  
    31  	"google.golang.org/grpc/codes"
    32  	"google.golang.org/grpc/grpclog"
    33  	internalgrpclog "google.golang.org/grpc/internal/grpclog"
    34  	"google.golang.org/grpc/status"
    35  	"google.golang.org/grpc/xds/internal/xdsclient"
    36  
    37  	v3statusgrpc "github.com/envoyproxy/go-control-plane/envoy/service/status/v3"
    38  	v3statuspb "github.com/envoyproxy/go-control-plane/envoy/service/status/v3"
    39  )
    40  
    41  var logger = grpclog.Component("xds")
    42  
    43  const prefix = "[csds-server %p] "
    44  
    45  func prefixLogger(s *ClientStatusDiscoveryServer) *internalgrpclog.PrefixLogger {
    46  	return internalgrpclog.NewPrefixLogger(logger, fmt.Sprintf(prefix, s))
    47  }
    48  
    49  // ClientStatusDiscoveryServer provides an implementation of the Client Status
    50  // Discovery Service (CSDS) for exposing the xDS config of a given client. See
    51  // https://github.com/envoyproxy/envoy/blob/main/api/envoy/service/status/v3/csds.proto.
    52  //
    53  // For more details about the gRPC implementation of CSDS, refer to gRPC A40 at:
    54  // https://github.com/grpc/proposal/blob/master/A40-csds-support.md.
    55  type ClientStatusDiscoveryServer struct {
    56  	logger *internalgrpclog.PrefixLogger
    57  }
    58  
    59  // NewClientStatusDiscoveryServer returns an implementation of the CSDS server
    60  // that can be registered on a gRPC server.
    61  func NewClientStatusDiscoveryServer() (*ClientStatusDiscoveryServer, error) {
    62  	s := &ClientStatusDiscoveryServer{}
    63  	s.logger = prefixLogger(s)
    64  	s.logger.Infof("Created CSDS server")
    65  	return s, nil
    66  }
    67  
    68  // StreamClientStatus implements interface ClientStatusDiscoveryServiceServer.
    69  func (s *ClientStatusDiscoveryServer) StreamClientStatus(stream v3statusgrpc.ClientStatusDiscoveryService_StreamClientStatusServer) error {
    70  	for {
    71  		req, err := stream.Recv()
    72  		if err == io.EOF {
    73  			return nil
    74  		}
    75  		if err != nil {
    76  			return err
    77  		}
    78  		resp, err := s.buildClientStatusRespForReq(req)
    79  		if err != nil {
    80  			return err
    81  		}
    82  		if err := stream.Send(resp); err != nil {
    83  			return err
    84  		}
    85  	}
    86  }
    87  
    88  // FetchClientStatus implements interface ClientStatusDiscoveryServiceServer.
    89  func (s *ClientStatusDiscoveryServer) FetchClientStatus(_ context.Context, req *v3statuspb.ClientStatusRequest) (*v3statuspb.ClientStatusResponse, error) {
    90  	return s.buildClientStatusRespForReq(req)
    91  }
    92  
    93  // buildClientStatusRespForReq fetches the status of xDS resources from the
    94  // xdsclient, and returns the response to be sent back to the csds client.
    95  //
    96  // If it returns an error, the error is a status error.
    97  func (s *ClientStatusDiscoveryServer) buildClientStatusRespForReq(req *v3statuspb.ClientStatusRequest) (*v3statuspb.ClientStatusResponse, error) {
    98  	// Field NodeMatchers is unsupported, by design
    99  	// https://github.com/grpc/proposal/blob/master/A40-csds-support.md#detail-node-matching.
   100  	if len(req.NodeMatchers) != 0 {
   101  		return nil, status.Errorf(codes.InvalidArgument, "node_matchers are not supported, request contains node_matchers: %v", req.NodeMatchers)
   102  	}
   103  
   104  	return xdsclient.DumpResources(), nil
   105  }
   106  
   107  // Close cleans up the resources.
   108  func (s *ClientStatusDiscoveryServer) Close() {}