Skip to content

Commit 0dc852d

Browse files
committed
dynamic_modules: add examples for network & listener modules
Signed-off-by: Rohit Agrawal <rohit.agrawal@databricks.com>
1 parent 2d4b47f commit 0dc852d

19 files changed

Lines changed: 2704 additions & 35 deletions

Dockerfile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ RUN CC="zig cc -target aarch64-linux-gnu" CXX="zig c++ -target aarch64-linux-gnu
4646
RUN CC="zig cc -target x86_64-linux-gnu" CXX="zig c++ -target x86_64-linux-gnu" CGO_ENABLED=1 GOARCH=amd64 go build -buildmode=c-shared -o /build/amd64_libgo_module.so .
4747

4848
##### Build the final image #####
49-
FROM envoyproxy/envoy:v1.36.2 AS envoy
49+
FROM envoyproxy/envoy:v1.37.0 AS envoy
5050
ARG TARGETARCH
5151
ENV ENVOY_DYNAMIC_MODULES_SEARCH_PATH=/usr/local/lib
5252
COPY --from=rust_builder /build/${TARGETARCH}_librust_module.so /usr/local/lib/librust_module.so

ENVOY_VERSION

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
dc2d3098ae5641555f15c71d5bb5ce0060a8015c
1+
6d9bb7d9a85d616b220d1f8fe67b61f82bbdb8d3

go/gosdk/abi.go

Lines changed: 24 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
package gosdk
44

55
// Following is a distillation of the Envoy ABI for dynamic modules:
6-
// https://github.com/envoyproxy/envoy/blob/dc2d3098ae5641555f15c71d5bb5ce0060a8015c/source/extensions/dynamic_modules/abi.h
6+
// https://github.com/envoyproxy/envoy/blob/6d9bb7d9a85d616b220d1f8fe67b61f82bbdb8d3/source/extensions/dynamic_modules/abi.h
77
//
88
// Why not using the header file directly? That is because Go runtime complains
99
// about passing pointers to C code on the boundary. In the following code, we replace
@@ -91,13 +91,25 @@ bool envoy_dynamic_module_callback_http_get_response_body_vector(
9191
bool envoy_dynamic_module_callback_http_get_response_body_vector_size(
9292
uintptr_t filter_envoy_ptr, size_t* size);
9393
94+
// envoy_dynamic_module_type_envoy_buffer is a struct representing a buffer owned by Envoy.
95+
typedef struct {
96+
const char* ptr;
97+
size_t length;
98+
} envoy_dynamic_module_type_envoy_buffer;
99+
100+
// envoy_dynamic_module_type_module_buffer is a struct representing a buffer owned by the module.
101+
typedef struct {
102+
uintptr_t ptr;
103+
size_t length;
104+
} envoy_dynamic_module_type_module_buffer;
105+
94106
#cgo noescape envoy_dynamic_module_callback_http_send_response
95-
// Uncomment once https://github.com/envoyproxy/envoy/pull/39206 is merged.
96-
// #cgo nocallback envoy_dynamic_module_callback_http_send_response
107+
#cgo nocallback envoy_dynamic_module_callback_http_send_response
97108
void envoy_dynamic_module_callback_http_send_response(
98109
uintptr_t filter_envoy_ptr, uint32_t status_code,
99110
uintptr_t headers_vector, size_t headers_vector_size,
100-
uintptr_t body, size_t body_length);
111+
envoy_dynamic_module_type_module_buffer body,
112+
envoy_dynamic_module_type_module_buffer details);
101113
102114
#cgo noescape envoy_dynamic_module_callback_http_get_request_headers_count
103115
#cgo nocallback envoy_dynamic_module_callback_http_get_request_headers_count
@@ -161,8 +173,8 @@ import (
161173
"unsafe"
162174
)
163175

164-
// https://github.com/envoyproxy/envoy/blob/dc2d3098ae5641555f15c71d5bb5ce0060a8015c/source/extensions/dynamic_modules/abi_version.h
165-
var version = append([]byte("ca2be3b80954d2a0e22b41d033b18eff9390c30261c8ec9ffe6e6bf971f41c27"), 0)
176+
// https://github.com/envoyproxy/envoy/blob/6d9bb7d9a85d616b220d1f8fe67b61f82bbdb8d3/source/extensions/dynamic_modules/abi.h
177+
var version = append([]byte("4dae397a7c9ff0238d318d57ea656ce8b3fbff595787dcd7ee2ff5b79c9fe10f"), 0)
166178

