Skip to content

Commit 889fc1b

Browse files
docs: fix Copilot review issues in wasm-functions.md
- 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>
1 parent f0c62a4 commit 889fc1b

File tree

1 file changed

+67
-59
lines changed

1 file changed

+67
-59
lines changed

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

Lines changed: 67 additions & 59 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
---
1+
---
22
title: "Using WASM Functions"
33
linkTitle: "Using WASM Functions"
44
weight: 5
@@ -62,7 +62,7 @@ kpt fn eval my-package --allow-alpha-wasm -i gcr.io/my-org/my-wasm-fn:v1.0.0 --
6262
You can run local `.wasm` files with the `--exec` flag:
6363

6464
```shell
65-
kpt fn eval my-package --allow-alpha-wasm --exec ./my-function.wasm
65+
kpt fn eval my-package --allow-alpha-wasm --allow-exec --exec ./my-function.wasm
6666
```
6767

6868
You can also declare local WASM files in your `Kptfile`:
@@ -134,23 +134,23 @@ Here's how to build a Go KRM function for WASM. You need two files - one for reg
134134
package main
135135

136136
import (
137-
"os"
138-
139-
"github.com/kptdev/krm-functions-sdk/go/fn"
137+
"os"
138+
139+
"github.com/kptdev/krm-functions-sdk/go/fn"
140140
)
141141

142142
func main() {
143-
if err := fn.AsMain(fn.ResourceListProcessorFunc(process)); err != nil {
144-
os.Exit(1)
145-
}
143+
if err := fn.AsMain(fn.ResourceListProcessorFunc(process)); err != nil {
144+
os.Exit(1)
145+
}
146146
}
147147

148148
func process(rl *fn.ResourceList) (bool, error) {
149-
for i := range rl.Items {
150-
// Your transformation logic
151-
rl.Items[i].SetAnnotation("processed-by", "my-fn")
152-
}
153-
return true, nil
149+
for i := range rl.Items {
150+
// Your transformation logic
151+
rl.Items[i].SetAnnotation("processed-by", "my-fn")
152+
}
153+
return true, nil
154154
}
155155
```
156156

@@ -162,69 +162,77 @@ func process(rl *fn.ResourceList) (bool, error) {
162162
package main
163163

164164
import (
165-
"syscall/js"
166-
167-
"github.com/kptdev/krm-functions-sdk/go/fn"
165+
"syscall/js"
166+
167+
"github.com/kptdev/krm-functions-sdk/go/fn"
168+
)
169+
170+
// Keep js.Func values referenced at package level to prevent garbage collection.
171+
var (
172+
processResourceListFunc js.Func
173+
processResourceListErrorsFunc js.Func
168174
)
169175

170176
func main() {
171-
if err := run(); err != nil {
172-
panic(err)
173-
}
177+
if err := run(); err != nil {
178+
panic(err)
179+
}
174180
}
175181

176182
func run() error {
177-
resourceList := []byte("")
178-
179-
js.Global().Set("processResourceList", resourceListWrapper(&resourceList))
180-
js.Global().Set("processResourceListErrors", resourceListErrors(&resourceList))
181-
182-
// Keep the program running
183-
select {}
183+
resourceList := []byte("")
184+
185+
processResourceListFunc = resourceListWrapper(&resourceList)
186+
js.Global().Set("processResourceList", processResourceListFunc)
187+
processResourceListErrorsFunc = resourceListErrors(&resourceList)
188+
js.Global().Set("processResourceListErrors", processResourceListErrorsFunc)
189+
190+
// Keep the program running
191+
select {}
184192
}
185193

186194
// process applies the same transformation logic as in the non-WASM build.
187195
func process(rl *fn.ResourceList) (bool, error) {
188-
for i := range rl.Items {
189-
// Your transformation logic
190-
rl.Items[i].SetAnnotation("processed-by", "my-fn")
191-
}
192-
return true, nil
196+
for i := range rl.Items {
197+
// Your transformation logic
198+
rl.Items[i].SetAnnotation("processed-by", "my-fn")
199+
}
200+
return true, nil
193201
}
194202

195203
func transform(input []byte) ([]byte, error) {
196-
return fn.Run(fn.ResourceListProcessorFunc(process), input)
204+
return fn.Run(fn.ResourceListProcessorFunc(process), input)
197205
}
198206

199207
func resourceListWrapper(resourceList *[]byte) js.Func {
200-
return js.FuncOf(func(this js.Value, args []js.Value) any {
201-
if len(args) != 1 {
202-
return "Invalid number of arguments"
203-
}
204-
input := args[0].String()
205-
output, err := transform([]byte(input))
206-
*resourceList = output
207-
if err != nil {
208-
return "unable to process: " + err.Error()
209-
}
210-
return string(output)
211-
})
208+
return js.FuncOf(func(this js.Value, args []js.Value) any {
209+
if len(args) != 1 {
210+
return "Invalid number of arguments"
211+
}
212+
input := args[0].String()
213+
output, err := transform([]byte(input))
214+
*resourceList = output
215+
if err != nil {
216+
return "unable to process: " + err.Error()
217+
}
218+
return string(output)
219+
})
212220
}
213221

214222
func resourceListErrors(resourceList *[]byte) js.Func {
215-
return js.FuncOf(func(this js.Value, args []js.Value) any {
216-
rl, err := fn.ParseResourceList(*resourceList)
217-
if err != nil {
218-
return ""
219-
}
220-
errors := ""
221-
for _, r := range rl.Results {
222-
if r.Severity == "error" {
223-
errors += r.Message
224-
}
225-
}
226-
return errors
227-
})
223+
return js.FuncOf(func(this js.Value, args []js.Value) any {
224+
rl, err := fn.ParseResourceList(*resourceList)
225+
if err != nil {
226+
return ""
227+
}
228+
errors := ""
229+
for _, r := range rl.Results {
230+
if r.Severity == "error" {
231+
errors += r.Message
232+
}
233+
}
234+
return errors
235+
})
228236
}
229237
```
230238

@@ -237,7 +245,7 @@ GOOS=js GOARCH=wasm go build -o my-function.wasm .
237245
### Test locally
238246

239247
```shell
240-
kpt fn eval ./test-package --allow-alpha-wasm --exec ./my-function.wasm
248+
kpt fn eval ./test-package --allow-alpha-wasm --allow-exec --exec ./my-function.wasm
241249
```
242250

243251
### Publish
@@ -262,7 +270,7 @@ From development to deployment:
262270
GOOS=js GOARCH=wasm go build -o my-function.wasm .
263271

264272
# 3. Test locally
265-
kpt fn eval ./test-package --allow-alpha-wasm --exec ./my-function.wasm
273+
kpt fn eval ./test-package --allow-alpha-wasm --allow-exec --exec ./my-function.wasm
266274

267275
# 4. Publish
268276
kpt alpha wasm push ./my-function.wasm gcr.io/my-org/my-wasm-fn:v1.0.0

0 commit comments

Comments
 (0)