Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[WIP] repo: Add filter examples #144

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
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
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -72,3 +72,6 @@ lerna-debug.log*
node_modules
dist
*.bak

filters/envoy
filters/bazel
566 changes: 566 additions & 0 deletions filters/.bazelrc

Large diffs are not rendered by default.

1 change: 1 addition & 0 deletions filters/.bazelversion
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
6.5.0
60 changes: 60 additions & 0 deletions filters/BUILD
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
package(default_visibility = ["//visibility:public"])

load(
"@envoy//bazel:envoy_build_system.bzl",
"envoy_cc_binary",
"envoy_cc_library",
"envoy_cc_test",
)

envoy_cc_binary(
name = "envoy",
repository = "@envoy",
deps = [
":echo2_config",
"@envoy//source/exe:envoy_main_entry_lib",
],
)

envoy_cc_library(
name = "echo2_lib",
srcs = ["echo2.cc"],
hdrs = ["echo2.h"],
repository = "@envoy",
deps = [
"@envoy//envoy/buffer:buffer_interface",
"@envoy//envoy/network:connection_interface",
"@envoy//envoy/network:filter_interface",
"@envoy//source/common/common:assert_lib",
"@envoy//source/common/common:logger_lib",
],
)

envoy_cc_library(
name = "echo2_config",
srcs = ["echo2_config.cc"],
repository = "@envoy",
deps = [
":echo2_lib",
"@envoy//envoy/network:filter_interface",
"@envoy//envoy/registry:registry",
"@envoy//envoy/server:filter_config_interface",
],
)

envoy_cc_test(
name = "echo2_integration_test",
srcs = ["echo2_integration_test.cc"],
data = ["echo2_server.yaml"],
repository = "@envoy",
deps = [
":echo2_config",
"@envoy//test/integration:integration_lib"
],
)

sh_test(
name = "envoy_binary_test",
srcs = ["envoy_binary_test.sh"],
data = [":envoy"],
)
30 changes: 30 additions & 0 deletions filters/WORKSPACE
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
workspace(name = "envoy_filter_example")

local_repository(
name = "envoy",
path = "envoy",
)

load("@envoy//bazel:api_binding.bzl", "envoy_api_binding")

envoy_api_binding()

load("@envoy//bazel:api_repositories.bzl", "envoy_api_dependencies")

envoy_api_dependencies()

load("@envoy//bazel:repositories.bzl", "envoy_dependencies")

envoy_dependencies()

load("@envoy//bazel:repositories_extra.bzl", "envoy_dependencies_extra")

envoy_dependencies_extra()

load("@envoy//bazel:python_dependencies.bzl", "envoy_python_dependencies")

envoy_python_dependencies()

load("@envoy//bazel:dependency_imports.bzl", "envoy_dependency_imports")

envoy_dependency_imports()
1 change: 1 addition & 0 deletions filters/bazel-bin
1 change: 1 addition & 0 deletions filters/bazel-out
1 change: 1 addition & 0 deletions filters/bazel-source
1 change: 1 addition & 0 deletions filters/bazel-testlogs
72 changes: 72 additions & 0 deletions filters/bazel/get_workspace_status
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
#!/usr/bin/env bash

# This file was imported from https://github.com/bazelbuild/bazel at d6fec93.

# This script will be run bazel when building process starts to
# generate key-value information that represents the status of the
# workspace. The output should be like
#
# KEY1 VALUE1
# KEY2 VALUE2
#
# If the script exits with non-zero code, it's considered as a failure
# and the output will be discarded.

# For Envoy in particular, we want to force binaries to relink when the Git
# SHA changes (https://github.com/envoyproxy/envoy/issues/2551). This can be
# done by prefixing keys with "STABLE_". To avoid breaking compatibility with
# other status scripts, this one still echos the non-stable ("volatile") names.

# If this SOURCE_VERSION file exists then it must have been placed here by a
# distribution doing a non-git, source build.
# Distributions would be expected to echo the commit/tag as BUILD_SCM_REVISION
if [ -f SOURCE_VERSION ]
then
echo "BUILD_SCM_REVISION $(cat SOURCE_VERSION)"
echo "ENVOY_BUILD_SCM_REVISION $(cat SOURCE_VERSION)"
echo "STABLE_BUILD_SCM_REVISION $(cat SOURCE_VERSION)"
echo "BUILD_SCM_STATUS Distribution"
exit 0
fi

