Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,6 @@ COPY ./rust .
RUN cargo build

# Finally, copy the built library to the final image.
FROM envoyproxy/envoy-dev:80c1ac2143a7a73932c9dff814d38fd6867fe691 AS envoy
FROM envoyproxy/envoy-dev:4a113b5118003682833ba612202eb68628861ac6 AS envoy
ENV ENVOY_DYNAMIC_MODULES_SEARCH_PATH=/usr/local/lib
COPY --from=rust_builder /app/target/debug/librust_module.so /usr/local/lib/librust_module.so
2 changes: 1 addition & 1 deletion ENVOY_VERSION
Original file line number Diff line number Diff line change
@@ -1 +1 @@
80c1ac2143a7a73932c9dff814d38fd6867fe691
4a113b5118003682833ba612202eb68628861ac6
6 changes: 3 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# Dynamic Modules Examples

> Envoy Version: [80c1ac2143a7a73932c9dff814d38fd6867fe691]
> Envoy Version: [4a113b5118003682833ba612202eb68628861ac6]

This repository hosts examples of dynamic modules for [Envoy] to extend its functionality.
The high level documentation is available [here][High Level Doc]. In short, a dynamic module is a shared library
Expand Down Expand Up @@ -63,10 +63,10 @@ To update the Envoy version used in this repository, execute the following comma

```
CURRENT_VERSION="$(cat ENVOY_VERSION)"
NEW_VERSION=80c1ac2143a7a73932c9dff814d38fd6867fe691 # Whatever the commit in envoyproxy/envoy repo.
NEW_VERSION=4a113b5118003682833ba612202eb68628861ac6 # Whatever the commit in envoyproxy/envoy repo.
grep -rlF "${CURRENT_VERSION}" . | xargs sed -i "s/${CURRENT_VERSION}/${NEW_VERSION}/g"
```

[80c1ac2143a7a73932c9dff814d38fd6867fe691]: https://github.com/envoyproxy/envoy/tree/80c1ac2143a7a73932c9dff814d38fd6867fe691
[4a113b5118003682833ba612202eb68628861ac6]: https://github.com/envoyproxy/envoy/tree/4a113b5118003682833ba612202eb68628861ac6
[Envoy]: https://github.com/envoyproxy/envoy
[High Level Doc]: https://www.envoyproxy.io/docs/envoy/latest/intro/arch_overview/advanced/dynamic_modules
14 changes: 13 additions & 1 deletion integration/envoy.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -21,13 +21,25 @@ static_resources:
route:
cluster: httpbin
http_filters:
- name: envoy.filters.http.ext_proc
- name: dynamic_modules/passthrough
typed_config:
# https://www.envoyproxy.io/docs/envoy/latest/api-v3/extensions/dynamic_modules/v3/dynamic_modules.proto#envoy-v3-api-msg-extensions-dynamic-modules-v3-dynamicmoduleconfig
"@type": type.googleapis.com/envoy.extensions.filters.http.dynamic_modules.v3.DynamicModuleFilter
dynamic_module_config:
name: rust_module
filter_name: passthrough
- name: dynamic_modules/access_logger
typed_config:
# https://www.envoyproxy.io/docs/envoy/latest/api-v3/extensions/dynamic_modules/v3/dynamic_modules.proto#envoy-v3-api-msg-extensions-dynamic-modules-v3-dynamicmoduleconfig
"@type": type.googleapis.com/envoy.extensions.filters.http.dynamic_modules.v3.DynamicModuleFilter
dynamic_module_config:
name: rust_module
filter_name: access_logger
filter_config: |
{
"num_workers": 2,
"dirname": "/tmp/"
}
- name: envoy.filters.http.router
typed_config:
"@type": type.googleapis.com/envoy.extensions.filters.http.router.v3.Router
Expand Down
71 changes: 68 additions & 3 deletions integration/main_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,30 @@ package main

