Skip to content

Commit

Permalink
experiment: compiling wasm for use in docs
Browse files Browse the repository at this point in the history
  • Loading branch information
ndbaker1 committed Dec 6, 2024
1 parent da3591e commit 75946a6
Show file tree
Hide file tree
Showing 8 changed files with 651 additions and 3 deletions.
2 changes: 1 addition & 1 deletion nodeadm/internal/configprovider/file.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,5 +35,5 @@ func (fcs *fileConfigProvider) Provide() (*internalapi.NodeConfig, error) {
if err != nil {
return nil, err
}
return parseMaybeMultipart(data)
return ParseMaybeMultipart(data)
}
2 changes: 1 addition & 1 deletion nodeadm/internal/configprovider/mime.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import (
apibridge "github.com/awslabs/amazon-eks-ami/nodeadm/internal/api/bridge"
)

func parseMaybeMultipart(data []byte) (*internalapi.NodeConfig, error) {
func ParseMaybeMultipart(data []byte) (*internalapi.NodeConfig, error) {
// if the MIME data fails to parse as a multipart document, then fall back
// to parsing the entire userdata as the node config.
if multipartReader, err := getMultipartReader(data); err == nil {
Expand Down
2 changes: 1 addition & 1 deletion nodeadm/internal/configprovider/userdata.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,5 +49,5 @@ func (p *userDataConfigProvider) Provide() (*internalapi.NodeConfig, error) {
if err != nil {
return nil, fmt.Errorf("failed to decompress user data: %v", err)
}
return parseMaybeMultipart(userData)
return ParseMaybeMultipart(userData)
}
28 changes: 28 additions & 0 deletions nodeadm/wasm/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
# WASM assets

A quick guide to building WASM binaries using nodeadm functions.

you can see the [go.dev webassembly](https://go.dev/wiki/WebAssembly) wiki for
greater detail, but the high level steps involved are:

cd into the nodeadm directory
```
git clone [email protected]:awslabs/amazon-eks-ami.git
cd amazon-eks-ami/nodeadm
```

Copy the javascript glue code from
```
cp "$(go env GOROOT)/misc/wasm/wasm_exec.js" ./wasm/assets/
```

Compile the wasm binary
```
GOOS=js GOARCH=wasm go build -o ./wasm/assets/main.wasm wasm/check/main.go
```

Serve the assets
```
cd ./wasm/assets
python -m http.server # or http server variant
```
23 changes: 23 additions & 0 deletions nodeadm/wasm/assets/index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<html>
<head>
<script type="module" src="https://cdn.jsdelivr.net/gh/vanillawc/wc-monaco-editor@1/index.js"></script>
<meta charset="utf-8"/>
<script src="wasm_exec.js"></script>
<script>
const go = new Go();
WebAssembly.instantiateStreaming(fetch("main.wasm"), go.importObject).then((result) => {
go.run(result.instance);
});
function check() {
document.getElementById("response").value = nodeadmCheck(editor.value)
}
</script>
</head>
<body>
<div style="display: grid; margin: auto; width: 90vw">
<wc-monaco-editor style="height:80vh" language="yaml"></wc-monaco-editor>
<button onclick="check()">check</button>
<textarea style="height:15vh" id="response"></textarea>
</div>
</body>
</html>
Binary file added nodeadm/wasm/assets/main.wasm
Binary file not shown.
Loading

0 comments on commit 75946a6

Please sign in to comment.