Skip to content

Commit 7e12068

Browse files
authored
bootstrap: add String method to ServerConfigs type (#7537)
1 parent ee5cbce commit 7e12068

18 files changed

+141
-141
lines changed

internal/testutils/xds/e2e/bootstrap.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -72,10 +72,10 @@ func DefaultBootstrapContents(t *testing.T, nodeID, serverURI string) []byte {
7272

7373
// Create the bootstrap configuration.
7474
bs, err := bootstrap.NewContentsForTesting(bootstrap.ConfigOptionsForTesting{
75-
Servers: []json.RawMessage{[]byte(fmt.Sprintf(`{
75+
Servers: []byte(fmt.Sprintf(`[{
7676
"server_uri": "passthrough:///%s",
7777
"channel_creds": [{"type": "insecure"}]
78-
}`, serverURI))},
78+
}]`, serverURI)),
7979
Node: []byte(fmt.Sprintf(`{"id": "%s"}`, nodeID)),
8080
CertificateProviders: cpc,
8181
ServerListenerResourceNameTemplate: ServerListenerResourceNameTemplate,

internal/xds/bootstrap/bootstrap.go

Lines changed: 20 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -117,6 +117,19 @@ func (scs *ServerConfigs) UnmarshalJSON(data []byte) error {
117117
return nil
118118
}
119119

120+
// String returns a string representation of the ServerConfigs, by concatenating
121+
// the string representations of the underlying server configs.
122+
func (scs *ServerConfigs) String() string {
123+
ret := ""
124+
for i, sc := range *scs {
125+
if i > 0 {
126+
ret += ", "
127+
}
128+
ret += sc.String()
129+
}
130+
return ret
131+
}
132+
120133
// Authority contains configuration for an xDS control plane authority.
121134
//
122135
// This type does not implement custom JSON marshal/unmarshal logic because it
@@ -237,14 +250,6 @@ func (sc *ServerConfig) Equal(other *ServerConfig) bool {
237250
}
238251

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

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

@@ -608,8 +613,9 @@ func newConfigFromContents(data []byte) (*Config, error) {
608613
//
609614
// # Testing-Only
610615
type ConfigOptionsForTesting struct {
611-
// Servers is the top-level xDS server configuration
612-
Servers []json.RawMessage
616+
// Servers is the top-level xDS server configuration. It contains a list of
617+
// server configurations.
618+
Servers json.RawMessage
613619
// CertificateProviders is the certificate providers configuration.
614620
CertificateProviders map[string]json.RawMessage
615621
// ServerListenerResourceNameTemplate is the listener resource name template
@@ -630,13 +636,9 @@ type ConfigOptionsForTesting struct {
630636
//
631637
// # Testing-Only
632638
func NewContentsForTesting(opts ConfigOptionsForTesting) ([]byte, error) {
633-
var servers []*ServerConfig
634-
for _, serverCfgJSON := range opts.Servers {
635-
server := &ServerConfig{}
636-
if err := server.UnmarshalJSON(serverCfgJSON); err != nil {
637-
return nil, err
638-
}
639-
servers = append(servers, server)
639+
var servers ServerConfigs
640+
if err := json.Unmarshal(opts.Servers, &servers); err != nil {
641+
return nil, err
640642
}
641643
certProviders := make(map[string]certproviderNameAndConfig)
642644
for k, v := range opts.CertificateProviders {

test/xds/xds_client_certificate_providers_test.go

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,6 @@ package xds_test
2121
import (
2222
"context"
2323
"crypto/tls"
24-
"encoding/json"
2524
"fmt"
2625
"strings"
2726
"testing"
@@ -119,10 +118,10 @@ func (s) TestClientSideXDS_WithNoCertificateProvidersInBootstrap_Failure(t *test
119118
// with no certificate providers.
120119
nodeID := uuid.New().String()
121120
bc, err := bootstrap.NewContentsForTesting(bootstrap.ConfigOptionsForTesting{
122-
Servers: []json.RawMessage{[]byte(fmt.Sprintf(`{
123-
"server_uri": %q,
124-
"channel_creds": [{"type": "insecure"}]
125-
}`, mgmtServer.Address))},
121+
Servers: []byte(fmt.Sprintf(`[{
122+
"server_uri": %q,
123+
"channel_creds": [{"type": "insecure"}]
124+
}]`, mgmtServer.Address)),
126125
Node: []byte(fmt.Sprintf(`{"id": "%s"}`, nodeID)),
127126
})
128127
if err != nil {

test/xds/xds_client_federation_test.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -65,10 +65,10 @@ func (s) TestClientSideFederation(t *testing.T) {
6565
// Create a bootstrap file in a temporary directory.
6666
nodeID := uuid.New().String()
6767
bootstrapContents, err := bootstrap.NewContentsForTesting(bootstrap.ConfigOptionsForTesting{
68-
Servers: []json.RawMessage{[]byte(fmt.Sprintf(`{
68+
Servers: []byte(fmt.Sprintf(`[{
6969
"server_uri": %q,
7070
"channel_creds": [{"type": "insecure"}]
71-
}`, serverDefaultAuth.Address))},
71+
}]`, serverDefaultAuth.Address)),
7272
Node: []byte(fmt.Sprintf(`{"id": "%s"}`, nodeID)),
7373
ServerListenerResourceNameTemplate: e2e.ServerListenerResourceNameTemplate,
7474
// Specify the address of the non-default authority.
@@ -159,10 +159,10 @@ func (s) TestClientSideFederationWithOnlyXDSTPStyleLDS(t *testing.T) {
159159
// Create a bootstrap file in a temporary directory.
160160
nodeID := uuid.New().String()
161161
bootstrapContents, err := bootstrap.NewContentsForTesting(bootstrap.ConfigOptionsForTesting{
162-
Servers: []json.RawMessage{[]byte(fmt.Sprintf(`{
162+
Servers: []byte(fmt.Sprintf(`[{
163163
"server_uri": %q,
164164
"channel_creds": [{"type": "insecure"}]
165-
}`, mgmtServer.Address))},
165+
}]`, mgmtServer.Address)),
166166
Node: []byte(fmt.Sprintf(`{"id": "%s"}`, nodeID)),
167167
ClientDefaultListenerResourceNameTemplate: fmt.Sprintf("xdstp://%s/envoy.config.listener.v3.Listener/%%s", authority),
168168
// Specify the address of the non-default authority.

test/xds/xds_client_ignore_resource_deletion_test.go

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -260,22 +260,22 @@ func testResourceDeletionNotIgnored(t *testing.T, initialResource func(string) e
260260
// This helper generates a custom bootstrap config for the test.
261261
func generateBootstrapContents(t *testing.T, serverURI string, ignoreResourceDeletion bool, nodeID string) []byte {
262262
t.Helper()
263-
var serverCfg json.RawMessage
263+
var serverCfgs json.RawMessage
264264
if ignoreResourceDeletion {
265-
serverCfg = []byte(fmt.Sprintf(`{
265+
serverCfgs = []byte(fmt.Sprintf(`[{
266266
"server_uri": %q,
267267
"channel_creds": [{"type": "insecure"}],
268268
"server_features": ["ignore_resource_deletion"]
269-
}`, serverURI))
269+
}]`, serverURI))
270270
} else {
271-
serverCfg = []byte(fmt.Sprintf(`{
271+
serverCfgs = []byte(fmt.Sprintf(`[{
272272
"server_uri": %q,
273273
"channel_creds": [{"type": "insecure"}]
274-
}`, serverURI))
274+
}]`, serverURI))
275275

276276
}
277277
bootstrapContents, err := bootstrap.NewContentsForTesting(bootstrap.ConfigOptionsForTesting{
278-
Servers: []json.RawMessage{serverCfg},
278+
Servers: serverCfgs,
279279
Node: []byte(fmt.Sprintf(`{"id": "%s"}`, nodeID)),
280280
ServerListenerResourceNameTemplate: e2e.ServerListenerResourceNameTemplate,
281281
})

test/xds/xds_server_certificate_providers_test.go

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,6 @@ package xds_test
2020

2121
import (
2222
"context"
23-
"encoding/json"
2423
"fmt"
2524
"net"
2625
"strconv"
@@ -131,10 +130,10 @@ func (s) TestServerSideXDS_WithNoCertificateProvidersInBootstrap_Failure(t *test
131130
// Create bootstrap configuration with no certificate providers.
132131
nodeID := uuid.New().String()
133132
bs, err := bootstrap.NewContentsForTesting(bootstrap.ConfigOptionsForTesting{
134-
Servers: []json.RawMessage{[]byte(fmt.Sprintf(`{
133+
Servers: []byte(fmt.Sprintf(`[{
135134
"server_uri": %q,
136135
"channel_creds": [{"type": "insecure"}]
137-
}`, mgmtServer.Address))},
136+
}]`, mgmtServer.Address)),
138137
Node: []byte(fmt.Sprintf(`{"id": "%s"}`, nodeID)),
139138
ServerListenerResourceNameTemplate: e2e.ServerListenerResourceNameTemplate,
140139
})

xds/googledirectpath/googlec2p_test.go

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -173,11 +173,11 @@ func (s) TestBuildXDS(t *testing.T) {
173173
{
174174
desc: "ipv6 false",
175175
wantBootstrapConfig: bootstrapConfig(t, bootstrap.ConfigOptionsForTesting{
176-
Servers: []json.RawMessage{[]byte(`{
177-
"server_uri": "dns:///directpath-pa.googleapis.com",
178-
"channel_creds": [{"type": "google_default"}],
179-
"server_features": ["ignore_resource_deletion"]
180-
}`)},
176+
Servers: []byte(`[{
177+
"server_uri": "dns:///directpath-pa.googleapis.com",
178+
"channel_creds": [{"type": "google_default"}],
179+
"server_features": ["ignore_resource_deletion"]
180+
}]`),
181181
Authorities: map[string]json.RawMessage{
182182
"traffic-director-c2p.xds.googleapis.com": []byte(`{
183183
"xds_servers": [
@@ -199,11 +199,11 @@ func (s) TestBuildXDS(t *testing.T) {
199199
desc: "ipv6 true",
200200
ipv6Capable: true,
201201
wantBootstrapConfig: bootstrapConfig(t, bootstrap.ConfigOptionsForTesting{
202-
Servers: []json.RawMessage{[]byte(`{
203-
"server_uri": "dns:///directpath-pa.googleapis.com",
204-
"channel_creds": [{"type": "google_default"}],
205-
"server_features": ["ignore_resource_deletion"]
206-
}`)},
202+
Servers: []byte(`[{
203+
"server_uri": "dns:///directpath-pa.googleapis.com",
204+
"channel_creds": [{"type": "google_default"}],
205+
"server_features": ["ignore_resource_deletion"]
206+
}]`),
207207
Authorities: map[string]json.RawMessage{
208208
"traffic-director-c2p.xds.googleapis.com": []byte(`{
209209
"xds_servers": [
@@ -229,11 +229,11 @@ func (s) TestBuildXDS(t *testing.T) {
229229
ipv6Capable: true,
230230
tdURIOverride: "test-uri",
231231
wantBootstrapConfig: bootstrapConfig(t, bootstrap.ConfigOptionsForTesting{
232-
Servers: []json.RawMessage{[]byte(`{
233-
"server_uri": "test-uri",
234-
"channel_creds": [{"type": "google_default"}],
235-
"server_features": ["ignore_resource_deletion"]
236-
}`)},
232+
Servers: []byte(`[{
233+
"server_uri": "test-uri",
234+
"channel_creds": [{"type": "google_default"}],
235+
"server_features": ["ignore_resource_deletion"]
236+
}]`),
237237
Authorities: map[string]json.RawMessage{
238238
"traffic-director-c2p.xds.googleapis.com": []byte(`{
239239
"xds_servers": [

xds/internal/balancer/cdsbalancer/cdsbalancer_security_test.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -375,10 +375,10 @@ func (s) TestSecurityConfigNotFoundInBootstrap(t *testing.T) {
375375
// and one that does not have certificate providers configuration.
376376
nodeID := uuid.New().String()
377377
bootstrapContents, err := bootstrap.NewContentsForTesting(bootstrap.ConfigOptionsForTesting{
378-
Servers: []json.RawMessage{[]byte(fmt.Sprintf(`{
378+
Servers: []byte(fmt.Sprintf(`[{
379379
"server_uri": %q,
380380
"channel_creds": [{"type": "insecure"}]
381-
}`, mgmtServer.Address))},
381+
}]`, mgmtServer.Address)),
382382
Node: []byte(fmt.Sprintf(`{"id": "%s"}`, nodeID)),
383383
ServerListenerResourceNameTemplate: e2e.ServerListenerResourceNameTemplate,
384384
})
@@ -441,10 +441,10 @@ func (s) TestCertproviderStoreError(t *testing.T) {
441441
"config": {}
442442
}`, errCertProviderName))
443443
bootstrapContents, err := bootstrap.NewContentsForTesting(bootstrap.ConfigOptionsForTesting{
444-
Servers: []json.RawMessage{[]byte(fmt.Sprintf(`{
444+
Servers: []byte(fmt.Sprintf(`[{
445445
"server_uri": %q,
446446
"channel_creds": [{"type": "insecure"}]
447-
}`, mgmtServer.Address))},
447+
}]`, mgmtServer.Address)),
448448
Node: []byte(fmt.Sprintf(`{"id": "%s"}`, nodeID)),
449449
ServerListenerResourceNameTemplate: e2e.ServerListenerResourceNameTemplate,
450450
CertificateProviders: map[string]json.RawMessage{e2e.ClientSideCertProviderInstance: providerCfg},

xds/internal/resolver/xds_resolver_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -161,10 +161,10 @@ func (s) TestResolverResourceName(t *testing.T) {
161161

162162
// Create a bootstrap configuration with test options.
163163
opts := bootstrap.ConfigOptionsForTesting{
164-
Servers: []json.RawMessage{[]byte(fmt.Sprintf(`{
164+
Servers: []byte(fmt.Sprintf(`[{
165165
"server_uri": %q,
166166
"channel_creds": [{"type": "insecure"}]
167-
}`, mgmtServer.Address))},
167+
}]`, mgmtServer.Address)),
168168
ClientDefaultListenerResourceNameTemplate: tt.listenerResourceNameTemplate,
169169
Node: []byte(fmt.Sprintf(`{"id": "%s"}`, nodeID)),
170170
}

xds/internal/xdsclient/tests/authority_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -83,10 +83,10 @@ func setupForAuthorityTests(ctx context.Context, t *testing.T, idleTimeout time.
8383
// config, which points to the above management server.
8484
nodeID := uuid.New().String()
8585
bootstrapContents, err := bootstrap.NewContentsForTesting(bootstrap.ConfigOptionsForTesting{
86-
Servers: []json.RawMessage{[]byte(fmt.Sprintf(`{
86+
Servers: []byte(fmt.Sprintf(`[{
8787
"server_uri": %q,
8888
"channel_creds": [{"type": "insecure"}]
89-
}`, defaultAuthorityServer.Address))},
89+
}]`, defaultAuthorityServer.Address)),
9090
Node: []byte(fmt.Sprintf(`{"id": "%s"}`, nodeID)),
9191
Authorities: map[string]json.RawMessage{
9292
testAuthority1: []byte(`{}`),

0 commit comments

Comments
 (0)