MCPcopy
hub / github.com/grpc/grpc-go / dnsHostNameFromCluster

Function dnsHostNameFromCluster

internal/xds/xdsclient/xdsresource/unmarshal_cds.go:285–317  ·  view source on GitHub ↗

dnsHostNameFromCluster extracts the DNS host name from the cluster's load assignment. There should be exactly one locality, with one endpoint, whose address contains the address and port.

(cluster *v3clusterpb.Cluster)

Source from the content-addressed store, hash-verified

283// There should be exactly one locality, with one endpoint, whose address
284// contains the address and port.
285func dnsHostNameFromCluster(cluster *v3clusterpb.Cluster) (string, error) {
286 loadAssignment := cluster.GetLoadAssignment()
287 if loadAssignment == nil {
288 return "", fmt.Errorf("load_assignment not present for LOGICAL_DNS cluster")
289 }
290 if len(loadAssignment.GetEndpoints()) != 1 {
291 return "", fmt.Errorf("load_assignment for LOGICAL_DNS cluster must have exactly one locality, got: %+v", loadAssignment)
292 }
293 endpoints := loadAssignment.GetEndpoints()[0].GetLbEndpoints()
294 if len(endpoints) != 1 {
295 return "", fmt.Errorf("locality for LOGICAL_DNS cluster must have exactly one endpoint, got: %+v", endpoints)
296 }
297 endpoint := endpoints[0].GetEndpoint()
298 if endpoint == nil {
299 return "", fmt.Errorf("endpoint for LOGICAL_DNS cluster not set")
300 }
301 socketAddr := endpoint.GetAddress().GetSocketAddress()
302 if socketAddr == nil {
303 return "", fmt.Errorf("socket address for endpoint for LOGICAL_DNS cluster not set")
304 }
305 if socketAddr.GetResolverName() != "" {
306 return "", fmt.Errorf("socket address for endpoint for LOGICAL_DNS cluster not set has unexpected custom resolver name: %v", socketAddr.GetResolverName())
307 }
308 host := socketAddr.GetAddress()
309 if host == "" {
310 return "", fmt.Errorf("host for endpoint for LOGICAL_DNS cluster not set")
311 }
312 port := socketAddr.GetPortValue()
313 if port == 0 {
314 return "", fmt.Errorf("port for endpoint for LOGICAL_DNS cluster not set")
315 }
316 return net.JoinHostPort(host, strconv.Itoa(int(port))), nil
317}
318
319// securityConfigFromCluster extracts the relevant security configuration from
320// the received Cluster resource.

Calls 2

ErrorfMethod · 0.65
GetAddressMethod · 0.45

Tested by

no test coverage detected