Skip to content

Docs/wasm function support#4419

Open
SurbhiAgarwal1 wants to merge 7 commits intokptdev:mainfrom
SurbhiAgarwal1:docs/wasm-function-support
Open

Docs/wasm function support#4419
SurbhiAgarwal1 wants to merge 7 commits intokptdev:mainfrom
SurbhiAgarwal1:docs/wasm-function-support

Conversation

@SurbhiAgarwal1
Copy link
Contributor

Description

WASM functions are supported in kpt but there's no documentation on how to run, develop, or deploy them. This PR adds a comprehensive guide covering the complete WASM function workflow.

Motivation

Users need documentation to understand:

  • How to run WASM functions with the --allow-alpha-wasm flag
  • How to publish WASM modules using kpt alpha wasm push/pull
  • How to develop WASM functions with proper build tags
  • The benefits and limitations of WASM functions vs container-based functions

Without this documentation, users have to dig through code or CLI help to figure out WASM support.

Changes

Added documentation/content/en/book/04-using-functions/wasm-functions.md covering:

  • Running WASM functions with fn render and fn eval
  • Publishing and pulling WASM modules to/from OCI registries
  • Developing Go-based WASM functions with complete code examples
  • Benefits (faster startup, smaller size, better security)
  • Limitations (alpha status, sandboxed execution, compatibility)

The code examples are based on actual WASM functions in krm-functions-catalog (set-namespace, set-labels, starlark) and follow the same pattern with separate build tags for regular and WASM builds.

Fixes #4296

Copilot AI review requested due to automatic review settings February 28, 2026 17:38
@netlify
Copy link

netlify bot commented Feb 28, 2026

Deploy Preview for kptdocs ready!

Name Link
🔨 Latest commit c067758
🔍 Latest deploy log https://app.netlify.com/projects/kptdocs/deploys/69bc2694f444060007946abd
😎 Deploy Preview https://deploy-preview-4419--kptdocs.netlify.app
📱 Preview on mobile
Toggle QR Code...

QR Code

Use your smartphone camera to open QR code link.

To edit notification comments on pull requests, go to your Netlify project configuration.

@dosubot dosubot bot added size:XL This PR changes 500-999 lines, ignoring generated files. documentation Improvements or additions to documentation labels Feb 28, 2026
Copy link
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This PR adds end-user documentation for running/developing/publishing WASM functions in kpt, and (in the same change set) introduces a new CEL-based condition field on Kptfile pipeline functions to enable conditional function execution.

Changes:

  • Adds a comprehensive “Using WASM Functions” guide (run, eval, push/pull, Go build tags, limitations).
  • Extends kptfile.v1.Function with a condition field and evaluates it before executing a function.
  • Adds a CEL evaluator implementation + unit/E2E-style tests, and updates Go module dependencies for CEL.

Reviewed changes

Copilot reviewed 7 out of 8 changed files in this pull request and generated 8 comments.

Show a summary per file
File Description
pkg/api/kptfile/v1/types.go Adds Function.Condition (CEL) field to the Kptfile API.
internal/fnruntime/runner.go Initializes and uses a CEL evaluator to skip function execution when condition is false.
internal/fnruntime/celeval.go New CEL evaluator implementation for evaluating conditions against resource inputs.
internal/fnruntime/celeval_test.go Unit tests for CEL evaluator behavior and errors.
internal/fnruntime/conditional_e2e_test.go End-to-end style tests for conditional execution behavior in FunctionRunner.
go.mod / go.sum Adds github.com/google/cel-go and related transitive dependencies.
documentation/content/en/book/04-using-functions/wasm-functions.md New documentation page describing the WASM function workflow in kpt.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Copilot AI review requested due to automatic review settings February 28, 2026 17:45
Copy link
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Copilot reviewed 7 out of 8 changed files in this pull request and generated 5 comments.


💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment on lines +282 to +287
### Security

WASM functions run in a sandbox:
- No network access
- No filesystem access (except input/output resources)
- Can't execute system commands
Copy link

