github.com/hxx258456/ccgo@v0.0.5-0.20230213014102-48b35f46f66f/grpc/internal/xds/bootstrap.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 xds contains types that need to be shared between code under
    20  // google.golang.org/grpc/xds/... and the rest of gRPC.
    21  package xds
    22  
    23  import (
    24  	"encoding/json"
    25  	"fmt"
    26  	"io/ioutil"
    27  	"os"
    28  
    29  	"github.com/hxx258456/ccgo/grpc/grpclog"
    30  	"github.com/hxx258456/ccgo/grpc/internal/envconfig"
    31  )
    32  
    33  var logger = grpclog.Component("internal/xds")
    34  
    35  // TransportAPI refers to the API version for xDS transport protocol.
    36  type TransportAPI int
    37  
    38  const (
    39  	// TransportV2 refers to the v2 xDS transport protocol.
    40  	TransportV2 TransportAPI = iota
    41  	// TransportV3 refers to the v3 xDS transport protocol.
    42  	TransportV3
    43  )
    44  
    45  // BootstrapOptions wraps the parameters passed to SetupBootstrapFile.
    46  type BootstrapOptions struct {
    47  	// Version is the xDS transport protocol version.
    48  	Version TransportAPI
    49  	// NodeID is the node identifier of the gRPC client/server node in the
    50  	// proxyless service mesh.
    51  	NodeID string
    52  	// ServerURI is the address of the management server.
    53  	ServerURI string
    54  	// ServerListenerResourceNameTemplate is the Listener resource name to fetch.
    55  	ServerListenerResourceNameTemplate string
    56  	// CertificateProviders is the certificate providers configuration.
    57  	CertificateProviders map[string]json.RawMessage
    58  	// Authorities is a list of non-default authorities.
    59  	//
    60  	// In the config, an authority contains {ServerURI, xds-version, creds,
    61  	// features, etc}. Note that this fields only has ServerURI (it's a
    62  	// map[authority-name]ServerURI). The other fields (version, creds,
    63  	// features) are assumed to be the same as the default authority (they can
    64  	// be added later if needed).
    65  	Authorities map[string]string
    66  }
    67  
    68  // SetupBootstrapFile creates a temporary file with bootstrap contents, based on
    69  // the passed in options, and updates the bootstrap environment variable to
    70  // point to this file.
    71  //
    72  // Returns a cleanup function which will be non-nil if the setup process was
    73  // completed successfully. It is the responsibility of the caller to invoke the
    74  // cleanup function at the end of the test.
    75  func SetupBootstrapFile(opts BootstrapOptions) (func(), error) {
    76  	bootstrapContents, err := BootstrapContents(opts)
    77  	if err != nil {
    78  		return nil, err
    79  	}
    80  	f, err := ioutil.TempFile("", "test_xds_bootstrap_*")
    81  	if err != nil {
    82  		return nil, fmt.Errorf("failed to created bootstrap file: %v", err)
    83  	}
    84  
    85  	if err := ioutil.WriteFile(f.Name(), bootstrapContents, 0644); err != nil {
    86  		return nil, fmt.Errorf("failed to created bootstrap file: %v", err)
    87  	}
    88  	logger.Infof("Created bootstrap file at %q with contents: %s\n", f.Name(), bootstrapContents)
    89  
    90  	origBootstrapFileName := envconfig.XDSBootstrapFileName
    91  	envconfig.XDSBootstrapFileName = f.Name()
    92  	return func() {
    93  		os.Remove(f.Name())
    94  		envconfig.XDSBootstrapFileName = origBootstrapFileName
    95  	}, nil
    96  }
    97  
    98  // BootstrapContents returns the contents to go into a bootstrap file,
    99  // environment, or configuration passed to
   100  // xds.NewXDSResolverWithConfigForTesting.
   101  func BootstrapContents(opts BootstrapOptions) ([]byte, error) {
   102  	cfg := &bootstrapConfig{
   103  		XdsServers: []server{
   104  			{
   105  				ServerURI:    opts.ServerURI,
   106  				ChannelCreds: []creds{{Type: "insecure"}},
   107  			},
   108  		},
   109  		Node: node{
   110  			ID: opts.NodeID,
   111  		},
   112  		CertificateProviders:               opts.CertificateProviders,
   113  		ServerListenerResourceNameTemplate: opts.ServerListenerResourceNameTemplate,
   114  	}
   115  	switch opts.Version {
   116  	case TransportV2:
   117  		// TODO: Add any v2 specific fields.
   118  	case TransportV3:
   119  		cfg.XdsServers[0].ServerFeatures = append(cfg.XdsServers[0].ServerFeatures, "xds_v3")
   120  	default:
   121  		return nil, fmt.Errorf("unsupported xDS transport protocol version: %v", opts.Version)
   122  	}
   123  
   124  	auths := make(map[string]authority)
   125  	for n, auURI := range opts.Authorities {
   126  		auths[n] = authority{XdsServers: []server{{
   127  			ServerURI:      auURI,
   128  			ChannelCreds:   []creds{{Type: "insecure"}},
   129  			ServerFeatures: cfg.XdsServers[0].ServerFeatures,
   130  		}}}
   131  	}
   132  	cfg.Authorities = auths
   133  
   134  	bootstrapContents, err := json.MarshalIndent(cfg, "", "  ")
   135  	if err != nil {
   136  		return nil, fmt.Errorf("failed to created bootstrap file: %v", err)
   137  	}
   138  	return bootstrapContents, nil
   139  }
   140  
   141  type bootstrapConfig struct {
   142  	XdsServers                         []server                   `json:"xds_servers,omitempty"`
   143  	Node                               node                       `json:"node,omitempty"`
   144  	CertificateProviders               map[string]json.RawMessage `json:"certificate_providers,omitempty"`
   145  	ServerListenerResourceNameTemplate string                     `json:"server_listener_resource_name_template,omitempty"`
   146  	Authorities                        map[string]authority       `json:"authorities,omitempty"`
   147  }
   148  
   149  type authority struct {
   150  	XdsServers []server `json:"xds_servers,omitempty"`
   151  }
   152  
   153  type server struct {
   154  	ServerURI      string   `json:"server_uri,omitempty"`
   155  	ChannelCreds   []creds  `json:"channel_creds,omitempty"`
   156  	ServerFeatures []string `json:"server_features,omitempty"`
   157  }
   158  
   159  type creds struct {
   160  	Type   string      `json:"type,omitempty"`
   161  	Config interface{} `json:"config,omitempty"`
   162  }
   163  
   164  type node struct {
   165  	ID string `json:"id,omitempty"`
   166  }