if [[ -n "$BAZEL_FAKE_SCM_REVISION" ]]; then
echo "BUILD_SCM_REVISION $BAZEL_FAKE_SCM_REVISION"
echo "ENVOY_BUILD_SCM_REVISION $BAZEL_FAKE_SCM_REVISION"
echo "STABLE_BUILD_SCM_REVISION $BAZEL_FAKE_SCM_REVISION"
else
# The code below presents an implementation that works for git repository
git_rev=$(git rev-parse HEAD) || exit 1
echo "BUILD_SCM_REVISION ${git_rev}"
echo "ENVOY_BUILD_SCM_REVISION ${git_rev}"
echo "STABLE_BUILD_SCM_REVISION ${git_rev}"
fi

# If BAZEL_VOLATILE_DIRTY is set then stamped builds will rebuild uncached when
# either a tracked file changes or an untracked file is added or removed.
# Otherwise this just tracks changes to tracked files.
tracked_hash="$(git ls-files -s | sha256sum | head -c 40)"
if [[ -n "$BAZEL_VOLATILE_DIRTY" ]]; then
porcelain_status="$(git status --porcelain | sha256sum)"
diff_status="$(git --no-pager diff | sha256sum)"
tree_hash="$(echo "${tracked_hash}:${porcelain_status}:${diff_status}" | sha256sum | head -c 40)"
echo "BUILD_SCM_HASH ${tree_hash}"
else
echo "BUILD_SCM_HASH ${tracked_hash}"
fi

# Check whether there are any uncommitted changes
tree_status="Clean"
git diff-index --quiet HEAD -- || {
tree_status="Modified"
}

echo "BUILD_SCM_STATUS ${tree_status}"
echo "STABLE_BUILD_SCM_STATUS ${tree_status}"

git_branch=$(git rev-parse --abbrev-ref HEAD)
echo "BUILD_SCM_BRANCH ${git_branch}"

git_remote=$(git remote get-url origin)
if [[ -n "$git_remote" ]]; then
echo "BUILD_SCM_REMOTE ${git_remote}"
fi
36 changes: 36 additions & 0 deletions filters/bazel/platform_mappings
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
flags:
--cpu=arm64-v8a
--crosstool_top=//external:android/crosstool
@envoy//bazel:android_aarch64

--cpu=armeabi-v7a
--crosstool_top=//external:android/crosstool
@envoy//bazel:android_armeabi

--cpu=x86
--crosstool_top=//external:android/crosstool
@envoy//bazel:android_x86

--cpu=x86_64
--crosstool_top=//external:android/crosstool
@envoy//bazel:android_x86_64

--cpu=darwin_x86_64
--apple_platform_type=macos
@envoy//bazel:macos_x86_64

--cpu=darwin_arm64
--apple_platform_type=macos
@envoy//bazel:macos_arm64

--cpu=ios_x86_64
--apple_platform_type=ios
@envoy//bazel:ios_x86_64_platform

--cpu=ios_sim_arm64
--apple_platform_type=ios
@envoy//bazel:ios_sim_arm64_platform

--cpu=ios_arm64
--apple_platform_type=ios
@envoy//bazel:ios_arm64_platform
1 change: 1 addition & 0 deletions filters/bazel/protoc/BUILD
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
licenses(["notice"]) # Apache 2
18 changes: 18 additions & 0 deletions filters/bazel/protoc/BUILD.protoc
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
load("@envoy//bazel/protoc:protoc.bzl", "protoc_binary")

protoc_binary(
name = "protoc",
srcs = select({
":windows": ["bin/protoc.exe"],
"//conditions:default": ["bin/protoc"],
}),
executable = "protoc.exe",
visibility = ["//visibility:public"],
)

config_setting(
name = "windows",
constraint_values = [
"@platforms//os:windows",
],
)
15 changes: 15 additions & 0 deletions filters/bazel/protoc/protoc.bzl
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
def protoc_binary(name, srcs, executable, **kwargs):
"""protoc_binary makes a copy of the protoc binary to bazel-bin.

This is a workaround to make sure protoc can be used with attributes
which don't allow files."""