Copilot AI Feb 28, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The “Security” section states WASM functions have no network/filesystem access and no host access by default. kpt supports both wasmtime (default) and node.js-based runtimes (selectable via KPT_FN_WASM_RUNTIME), and the node.js runtime can expose broader host capabilities to Go WASM via syscall/js. Please qualify these claims (e.g., “with wasmtime runtime…”) to avoid overstating the sandbox guarantees.

Copilot uses AI. Check for mistakes.
@SurbhiAgarwal1 SurbhiAgarwal1 force-pushed the docs/wasm-function-support branch from e979fbb to 1abf41f Compare February 28, 2026 18:03
@dosubot dosubot bot added size:L This PR changes 100-499 lines, ignoring generated files. and removed size:XL This PR changes 500-999 lines, ignoring generated files. labels Feb 28, 2026
@liamfallon liamfallon requested a review from Copilot March 4, 2026 18:58
Copy link
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Copilot reviewed 1 out of 1 changed files in this pull request and generated 3 comments.


💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

You can also share your feedback on Copilot code review. Take the survey.


What this does:
1. Compresses the WASM file into a tar archive
2. Creates an OCI image with `wasm/js` platform
Copy link

Copilot AI Mar 4, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The same incorrect wasm/js platform identifier is used here. It should be js/wasm to match the OCI convention of OS/Architecture, consistent with the code in pkg/wasm/client.go:122-124 where OS: "js" and Architecture: "wasm".

Copilot uses AI. Check for mistakes.
Comment on lines +123 to +151
Here's how to build a Go KRM function for WASM. You need two files - one for regular builds and one for WASM:

`main.go` (regular build):

```go
//go:build !(js && wasm)

package main

import (
"os"

"github.com/kptdev/krm-functions-sdk/go/fn"
)

func main() {
if err := fn.AsMain(fn.ResourceListProcessorFunc(process)); err != nil {
os.Exit(1)
}
}

func process(rl *fn.ResourceList) (bool, error) {
for i := range rl.Items {
// Your transformation logic
rl.Items[i].SetAnnotation("processed-by", "my-fn")
}
return true, nil
}
```
Copy link

Copilot AI Mar 4, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The code example has a compilation issue: main.go defines the process function with the build tag //go:build !(js && wasm), meaning it is excluded during WASM builds. However, main_js.go (with build tag //go:build js && wasm) references process on line 184 via fn.ResourceListProcessorFunc(process). Since process is not available during WASM builds, this code will fail to compile with GOOS=js GOARCH=wasm.

The process function should be placed in a third file (e.g., process.go) without any build tags, so it's available for both regular and WASM builds. This is the typical pattern used in the referenced krm-functions-catalog.

Copilot uses AI. Check for mistakes.
Copilot AI review requested due to automatic review settings March 4, 2026 19:25
Copy link
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Copilot reviewed 1 out of 1 changed files in this pull request and generated 2 comments.


💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment on lines +95 to +96
2. Creates an OCI image with `wasm/js` platform
3. Pushes to the registry
Copy link

Copilot AI Mar 4, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The platform format wasm/js here is inconsistent with OCI conventions. The OCI platform format is OS/Architecture, and looking at pkg/wasm/client.go:122-125, the platform is {OS: "js", Architecture: "wasm"}. This should be written as js/wasm to match the standard OCI platform format.

Copilot uses AI. Check for mistakes.
Copilot AI review requested due to automatic review settings March 5, 2026 04:11
Copy link
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Copilot reviewed 1 out of 1 changed files in this pull request and generated 1 comment.


💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment on lines +186 to +187
// TODO: copy the implementation from main.go so behavior is consistent.
return false, nil
Copy link

Copilot AI Mar 5, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This example process function in the WASM build file has a TODO comment and returns false, nil without implementing any transformation logic. A user copying this documentation example would get a WASM function that does nothing (and returns false indicating no changes). The main.go version adds an annotation, but this version doesn't.

For a documentation example, the implementation should be consistent between the two files. Either duplicate the logic (adding the annotation) here, or refactor the example to show the recommended pattern of extracting shared logic into a separate file (e.g., processor.go) with no build tags, so both builds share the same process function. The latter pattern avoids the exact problem this code demonstrates — divergent implementations in WASM and non-WASM builds.

