Skip to content

Commit c655cfc

Browse files
committed
Add WASM plugin support
Signed-off-by: Sascha Grunert <sgrunert@redhat.com>
1 parent 6d486ac commit c655cfc

File tree

37 files changed

+14004
-394
lines changed

37 files changed

+14004
-394
lines changed

.codespellrc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11
[codespell]
2-
skip = .git,*.pdf,*.svg,go.sum,go.mod
2+
skip = .git,*.pdf,*.svg,go.sum,go.mod,*.pb.go
33
ignore-words-list = clos

.github/workflows/ci.yml

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,10 @@ jobs:
4545
with:
4646
go-version: 1.21.x
4747

48+
- uses: acifani/setup-tinygo@v2
49+
with:
50+
tinygo-version: "0.34.0"
51+
4852
- name: Set env
4953
shell: bash
5054
run: |
@@ -53,7 +57,7 @@ jobs:
5357
- name: Install protoc and plugins
5458
run: |
5559
sudo make install-protoc
56-
make install-protoc-dependencies install-ttrpc-plugin
60+
make install-protoc-dependencies install-ttrpc-plugin install-wasm-plugin
5761
5862
- name: Force regeneration of protobuf files on build
5963
run: |

Makefile

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,8 @@ PROTO_GOFILES = $(patsubst %.proto,%.pb.go,$(PROTO_SOURCES))
1717
PROTO_INCLUDE = -I$(PWD):/usr/local/include:/usr/include
1818
PROTO_OPTIONS = --proto_path=. $(PROTO_INCLUDE) \
1919
--go_opt=paths=source_relative --go_out=. \
20-
--go-ttrpc_opt=paths=source_relative --go-ttrpc_out=.
20+
--go-ttrpc_opt=paths=source_relative --go-ttrpc_out=. \
21+
--go-plugin_opt=paths=source_relative,disable_pb_gen=true --go-plugin_out=.
2122
PROTO_COMPILE = PATH=$(PATH):$(shell go env GOPATH)/bin; protoc $(PROTO_OPTIONS)
2223

2324
GO_CMD := go
@@ -33,6 +34,9 @@ GO_MODULES := $(shell $(GO_CMD) list ./...)
3334
GOLANG_CILINT := golangci-lint
3435
GINKGO := ginkgo
3536

37+
TINYGO_CMD := tinygo
38+
TINYGO_BUILD := $(TINYGO_CMD) build -scheduler=none -target=wasi -no-debug
39+
3640
BUILD_PATH := $(shell pwd)/build
3741
BIN_PATH := $(BUILD_PATH)/bin
3842
COVERAGE_PATH := $(BUILD_PATH)/coverage
@@ -44,7 +48,8 @@ PLUGINS := \
4448
$(BIN_PATH)/differ \
4549
$(BIN_PATH)/ulimit-adjuster \
4650
$(BIN_PATH)/v010-adapter \
47-
$(BIN_PATH)/template
51+
$(BIN_PATH)/template \
52+
$(BIN_PATH)/wasm
4853

4954

