Skip to content

Commit

Permalink
bootstrap: add String method to ServerConfigs type (#7537)
Browse files Browse the repository at this point in the history
  • Loading branch information
easwars authored Aug 20, 2024
1 parent ee5cbce commit 7e12068
Show file tree
Hide file tree
Showing 18 changed files with 141 additions and 141 deletions.
4 changes: 2 additions & 2 deletions internal/testutils/xds/e2e/bootstrap.go
Original file line number Diff line number Diff line change
Expand Up @@ -72,10 +72,10 @@ func DefaultBootstrapContents(t *testing.T, nodeID, serverURI string) []byte {

// Create the bootstrap configuration.
bs, err := bootstrap.NewContentsForTesting(bootstrap.ConfigOptionsForTesting{
Servers: []json.RawMessage{[]byte(fmt.Sprintf(`{
Servers: []byte(fmt.Sprintf(`[{
"server_uri": "passthrough:///%s",
"channel_creds": [{"type": "insecure"}]
}`, serverURI))},
}]`, serverURI)),
Node: []byte(fmt.Sprintf(`{"id": "%s"}`, nodeID)),
CertificateProviders: cpc,
ServerListenerResourceNameTemplate: ServerListenerResourceNameTemplate,
Expand Down
38 changes: 20 additions & 18 deletions internal/xds/bootstrap/bootstrap.go
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,19 @@ func (scs *ServerConfigs) UnmarshalJSON(data []byte) error {
return nil
}

// String returns a string representation of the ServerConfigs, by concatenating
// the string representations of the underlying server configs.
func (scs *ServerConfigs) String() string {
ret := ""
for i, sc := range *scs {
if i > 0 {
ret += ", "
}
ret += sc.String()
}
return ret
}

// Authority contains configuration for an xDS control plane authority.
//
// This type does not implement custom JSON marshal/unmarshal logic because it
Expand Down Expand Up @@ -237,14 +250,6 @@ func (sc *ServerConfig) Equal(other *ServerConfig) bool {
}

// String returns the string representation of the ServerConfig.
//
// This string representation will be used as map keys in federation
// (`map[ServerConfig]authority`), so that the xDS ClientConn and stream will be
// shared by authorities with different names but the same server config.
//
// It covers (almost) all the fields so the string can represent the config
// content. It doesn't cover NodeProto because NodeProto isn't used by
// federation.
func (sc *ServerConfig) String() string {
if len(sc.serverFeatures) == 0 {
return fmt.Sprintf("%s-%s", sc.serverURI, sc.selectedCreds.String())
Expand Down Expand Up @@ -361,7 +366,7 @@ type Config struct {

// XDSServers returns the top-level list of management servers to connect to,
// ordered by priority.
func (c *Config) XDSServers() []*ServerConfig {
func (c *Config) XDSServers() ServerConfigs {
return c.xDSServers
}

Expand Down Expand Up @@ -608,8 +613,9 @@ func newConfigFromContents(data []byte) (*Config, error) {
//
// # Testing-Only
type ConfigOptionsForTesting struct {
// Servers is the top-level xDS server configuration
Servers []json.RawMessage
// Servers is the top-level xDS server configuration. It contains a list of
// server configurations.
Servers json.RawMessage
// CertificateProviders is the certificate providers configuration.
CertificateProviders map[string]json.RawMessage
// ServerListenerResourceNameTemplate is the listener resource name template
Expand All @@ -630,13 +636,9 @@ type ConfigOptionsForTesting struct {
//
// # Testing-Only
func NewContentsForTesting(opts ConfigOptionsForTesting) ([]byte, error) {
var servers []*ServerConfig
for _, serverCfgJSON := range opts.Servers {
server := &ServerConfig{}
if err := server.UnmarshalJSON(serverCfgJSON); err != nil {
return nil, err
}
servers = append(servers, server)
var servers ServerConfigs
if err := json.Unmarshal(opts.Servers, &servers); err != nil {
return nil, err
}
certProviders := make(map[string]certproviderNameAndConfig)
for k, v := range opts.CertificateProviders {
Expand Down
9 changes: 4 additions & 5 deletions test/xds/xds_client_certificate_providers_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@ package xds_test
import (
"context"
"crypto/tls"
"encoding/json"
"fmt"
"strings"
"testing"
Expand Down Expand Up @@ -119,10 +118,10 @@ func (s) TestClientSideXDS_WithNoCertificateProvidersInBootstrap_Failure(t *test
// with no certificate providers.
nodeID := uuid.New().String()
bc, err := bootstrap.NewContentsForTesting(bootstrap.ConfigOptionsForTesting{
Servers: []json.RawMessage{[]byte(fmt.Sprintf(`{
"server_uri": %q,
"channel_creds": [{"type": "insecure"}]
}`, mgmtServer.Address))},
Servers: []byte(fmt.Sprintf(`[{
"server_uri": %q,
"channel_creds": [{"type": "insecure"}]
}]`, mgmtServer.Address)),
Node: []byte(fmt.Sprintf(`{"id": "%s"}`, nodeID)),
})
if err != nil {
Expand Down
8 changes: 4 additions & 4 deletions test/xds/xds_client_federation_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -65,10 +65,10 @@ func (s) TestClientSideFederation(t *testing.T) {
// Create a bootstrap file in a temporary directory.
nodeID := uuid.New().String()
bootstrapContents, err := bootstrap.NewContentsForTesting(bootstrap.ConfigOptionsForTesting{
Servers: []json.RawMessage{[]byte(fmt.Sprintf(`{
Servers: []byte(fmt.Sprintf(`[{
"server_uri": %q,
"channel_creds": [{"type": "insecure"}]
}`, serverDefaultAuth.Address))},
}]`, serverDefaultAuth.Address)),
Node: []byte(fmt.Sprintf(`{"id": "%s"}`, nodeID)),
ServerListenerResourceNameTemplate: e2e.ServerListenerResourceNameTemplate,
// Specify the address of the non-default authority.
Expand Down Expand Up @@ -159,10 +159,10 @@ func (s) TestClientSideFederationWithOnlyXDSTPStyleLDS(t *testing.T) {
// Create a bootstrap file in a temporary directory.
nodeID := uuid.New().String()
bootstrapContents, err := bootstrap.NewContentsForTesting(bootstrap.ConfigOptionsForTesting{
Servers: []json.RawMessage{[]byte(fmt.Sprintf(`{
Servers: []byte(fmt.Sprintf(`[{
"server_uri": %q,
"channel_creds": [{"type": "insecure"}]
}`, mgmtServer.Address))},
}]`, mgmtServer.Address)),
Node: []byte(fmt.Sprintf(`{"id": "%s"}`, nodeID)),
ClientDefaultListenerResourceNameTemplate: fmt.Sprintf("xdstp://%s/envoy.config.listener.v3.Listener/%%s", authority),
// Specify the address of the non-default authority.
Expand Down
12 changes: 6 additions & 6 deletions test/xds/xds_client_ignore_resource_deletion_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -260,22 +260,22 @@ func testResourceDeletionNotIgnored(t *testing.T, initialResource func(string) e
// This helper generates a custom bootstrap config for the test.
func generateBootstrapContents(t *testing.T, serverURI string, ignoreResourceDeletion bool, nodeID string) []byte {
t.Helper()
var serverCfg json.RawMessage
var serverCfgs json.RawMessage
if ignoreResourceDeletion {
serverCfg = []byte(fmt.Sprintf(`{
serverCfgs = []byte(fmt.Sprintf(`[{
"server_uri": %q,
"channel_creds": [{"type": "insecure"}],
"server_features": ["ignore_resource_deletion"]
}`, serverURI))
}]`, serverURI))
} else {
serverCfg = []byte(fmt.Sprintf(`{
serverCfgs = []byte(fmt.Sprintf(`[{
"server_uri": %q,
"channel_creds": [{"type": "insecure"}]
}`, serverURI))
}]`, serverURI))

}
bootstrapContents, err := bootstrap.NewContentsForTesting(bootstrap.ConfigOptionsForTesting{
Servers: []json.RawMessage{serverCfg},
Servers: serverCfgs,
Node: []byte(fmt.Sprintf(`{"id": "%s"}`, nodeID)),
ServerListenerResourceNameTemplate: e2e.ServerListenerResourceNameTemplate,
})
Expand Down
5 changes: 2 additions & 3 deletions test/xds/xds_server_certificate_providers_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@ package xds_test

import (
"context"
"encoding/json"
"fmt"
"net"
"strconv"
Expand Down Expand Up @@ -131,10 +130,10 @@ func (s) TestServerSideXDS_WithNoCertificateProvidersInBootstrap_Failure(t *test
// Create bootstrap configuration with no certificate providers.
nodeID := uuid.New().String()
bs, err := bootstrap.NewContentsForTesting(bootstrap.ConfigOptionsForTesting{
Servers: []json.RawMessage{[]byte(fmt.Sprintf(`{
Servers: []byte(fmt.Sprintf(`[{
"server_uri": %q,
"channel_creds": [{"type": "insecure"}]
}`, mgmtServer.Address))},
}]`, mgmtServer.Address)),
Node: []byte(fmt.Sprintf(`{"id": "%s"}`, nodeID)),
ServerListenerResourceNameTemplate: e2e.ServerListenerResourceNameTemplate,
})
Expand Down
30 changes: 15 additions & 15 deletions xds/googledirectpath/googlec2p_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -173,11 +173,11 @@ func (s) TestBuildXDS(t *testing.T) {
{
desc: "ipv6 false",
wantBootstrapConfig: bootstrapConfig(t, bootstrap.ConfigOptionsForTesting{
Servers: []json.RawMessage{[]byte(`{
"server_uri": "dns:///directpath-pa.googleapis.com",
"channel_creds": [{"type": "google_default"}],
"server_features": ["ignore_resource_deletion"]
}`)},
Servers: []byte(`[{
"server_uri": "dns:///directpath-pa.googleapis.com",
"channel_creds": [{"type": "google_default"}],
"server_features": ["ignore_resource_deletion"]
}]`),
Authorities: map[string]json.RawMessage{
"traffic-director-c2p.xds.googleapis.com": []byte(`{
"xds_servers": [
Expand All @@ -199,11 +199,11 @@ func (s) TestBuildXDS(t *testing.T) {
desc: "ipv6 true",
ipv6Capable: true,
wantBootstrapConfig: bootstrapConfig(t, bootstrap.ConfigOptionsForTesting{
Servers: []json.RawMessage{[]byte(`{
"server_uri": "dns:///directpath-pa.googleapis.com",
"channel_creds": [{"type": "google_default"}],
"server_features": ["ignore_resource_deletion"]
}`)},
Servers: []byte(`[{
"server_uri": "dns:///directpath-pa.googleapis.com",
"channel_creds": [{"type": "google_default"}],
"server_features": ["ignore_resource_deletion"]
}]`),
Authorities: map[string]json.RawMessage{
"traffic-director-c2p.xds.googleapis.com": []byte(`{
"xds_servers": [
Expand All @@ -229,11 +229,11 @@ func (s) TestBuildXDS(t *testing.T) {
ipv6Capable: true,
tdURIOverride: "test-uri",
wantBootstrapConfig: bootstrapConfig(t, bootstrap.ConfigOptionsForTesting{
Servers: []json.RawMessage{[]byte(`{
"server_uri": "test-uri",
"channel_creds": [{"type": "google_default"}],
"server_features": ["ignore_resource_deletion"]
}`)},
Servers: []byte(`[{
"server_uri": "test-uri",
"channel_creds": [{"type": "google_default"}],
"server_features": ["ignore_resource_deletion"]
}]`),
Authorities: map[string]json.RawMessage{
"traffic-director-c2p.xds.googleapis.com": []byte(`{
"xds_servers": [
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -375,10 +375,10 @@ func (s) TestSecurityConfigNotFoundInBootstrap(t *testing.T) {
// and one that does not have certificate providers configuration.
nodeID := uuid.New().String()
bootstrapContents, err := bootstrap.NewContentsForTesting(bootstrap.ConfigOptionsForTesting{
Servers: []json.RawMessage{[]byte(fmt.Sprintf(`{
Servers: []byte(fmt.Sprintf(`[{
"server_uri": %q,
"channel_creds": [{"type": "insecure"}]
}`, mgmtServer.Address))},
}]`, mgmtServer.Address)),
Node: []byte(fmt.Sprintf(`{"id": "%s"}`, nodeID)),
ServerListenerResourceNameTemplate: e2e.ServerListenerResourceNameTemplate,
})
Expand Down Expand Up @@ -441,10 +441,10 @@ func (s) TestCertproviderStoreError(t *testing.T) {
"config": {}
}`, errCertProviderName))
bootstrapContents, err := bootstrap.NewContentsForTesting(bootstrap.ConfigOptionsForTesting{
Servers: []json.RawMessage{[]byte(fmt.Sprintf(`{
Servers: []byte(fmt.Sprintf(`[{
"server_uri": %q,
"channel_creds": [{"type": "insecure"}]
}`, mgmtServer.Address))},
}]`, mgmtServer.Address)),
Node: []byte(fmt.Sprintf(`{"id": "%s"}`, nodeID)),
ServerListenerResourceNameTemplate: e2e.ServerListenerResourceNameTemplate,
CertificateProviders: map[string]json.RawMessage{e2e.ClientSideCertProviderInstance: providerCfg},
Expand Down
4 changes: 2 additions & 2 deletions xds/internal/resolver/xds_resolver_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -161,10 +161,10 @@ func (s) TestResolverResourceName(t *testing.T) {

// Create a bootstrap configuration with test options.
opts := bootstrap.ConfigOptionsForTesting{
Servers: []json.RawMessage{[]byte(fmt.Sprintf(`{
Servers: []byte(fmt.Sprintf(`[{
"server_uri": %q,
"channel_creds": [{"type": "insecure"}]
}`, mgmtServer.Address))},
}]`, mgmtServer.Address)),
ClientDefaultListenerResourceNameTemplate: tt.listenerResourceNameTemplate,
Node: []byte(fmt.Sprintf(`{"id": "%s"}`, nodeID)),
}
Expand Down
4 changes: 2 additions & 2 deletions xds/internal/xdsclient/tests/authority_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -83,10 +83,10 @@ func setupForAuthorityTests(ctx context.Context, t *testing.T, idleTimeout time.
// config, which points to the above management server.
nodeID := uuid.New().String()
bootstrapContents, err := bootstrap.NewContentsForTesting(bootstrap.ConfigOptionsForTesting{
Servers: []json.RawMessage{[]byte(fmt.Sprintf(`{
Servers: []byte(fmt.Sprintf(`[{
"server_uri": %q,
"channel_creds": [{"type": "insecure"}]
}`, defaultAuthorityServer.Address))},
}]`, defaultAuthorityServer.Address)),
Node: []byte(fmt.Sprintf(`{"id": "%s"}`, nodeID)),
Authorities: map[string]json.RawMessage{
testAuthority1: []byte(`{}`),
Expand Down
40 changes: 20 additions & 20 deletions xds/internal/xdsclient/tests/cds_watchers_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -189,10 +189,10 @@ func (s) TestCDSWatch(t *testing.T) {

nodeID := uuid.New().String()
bc, err := bootstrap.NewContentsForTesting(bootstrap.ConfigOptionsForTesting{
Servers: []json.RawMessage{[]byte(fmt.Sprintf(`{
Servers: []byte(fmt.Sprintf(`[{
"server_uri": %q,
"channel_creds": [{"type": "insecure"}]
}`, mgmtServer.Address))},
}]`, mgmtServer.Address)),
Node: []byte(fmt.Sprintf(`{"id": "%s"}`, nodeID)),
Authorities: map[string]json.RawMessage{
// Xdstp resource names used in this test do not specify an
Expand Down Expand Up @@ -339,10 +339,10 @@ func (s) TestCDSWatch_TwoWatchesForSameResourceName(t *testing.T) {

nodeID := uuid.New().String()
bc, err := bootstrap.NewContentsForTesting(bootstrap.ConfigOptionsForTesting{
Servers: []json.RawMessage{[]byte(fmt.Sprintf(`{
Servers: []byte(fmt.Sprintf(`[{
"server_uri": %q,
"channel_creds": [{"type": "insecure"}]
}`, mgmtServer.Address))},
}]`, mgmtServer.Address)),
Node: []byte(fmt.Sprintf(`{"id": "%s"}`, nodeID)),
Authorities: map[string]json.RawMessage{
// Xdstp resource names used in this test do not specify an
Expand Down Expand Up @@ -443,10 +443,10 @@ func (s) TestCDSWatch_ThreeWatchesForDifferentResourceNames(t *testing.T) {
nodeID := uuid.New().String()
authority := makeAuthorityName(t.Name())
bc, err := bootstrap.NewContentsForTesting(bootstrap.ConfigOptionsForTesting{
Servers: []json.RawMessage{[]byte(fmt.Sprintf(`{
"server_uri": %q,
"channel_creds": [{"type": "insecure"}]
}`, mgmtServer.Address))},
Servers: []byte(fmt.Sprintf(`[{
"server_uri": %q,
"channel_creds": [{"type": "insecure"}]
}]`, mgmtServer.Address)),
Node: []byte(fmt.Sprintf(`{"id": "%s"}`, nodeID)),
Authorities: map[string]json.RawMessage{
// Xdstp style resource names used in this test use a slash removed
Expand Down Expand Up @@ -736,10 +736,10 @@ func (s) TestCDSWatch_ResourceRemoved(t *testing.T) {
nodeID := uuid.New().String()
authority := makeAuthorityName(t.Name())
bc, err := bootstrap.NewContentsForTesting(bootstrap.ConfigOptionsForTesting{
Servers: []json.RawMessage{[]byte(fmt.Sprintf(`{
"server_uri": %q,
"channel_creds": [{"type": "insecure"}]
}`, mgmtServer.Address))},
Servers: []byte(fmt.Sprintf(`[{
"server_uri": %q,
"channel_creds": [{"type": "insecure"}]
}]`, mgmtServer.Address)),
Node: []byte(fmt.Sprintf(`{"id": "%s"}`, nodeID)),
Authorities: map[string]json.RawMessage{
// Xdstp style resource names used in this test use a slash removed
Expand Down Expand Up @@ -917,10 +917,10 @@ func (s) TestCDSWatch_PartialValid(t *testing.T) {
nodeID := uuid.New().String()
authority := makeAuthorityName(t.Name())
bc, err := bootstrap.NewContentsForTesting(bootstrap.ConfigOptionsForTesting{
Servers: []json.RawMessage{[]byte(fmt.Sprintf(`{
"server_uri": %q,
"channel_creds": [{"type": "insecure"}]
}`, mgmtServer.Address))},
Servers: []byte(fmt.Sprintf(`[{
"server_uri": %q,
"channel_creds": [{"type": "insecure"}]
}]`, mgmtServer.Address)),
Node: []byte(fmt.Sprintf(`{"id": "%s"}`, nodeID)),
Authorities: map[string]json.RawMessage{
// Xdstp style resource names used in this test use a slash removed
Expand Down Expand Up @@ -1009,10 +1009,10 @@ func (s) TestCDSWatch_PartialResponse(t *testing.T) {
nodeID := uuid.New().String()
authority := makeAuthorityName(t.Name())
bc, err := bootstrap.NewContentsForTesting(bootstrap.ConfigOptionsForTesting{
Servers: []json.RawMessage{[]byte(fmt.Sprintf(`{
"server_uri": %q,
"channel_creds": [{"type": "insecure"}]
}`, mgmtServer.Address))},
Servers: []byte(fmt.Sprintf(`[{
"server_uri": %q,
"channel_creds": [{"type": "insecure"}]
}]`, mgmtServer.Address)),
Node: []byte(fmt.Sprintf(`{"id": "%s"}`, nodeID)),
Authorities: map[string]json.RawMessage{
// Xdstp style resource names used in this test use a slash removed
Expand Down
8 changes: 4 additions & 4 deletions xds/internal/xdsclient/tests/dump_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -389,10 +389,10 @@ func (s) TestDumpResources_ManyToMany(t *testing.T) {
// server corresponding to the test authority.
nodeID := uuid.New().String()
bc, err := bootstrap.NewContentsForTesting(bootstrap.ConfigOptionsForTesting{
Servers: []json.RawMessage{[]byte(fmt.Sprintf(`{
"server_uri": %q,
"channel_creds": [{"type": "insecure"}]
}`, mgmtServer1.Address))},
Servers: []byte(fmt.Sprintf(`[{
"server_uri": %q,
"channel_creds": [{"type": "insecure"}]
}]`, mgmtServer1.Address)),
Node: []byte(fmt.Sprintf(`{"id": "%s"}`, nodeID)),
Authorities: map[string]json.RawMessage{
authority: []byte(fmt.Sprintf(`{
Expand Down
Loading

0 comments on commit 7e12068

Please sign in to comment.