Suggested change
// TODO: copy the implementation from main.go so behavior is consistent.
return false, nil
changed := false
const annotationKey = "example.com/wasm"
const annotationValue = "processed"
for _, obj := range rl.Items {
if obj == nil {
continue
}
annotations := obj.GetAnnotations()
if existing, ok := annotations[annotationKey]; ok && existing == annotationValue {
continue
}
obj.SetAnnotation(annotationKey, annotationValue)
changed = true
}
return changed, nil

Copilot uses AI. Check for mistakes.
Copy link
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Copilot reviewed 1 out of 1 changed files in this pull request and generated no new comments.


💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Copy link
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Copilot reviewed 1 out of 1 changed files in this pull request and generated 4 comments.


💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment on lines +322 to +323
- [kpt alpha wasm push](../../../reference/cli/alpha/wasm/push/)
- [kpt alpha wasm pull](../../../reference/cli/alpha/wasm/pull/)
Comment on lines +297 to +303
WASM functions run in a sandbox:
- No network access
- No filesystem access (except input/output resources)
- Can't execute system commands

This is more secure but also more restrictive.

Comment on lines +60 to +66
### 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
```
@SurbhiAgarwal1 SurbhiAgarwal1 force-pushed the docs/wasm-function-support branch from 318480e to 76ebd1e Compare March 18, 2026 12:40
Copilot AI review requested due to automatic review settings March 18, 2026 12:41
Copy link
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Copilot reviewed 1 out of 1 changed files in this pull request and generated 4 comments.


💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

kpt fn render my-package --allow-alpha-wasm
```

kpt will detect the function as WASM if the OCI image has a `js/wasm` platform manifest.

- A language that compiles to WASM (Go, Rust, etc.)
- WASM build toolchain
- KRM functions SDK
Comment on lines +297 to +302
WASM functions run in a sandbox:
- No network access
- No filesystem access (except input/output resources)
- Can't execute system commands

This is more secure but also more restrictive.
Comment on lines +183 to +184
<-make(chan bool)
return nil
@SurbhiAgarwal1 SurbhiAgarwal1 force-pushed the docs/wasm-function-support branch from 96cfcf3 to 4f42b2c Compare March 18, 2026 12:49
Copilot AI review requested due to automatic review settings March 18, 2026 12:54
Copy link
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Copilot reviewed 1 out of 1 changed files in this pull request and generated 4 comments.


💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment on lines +296 to +301
WASM functions run in a sandbox:
- No network access
- No filesystem access (except input/output resources)
- Can't execute system commands