167179
//export envoy_dynamic_module_on_program_init
168180
func envoy_dynamic_module_on_program_init() uintptr {
@@ -172,13 +184,11 @@ func envoy_dynamic_module_on_program_init() uintptr {
172184
//export envoy_dynamic_module_on_http_filter_config_new
173185
func envoy_dynamic_module_on_http_filter_config_new(
174186
_ uintptr,
175-
namePtr *C.char,
176-
nameSize C.size_t,
177-
configPtr *C.char,
178-
configSize C.size_t,
187+
nameBuffer C.envoy_dynamic_module_type_envoy_buffer,
188+
configBuffer C.envoy_dynamic_module_type_envoy_buffer,
179189
) uintptr {
180-
name := C.GoStringN(namePtr, C.int(nameSize))
181-
config := C.GoBytes(unsafe.Pointer(configPtr), C.int(configSize))
190+
name := C.GoStringN(nameBuffer.ptr, C.int(nameBuffer.length))
191+
config := C.GoBytes(unsafe.Pointer(configBuffer.ptr), C.int(configBuffer.length))
182192
filterConfig := NewHttpFilterConfig(name, config)
183193
if filterConfig == nil {
184194
return 0
@@ -546,8 +556,8 @@ func (e envoyFilter) SendLocalReply(statusCode uint32, headers [][2]string, body
546556
C.uint32_t(statusCode),
547557
C.uintptr_t(headersVecPtr),
548558
C.size_t(headersVecSize),
549-
C.uintptr_t(bodyPtr),
550-
C.size_t(bodySize),
559+
C.envoy_dynamic_module_type_module_buffer{ptr: C.uintptr_t(bodyPtr), length: C.size_t(bodySize)},
560+
C.envoy_dynamic_module_type_module_buffer{ptr: 0, length: 0}, // Empty details
551561
)
552562
runtime.KeepAlive(headers)
553563
runtime.KeepAlive(body)

rust/Cargo.lock

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

rust/Cargo.toml

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
21
[package]
32
name = "envoy-proxy-dynamic-modules-rust-sdk-examples"
43
version = "0.1.0"
@@ -8,7 +7,7 @@ repository = "https://github.com/envoyproxy/dynamic-modules-example"
87

98
[dependencies]
109
# The SDK version must match the Envoy version due to the strict compatibility requirements.
11-
envoy-proxy-dynamic-modules-rust-sdk = { git = "https://github.com/envoyproxy/envoy", rev = "dc2d3098ae5641555f15c71d5bb5ce0060a8015c" }
10+
envoy-proxy-dynamic-modules-rust-sdk = { git = "https://github.com/envoyproxy/envoy", rev = "6d9bb7d9a85d616b220d1f8fe67b61f82bbdb8d3" }
1211
serde = { version = "1.0", features = ["derive"] }
1312
serde_json = "1.0"
1413
rand = "0.9.0"
@@ -17,7 +16,8 @@ matchers = "0.2.0"
1716
[dev-dependencies]
1817
tempfile = "3.16.0"
1918

19+
# Main library - HTTP filters (backward compatible)
2020
[lib]
2121
name = "rust_module"
2222
path = "src/lib.rs"
23-
crate-type = ["cdylib"]
23+
crate-type = ["cdylib", "rlib"]

rust/src/http_access_logger.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,7 @@ impl FilterConfig {
7777

7878
impl<EHF: EnvoyHttpFilter> HttpFilterConfig<EHF> for FilterConfig {
7979
/// This is called for each new HTTP filter.
80-
fn new_http_filter(&mut self, _envoy: &mut EHF) -> Box<dyn HttpFilter<EHF>> {
80+
fn new_http_filter(&self, _envoy: &mut EHF) -> Box<dyn HttpFilter<EHF>> {
8181
let tx = self.tx.clone();
8282
Box::new(Filter {
8383
tx,

rust/src/http_header_mutation.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ impl FilterConfig {
3131

3232
impl<EHF: EnvoyHttpFilter> HttpFilterConfig<EHF> for FilterConfig {
3333
/// This is called for each new HTTP filter.
34-
fn new_http_filter(&mut self, _envoy: &mut EHF) -> Box<dyn HttpFilter<EHF>> {
34+
fn new_http_filter(&self, _envoy: &mut EHF) -> Box<dyn HttpFilter<EHF>> {
3535
Box::new(Filter {
3636
request_headers: self.request_headers.clone(),
3737
remove_request_headers: self.remove_request_headers.clone(),

rust/src/http_metrics.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ impl FilterConfig {
3838

3939
impl<EHF: EnvoyHttpFilter> HttpFilterConfig<EHF> for FilterConfig {
4040
/// This is called for each new HTTP filter.
41-
fn new_http_filter(&mut self, _envoy: &mut EHF) -> Box<dyn HttpFilter<EHF>> {
41+
fn new_http_filter(&self, _envoy: &mut EHF) -> Box<dyn HttpFilter<EHF>> {
4242
Box::new(Filter {
4343
version: self.config.version.clone(),
4444
start_time: None,

rust/src/http_passthrough.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ impl FilterConfig {
2121

2222
impl<EHF: EnvoyHttpFilter> HttpFilterConfig<EHF> for FilterConfig {
2323
/// This is called for each new HTTP filter.
24-
fn new_http_filter(&mut self, _envoy: &mut EHF) -> Box<dyn HttpFilter<EHF>> {
24+
fn new_http_filter(&self, _envoy: &mut EHF) -> Box<dyn HttpFilter<EHF>> {
2525
Box::new(Filter {})
2626
}
2727
}

rust/src/http_random_auth.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ impl FilterConfig {
1818

1919
impl<EHF: EnvoyHttpFilter> HttpFilterConfig<EHF> for FilterConfig {
2020
/// This is called for each new HTTP filter.
21-
fn new_http_filter(&mut self, _envoy: &mut EHF) -> Box<dyn HttpFilter<EHF>> {
21+
fn new_http_filter(&self, _envoy: &mut EHF) -> Box<dyn HttpFilter<EHF>> {
2222
Box::new(Filter {})
2323
}
2424
}
@@ -37,7 +37,7 @@ impl<EHF: EnvoyHttpFilter> HttpFilter<EHF> for Filter {
3737
) -> abi::envoy_dynamic_module_type_on_http_filter_request_headers_status {
3838
let reject = rand::rng().random::<bool>();
3939
if reject {
40-
envoy_filter.send_response(403, vec![], Some(b"Access forbidden"));
40+
envoy_filter.send_response(403, vec![], Some(b"Access forbidden"), None);
4141
return abi::envoy_dynamic_module_type_on_http_filter_request_headers_status::StopIteration;
4242
}
4343
abi::envoy_dynamic_module_type_on_http_filter_request_headers_status::Continue

0 commit comments

Comments
 (0)