-
Notifications
You must be signed in to change notification settings - Fork 4.9k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
core & ios: implement typed configurations & template (#328)
Introduces a `Configuration` type which consumers will use to start instances of `Envoy`. This change allows us to: - Provide an ergonomic interface for using Envoy Mobile without having to learn Envoy's configuration YAML structure - Utilize a solid set of default settings for this library - Prevent consumers from accessing features that are unsupported - Validate configuration at compile time rather than runtime This PR: - Adds a templated configuration in a `.cc` file, exposed through the common layer all the way through to the platform layers - Adds a `Configuration` type for accessing this configuration - Updates the iOS example apps to use the new typed configuration instead of their own yaml files - Maintains an existing initializer for exposing a string configuration for consumers who may still want this functionality Tests will be added in a separate PR that updates this interface further with a builder for `Envoy` instances. Resolves envoyproxy/envoy-mobile#169. Related issue (why this is in C++ instead of a `yaml` resource): envoyproxy/envoy-mobile#341 Signed-off-by: Michael Rebello <[email protected]> Signed-off-by: JP Simard <[email protected]>
- Loading branch information
Showing
15 changed files
with
210 additions
and
36 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -8,7 +8,6 @@ objc_library( | |
"*.h", | ||
"*.mm", | ||
]), | ||
data = ["config.yaml"], | ||
deps = ["//dist:envoy_mobile_ios"], | ||
) | ||
|
||
|
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,90 @@ | ||
// NOLINT(namespace-envoy) | ||
|
||
const char* config_template = R"( | ||
static_resources: | ||
listeners: | ||
- address: | ||
socket_address: {address: 0.0.0.0, port_value: 9000, protocol: TCP} | ||
filter_chains: | ||
- filters: | ||
- name: envoy.http_connection_manager | ||
config: | ||
stat_prefix: base | ||
route_config: | ||
virtual_hosts: | ||
- name: all | ||
domains: ["*"] | ||
routes: | ||
- match: {prefix: "/"} | ||
route: {cluster: base} | ||
http_filters: | ||
- name: envoy.router | ||
- address: | ||
socket_address: | ||
protocol: TCP | ||
address: 0.0.0.0 | ||
port_value: 9001 | ||
filter_chains: | ||
- filters: | ||
- name: envoy.http_connection_manager | ||
typed_config: | ||
"@type": type.googleapis.com/envoy.config.filter.network.http_connection_manager.v2.HttpConnectionManager | ||
stat_prefix: client | ||
http_protocol_options: | ||
allow_absolute_url: true | ||
route_config: | ||
name: local_route | ||
virtual_hosts: | ||
- name: all | ||
domains: ["*"] | ||
routes: | ||
- match: | ||
prefix: "/" | ||
route: | ||
cluster: default_egress | ||
http_filters: | ||
- name: envoy.filters.http.dynamic_forward_proxy | ||
config: | ||
dns_cache_config: | ||
name: dynamic_forward_proxy_cache_config | ||
dns_lookup_family: AUTO | ||
- name: envoy.router | ||
clusters: | ||
- name: base | ||
connect_timeout: {{ connect_timeout }} | ||
dns_refresh_rate: {{ dns_refresh_rate }} | ||
dns_lookup_family: V4_ONLY | ||
lb_policy: ROUND_ROBIN | ||
load_assignment: | ||
cluster_name: base | ||
endpoints: | ||
- lb_endpoints: | ||
- endpoint: | ||
address: | ||
socket_address: {address: example.com, port_value: 443} | ||
tls_context: | ||
sni: example.com | ||
type: LOGICAL_DNS | ||
- name: default_egress | ||
connect_timeout: {{ connect_timeout }} | ||
dns_refresh_rate: {{ dns_refresh_rate }} | ||
lb_policy: CLUSTER_PROVIDED | ||
cluster_type: | ||
name: envoy.clusters.dynamic_forward_proxy | ||
typed_config: | ||
"@type": type.googleapis.com/envoy.config.cluster.dynamic_forward_proxy.v2alpha.ClusterConfig | ||
dns_cache_config: | ||
name: dynamic_forward_proxy_cache_config | ||
dns_lookup_family: AUTO | ||
# WARNING! | ||
# TODO: Enable TLS in https://github.com/lyft/envoy-mobile/issues/322 | ||
# | ||
# tls_context: | ||
# common_tls_context: | ||
# validation_context: | ||
# trusted_ca: {filename: /etc/ssl/certs/ca-certificates.crt} | ||
stats_flush_interval: {{ stats_flush_interval }} | ||
watchdog: | ||
megamiss_timeout: 60s | ||
miss_timeout: 60s | ||
)"; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
import Foundation | ||
|
||
/// Error that may be thrown by the configuration type. | ||
@objc | ||
public enum ConfigurationError: Int, Swift.Error { | ||
/// Not all keys within the provided template were resolved. | ||
case unresolvedTemplateKey | ||
} | ||
|
||
/// Builder used for creating configurations for new Envoy instances. | ||
@objcMembers | ||
public final class Configuration: NSObject { | ||
/// Timeout to apply to connections. | ||
public private(set) var connectTimeoutSeconds: UInt = 30 | ||
|
||
/// Interval at which DNS should be refreshed. | ||
public private(set) var dnsRefreshSeconds: UInt = 60 | ||
|
||
/// Interval at which stats should be flushed. | ||
public private(set) var statsFlushSeconds: UInt = 60 | ||
|
||
public func withConnectTimeoutSeconds(_ connectTimeoutSeconds: UInt) -> Configuration { | ||
self.connectTimeoutSeconds = connectTimeoutSeconds | ||
return self | ||
} | ||
|
||
public func withDNSRefreshSeconds(_ dnsRefreshSeconds: UInt) -> Configuration { | ||
self.dnsRefreshSeconds = dnsRefreshSeconds | ||
return self | ||
} | ||
|
||
public func withStatsFlushSeconds(_ statsFlushSeconds: UInt) -> Configuration { | ||
self.statsFlushSeconds = statsFlushSeconds | ||
return self | ||
} | ||
|
||
// MARK: - Internal | ||
|
||
/// Converts the structure into a file by replacing values in a default template configuration. | ||
/// | ||
/// - returns: A file that may be used for starting an instance of Envoy. | ||
public func build() throws -> String { | ||
var template = EnvoyConfiguration.templateString() | ||
let templateKeysToValues: [String: String] = [ | ||
"connect_timeout": "\(self.connectTimeoutSeconds)s", | ||
"dns_refresh_rate": "\(self.dnsRefreshSeconds)s", | ||
"stats_flush_interval": "\(self.statsFlushSeconds)s", | ||
] | ||
|
||
for (templateKey, value) in templateKeysToValues { | ||
while let range = template.range(of: "{{ \(templateKey) }}") { | ||
template = template.replacingCharacters(in: range, with: value) | ||
} | ||
} | ||
|
||
if template.contains("{{") { | ||
throw ConfigurationError.unresolvedTemplateKey | ||
} | ||
|
||
return template | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters