forked from envoyproxy/envoy
-
Notifications
You must be signed in to change notification settings - Fork 0
/
utility.h
148 lines (115 loc) · 5.91 KB
/
utility.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
#pragma once
#include <chrono>
#include <functional>
#include <string>
#include <vector>
#include "envoy/api/v2/cds.pb.h"
#include "envoy/api/v2/core/base.pb.h"
#include "envoy/api/v2/core/protocol.pb.h"
#include "envoy/api/v2/eds.pb.h"
#include "envoy/api/v2/route/route.pb.h"
#include "envoy/config/bootstrap/v2/bootstrap.pb.h"
#include "envoy/config/filter/network/http_connection_manager/v2/http_connection_manager.pb.h"
#include "envoy/http/codes.h"
#include "common/network/address_impl.h"
#include "test/integration/server_stats.h"
namespace Envoy {
class ConfigHelper {
public:
// Set up basic config, using the specified IpVersion for all connections: listeners, upstream,
// and admin connections.
//
// By default, this runs with an L7 proxy config, but config can be set to TCP_PROXY_CONFIG
// to test L4 proxying.
ConfigHelper(const Network::Address::IpVersion version,
const std::string& config = HTTP_PROXY_CONFIG);
typedef std::function<void(envoy::config::bootstrap::v2::Bootstrap&)> ConfigModifierFunction;
typedef std::function<void(
envoy::config::filter::network::http_connection_manager::v2::HttpConnectionManager&)>
HttpModifierFunction;
// A basic configuration (admin port, cluster_0, one listener) with no network filters.
static const std::string BASE_CONFIG;
// A basic configuration for L4 proxying.
static const std::string TCP_PROXY_CONFIG;
// A basic configuration for L7 proxying.
static const std::string HTTP_PROXY_CONFIG;
// A string for a basic buffer filter, which can be used with addFilter()
static const std::string DEFAULT_BUFFER_FILTER;
// A string for a small buffer filter, which can be used with addFilter()
static const std::string SMALL_BUFFER_FILTER;
// a string for a health check filter which can be used with addFilter()
static const std::string DEFAULT_HEALTH_CHECK_FILTER;
// a string for a squash filter which can be used with addFilter()
static const std::string DEFAULT_SQUASH_FILTER;
// Run the final config modifiers, and then set the upstream ports based on upstream connections.
// This is the last operation run on |bootstrap_| before it is handed to Envoy.
// Ports are assigned by looping through clusters, hosts, and addresses in the
// order they are stored in |bootstrap_|
void finalize(const std::vector<uint32_t>& ports);
// Set source_address in the bootstrap bind config.
void setSourceAddress(const std::string& address_string);
// Overwrite the first host and route for the primary listener.
void setDefaultHostAndRoute(const std::string& host, const std::string& route);
// Sets byte limits on upstream and downstream connections.
void setBufferLimits(uint32_t upstream_buffer_limit, uint32_t downstream_buffer_limit);
// Set the connect timeout on upstream connections.
void setConnectTimeout(std::chrono::milliseconds timeout);
// Add an additional route to the configuration.
void addRoute(const std::string& host, const std::string& route, const std::string& cluster,
bool validate_clusters,
envoy::api::v2::route::RouteAction::ClusterNotFoundResponseCode code,
envoy::api::v2::route::VirtualHost::TlsRequirementType type =
envoy::api::v2::route::VirtualHost::NONE);
// Add an HTTP filter prior to existing filters.
void addFilter(const std::string& filter_yaml);
// Sets the client codec to the specified type.
void setClientCodec(
envoy::config::filter::network::http_connection_manager::v2::HttpConnectionManager::CodecType
type);
// Add the default SSL configuration.
void addSslConfig();
// Renames the first listener to the name specified.
void renameListener(const std::string& name);
// Allows callers to do their own modification to |bootstrap_| which will be
// applied just before ports are modified in finalize().
void addConfigModifier(ConfigModifierFunction function);
// Allows callers to easily modify the HttpConnectionManager configuration.
// Modifiers will be applied just before ports are modified in finalize
void addConfigModifier(HttpModifierFunction function);
// Return the bootstrap configuration for hand-off to Envoy.
const envoy::config::bootstrap::v2::Bootstrap& bootstrap() { return bootstrap_; }
private:
// Load the first HCM struct from the first listener into a parsed proto.
bool loadHttpConnectionManager(
envoy::config::filter::network::http_connection_manager::v2::HttpConnectionManager& hcm);
// Stick the contents of the procided HCM proto and stuff them into the first HCM
// struct of the first listener.
void storeHttpConnectionManager(
const envoy::config::filter::network::http_connection_manager::v2::HttpConnectionManager&
hcm);
// Finds the filter named 'name' from the first filter chain from the first listener.
envoy::api::v2::listener::Filter* getFilterFromListener(const std::string& name);
// The bootstrap proto Envoy will start up with.
envoy::config::bootstrap::v2::Bootstrap bootstrap_;
// The config modifiers added via addConfigModifier() which will be applied in finalize()
std::vector<ConfigModifierFunction> config_modifiers_;
// Track if the connect timeout has been set (to avoid clobbering a custom setting with the
// default).
bool connect_timeout_set_{false};
// A sanity check guard to make sure config is not modified after handing it to Envoy.
bool finalized_{false};
};
// Common code for tests that deliver EDS update via the filesystem.
class EdsHelper {
public:
EdsHelper();
// Set EDS contents on filesystem and wait for Envoy to pick this up.
void setEds(const std::vector<envoy::api::v2::ClusterLoadAssignment>& cluster_load_assignments,
IntegrationTestServerStats& server_stats);
const std::string& eds_path() const { return eds_path_; }
private:
const std::string eds_path_;
uint32_t eds_version_{};
uint32_t update_successes_{};
};
} // namespace Envoy