Repository: grpc/grpc-go
File: internal/xds/xdsclient/clientimpl.go (Go)

Code before:
	return c.bootstrapConfig
}

func (c *clientImpl) incrRef() int32 {
	return atomic.AddInt32(&c.refCount, 1)
}

func (c *clientImpl) decrRef() int32 {
	return atomic.AddInt32(&c.refCount, -1)
}

func buildServerConfigs(bootstrapSC []*bootstrap.ServerConfig, grpcTransportConfigs map[string]grpctransport.Config, gServerCfgMap map[xdsclient.ServerConfig]*bootstrap.ServerConfig) ([]xdsclient.ServerConfig, error) {
	var gServerCfg []xdsclient.ServerConfig
	for _, sc := range bootstrapSC {
		if err := populateGRPCTransportConfigsFromServerConfig(sc, grpcTransportConfigs); err != nil {
			return nil, err
		}
		var serverFeatures xdsclient.ServerFeature
		if sc.ServerFeaturesIgnoreResourceDeletion() {
			serverFeatures = serverFeatures | xdsclient.ServerFeatureIgnoreResourceDeletion
		}
		if sc.ServerFeaturesTrustedXDSServer() {
			serverFeatures = serverFeatures | xdsclient.ServerFeatureTrustedXDSServer
		}
		gsc := xdsclient.ServerConfig{
			ServerIdentifier: clients.ServerIdentifier{ServerURI: sc.ServerURI(), Extensions: grpctransport.ServerIdentifierExtension{ConfigName: sc.SelectedChannelCreds().Type}},
			ServerFeature:    serverFeatures,
		}
		gServerCfg = append(gServerCfg, gsc)
		gServerCfgMap[gsc] = sc
	}
	return gServerCfg, nil
}

// buildXDSClientConfig builds the xdsclient.Config from the bootstrap.Config.
func buildXDSClientConfig(config *bootstrap.Config, metricsRecorder estats.MetricsRecorder, target string, watchExpiryTimeout time.Duration) (xdsclient.Config, error) {
	grpcTransportConfigs := make(map[string]grpctransport.Config)
	gServerCfgMap := make(map[xdsclient.ServerConfig]*bootstrap.ServerConfig)

	gAuthorities := make(map[string]xdsclient.Authority)
	for name, cfg := range config.Authorities() {
		// If server configs are specified in the authorities map, use that.
		// Else, use the top-level server configs.
		serverCfg := config.XDSServers()
		if len(cfg.XDSServers) >= 1 {
			serverCfg = cfg.XDSServers
		}
		gsc, err := buildServerConfigs(serverCfg, grpcTransportConfigs, gServerCfgMap)
		if err != nil {
			return xdsclient.Config{}, err
		}

Review comment (refactor):
Nit: Please use a separate line for individual fields (in sub-struct fields as well). I understand this is continuing to do what the existing code was doing. But that happened to be a part of a giant refactor, and it was often easy to miss things during the review. See: go/go-style/decisions#literal-formatting

Thanks.

Code after:
	return c.bootstrapConfig
}

func (c *clientImpl) incrRef() int32 {
	return atomic.AddInt32(&c.refCount, 1)
}

func (c *clientImpl) decrRef() int32 {
	return atomic.AddInt32(&c.refCount, -1)
}

func buildServerConfigs(bootstrapSC []*bootstrap.ServerConfig, grpcTransportConfigs map[string]grpctransport.Config, gServerCfgMap map[xdsclient.ServerConfig]*bootstrap.ServerConfig) ([]xdsclient.ServerConfig, error) {
	var gServerCfg []xdsclient.ServerConfig
	for _, sc := range bootstrapSC {
		if err := populateGRPCTransportConfigsFromServerConfig(sc, grpcTransportConfigs); err != nil {
			return nil, err
		}
		var serverFeatures xdsclient.ServerFeature
		if sc.ServerFeaturesIgnoreResourceDeletion() {
			serverFeatures = serverFeatures | xdsclient.ServerFeatureIgnoreResourceDeletion
		}
		if sc.ServerFeaturesTrustedXDSServer() {
			serverFeatures = serverFeatures | xdsclient.ServerFeatureTrustedXDSServer
		}
		gsc := xdsclient.ServerConfig{
			ServerIdentifier: clients.ServerIdentifier{
				ServerURI:  sc.ServerURI(),
				Extensions: grpctransport.ServerIdentifierExtension{ConfigName: sc.SelectedChannelCreds().Type},
			},
			ServerFeature: serverFeatures,
		}
		gServerCfg = append(gServerCfg, gsc)
		gServerCfgMap[gsc] = sc
	}
	return gServerCfg, nil
}

// buildXDSClientConfig builds the xdsclient.Config from the bootstrap.Config.
func buildXDSClientConfig(config *bootstrap.Config, metricsRecorder estats.MetricsRecorder, target string, watchExpiryTimeout time.Duration) (xdsclient.Config, error) {
	grpcTransportConfigs := make(map[string]grpctransport.Config)
	gServerCfgMap := make(map[xdsclient.ServerConfig]*bootstrap.ServerConfig)

	gAuthorities := make(map[string]xdsclient.Authority)
	for name, cfg := range config.Authorities() {
		// If server configs are specified in the authorities map, use that.
		// Else, use the top-level server configs.
		serverCfg := config.XDSServers()
		if len(cfg.XDSServers) >= 1 {
			serverCfg = cfg.XDSServers
		}
		gsc, err := buildServerConfigs(serverCfg, grpcTransportConfigs, gServerCfgMap)