import (
_ "embed"
"encoding/json"
"fmt"
"io"
"net/http"
"os"
"os/exec"
"strconv"
"strings"
"testing"
"time"

"github.com/stretchr/testify/require"
)

//go:embed envoy.yaml
var envoyYaml string
var originalEnvoyYaml string

func requireEnvoyYaml(t *testing.T, tmpdir string) (yamlPath string) {
yamlPath = tmpdir + "/envoy.yaml"
replacedYaml := strings.ReplaceAll(originalEnvoyYaml, "/tmp/", tmpdir)
require.NoError(t, os.WriteFile(yamlPath, []byte(replacedYaml), 0644))
fmt.Println("Envoy config:", replacedYaml)
return
}

func TestIntegration(t *testing.T) {
envoyImage := "envoy-with-dynamic-modules:latest"
Expand All @@ -24,15 +36,22 @@ func TestIntegration(t *testing.T) {
cwd, err := os.Getwd()
require.NoError(t, err)

tmpdir := t.TempDir()
// Grant write permission to the tmpdir for the envoy process.
require.NoError(t, exec.Command("chmod", "777", tmpdir).Run())
yamlPath := requireEnvoyYaml(t, tmpdir)

cmd := exec.Command(
"docker",
"run",
"--network", "host",
"-v", cwd+":/integration",
"-w", "/integration",
"-v", tmpdir+":"+tmpdir,
"-w", tmpdir,
envoyImage,
"--concurrency", "1",
"--config-path", "envoy.yaml",
"--config-path", yamlPath,
"--base-id", strconv.Itoa(time.Now().Nanosecond()),
)
cmd.Stderr = os.Stderr
cmd.Stdout = os.Stdout
Expand Down Expand Up @@ -66,4 +85,50 @@ func TestIntegration(t *testing.T) {
return resp.StatusCode == 200
}, 30*time.Second, 1*time.Second)
})

t.Run("check access log", func(t *testing.T) {
require.Eventually(t, func() bool {
// List files in the access log directory
files, err := os.ReadDir(tmpdir)
require.NoError(t, err)

var accessLogFiles []string
for _, file := range files {
if strings.HasPrefix(file.Name(), "access_log") {
accessLogFiles = append(accessLogFiles, file.Name())
}
}

if len(accessLogFiles) == 0 {
t.Logf("No access log files found yet")
return false
}

// Read the first access log file
file, err := os.Open(tmpdir + "/" + accessLogFiles[0])
require.NoError(t, err)
defer file.Close()
content, err := io.ReadAll(file)
require.NoError(t, err)

type logLine struct {
RequestHeaders []string `json:"request_headers"`
ResponseHeaders []string `json:"response_headers"`
}

var found bool
for _, line := range strings.Split(string(content), "\n") {
t.Log(line)
if line == "" {
continue
}
var log logLine
require.NoError(t, json.Unmarshal([]byte(line), &log))
if len(log.RequestHeaders) > 0 && len(log.ResponseHeaders) > 0 {
found = true
}
}
return found
}, 30*time.Second, 1*time.Second)
})
}
143 changes: 142 additions & 1 deletion rust/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

7 changes: 6 additions & 1 deletion rust/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,12 @@ repository = "https://github.com/envoyproxy/dynamic-modules-example"

[dependencies]
# The SDK version must match the Envoy version due to the strict compatibility requirements.
envoy-proxy-dynamic-modules-rust-sdk = { git = "https://github.com/envoyproxy/envoy", rev = "80c1ac2143a7a73932c9dff814d38fd6867fe691" }
envoy-proxy-dynamic-modules-rust-sdk = { git = "https://github.com/envoyproxy/envoy", rev = "4a113b5118003682833ba612202eb68628861ac6" }
serde = { version = "1.0", features = ["derive"] }
serde_json = "1.0"

[dev-dependencies]
tempfile = "3.16.0"

[lib]
name = "rust_module"
Expand Down
Loading
Loading