Skip to content
This repository was archived by the owner on Jul 11, 2023. It is now read-only.

Commit 3bf989a

Browse files
authored
Refactor Envoy bootstrap from BuildFromConfig() to Builder{}.Build() + health probe tests (#4858)
* leverage builder pattern for building the bootstrap config Signed-off-by: Sean Teeling <[email protected]> * address comments Signed-off-by: Sean Teeling <[email protected]>
1 parent c8d7559 commit 3bf989a

File tree

4 files changed

+234
-230
lines changed

4 files changed

+234
-230
lines changed

pkg/envoy/bootstrap/config.go

+27-49
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@ import (
1616
"github.com/golang/protobuf/ptypes/any"
1717
"google.golang.org/protobuf/types/known/anypb"
1818

19-
"github.com/openservicemesh/osm/pkg/configurator"
2019
"github.com/openservicemesh/osm/pkg/constants"
2120
"github.com/openservicemesh/osm/pkg/envoy"
2221
"github.com/openservicemesh/osm/pkg/errcode"
@@ -115,8 +114,8 @@ func BuildValidationSecret() (*xds_discovery.DiscoveryResponse, error) {
115114
}, nil
116115
}
117116

118-
// BuildFromConfig builds and returns an Envoy Bootstrap object from the given config
119-
func BuildFromConfig(config Config) (*xds_bootstrap.Bootstrap, error) {
117+
// Build builds and returns an Envoy Bootstrap object from the given config
118+
func (b *Builder) Build() (*xds_bootstrap.Bootstrap, error) {
120119
httpProtocolOptions := &xds_upstream_http.HttpProtocolOptions{
121120
UpstreamProtocolOptions: &xds_upstream_http.HttpProtocolOptions_ExplicitHttpConfig_{
122121
ExplicitHttpConfig: &xds_upstream_http.HttpProtocolOptions_ExplicitHttpConfig{
@@ -139,8 +138,8 @@ func BuildFromConfig(config Config) (*xds_bootstrap.Bootstrap, error) {
139138
return nil, err
140139
}
141140

142-
minVersionInt := xds_transport_sockets.TlsParameters_TlsProtocol_value[config.TLSMinProtocolVersion]
143-
maxVersionInt := xds_transport_sockets.TlsParameters_TlsProtocol_value[config.TLSMaxProtocolVersion]
141+
minVersionInt := xds_transport_sockets.TlsParameters_TlsProtocol_value[b.TLSMinProtocolVersion]
142+
maxVersionInt := xds_transport_sockets.TlsParameters_TlsProtocol_value[b.TLSMaxProtocolVersion]
144143
tlsMinVersion := xds_transport_sockets.TlsParameters_TlsProtocol(minVersionInt)
145144
tlsMaxVersion := xds_transport_sockets.TlsParameters_TlsProtocol(maxVersionInt)
146145

@@ -162,8 +161,8 @@ func BuildFromConfig(config Config) (*xds_bootstrap.Bootstrap, error) {
162161
TlsParams: &xds_transport_sockets.TlsParameters{
163162
TlsMinimumProtocolVersion: tlsMinVersion,
164163
TlsMaximumProtocolVersion: tlsMaxVersion,
165-
CipherSuites: config.CipherSuites,
166-
EcdhCurves: config.ECDHCurves,
164+
CipherSuites: b.CipherSuites,
165+
EcdhCurves: b.ECDHCurves,
167166
},
168167
TlsCertificateSdsSecretConfigs: []*xds_transport_sockets.SdsSecretConfig{
169168
{
@@ -186,7 +185,7 @@ func BuildFromConfig(config Config) (*xds_bootstrap.Bootstrap, error) {
186185

187186
bootstrap := &xds_bootstrap.Bootstrap{
188187
Node: &xds_core.Node{
189-
Id: config.NodeID,
188+
Id: b.NodeID,
190189
},
191190
Admin: &xds_bootstrap.Admin{
192191
AccessLog: []*xds_accesslog_config.AccessLog{
@@ -202,7 +201,7 @@ func BuildFromConfig(config Config) (*xds_bootstrap.Bootstrap, error) {
202201
SocketAddress: &xds_core.SocketAddress{
203202
Address: constants.LocalhostIPAddress,
204203
PortSpecifier: &xds_core.SocketAddress_PortValue{
205-
PortValue: config.AdminPort,
204+
PortValue: constants.EnvoyAdminPort,
206205
},
207206
},
208207
},
@@ -216,7 +215,7 @@ func BuildFromConfig(config Config) (*xds_bootstrap.Bootstrap, error) {
216215
{
217216
TargetSpecifier: &xds_core.GrpcService_EnvoyGrpc_{
218217
EnvoyGrpc: &xds_core.GrpcService_EnvoyGrpc{
219-
ClusterName: config.XDSClusterName,
218+
ClusterName: constants.OSMControllerName,
220219
},
221220
},
222221
},
@@ -239,7 +238,7 @@ func BuildFromConfig(config Config) (*xds_bootstrap.Bootstrap, error) {
239238
StaticResources: &xds_bootstrap.Bootstrap_StaticResources{
240239
Clusters: []*xds_cluster.Cluster{
241240
{
242-
Name: config.XDSClusterName,
241+
Name: constants.OSMControllerName,
243242
ClusterDiscoveryType: &xds_cluster.Cluster_Type{
244243
Type: xds_cluster.Cluster_LOGICAL_DNS,
245244
},
@@ -254,7 +253,7 @@ func BuildFromConfig(config Config) (*xds_bootstrap.Bootstrap, error) {
254253
},
255254
LbPolicy: xds_cluster.Cluster_ROUND_ROBIN,
256255
LoadAssignment: &xds_endpoint.ClusterLoadAssignment{
257-
ClusterName: config.XDSClusterName,
256+
ClusterName: constants.OSMControllerName,
258257
Endpoints: []*xds_endpoint.LocalityLbEndpoints{
259258
{
260259
LbEndpoints: []*xds_endpoint.LbEndpoint{
@@ -264,9 +263,9 @@ func BuildFromConfig(config Config) (*xds_bootstrap.Bootstrap, error) {
264263
Address: &xds_core.Address{
265264
Address: &xds_core.Address_SocketAddress{
266265
SocketAddress: &xds_core.SocketAddress{
267-
Address: config.XDSHost,
266+
Address: b.XDSHost,
268267
PortSpecifier: &xds_core.SocketAddress_PortValue{
269-
PortValue: config.XDSPort,
268+
PortValue: constants.ADSServerPort,
270269
},
271270
},
272271
},
@@ -283,35 +282,14 @@ func BuildFromConfig(config Config) (*xds_bootstrap.Bootstrap, error) {
283282
},
284283
}
285284

286-
return bootstrap, nil
287-
}
288-
289-
// GenerateEnvoyConfig generates an envoy bootstrap configuration from the given metadata.
290-
func GenerateEnvoyConfig(config EnvoyBootstrapConfigMeta, cfg configurator.Configurator) (*xds_bootstrap.Bootstrap, error) {
291-
bootstrapConfig, err := BuildFromConfig(Config{
292-
NodeID: config.NodeID,
293-
AdminPort: constants.EnvoyAdminPort,
294-
XDSClusterName: constants.OSMControllerName,
295-
XDSHost: config.XDSHost,
296-
XDSPort: config.XDSPort,
297-
TLSMinProtocolVersion: config.TLSMinProtocolVersion,
298-
TLSMaxProtocolVersion: config.TLSMaxProtocolVersion,
299-
CipherSuites: config.CipherSuites,
300-
ECDHCurves: config.ECDHCurves,
301-
})
302-
if err != nil {
303-
log.Error().Err(err).Msgf("Error building Envoy boostrap config")
304-
return nil, err
305-
}
306-
307-
probeListeners, probeClusters, err := getProbeResources(config)
285+
probeListeners, probeClusters, err := b.getProbeResources()
308286
if err != nil {
309287
return nil, err
310288
}
311-
bootstrapConfig.StaticResources.Listeners = append(bootstrapConfig.StaticResources.Listeners, probeListeners...)
312-
bootstrapConfig.StaticResources.Clusters = append(bootstrapConfig.StaticResources.Clusters, probeClusters...)
289+
bootstrap.StaticResources.Listeners = append(bootstrap.StaticResources.Listeners, probeListeners...)
290+
bootstrap.StaticResources.Clusters = append(bootstrap.StaticResources.Clusters, probeClusters...)
313291

314-
return bootstrapConfig, nil
292+
return bootstrap, nil
315293
}
316294

317295
// GetTLSSDSConfigYAML returns the statically used TLS SDS config YAML.
@@ -353,42 +331,42 @@ func GetValidationContextSDSConfigYAML() ([]byte, error) {
353331
// These will not change during the lifetime of the Pod.
354332
// If the original probe defined a TCPSocket action, listener and cluster objects are not configured
355333
// to serve that probe.
356-
func getProbeResources(config EnvoyBootstrapConfigMeta) ([]*xds_listener.Listener, []*xds_cluster.Cluster, error) {
334+
func (b *Builder) getProbeResources() ([]*xds_listener.Listener, []*xds_cluster.Cluster, error) {
357335
// This slice is the list of listeners for liveness, readiness, startup IF these have been configured in the Pod Spec
358336
var listeners []*xds_listener.Listener
359337
var clusters []*xds_cluster.Cluster
360338

361339
// Is there a liveness probe in the Pod Spec?
362-
if config.OriginalHealthProbes.Liveness != nil && !config.OriginalHealthProbes.Liveness.IsTCPSocket {
363-
listener, err := getLivenessListener(config.OriginalHealthProbes.Liveness)
340+
if b.OriginalHealthProbes.Liveness != nil && !b.OriginalHealthProbes.Liveness.IsTCPSocket {
341+
listener, err := getLivenessListener(b.OriginalHealthProbes.Liveness)
364342
if err != nil {
365343
log.Error().Err(err).Msgf("Error getting liveness listener")
366344
return nil, nil, err
367345
}
368346
listeners = append(listeners, listener)
369-
clusters = append(clusters, getLivenessCluster(config.OriginalHealthProbes.Liveness))
347+
clusters = append(clusters, getLivenessCluster(b.OriginalHealthProbes.Liveness))
370348
}
371349

372350
// Is there a readiness probe in the Pod Spec?
373-
if config.OriginalHealthProbes.Readiness != nil && !config.OriginalHealthProbes.Readiness.IsTCPSocket {
374-
listener, err := getReadinessListener(config.OriginalHealthProbes.Readiness)
351+
if b.OriginalHealthProbes.Readiness != nil && !b.OriginalHealthProbes.Readiness.IsTCPSocket {
352+
listener, err := getReadinessListener(b.OriginalHealthProbes.Readiness)
375353
if err != nil {
376354
log.Error().Err(err).Msgf("Error getting readiness listener")
377355
return nil, nil, err
378356
}
379357
listeners = append(listeners, listener)
380-
clusters = append(clusters, getReadinessCluster(config.OriginalHealthProbes.Readiness))
358+
clusters = append(clusters, getReadinessCluster(b.OriginalHealthProbes.Readiness))
381359
}
382360

383361
// Is there a startup probe in the Pod Spec?
384-
if config.OriginalHealthProbes.Startup != nil && !config.OriginalHealthProbes.Startup.IsTCPSocket {
385-
listener, err := getStartupListener(config.OriginalHealthProbes.Startup)
362+
if b.OriginalHealthProbes.Startup != nil && !b.OriginalHealthProbes.Startup.IsTCPSocket {
363+
listener, err := getStartupListener(b.OriginalHealthProbes.Startup)
386364
if err != nil {
387365
log.Error().Err(err).Msgf("Error getting startup listener")
388366
return nil, nil, err
389367
}
390368
listeners = append(listeners, listener)
391-
clusters = append(clusters, getStartupCluster(config.OriginalHealthProbes.Startup))
369+
clusters = append(clusters, getStartupCluster(b.OriginalHealthProbes.Startup))
392370
}
393371

394372
return listeners, clusters, nil

0 commit comments

Comments
 (0)