Open flavio opened 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.
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
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).
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
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.
Tried with tinygo 0.22.0, sadly we are still missing reflection methods, giving us the already shared error.
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
Latest release of TinyGo introduces massive improvements, the most notable one is better support of the
Reflect
module, which leads to finally have support ofencoding/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:
encoding/json
: read the policy settings by unmarshalling the incoming bytes into a native Go struct. This is way better than using gjson to painfully build structs.encoding/json
. Try to build an existing policy using vanilla gjson. If everything works as expected we can get rid of our fork:encoding/json
. This makes our code more robust and easier to maintain