From ccea33655f142cc6cc33ad7b44350519eccc4e04 Mon Sep 17 00:00:00 2001 From: rinfx <893383980@qq.com> Date: Fri, 26 Jan 2024 16:59:06 +0800 Subject: [PATCH] add devcontainer for wasm-go plugin developing (#749) Co-authored-by: Xunzhuo --- plugins/wasm-go/.devcontainer/Dockerfile | 23 ++++++ .../wasm-go/.devcontainer/devcontainer.json | 21 +++++ plugins/wasm-go/.devcontainer/gen_config.py | 76 +++++++++++++++++++ plugins/wasm-go/Makefile | 13 ++++ 4 files changed, 133 insertions(+) create mode 100644 plugins/wasm-go/.devcontainer/Dockerfile create mode 100644 plugins/wasm-go/.devcontainer/devcontainer.json create mode 100644 plugins/wasm-go/.devcontainer/gen_config.py diff --git a/plugins/wasm-go/.devcontainer/Dockerfile b/plugins/wasm-go/.devcontainer/Dockerfile new file mode 100644 index 000000000..8691eff95 --- /dev/null +++ b/plugins/wasm-go/.devcontainer/Dockerfile @@ -0,0 +1,23 @@ +FROM higress-registry.cn-hangzhou.cr.aliyuncs.com/higress/gateway:1.3.1 + +FROM ubuntu:20.04 + +RUN apt -y update \ + && apt install -y --no-install-recommends python3-pip net-tools vim wget make curl git 2>&1 \ + && apt install -y --reinstall ca-certificates \ + && apt-get autoremove -y && apt-get clean \ + && rm -rf /tmp/* /var/tmp/* \ + && rm -rf /var/lib/apt/lists/* + +ENV PATH=/opt/tinygo/bin:/opt/go/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin + +RUN wget --no-check-certificate https://github.com/tinygo-org/tinygo/releases/download/v0.29.0/tinygo0.29.0.linux-amd64.tar.gz \ + && tar -zvxf tinygo0.29.0.linux-amd64.tar.gz -C /opt \ + && rm tinygo0.29.0.linux-amd64.tar.gz + +RUN wget --no-check-certificate https://go.dev/dl/go1.19.linux-amd64.tar.gz \ + && tar -zvxf go1.19.linux-amd64.tar.gz -C /opt \ + && rm go1.19.linux-amd64.tar.gz \ + && go install -v golang.org/x/tools/gopls@latest + +COPY --from=0 /usr/local/bin/envoy /usr/local/bin/envoy \ No newline at end of file diff --git a/plugins/wasm-go/.devcontainer/devcontainer.json b/plugins/wasm-go/.devcontainer/devcontainer.json new file mode 100644 index 000000000..5bb98f633 --- /dev/null +++ b/plugins/wasm-go/.devcontainer/devcontainer.json @@ -0,0 +1,21 @@ +{ + "name": "Wasm Go Dev", + // "dockerFile": "Dockerfile", + "image": "liuxr25/wasm-go:tinygo-0.29.0", + "runArgs": [ + "--user=root" + ], + "customizations": { + "vscode": { + "settings": { + "terminal.integrated.shell.linux": "/bin/bash", + "python.pythonPath": "/usr/bin/python3" + }, + "extensions": [ + "ms-python.python", + "golang.go" + ] + } + } + } + \ No newline at end of file diff --git a/plugins/wasm-go/.devcontainer/gen_config.py b/plugins/wasm-go/.devcontainer/gen_config.py new file mode 100644 index 000000000..5174a0c6d --- /dev/null +++ b/plugins/wasm-go/.devcontainer/gen_config.py @@ -0,0 +1,76 @@ +import json +import sys + + +plugin_name = sys.argv[1] + +with open("extensions/"+plugin_name+"/config.json", "r") as f: + plugin_config = json.load(f) + +config = f'''static_resources: + listeners: + - address: + socket_address: + address: 0.0.0.0 + port_value: 8080 + filter_chains: + - filters: + - name: envoy.filters.network.http_connection_manager + typed_config: + '@type': type.googleapis.com/envoy.extensions.filters.network.http_connection_manager.v3.HttpConnectionManager + codec_type: AUTO + stat_prefix: ingress_http + route_config: + name: test + virtual_hosts: + - name: direct_response_service + domains: + - "*" + routes: + - match: + prefix: "/" + direct_response: + status: 200 + body: + inline_string: "hello world\\n" + # - match: + # prefix: "/" + # route: + # cluster: service-backend + http_filters: + - name: {plugin_name} + typed_config: + "@type": type.googleapis.com/udpa.type.v1.TypedStruct + type_url: type.googleapis.com/envoy.extensions.filters.http.wasm.v3.Wasm + value: + config: + name: wasmdemo + vm_config: + runtime: envoy.wasm.runtime.v8 + code: + local: + filename: ./extensions/{plugin_name}/main.wasm + configuration: + "@type": "type.googleapis.com/google.protobuf.StringValue" + value: '{json.dumps(plugin_config)}' + - name: envoy.filters.http.router + typed_config: + '@type': type.googleapis.com/envoy.extensions.filters.http.router.v3.Router + # clusters: + # - name: service-backend + # connect_timeout: 600s + # type: STATIC + # lb_policy: ROUND_ROBIN + # load_assignment: + # cluster_name: service-backend + # endpoints: + # - lb_endpoints: + # - endpoint: + # address: + # socket_address: + # address: 127.0.0.1 + # port_value: 8000 +''' + +with open("extensions/"+plugin_name+"/config.yaml", "w") as f: + f.write(config) \ No newline at end of file diff --git a/plugins/wasm-go/Makefile b/plugins/wasm-go/Makefile index db6c31d00..d55ef3986 100644 --- a/plugins/wasm-go/Makefile +++ b/plugins/wasm-go/Makefile @@ -60,3 +60,16 @@ builder: . @echo "" @echo "image: ${BUILDER}" + +local-build: + tinygo build -scheduler=none -target=wasi -gc=custom -tags='custommalloc nottinygc_finalizer' \ + -o extensions/${PLUGIN_NAME}/main.wasm \ + extensions/${PLUGIN_NAME}/main.go + @echo "" + @echo "wasm: extensions/${PLUGIN_NAME}/main.wasm" + +local-run: + python3 .devcontainer/gen_config.py ${PLUGIN_NAME} + envoy -c extensions/${PLUGIN_NAME}/config.yaml --concurrency 0 --log-level info --component-log-level wasm:debug + +local-all: local-build local-run \ No newline at end of file