5055
ifneq ($(V),1)
@@ -118,6 +123,11 @@ $(BIN_PATH)/template: $(wildcard plugins/template/*.go)
118123
$(Q)echo "Building $@..."; \
119124
cd $(dir $<) && $(GO_BUILD) -o $@ .
120125

126+
$(BIN_PATH)/wasm: $(wildcard plugins/wasm/*.go)
127+
$(Q)echo "Building $@..."; \
128+
mkdir -p $(BIN_PATH) && \
129+
cd $(dir $<) && $(TINYGO_BUILD) -o $@ .
130+
121131
#
122132
# test targets
123133
#
@@ -180,6 +190,8 @@ validate-repo-no-changes:
180190
%.pb.go: %.proto
181191
$(Q)echo "Generating $@..."; \
182192
$(PROTO_COMPILE) $<
193+
sed -i 's;\(//.*protoc.*\)v.*;\1[stripped];g' $(shell find . -name '*.pb.go')
194+
sed -i '1s;^;//go:build !tinygo.wasm\n\n;' pkg/api/api_ttrpc.pb.go
183195

184196
#
185197
# targets for installing dependencies
@@ -191,6 +203,9 @@ install-protoc install-protobuf:
191203
install-ttrpc-plugin:
192204
$(Q)$(GO_INSTALL) -mod=mod github.com/containerd/ttrpc/cmd/protoc-gen-go-ttrpc@74421d10189e8c118870d294c9f7f62db2d33ec1
193205

206+
install-wasm-plugin:
207+
$(Q)$(GO_INSTALL) -mod=mod github.com/knqyf263/go-plugin/cmd/protoc-gen-go-plugin@d8d42059d8f1b52968cff7226b7094e5c6a0c342
208+
194209
install-protoc-dependencies:
195210
$(Q)$(GO_INSTALL) -mod=mod google.golang.org/protobuf/cmd/protoc-gen-go@v1.28.0
196211

go.mod

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,16 @@ go 1.21
44

55
require (
66
github.com/containerd/ttrpc v1.2.6-0.20240827082320-b5cd6e4b3287
7+
github.com/h2non/filetype v1.1.3
8+
github.com/knqyf263/go-plugin v0.8.1-0.20240827022226-114c6257e441
79
github.com/moby/sys/mountinfo v0.6.2
810
github.com/onsi/ginkgo/v2 v2.19.1
911
github.com/onsi/gomega v1.34.0
1012
github.com/opencontainers/runtime-spec v1.0.3-0.20220825212826-86290f6a00fb
1113
github.com/opencontainers/runtime-tools v0.9.0
1214
github.com/sirupsen/logrus v1.9.3
1315
github.com/stretchr/testify v1.8.4
16+
github.com/tetratelabs/wazero v1.8.2-0.20241030035603-dc08732e57d5
1417
golang.org/x/sys v0.21.0
1518
google.golang.org/grpc v1.57.1
1619
google.golang.org/protobuf v1.34.1

go.sum

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,12 +22,16 @@ github.com/google/go-cmp v0.6.0/go.mod h1:17dUlkBOakJ0+DkrSSNjCkIjxS6bF9zb3elmeN
2222
github.com/google/pprof v0.0.0-20240424215950-a892ee059fd6 h1:k7nVchz72niMH6YLQNvHSdIE7iqsQxK1P41mySCvssg=
2323
github.com/google/pprof v0.0.0-20240424215950-a892ee059fd6/go.mod h1:kf6iHlnVGwgKolg33glAes7Yg/8iWP8ukqeldJSO7jw=
2424
github.com/google/uuid v1.3.0/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo=
25+
github.com/h2non/filetype v1.1.3 h1:FKkx9QbD7HR/zjK1Ia5XiBsq9zdLi5Kf3zGyFTAFkGg=
26+
github.com/h2non/filetype v1.1.3/go.mod h1:319b3zT68BvV+WRj7cwy856M2ehB3HqNOt6sy1HndBY=
2527
github.com/hashicorp/errwrap v1.0.0 h1:hLrqtEDnRye3+sgx6z4qVLNuviH3MR5aQ0ykNJa/UYA=
2628
github.com/hashicorp/errwrap v1.0.0/go.mod h1:YH+1FKiLXxHSkmPseP+kNlulaMuP3n2brvKWEqk/Jc4=
2729
github.com/hashicorp/go-multierror v1.1.1 h1:H5DkEtf6CXdFp0N0Em5UCwQpXMWke8IA0+lD48awMYo=
2830
github.com/hashicorp/go-multierror v1.1.1/go.mod h1:iw975J/qwKPdAO1clOe2L8331t/9/fmwbPZ6JB6eMoM=
2931
github.com/kisielk/errcheck v1.5.0/go.mod h1:pFxgyoBC7bSaBwPgfKdkLd5X25qrDl4LWUI2bnpBCr8=
3032
github.com/kisielk/gotool v1.0.0/go.mod h1:XhKaO+MFFWcvkIS/tQcRk01m1F5IRFswLeQ+oQHNcck=
33+
github.com/knqyf263/go-plugin v0.8.1-0.20240827022226-114c6257e441 h1:Q/sZeuWkXprbKJSs7AwXryuZKSEL/a8ltC7e7xSspN0=
34+
github.com/knqyf263/go-plugin v0.8.1-0.20240827022226-114c6257e441/go.mod h1:CvCrNDMiKFlAlLFLmcoEfsTROEfNKbEZAMMrwQnLXCM=
3135
github.com/kr/text v0.2.0 h1:5Nx0Ya0ZqY2ygV366QzturHI13Jq95ApcVaJBhpS+AY=
3236
github.com/kr/text v0.2.0/go.mod h1:eLer722TekiGuMkidMxC/pM04lWEeraHUUmBw8l2grE=
3337
github.com/mndrix/tap-go v0.0.0-20171203230836-629fa407e90b/go.mod h1:pzzDgJWZ34fGzaAZGFW22KVZDfyrYW+QABMrWnJBnSs=
@@ -61,6 +65,8 @@ github.com/stretchr/testify v1.8.4 h1:CcVxjf3Q8PM0mHUKJCdn+eZZtm5yQwehR5yeSVQQcU
6165
github.com/stretchr/testify v1.8.4/go.mod h1:sz/lmYIOXD/1dqDmKjjqLyZ2RngseejIcXlSw2iwfAo=
6266
github.com/syndtr/gocapability v0.0.0-20200815063812-42c35b437635 h1:kdXcSzyDtseVEc4yCz2qF8ZrQvIDBJLl4S1c3GCXmoI=
6367
github.com/syndtr/gocapability v0.0.0-20200815063812-42c35b437635/go.mod h1:hkRG7XYTFWNJGYcbNJQlaLq0fg1yr4J4t/NcTQtrfww=
68+
github.com/tetratelabs/wazero v1.8.2-0.20241030035603-dc08732e57d5 h1:F+AT6Jxxww3j4/B/wXU01Raq4J8fg/Cg2HD4XsETGaU=
69+
github.com/tetratelabs/wazero v1.8.2-0.20241030035603-dc08732e57d5/go.mod h1:yAI0XTsMBhREkM/YDAK/zNou3GoiAce1P6+rp/wQhjs=
6470
github.com/urfave/cli v1.19.1/go.mod h1:70zkFmudgCuE/ngEzBv17Jvp/497gISqfk5gWijbERA=
6571
github.com/xeipuuv/gojsonpointer v0.0.0-20180127040702-4e3ac2762d5f h1:J9EGpcZtP0E/raorCMxlFGSTBrsSlaDGf3jU/qvAE2c=
6672
github.com/xeipuuv/gojsonpointer v0.0.0-20180127040702-4e3ac2762d5f/go.mod h1:N2zxlSyiKSe5eX1tZViRH5QA0qijqEDrYZiPEAiq3wU=

pkg/adaptation/adaptation.go

Lines changed: 28 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -53,19 +53,20 @@ type UpdateFn func(context.Context, []*ContainerUpdate) ([]*ContainerUpdate, err
5353
// Adaptation is the NRI abstraction for container runtime NRI adaptation/integration.
5454
type Adaptation struct {
5555
sync.Mutex
56-
name string
57-
version string
58-
dropinPath string
59-
pluginPath string
60-
socketPath string
61-
dontListen bool
62-
syncFn SyncFn
63-
updateFn UpdateFn
64-
clientOpts []ttrpc.ClientOpts
65-
serverOpts []ttrpc.ServerOpt
66-
listener net.Listener
67-
plugins []*plugin
68-
syncLock sync.RWMutex
56+
name string
57+
version string
58+
dropinPath string
59+
pluginPath string
60+
socketPath string
61+
dontListen bool
62+
syncFn SyncFn
63+
updateFn UpdateFn
64+
clientOpts []ttrpc.ClientOpts
65+
serverOpts []ttrpc.ServerOpt
66+
listener net.Listener
67+
plugins []*plugin
68+
syncLock sync.RWMutex
69+
wasmService *api.PluginPlugin
6970
}
7071

7172
var (
@@ -128,15 +129,21 @@ func New(name, version string, syncFn SyncFn, updateFn UpdateFn, opts ...Option)
128129
return nil, fmt.Errorf("failed to create NRI adaptation, nil UpdateFn")
129130
}
130131

132+
wasmPlugins, err := api.NewPluginPlugin(context.Background())
133+
if err != nil {
134+
return nil, fmt.Errorf("unable to initialize WASM service: %w", err)
135+
}
136+
131137
r := &Adaptation{
132-
name: name,
133-
version: version,
134-
syncFn: syncFn,
135-
updateFn: updateFn,
136-
pluginPath: DefaultPluginPath,
137-
dropinPath: DefaultPluginConfigPath,
138-
socketPath: DefaultSocketPath,
139-
syncLock: sync.RWMutex{},
138+
name: name,
139+
version: version,
140+
syncFn: syncFn,
141+
updateFn: updateFn,
142+
pluginPath: DefaultPluginPath,
143+
dropinPath: DefaultPluginConfigPath,
144+
socketPath: DefaultSocketPath,
145+
syncLock: sync.RWMutex{},
146+
wasmService: wasmPlugins,
140147
}
141148

142149
for _, o := range opts {

0 commit comments

Comments
 (0)