native.genrule(
name = name,
executable = True,
srcs = srcs,
outs = [executable],
cmd_bash = "cp $< $@ && chmod +x $@",
cmd_bat = "copy $< $@",
**kwargs
)
14 changes: 14 additions & 0 deletions filters/docker-compose-filters.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
services:
filter_example_compile:
build:
context: ../shared/build
command: >
bash -c "
bazel build //:envoy"
entrypoint: /usr/local/bin/build-entrypoint.sh
environment:
- BUILD_UID=${UID:-1000}
working_dir: /source
volumes:
- ${ENVOY_DOCKER_BUILD_DIR:-/tmp/envoy-docker-build}:/tmp
- .:/source
1 change: 1 addition & 0 deletions filters/envoy
Submodule envoy added at a01c04
22 changes: 22 additions & 0 deletions filters/filter_example_setup.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
#!/usr/bin/env bash

# Configure environment for Envoy Filter Example build and test.

set -e

# shellcheck disable=SC2034
ENVOY_FILTER_EXAMPLE_TESTS=(
"//:echo2_integration_test"
"//http-filter-example:http_filter_integration_test"
"//:envoy_binary_test")

mkdir -p "${ENVOY_FILTER_EXAMPLE_SRCDIR}"/bazel
ln -sf "${ENVOY_SRCDIR}"/bazel/get_workspace_status "${ENVOY_FILTER_EXAMPLE_SRCDIR}"/bazel/
ln -sf "${ENVOY_SRCDIR}"/bazel/platform_mappings "${ENVOY_FILTER_EXAMPLE_SRCDIR}"/bazel/
cp -a "${ENVOY_SRCDIR}"/bazel/protoc "${ENVOY_FILTER_EXAMPLE_SRCDIR}"/bazel/
cp -f "${ENVOY_SRCDIR}"/.bazelrc "${ENVOY_FILTER_EXAMPLE_SRCDIR}"/
rm -f "${ENVOY_FILTER_EXAMPLE_SRCDIR}"/.bazelversion
cp -f "${ENVOY_SRCDIR}"/.bazelversion "${ENVOY_FILTER_EXAMPLE_SRCDIR}"/
cp -f "${ENVOY_SRCDIR}"/*.bazelrc "${ENVOY_FILTER_EXAMPLE_SRCDIR}"/

export FILTER_WORKSPACE_SET=1
16 changes: 16 additions & 0 deletions filters/verify.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@


if [[ ! -e envoy ]]; then
git clone https://github.com/envoyproxy/envoy
fi

echo FILTER_DEV > SOURCE_VERSION

mkdir -p bazel
cp -a envoy/bazel/get_workspace_status bazel/
cp -a envoy/bazel/platform_mappings bazel/
cp -a envoy/bazel/protoc bazel/
cp -f envoy/.bazelrc .
cp -f envoy/.bazelversion .

docker compose -f docker-compose-filters.yaml up --build --quiet-pull --remove-orphans filter_example_compile
4 changes: 3 additions & 1 deletion shared/build/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ FROM envoyproxy/envoy-build-ubuntu:f94a38f62220a2b017878b790b6ea98a0f6c5f9c@sha2
ENV DEBIAN_FRONTEND=noninteractive
RUN --mount=type=cache,target=/var/cache/apt,sharing=locked \
--mount=type=cache,target=/var/lib/apt/lists,sharing=locked \
apt-get -qq install --no-install-recommends -y gosu \
apt-get update \
&& apt-get -qq install --no-install-recommends -y gosu \
&& groupadd -f envoygroup \
&& useradd -g envoygroup -m -d /home/envoybuild envoybuild
COPY ./build-entrypoint.sh /usr/local/bin
4 changes: 4 additions & 0 deletions shared/build/build-entrypoint.sh
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,10 @@ if [[ $(id -u envoybuild) != "${BUILD_UID}" ]]; then
chown envoybuild /home/envoybuild
fi

if [[ ! -e /output ]]; then
mkdir /output
fi

chown envoybuild /output
chmod 1777 /tmp

Expand Down
Loading