Repository: grpc/grpc-go
File: internal/xds/balancer/clusterresolver/configbuilder.go (Go)

Code before:
func convertClusterImplMapToOutlierDetection(ciCfgs map[string]*clusterimpl.LBConfig, odCfg outlierdetection.LBConfig) map[string]*outlierdetection.LBConfig {
	odCfgs := make(map[string]*outlierdetection.LBConfig, len(ciCfgs))
	for n, c := range ciCfgs {
		odCfgs[n] = makeClusterImplOutlierDetectionChild(c, odCfg)
	}
	return odCfgs
}

func makeClusterImplOutlierDetectionChild(ciCfg *clusterimpl.LBConfig, odCfg outlierdetection.LBConfig) *outlierdetection.LBConfig {
	odCfgRet := odCfg
	odCfgRet.ChildPolicy = &internalserviceconfig.BalancerConfig{Name: clusterimpl.Name, Config: ciCfg}
	return &odCfgRet
}

func buildClusterImplConfigForDNS(g *nameGenerator, endpoints []resolver.Endpoint, mechanism DiscoveryMechanism, xdsLBPolicy *internalserviceconfig.BalancerConfig) (string, *clusterimpl.LBConfig, []resolver.Endpoint) {
	retEndpoints := make([]resolver.Endpoint, len(endpoints))
	pName := fmt.Sprintf("priority-%v", g.prefix)
	for i, e := range endpoints {
		lid := clients.Locality{}
		localityStr := xdsinternal.LocalityString(lid)
		retEndpoints[i] = hierarchy.SetInEndpoint(e, []string{pName, localityStr})
		retEndpoints[i] = xdsinternal.SetLocalityIDInEndpoint(retEndpoints[i], lid)
		var lw uint32 = 1
		retEndpoints[i] = wrrlocality.SetAddrInfoInEndpoint(retEndpoints[i], wrrlocality.AddrInfo{LocalityWeight: lw})
		var ew uint32 = 1
		retEndpoints[i] = weight.Set(retEndpoints[i], weight.EndpointInfo{Weight: lw * ew})
		// Copy the nested address field as slice fields are shared by the
		// iteration variable and the original slice.
		retEndpoints[i].Addresses = append([]resolver.Address{}, e.Addresses...)
	}
	return pName, &clusterimpl.LBConfig{
		Cluster:               mechanism.Cluster,
		TelemetryLabels:       mechanism.TelemetryLabels,
		ChildPolicy:           xdsLBPolicy,
		MaxConcurrentRequests: mechanism.MaxConcurrentRequests,
		LoadReportingServer:   mechanism.LoadReportingServer,
	}, retEndpoints
}

// buildClusterImplConfigForEDS returns a list of cluster_impl configs, one for
// each priority, sorted by priority, and the addresses for each priority (with
// hierarchy attributes set).
//
// For example, if there are two priorities, the returned values will be
// - ["p0", "p1"]
// - map{"p0":p0_config, "p1":p1_config}
// - [p0_address_0, p0_address_1, p1_address_0, p1_address_1]
//   - p0 addresses' hierarchy attributes are set to p0
func buildClusterImplConfigForEDS(g *nameGenerator, edsResp xdsresource.EndpointsUpdate, mechanism DiscoveryMechanism, xdsLBPolicy *internalserviceconfig.BalancerConfig) ([]string, map[string]*clusterimpl.LBConfig, []resolver.Endpoint, error) {
	drops := make([]clusterimpl.DropConfig, 0, len(edsResp.Drops))
	for _, d := range edsResp.Drops {

Review comment (suggestion):
Again here, we should be operating on a single endpoint, and that should be an invariant that this block of code expects. Based on my other comment, if we guarantee that from `resource_resolver_dns.go`, we should be good here.

Why do you have to set the weights? IIRC, weights will be assumed to be 1 if you don't set it. 

Also, why do you have to set the empty locality in the hierarchy? Could you add a comment for why you need to do that.

Also, you could remove a bunch of these local variables and inline them and make this code block smaller.

Code after:

func convertClusterImplMapToOutlierDetection(ciCfgs map[string]*clusterimpl.LBConfig, odCfg outlierdetection.LBConfig) map[string]*outlierdetection.LBConfig {
	odCfgs := make(map[string]*outlierdetection.LBConfig, len(ciCfgs))
	for n, c := range ciCfgs {
		odCfgs[n] = makeClusterImplOutlierDetectionChild(c, odCfg)
	}
	return odCfgs
}

func makeClusterImplOutlierDetectionChild(ciCfg *clusterimpl.LBConfig, odCfg outlierdetection.LBConfig) *outlierdetection.LBConfig {
	odCfgRet := odCfg
	odCfgRet.ChildPolicy = &internalserviceconfig.BalancerConfig{Name: clusterimpl.Name, Config: ciCfg}
	return &odCfgRet
}

func buildClusterImplConfigForDNS(g *nameGenerator, endpoints []resolver.Endpoint, mechanism DiscoveryMechanism, xdsLBPolicy *internalserviceconfig.BalancerConfig) (string, *clusterimpl.LBConfig, []resolver.Endpoint) {
	pName := fmt.Sprintf("priority-%v", g.prefix)
	lbconfig := &clusterimpl.LBConfig{
		Cluster:               mechanism.Cluster,
		TelemetryLabels:       mechanism.TelemetryLabels,
		ChildPolicy:           xdsLBPolicy,
		MaxConcurrentRequests: mechanism.MaxConcurrentRequests,
		LoadReportingServer:   mechanism.LoadReportingServer,
	}
	if len(endpoints) == 0 {
		return pName, lbconfig, nil
	}
	var retEndpoint resolver.Endpoint
	for _, e := range endpoints {
		// LOGICAL_DNS requires all resolved addresses to be grouped into a
		// single logical endpoint. We iterate over the input endpoints and
		// aggregate their addresses into a new endpoint variable.
		retEndpoint.Addresses = append(retEndpoint.Addresses, e.Addresses...)
	}
	// Even though localities are not a thing for the LOGICAL_DNS cluster and
	// its endpoint(s), we add an empty locality attribute here to ensure that
	// LB policies that rely on locality information (like weighted_target)
	// continue to work.
	localityStr := xdsinternal.LocalityString(clients.Locality{})
	retEndpoint = xdsresource.SetHostname(hierarchy.SetInEndpoint(retEndpoint, []string{pName, localityStr}), mechanism.DNSHostname)
	// Set the locality weight to 1. This is required because the child policy
	// like weighted_target which relies on locality weights to distribute
	// traffic. These policies may drop traffic if the weight is 0.
	retEndpoint = wrrlocality.SetAddrInfo(retEndpoint, wrrlocality.AddrInfo{LocalityWeight: 1})
	return pName, lbconfig, []resolver.Endpoint{retEndpoint}
}

// buildClusterImplConfigForEDS returns a list of cluster_impl configs, one for
// each priority, sorted by priority, and the addresses for each priority (with
// hierarchy attributes set).
//