-
Notifications
You must be signed in to change notification settings - Fork 257
Docs/wasm function support #4419
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
SurbhiAgarwal1
wants to merge
8
commits into
kptdev:main
Choose a base branch
from
SurbhiAgarwal1:docs/wasm-function-support
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+197
−3
Open
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
3327b4b
docs: Add WASM function support documentation
SurbhiAgarwal1 f0c62a4
docs: fix unreachable return nil in WASM run() example
SurbhiAgarwal1 889fc1b
docs: fix Copilot review issues in wasm-functions.md
SurbhiAgarwal1 23b2b31
docs: address efiacor review comments on wasm-functions.md
SurbhiAgarwal1 361de49
Potential fix for pull request finding
SurbhiAgarwal1 d22a898
Potential fix for pull request finding
SurbhiAgarwal1 c067758
Potential fix for pull request finding
SurbhiAgarwal1 0a1f8b1
docs: resolve WASM function support feedback and logic fixes
SurbhiAgarwal1 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Some comments aren't visible on the classic Files Changed page.
There are no files selected for viewing
188 changes: 188 additions & 0 deletions
188
documentation/content/en/book/04-using-functions/wasm-functions.md
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,188 @@ | ||
| --- | ||
| title: "Using WASM Functions" | ||
| linkTitle: "Using WASM Functions" | ||
| weight: 5 | ||
| description: > | ||
| How to run, develop, and deploy WebAssembly (WASM) functions with kpt. | ||
| --- | ||
|
|
||
| WASM functions are an alternative to container-based KRM functions. They're faster to start, smaller to distribute, and run in a sandboxed environment. | ||
|
|
||
| ## Why use WASM functions? | ||
|
|
||
| {{< warning type=warning title="Note" >}} | ||
| WASM support in kpt is currently in alpha and not ready for production use. The API and behavior may change in future releases. | ||
| {{< /warning >}} | ||
|
|
||
| WASM functions have some advantages over container-based functions: | ||
|
|
||
| - Faster startup - no container runtime needed | ||
| - Smaller size - WASM modules are typically much smaller than container images | ||
| - Better isolation - sandboxed execution with limited host access | ||
| - More portable - run anywhere WASM is supported | ||
| - Lower resource usage | ||
|
|
||
| ## Running WASM functions | ||
|
|
||
| WASM support is an alpha feature. You need to use the `--allow-alpha-wasm` flag to enable it. | ||
|
|
||
|
Comment on lines
+27
to
+28
|
||
| ### With `fn render` | ||
|
|
||
| Add the WASM function to your Kptfile pipeline: | ||
|
|
||
| ```yaml | ||
| # Kptfile | ||
| apiVersion: kpt.dev/v1 | ||
| kind: Kptfile | ||
| metadata: | ||
| name: my-package | ||
| pipeline: | ||
| mutators: | ||
| - image: example.registry.io/my-org/my-wasm-fn:v1.0.0 | ||
| configMap: | ||
| key: value | ||
| ``` | ||
|
|
||
| ```shell | ||
| kpt fn render my-package --allow-alpha-wasm | ||
| ``` | ||
|
|
||
| kpt will use the WASM runtime for an OCI image if the `--allow-alpha-wasm` flag is present and the image has a `js/wasm` platform manifest. | ||
|
|
||
| ### With `fn eval` | ||
|
|
||
| Run WASM functions imperatively: | ||
|
|
||
| ```shell | ||
| kpt fn eval my-package --allow-alpha-wasm -i example.registry.io/my-org/my-wasm-fn:v1.0.0 -- key=value | ||
| ``` | ||
|
|
||
|
|
||
| ### Using local WASM files | ||
|
|
||
| You can run local `.wasm` files with the `--exec` flag: | ||
|
|
||
| ```shell | ||
| kpt fn eval my-package --allow-alpha-wasm --exec ./my-function.wasm | ||
| ``` | ||
|
Comment on lines
+60
to
+66
|
||
|
|
||
| You can also declare local WASM files in your `Kptfile`: | ||
|
|
||
| ```yaml | ||
| # Kptfile | ||
| apiVersion: kpt.dev/v1 | ||
| kind: Kptfile | ||
| metadata: | ||
| name: my-package | ||
| pipeline: | ||
| mutators: | ||
| - exec: ./functions/my-function.wasm | ||
| ``` | ||
|
|
||
| ```shell | ||
| kpt fn render my-package --allow-alpha-wasm | ||
| ``` | ||
|
|
||
| Note: Using local WASM files makes your package less portable since the file needs to exist on every system. | ||
|
|
||
| ## Publishing WASM functions | ||
|
|
||
| ### Push a WASM module | ||
|
|
||
| Compress and push a WASM module to an OCI registry: | ||
|
|
||
| ```shell | ||
| kpt alpha wasm push ./my-function.wasm example.registry.io/my-org/my-wasm-fn:v1.0.0 | ||
| ``` | ||
|
|
||
| What this does: | ||
| 1. Compresses the WASM file into a tar archive | ||
| 2. Creates an OCI image with `js/wasm` platform | ||
| 3. Pushes to the registry | ||
|
SurbhiAgarwal1 marked this conversation as resolved.
|
||
|
|
||
| ### Pull a WASM module | ||
|
|
||
| Download and decompress a WASM module: | ||
|
|
||
| ```shell | ||
| kpt alpha wasm pull example.registry.io/my-org/my-wasm-fn:v1.0.0 ./my-function.wasm | ||
| ``` | ||
|
|
||
| Useful for: | ||
| - Testing WASM functions locally | ||
| - Inspecting modules | ||
| - Offline caching | ||
|
|
||
| ## Developing WASM functions | ||
|
|
||
| WASM functions follow the [KRM Functions Specification](https://github.com/kubernetes-sigs/kustomize/blob/master/cmd/config/docs/api-conventions/functions-spec.md). They receive a `ResourceList` as input and return a `ResourceList` as output. | ||
|
|
||
| ### What you need | ||
|
|
||
| - A language that compiles to WASM (Go, Rust, etc.) | ||
| - WASM build toolchain | ||
| - KRM functions SDK | ||
|
|
||
|
|
||
| ### Example: Go WASM function | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I would avoid adding code examples to the docs and maybe ref/link to an example function instead |
||
|
|
||
| For a complete working example of a Go WASM function, see the [starlark function in the krm-functions-catalog](https://github.com/kptdev/krm-functions-catalog/tree/main/functions/go/starlark), which shows the two-file pattern (`run.go` for regular builds and `run_js.go` for WASM builds). | ||
|
|
||
| ### Build for WASM | ||
|
|
||
| ```shell | ||
| GOOS=js GOARCH=wasm go build -o my-function.wasm . | ||
| ``` | ||
|
|
||
| ### Test locally | ||
|
|
||
| ```shell | ||
| kpt fn eval ./test-package --allow-alpha-wasm --exec ./my-function.wasm | ||
| ``` | ||
|
|
||
| ### Publish | ||
|
|
||
| Push to a registry: | ||
|
|
||
| ```shell | ||
| kpt alpha wasm push ./my-function.wasm example.registry.io/my-org/my-wasm-fn:v1.0.0 | ||
|
|
||
| # Test the published version | ||
| kpt fn eval ./test-package --allow-alpha-wasm -i example.registry.io/my-org/my-wasm-fn:v1.0.0 | ||
| ``` | ||
|
|
||
| ## Mixed pipelines | ||
|
|
||
| {{< note title="Limitation" >}} | ||
| Currently, enabling `--allow-alpha-wasm` will cause kpt to attempt to run all **image-based** functions in the pipeline using the WASM runtime. Mixed pipelines containing both WASM and standard container images are not yet fully supported. However, mixed pipelines with local `.wasm` files and standard executables are supported. | ||
| {{< /note >}} | ||
|
|
||
| ## Limitations | ||
|
|
||
| ### Security | ||
|
|
||
| WASM functions run in a sandboxed environment with restricted access. The current implementation using Wasmtime provides the following protections: | ||
| - No network access by default. | ||
| - Restricted filesystem access (functions cannot access host files outside the KRM interface). | ||
| - No ability to execute system commands. | ||
|
|
||
| These restrictions provide a higher level of isolation compared to traditional executables, though they may limit some use cases. | ||
|
|
||
| ### Performance | ||
|
|
||
| - Faster startup than containers as no container runtime overhead is involved. | ||
| - CPU-intensive operations may be slower than native execution. | ||
| - Memory usage patterns differ from container-based functions. | ||
|
|
||
| ### Compatibility | ||
|
|
||
| Not all container functions can convert to WASM, especially those needing: | ||
| - Host network access. | ||
| - Complex system dependencies. | ||
| - OS-specific features (e.g. syscalls not supported by WASI). | ||
|
|
||
| ## See also | ||
|
|
||
| - [KRM Functions Specification](https://github.com/kubernetes-sigs/kustomize/blob/master/cmd/config/docs/api-conventions/functions-spec.md) | ||
| - [Functions Catalog](https://catalog.kpt.dev/) | ||
| - [kpt alpha wasm push]({{< relref "/reference/cli/alpha/wasm/push" >}}) | ||
| - [kpt alpha wasm pull]({{< relref "/reference/cli/alpha/wasm/pull" >}}) | ||
| - [WASM function examples](https://github.com/kptdev/krm-functions-catalog/tree/main/functions/go) | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.