Skip to content

Commit d2629bd

Browse files
authored
xds/bootstrap: Add support for grpc_server_resource_name_id. (#4030)
1 parent 6caf9d8 commit d2629bd

File tree

2 files changed

+102
-12
lines changed

2 files changed

+102
-12
lines changed

xds/internal/client/bootstrap/bootstrap.go

+21-12
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,10 @@ type Config struct {
7878
// CertProviderConfigs contains a mapping from certificate provider plugin
7979
// instance names to parsed buildable configs.
8080
CertProviderConfigs map[string]*certprovider.BuildableConfig
81+
// ServerResourceNameID contains the value to be used as the id in the
82+
// resource name used to fetch the Listener resource on the xDS-enabled gRPC
83+
// server.
84+
ServerResourceNameID string
8185
}
8286

8387
type channelCreds struct {
@@ -103,19 +107,20 @@ type xdsServer struct {
103107
// "config": <JSON object containing config for the type>
104108
// }
105109
// ],
106-
// "server_features": [ ... ]
107-
// "certificate_providers" : {
108-
// "default": {
109-
// "plugin_name": "default-plugin-name",
110-
// "config": { default plugin config in JSON }
111-
// },
112-
// "foo": {
113-
// "plugin_name": "foo",
114-
// "config": { foo plugin config in JSON }
115-
// }
116-
// }
117110
// },
118-
// "node": <JSON form of Node proto>
111+
// "node": <JSON form of Node proto>,
112+
// "server_features": [ ... ],
113+
// "certificate_providers" : {
114+
// "default": {
115+
// "plugin_name": "default-plugin-name",
116+
// "config": { default plugin config in JSON }
117+
// },
118+
// "foo": {
119+
// "plugin_name": "foo",
120+
// "config": { foo plugin config in JSON }
121+
// }
122+
// },
123+
// "grpc_server_resource_name_id": "grpc/server"
119124
// }
120125
//
121126
// Currently, we support exactly one type of credential, which is
@@ -222,6 +227,10 @@ func NewConfig() (*Config, error) {
222227
configs[instance] = bc
223228
}
224229
config.CertProviderConfigs = configs
230+
case "grpc_server_resource_name_id":
231+
if err := json.Unmarshal(v, &config.ServerResourceNameID); err != nil {
232+
return nil, fmt.Errorf("xds: json.Unmarshal(%v) for field %q failed during bootstrap: %v", string(v), k, err)
233+
}
225234
}
226235
// Do not fail the xDS bootstrap when an unknown field is seen. This can
227236
// happen when an older version client reads a newer version bootstrap

xds/internal/client/bootstrap/bootstrap_test.go

+81
Original file line numberDiff line numberDiff line change
@@ -239,6 +239,9 @@ func (c *Config) compare(want *Config) error {
239239
if diff := cmp.Diff(want.NodeProto, c.NodeProto, cmp.Comparer(proto.Equal)); diff != "" {
240240
return fmt.Errorf("config.NodeProto diff (-want, +got):\n%s", diff)
241241
}
242+
if c.ServerResourceNameID != want.ServerResourceNameID {
243+
return fmt.Errorf("config.ServerResourceNameID is %q, want %q", c.ServerResourceNameID, want.ServerResourceNameID)
244+
}
242245

243246
// A vanilla cmp.Equal or cmp.Diff will not produce useful error message
244247
// here. So, we iterate through the list of configs and compare them one at
@@ -699,3 +702,81 @@ func TestNewConfigWithCertificateProviders(t *testing.T) {
699702
})
700703
}
701704
}
705+
706+
func TestNewConfigWithServerResourceNameID(t *testing.T) {
707+
cancel := setupBootstrapOverride(map[string]string{
708+
"badServerResourceNameID": `
709+
{
710+
"node": {
711+
"id": "ENVOY_NODE_ID",
712+
"metadata": {
713+
"TRAFFICDIRECTOR_GRPC_HOSTNAME": "trafficdirector"
714+
}
715+
},
716+
"xds_servers" : [{
717+
"server_uri": "trafficdirector.googleapis.com:443",
718+
"channel_creds": [
719+
{ "type": "google_default" }
720+
]
721+
}],
722+
"grpc_server_resource_name_id": 123456789
723+
}`,
724+
"goodServerResourceNameID": `
725+
{
726+
"node": {
727+
"id": "ENVOY_NODE_ID",
728+
"metadata": {
729+
"TRAFFICDIRECTOR_GRPC_HOSTNAME": "trafficdirector"
730+
}
731+
},
732+
"xds_servers" : [{
733+
"server_uri": "trafficdirector.googleapis.com:443",
734+
"channel_creds": [
735+
{ "type": "google_default" }
736+
]
737+
}],
738+
"grpc_server_resource_name_id": "grpc/server"
739+
}`,
740+
})
741+
defer cancel()
742+
743+
tests := []struct {
744+
name string
745+
wantConfig *Config
746+
wantErr bool
747+
}{
748+
{
749+
name: "badServerResourceNameID",
750+
wantErr: true,
751+
},
752+
{
753+
name: "goodServerResourceNameID",
754+
wantConfig: &Config{
755+
BalancerName: "trafficdirector.googleapis.com:443",
756+
Creds: grpc.WithCredentialsBundle(google.NewComputeEngineCredentials()),
757+
TransportAPI: version.TransportV2,
758+
NodeProto: v2NodeProto,
759+
ServerResourceNameID: "grpc/server",
760+
},
761+
},
762+
}
763+
764+
for _, test := range tests {
765+
t.Run(test.name, func(t *testing.T) {
766+
origBootstrapFileName := env.BootstrapFileName
767+
env.BootstrapFileName = test.name
768+
defer func() { env.BootstrapFileName = origBootstrapFileName }()
769+
770+
c, err := NewConfig()
771+
if (err != nil) != test.wantErr {
772+
t.Fatalf("NewConfig() returned (%+v, %v), wantErr: %v", c, err, test.wantErr)
773+
}
774+
if test.wantErr {
775+
return
776+
}
777+
if err := c.compare(test.wantConfig); err != nil {
778+
t.Fatal(err)
779+
}
780+
})
781+
}
782+
}

0 commit comments

Comments
 (0)