Repository: grpc/grpc-go
File: xds/googledirectpath/googlec2p.go (Go)

Code before:
		"node":        nodeCfg,
	}
	cfgJSON, err := json.Marshal(cfg)
	if err != nil {
		return nil, fmt.Errorf("failed to marshal bootstrap configuration: %v", err)
	}
	config, err := bootstrap.NewConfigFromContents(cfgJSON)
	if err != nil {
		return nil, fmt.Errorf("failed to parse bootstrap contents: %s, %v", string(cfgJSON), err)
	}

	t = resolver.Target{
		URL: url.URL{
			Scheme: xdsName,
			Host:   c2pAuthority,
			Path:   t.URL.Path,
		},
	}
	_, cancel, err := xdsClientPool.NewClientWithConfig(t.String(), opts.MetricsRecorder, config)
	if err != nil {
		return nil, fmt.Errorf("failed to create xds client: %v", err)
	}

	r, err := resolver.Get(xdsName).Build(t, cc, opts)
	if err != nil {
		cancel()
		return nil, err
	}
	return &c2pResolverWrapper{Resolver: r, cancel: cancel}, nil
}

func (b c2pResolverBuilder) Scheme() string {
	return c2pScheme
}

func newNodeConfig(zone string, ipv6Capable bool) map[string]any {
	node := map[string]any{
		"id":       fmt.Sprintf("C2P-%d", randInt()),
		"locality": map[string]any{"zone": zone},
	}
	if envconfig.NewPickFirstEnabled {
		// Enable dualstack endpoints in TD.
		// TODO(https://github.com/grpc/grpc-go/issues/8561): remove IPv6 metadata server queries entirely after old pick first is removed.
		ipv6Capable = true
	} else {
		logger.Infof("GRPC_EXPERIMENTAL_ENABLE_NEW_PICK_FIRST is disabled, setting ipv6Capable node metadata based on metadata server query")
	}
	if ipv6Capable {
		node["metadata"] = map[string]any{ipv6CapableMetadataName: true}
	}
	return node

Review comment (suggestion):
Please add a comment here explaining that we are creating the client here, but we don't use it. But when the xDS resolver attempts to create a client for the same name, the pool will end up returning the one that we created here.

Code after:
		"node":        nodeCfg,
	}
	cfgJSON, err := json.Marshal(cfg)
	if err != nil {
		return nil, fmt.Errorf("failed to marshal bootstrap configuration: %v", err)
	}
	config, err := bootstrap.NewConfigFromContents(cfgJSON)
	if err != nil {
		return nil, fmt.Errorf("failed to parse bootstrap contents: %s, %v", string(cfgJSON), err)
	}

	t = resolver.Target{
		URL: url.URL{
			Scheme: xdsName,
			Host:   c2pAuthority,
			Path:   t.URL.Path,
		},
	}

	// Create a new xDS client for this target using the provided bootstrap
	// configuration. This client is stored in the xdsclient pool’s internal
	// cache, keeping it alive and associated with this resolver until Closed().
	// While the c2p resolver itself does not directly use the client, creating
	// it ensures that when the xDS resolver later requests a client for the
	// same target, the existing instance will be reused.
	_, cancel, err := xdsClientPool.NewClientWithConfig(t.String(), opts.MetricsRecorder, config)
	if err != nil {
		return nil, fmt.Errorf("failed to create xds client: %v", err)
	}

	r, err := resolver.Get(xdsName).Build(t, cc, opts)
	if err != nil {
		cancel()
		return nil, err
	}
	return &c2pResolverWrapper{Resolver: r, cancel: cancel}, nil
}

func (b c2pResolverBuilder) Scheme() string {
	return c2pScheme
}

func newNodeConfig(zone string, ipv6Capable bool) map[string]any {
	node := map[string]any{
		"id":       fmt.Sprintf("C2P-%d", randInt()),
		"locality": map[string]any{"zone": zone},
	}
	if envconfig.NewPickFirstEnabled {
		// Enable dualstack endpoints in TD.
		// TODO(https://github.com/grpc/grpc-go/issues/8561): remove IPv6 metadata server queries entirely after old pick first is removed.
		ipv6Capable = true