kubewarden / policy-sdk-go

Kubewarden Policy SDK for the Go programming language
https://kubewarden.io
Apache License 2.0
8 stars 7 forks source link

Code cleanup thanks to TinyGo improvements #11

Open flavio opened 3 years ago

flavio commented 3 years ago

Latest release of TinyGo introduces massive improvements, the most notable one is better support of the Reflect module, which leads to finally have support of encoding/json.

The lack of support of encoding/json lead to quite some workarounds inside of our policies.

The goal of this card is to figure out if we can make our code more idiomatic:

flavio commented 3 years ago

I think this is too big for a single card, we can consider it an epic and then split the different tasks into smaller cards.

viccuad commented 3 years ago

Blocked, as with tinygo 0.19.0 this is still not possible. Here is the result of trying to unmarshal the settings in the go-policy-template:

vic@viccuad2 ~/suse/kw/go-policy-template ((1296fd5...) *$)$ make wasm
docker run --rm -v /home/vic/suse/kw/go-policy-template:/src -w /src tinygo/tinygo:0.19.0 tinygo build -o policy.wasm -target=wasi -no-debug .
-: blocking operation in exported function: __guest_call

traceback:
__guest_call
main.validateSettings
encoding/json.Unmarshal
(*encoding/json.decodeState).unmarshal
(*encoding/json.decodeState).value
(*encoding/json.decodeState).object
encoding/json.cachedTypeFields
(*sync.Map).LoadOrStore
(*sync.Mutex).Lock
make: *** [Makefile:2: wasm] Error 1
aykevl commented 2 years ago

The latest release has revamped goroutine support, you might want to try again now.

That said, we still don't fully support encoding/json. Strings, integers, floats and arrays mostly work, but structs still don't (there is a runtime panic).

viccuad commented 2 years ago

Thanks for the heads-up and the new release @aykevl! :). I think for our use case, the killer feature is still encoding/json (so we don't need our patched tidwall/gjson and to semi-manually unmarshall things). For those looking at this card (e.g: myself), create a simple go module with this encoding/json example and then:

$ docker run --rm -v ${PWD}:/src -w /src tinygo/tinygo:0.19.0 \
  tinygo build -o example.wasm -target=wasi -no-debug .
$ wasmtime example.wasm

On tinygo v0.21.0 we can see the (expected) panic:

$ wasmtime example.wasm 
panic: unimplemented: (reflect.Type).NumMethod()
Error: failed to run main module `policy.wasm`

Caused by:
    0: failed to invoke command default
    1: wasm trap: unreachable
       wasm backtrace:
           0: 0x4540 - <unknown>!runtime._panic
           1: 0x5231 - <unknown>!(reflect.rawType).NumMethod
           2: 0x274ca - <unknown>!encoding/json.indirect
           3: 0x26311 - <unknown>!(*encoding/json.decodeState).array
           4: 0x22ec9 - <unknown>!(*encoding/json.decodeState).value
           5: 0x2277d - <unknown>!(*encoding/json.decodeState).unmarshal
           6: 0x225b0 - <unknown>!encoding/json.Unmarshal
           7: 0x14ed6 - <unknown>!main.main
           8: 0x14d98 - <unknown>!runtime.run$1
           9: 0x14a15 - <unknown>!_start
flavio commented 2 years ago

I recently stumbled over bytedance/sonic, this looks interesting too. Unfortunately wasm compilation is not possible yet (https://github.com/bytedance/sonic/issues/140), but might be done in the future.

viccuad commented 2 years ago

Tried with tinygo 0.22.0, sadly we are still missing reflection methods, giving us the already shared error.

dkegel-fastly commented 2 years ago

There appear to be no files matching *_test.go in the repo, but kubewarden/go-policy-template uses this project and does have a couple small tests, so I tried that with tinygo:

$ tinygo test ./...
panic: unimplemented: (reflect.Type).Name()

For completeness, I think that's https://github.com/tinygo-org/tinygo/issues/2441