This is more secure but also more restrictive.
Comment on lines +321 to +322
- [kpt alpha wasm push](../../../reference/cli/alpha/wasm/push/)
- [kpt alpha wasm pull](../../../reference/cli/alpha/wasm/pull/)
return js.FuncOf(func(this js.Value, args []js.Value) any {
rl, err := fn.ParseResourceList(*resourceList)
if err != nil {
return ""
Copilot AI review requested due to automatic review settings March 18, 2026 20:14
Copy link
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Copilot reviewed 1 out of 1 changed files in this pull request and generated 5 comments.


💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment on lines +179 to +180
js.Global().Set("processResourceList", resourceListWrapper(&resourceList))
js.Global().Set("processResourceListErrors", resourceListErrors(&resourceList))
You can run local `.wasm` files with the `--exec` flag:

```shell
kpt fn eval my-package --allow-alpha-wasm --exec ./my-function.wasm
### Test locally

```shell
kpt fn eval ./test-package --allow-alpha-wasm --exec ./my-function.wasm
GOOS=js GOARCH=wasm go build -o my-function.wasm .

# 3. Test locally
kpt fn eval ./test-package --allow-alpha-wasm --exec ./my-function.wasm
Comment on lines +137 to +153
"os"

"github.com/kptdev/krm-functions-sdk/go/fn"
)

func main() {
if err := fn.AsMain(fn.ResourceListProcessorFunc(process)); err != nil {
os.Exit(1)
}
}

func process(rl *fn.ResourceList) (bool, error) {
for i := range rl.Items {
// Your transformation logic
rl.Items[i].SetAnnotation("processed-by", "my-fn")
}
return true, nil
Add comprehensive documentation for WASM function support in kpt,
covering how to run, develop, and deploy WASM functions.

Closes kptdev#4296

Signed-off-by: Surbhi <agarwalsurbhi1807@gmail.com>
Replace <-make(chan bool) + return nil with select{} to avoid
the unreachable code and clarify the blocking intent.

Signed-off-by: Surbhi <agarwalsurbhi1807@gmail.com>
- Store js.Func values in package-level vars to prevent GC
- Add --allow-exec flag to all fn eval --exec examples
- Fix gofmt indentation in Go code snippets (tabs not spaces)

Signed-off-by: Surbhi <agarwalsurbhi1807@gmail.com>
@SurbhiAgarwal1 SurbhiAgarwal1 force-pushed the docs/wasm-function-support branch from e316453 to 889fc1b Compare March 18, 2026 21:10
@SurbhiAgarwal1
Copy link
Contributor Author

Hey @liamfallon ,I had addressed all the Copilot review comments:

  • js.Func GC issue: stored processResourceListFunc and processResourceListErrorsFunc as package-level variables so they're kept alive for the program lifetime and won't be garbage collected
  • Missing --allow-exec: added the flag to all three fn eval --exec examples (local WASM files section, Test locally section, and Complete example section)
  • gofmt formatting: fixed indentation in both Go snippets to use tabs instead of spaces
  • DCO: rebased all commits with --signoff to fix the missing sign-off on the Copilot autofix commit

Should be good to go now!

- WASM build toolchain
- KRM functions SDK

### Example: Go WASM function
Copy link
Contributor

Choose a reason for hiding this comment

The 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

Push to a registry:

```shell
kpt alpha wasm push ./my-function.wasm gcr.io/my-org/my-wasm-fn:v1.0.0
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Avoid referencing gcr.io
Maybe use a dummy registry example


- [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](../../../reference/cli/alpha/wasm/push/)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Use relrefs, and make sure the docs preview renders the links correctly.
eg: ({{% relref "/reference/cli/alpha/wasm/push" %}})

- Replace Go code examples with link to starlark example in krm-functions-catalog
- Replace gcr.io registry references with example.registry.io dummy registry

Signed-off-by: Surbhi <agarwalsurbhi1807@gmail.com>
Copilot AI review requested due to automatic review settings March 19, 2026 12:18
Copy link
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Copilot reviewed 1 out of 1 changed files in this pull request and generated 4 comments.


💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.


- A language that compiles to WASM (Go, Rust, etc.)
- WASM build toolchain
- KRM functions SDK
You can run local `.wasm` files with the `--exec` flag:

```shell
kpt fn eval my-package --allow-alpha-wasm --allow-exec --exec ./my-function.wasm
kpt fn render my-package --allow-alpha-wasm
```

kpt will detect the function as WASM if the OCI image has a `js/wasm` platform manifest.
Co-authored-by: Copilot Autofix powered by AI <175728472+Copilot@users.noreply.github.com>
Copilot AI review requested due to automatic review settings March 19, 2026 12:27
Copy link
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Copilot reviewed 1 out of 1 changed files in this pull request and generated 3 comments.


💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment on lines +219 to +220
- [kpt alpha wasm push](../../../reference/cli/alpha/wasm/push/)
- [kpt alpha wasm pull](../../../reference/cli/alpha/wasm/pull/)
Co-authored-by: Copilot Autofix powered by AI <175728472+Copilot@users.noreply.github.com>
Copilot AI review requested due to automatic review settings March 19, 2026 16:38
Co-authored-by: Copilot Autofix powered by AI <175728472+Copilot@users.noreply.github.com>
Copy link
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Copilot reviewed 1 out of 1 changed files in this pull request and generated 3 comments.


💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

### Test locally

```shell
kpt fn eval ./test-package --allow-alpha-wasm --allow-exec --exec ./my-function.wasm
Comment on lines +162 to +164
# 3. Test locally
kpt fn eval ./test-package --allow-alpha-wasm --exec ./my-function.wasm

kpt fn render my-package --allow-alpha-wasm
```

kpt will detect the function as WASM if the OCI image has a `js/wasm` platform manifest.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

documentation Improvements or additions to documentation size:L This PR changes 100-499 lines, ignoring generated files.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Document WASM function support in kpt

5 participants