diff --git a/cli/fn/cmdeval/cmdeval.go b/cli/fn/cmdeval/cmdeval.go index cfd589629b..d7f00722a4 100644 --- a/cli/fn/cmdeval/cmdeval.go +++ b/cli/fn/cmdeval/cmdeval.go @@ -103,8 +103,8 @@ func GetEvalFnRunner(ctx context.Context, parent string) *EvalFnRunner { }) r.Command.Flags().BoolVar( - &r.RunnerOptions.AllowWasm, "allow-alpha-wasm", false, "allow alpha wasm functions to be run."+ - " If true, you can specify a wasm image with --image flag or a path to a wasm file (must have the .wasm file extension) with --exec flag.") + &r.RunnerOptions.AllowWasm, "allow-alpha-wasm", false, "support for WASM functions has been removed from kpt."+ + " If true, running a function given with --image, or a .wasm file given with --exec, fails with an error.") // selector flags r.Command.Flags().StringVar( diff --git a/cli/fn/render/cmdrender.go b/cli/fn/render/cmdrender.go index d4993f4de6..6fa6eeb7c7 100644 --- a/cli/fn/render/cmdrender.go +++ b/cli/fn/render/cmdrender.go @@ -68,7 +68,7 @@ func NewRunner(ctx context.Context, parent string) *Runner { c.Flags().BoolVar(&r.RunnerOptions.AllowNetwork, "allow-network", false, "allow functions to access network during pipeline execution.") c.Flags().BoolVar(&r.RunnerOptions.AllowWasm, "allow-alpha-wasm", r.RunnerOptions.AllowWasm, - "allow wasm to be used during pipeline execution.") + "support for WASM functions has been removed from kpt. If true, every pipeline function fails with an error.") cmdutil.FixDocs("kpt", parent, c) r.Command = c return r diff --git a/e2e/testdata/fn-eval/wasm-function/.expected/config.yaml b/e2e/testdata/fn-eval/wasm-function/.expected/config.yaml index 2473a37ea2..21d9ef4bc8 100644 --- a/e2e/testdata/fn-eval/wasm-function/.expected/config.yaml +++ b/e2e/testdata/fn-eval/wasm-function/.expected/config.yaml @@ -1,4 +1,4 @@ -# Copyright 2022 The kpt Authors +# Copyright 2022-2026 The kpt Authors # # Licensed under the Apache License, Version 2.0 (the "License"); # you may not use this file except in compliance with the License. @@ -17,3 +17,5 @@ image: ghcr.io/kptdev/krm-functions-catalog/wasm/set-namespace:v0.5.1 args: namespace: staging allowWasm: true +exitCode: 1 +stdErr: "support for WASM functions has been removed from kpt" diff --git a/e2e/testdata/fn-eval/wasm-function/.expected/diff.patch b/e2e/testdata/fn-eval/wasm-function/.expected/diff.patch deleted file mode 100644 index fd8c99c2cd..0000000000 --- a/e2e/testdata/fn-eval/wasm-function/.expected/diff.patch +++ /dev/null @@ -1,11 +0,0 @@ -diff --git a/resources.yaml b/resources.yaml -index 903c6a5..98b8a68 100644 ---- a/resources.yaml -+++ b/resources.yaml -@@ -15,5 +15,6 @@ apiVersion: apps/v1 - kind: Deployment - metadata: - name: nginx-deployment -+ namespace: staging - spec: - replicas: 3 diff --git a/e2e/testdata/fn-render/basicpipeline-wasm/.expected/config.yaml b/e2e/testdata/fn-render/basicpipeline-wasm/.expected/config.yaml index c496ddff01..8396fdf3af 100644 --- a/e2e/testdata/fn-render/basicpipeline-wasm/.expected/config.yaml +++ b/e2e/testdata/fn-render/basicpipeline-wasm/.expected/config.yaml @@ -1,4 +1,4 @@ -# Copyright 2022 The kpt Authors +# Copyright 2022-2026 The kpt Authors # # Licensed under the Apache License, Version 2.0 (the "License"); # you may not use this file except in compliance with the License. @@ -14,3 +14,5 @@ sequential: true allowWasm: true +exitCode: 1 +stdErr: "support for WASM functions has been removed from kpt" diff --git a/e2e/testdata/fn-render/basicpipeline-wasm/.expected/diff.patch b/e2e/testdata/fn-render/basicpipeline-wasm/.expected/diff.patch index 19a2818f6c..bb3f2469e8 100644 --- a/e2e/testdata/fn-render/basicpipeline-wasm/.expected/diff.patch +++ b/e2e/testdata/fn-render/basicpipeline-wasm/.expected/diff.patch @@ -1,52 +1,22 @@ diff --git a/Kptfile b/Kptfile -index 17a7822..98fa855 100644 +index 17a7822..b3032ae 100644 --- a/Kptfile +++ b/Kptfile -@@ -12,3 +12,22 @@ pipeline: +@@ -12,3 +12,17 @@ pipeline: - image: ghcr.io/kptdev/krm-functions-catalog/wasm/set-labels:v0.2.4 configMap: tier: backend +status: + conditions: + - type: Rendered -+ status: "True" -+ reason: RenderSuccess ++ status: "False" ++ reason: RenderFailed ++ message: |- ++ pkg.render: pkg .: ++ pipeline.run: support for WASM functions has been removed from kpt, run the function as a container image or an executable instead + renderStatus: + mutationSteps: + - image: ghcr.io/kptdev/krm-functions-catalog/wasm/set-namespace:v0.5.1 -+ exitCode: 0 -+ results: -+ - message: namespace [default] updated to "staging", 1 value(s) changed -+ severity: info -+ - message: all `depends-on` annotations are up-to-date. no `namespace` changed -+ severity: info -+ - image: ghcr.io/kptdev/krm-functions-catalog/wasm/set-labels:v0.2.4 -+ exitCode: 0 -+ results: -+ - message: set 4 labels in total -+ severity: info -diff --git a/resources.yaml b/resources.yaml -index eed43d6..c1de2b0 100644 ---- a/resources.yaml -+++ b/resources.yaml -@@ -15,12 +15,20 @@ apiVersion: apps/v1 - kind: Deployment - metadata: - name: nginx-deployment -+ namespace: staging -+ labels: -+ tier: backend - spec: - replicas: 3 -+ selector: -+ matchLabels: -+ tier: backend - --- - apiVersion: custom.io/v1 - kind: Custom - metadata: - name: custom -+ labels: -+ tier: backend - spec: - image: nginx:1.2.3 ++ executionError: support for WASM functions has been removed from kpt, run the function as a container image or an executable instead ++ exitCode: 1 ++ errorSummary: 'ghcr.io/kptdev/krm-functions-catalog/wasm/set-namespace:v0.5.1: support for WASM functions has been removed from kpt, run the function as a container image or an executable instead' diff --git a/e2e/testdata/fn-render/basicpipeline-wasm/.expected/setup.sh b/e2e/testdata/fn-render/basicpipeline-wasm/.expected/setup.sh deleted file mode 100755 index bcf8bc12c1..0000000000 --- a/e2e/testdata/fn-render/basicpipeline-wasm/.expected/setup.sh +++ /dev/null @@ -1,16 +0,0 @@ -#! /usr/bin/env bash -# Copyright 2022 The kpt Authors -# -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. - -export KPT_FN_WASM_RUNTIME=nodejs diff --git a/e2e/testdata/fn-render/basicpipeline-wasm/.expected/teardown.sh b/e2e/testdata/fn-render/basicpipeline-wasm/.expected/teardown.sh deleted file mode 100755 index 595dfa4d80..0000000000 --- a/e2e/testdata/fn-render/basicpipeline-wasm/.expected/teardown.sh +++ /dev/null @@ -1,16 +0,0 @@ -#! /usr/bin/env bash -# Copyright 2022 The kpt Authors -# -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. - -unset KPT_FN_WASM_RUNTIME diff --git a/go.mod b/go.mod index 7ffb9190a4..89532b4572 100644 --- a/go.mod +++ b/go.mod @@ -7,7 +7,6 @@ go 1.26.9 require ( github.com/Masterminds/semver/v3 v3.5.0 - github.com/bytecodealliance/wasmtime-go v1.0.0 github.com/cpuguy83/go-md2man/v2 v2.0.7 github.com/distribution/reference v0.6.0 github.com/go-errors/errors v1.5.1 @@ -18,7 +17,6 @@ require ( github.com/otiai10/copy v1.14.1 github.com/philopon/go-toposort v0.0.0-20170620085441-9be86dbd762f github.com/pkg/errors v0.9.1 - github.com/prep/wasmexec v0.0.0-20220807105708-6554945c1dec github.com/regclient/regclient v0.11.5 github.com/spf13/cobra v1.10.2 github.com/stretchr/testify v1.11.1 diff --git a/go.sum b/go.sum index 55307c3b09..0a32b205a7 100644 --- a/go.sum +++ b/go.sum @@ -14,8 +14,6 @@ github.com/beorn7/perks v1.0.1 h1:VlbKKnNfV8bJzeqoa4cOKqO6bYr3WgKZxO8Z16+hsOM= github.com/beorn7/perks v1.0.1/go.mod h1:G2ZrVWU2WbWT9wwq4/hrbKbnv/1ERSJQ0ibhJ6rlkpw= github.com/blang/semver/v4 v4.0.0 h1:1PFHFE6yCCTv8C1TeyNNarDzntLi7wMI5i/pzqYIsAM= github.com/blang/semver/v4 v4.0.0/go.mod h1:IbckMUScFkM3pff0VJDNKRiT6TG/YpiHIM2yvyW5YoQ= -github.com/bytecodealliance/wasmtime-go v1.0.0 h1:9u9gqaUiaJeN5IoD1L7egD8atOnTGyJcNp8BhkL9cUU= -github.com/bytecodealliance/wasmtime-go v1.0.0/go.mod h1:jjlqQbWUfVSbehpErw3UoWFndBXRRMvfikYH6KsCwOg= github.com/cespare/xxhash/v2 v2.3.0 h1:UL815xU9SqsFlibzuggzjXhog7bL6oX9BbNZnL2UFvs= github.com/cespare/xxhash/v2 v2.3.0/go.mod h1:VGX0DQ3Q6kWi7AoAeZDth3/j3BFtOZR5XLFGgcrjCOs= github.com/chai2010/gettext-go v1.0.3 h1:9liNh8t+u26xl5ddmWLmsOsdNLwkdRTg5AG+JnTiM80= @@ -164,8 +162,6 @@ github.com/pkg/errors v0.9.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINE github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= github.com/pmezard/go-difflib v1.0.1-0.20181226105442-5d4384ee4fb2 h1:Jamvg5psRIccs7FGNTlIRMkT8wgtp5eCXdBlqhYGL6U= github.com/pmezard/go-difflib v1.0.1-0.20181226105442-5d4384ee4fb2/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= -github.com/prep/wasmexec v0.0.0-20220807105708-6554945c1dec h1:Yz85KaIsPzF0YRrznNaD35o4GEk65/3mqio3m9sgqi4= -github.com/prep/wasmexec v0.0.0-20220807105708-6554945c1dec/go.mod h1:/AG7CoBOwtk42jdCTLbd280PA4h50oaFK88jee+BaVA= github.com/prometheus/client_golang v1.23.2 h1:Je96obch5RDVy3FDMndoUsjAhG5Edi49h0RJWRi/o0o= github.com/prometheus/client_golang v1.23.2/go.mod h1:Tb1a6LWHB3/SPIzCoaDXI4I8UHKeFTEQ1YCr+0Gyqmg= github.com/prometheus/client_model v0.6.2 h1:oBsgwpGs7iVziMvrGhE53c/GrLUsZdHnqNwqPLxwZyk= diff --git a/make/info.mk b/make/info.mk index 1d2458c594..1a00d91096 100644 --- a/make/info.mk +++ b/make/info.mk @@ -24,8 +24,6 @@ YEAR_GEN := $(shell date '+%Y') GOBIN := $(shell go env GOPATH)/bin KPT_VERSION := $(shell date '+development-%Y-%m-%dT%H:%M:%S') -export KPT_FN_WASM_RUNTIME ?= nodejs - LDFLAGS := -ldflags "-X main.version=${KPT_VERSION} ifeq ($(OS),Windows_NT) # Do nothing diff --git a/pkg/fn/runtime/jsglue.go b/pkg/fn/runtime/jsglue.go deleted file mode 100644 index 1aad3d7405..0000000000 --- a/pkg/fn/runtime/jsglue.go +++ /dev/null @@ -1,549 +0,0 @@ -// Copyright 2022,2026 The kpt Authors -// -// Licensed under the Apache License, Version 2.0 (the "License"); -// you may not use this file except in compliance with the License. -// You may obtain a copy of the License at -// -// http://www.apache.org/licenses/LICENSE-2.0 -// -// Unless required by applicable law or agreed to in writing, software -// distributed under the License is distributed on an "AS IS" BASIS, -// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -// See the License for the specific language governing permissions and -// limitations under the License. - -package runtime - -// The glue code below has been tested with node.js v18.6.0. -// It may need some small changes to work with v14 and v16. - -var glueCode = `// The code below run the wasm function. -const go = new Go(); -// The path to the wasm artifact is provided by env var to enable the reusibility of this script. -const wasmPath = process.env.` + WasmPathEnv + ` -if (!wasmPath) { - throw 'environment variable KPT_FN_WASM_PATH is expected to be set by the kpt wasm runtime' -} -const wasmBuffer = fs.readFileSync(wasmPath) -// Read the resourceList from stdin -var rl = fs.readFileSync(0, 'utf-8') -WebAssembly.instantiate(wasmBuffer, go.importObject).then((result) => { - go.run(result.instance); - var result = ` + jsEntrypointFunction + `(rl); - - // On success, output to console, on failure retrieve and output the error message - if (result.includes("kind: ResourceList")) { - console.log(result) - } else { - console.log(` + jsEntrypointFunction + "Errors" + `(rl)); - } -});` - -const golangWasmJSCode = `const fs = require('fs'); -const { - performance -} = require('perf_hooks'); -const crypto = require('crypto'); - -// The JS glue code below mostly comes from -// https://github.com/golang/go/blob/master/misc/wasm/wasm_exec.js with minor -// changes. "misc/wasm/wasm_exec.js" is shipped with all golang installation. - -"use strict"; -(() => { - const enosys = () => { - const err = new Error("not implemented"); - err.code = "ENOSYS"; - return err; - }; - if (!globalThis.fs) { - let outputBuf = ""; - globalThis.fs = { - constants: { O_WRONLY: -1, O_RDWR: -1, O_CREAT: -1, O_TRUNC: -1, O_APPEND: -1, O_EXCL: -1 }, // unused - writeSync(fd, buf) { - outputBuf += decoder.decode(buf); - const nl = outputBuf.lastIndexOf("\n"); - if (nl != -1) { - console.log(outputBuf.substr(0, nl)); - outputBuf = outputBuf.substr(nl + 1); - } - return buf.length; - }, - write(fd, buf, offset, length, position, callback) { - if (offset !== 0 || length !== buf.length || position !== null) { - callback(enosys()); - return; - } - const n = this.writeSync(fd, buf); - callback(null, n); - }, - chmod(path, mode, callback) { callback(enosys()); }, - chown(path, uid, gid, callback) { callback(enosys()); }, - close(fd, callback) { callback(enosys()); }, - fchmod(fd, mode, callback) { callback(enosys()); }, - fchown(fd, uid, gid, callback) { callback(enosys()); }, - fstat(fd, callback) { callback(enosys()); }, - fsync(fd, callback) { callback(null); }, - ftruncate(fd, length, callback) { callback(enosys()); }, - lchown(path, uid, gid, callback) { callback(enosys()); }, - link(path, link, callback) { callback(enosys()); }, - lstat(path, callback) { callback(enosys()); }, - mkdir(path, perm, callback) { callback(enosys()); }, - open(path, flags, mode, callback) { callback(enosys()); }, - read(fd, buffer, offset, length, position, callback) { callback(enosys()); }, - readdir(path, callback) { callback(enosys()); }, - readlink(path, callback) { callback(enosys()); }, - rename(from, to, callback) { callback(enosys()); }, - rmdir(path, callback) { callback(enosys()); }, - stat(path, callback) { callback(enosys()); }, - symlink(path, link, callback) { callback(enosys()); }, - truncate(path, length, callback) { callback(enosys()); }, - unlink(path, callback) { callback(enosys()); }, - utimes(path, atime, mtime, callback) { callback(enosys()); }, - }; - } - if (!globalThis.process) { - globalThis.process = { - getuid() { return -1; }, - getgid() { return -1; }, - geteuid() { return -1; }, - getegid() { return -1; }, - getgroups() { throw enosys(); }, - pid: -1, - ppid: -1, - umask() { throw enosys(); }, - cwd() { throw enosys(); }, - chdir() { throw enosys(); }, - } - } - /* - if (!globalThis.crypto) { - throw new Error("globalThis.crypto is not available, polyfill required (crypto.getRandomValues only)"); - } - if (!globalThis.performance) { - throw new Error("globalThis.performance is not available, polyfill required (performance.now only)"); - } - */ - if (!globalThis.TextEncoder) { - throw new Error("globalThis.TextEncoder is not available, polyfill required"); - } - if (!globalThis.TextDecoder) { - throw new Error("globalThis.TextDecoder is not available, polyfill required"); - } - const encoder = new TextEncoder("utf-8"); - const decoder = new TextDecoder("utf-8"); - globalThis.Go = class { - constructor() { - this.argv = ["js"]; - this.env = {}; - this.exit = (code) => { - if (code !== 0) { - console.warn("exit code:", code); - } - }; - this._exitPromise = new Promise((resolve) => { - this._resolveExitPromise = resolve; - }); - this._pendingEvent = null; - this._scheduledTimeouts = new Map(); - this._nextCallbackTimeoutID = 1; - const setInt64 = (addr, v) => { - this.mem.setUint32(addr + 0, v, true); - this.mem.setUint32(addr + 4, Math.floor(v / 4294967296), true); - } - const getInt64 = (addr) => { - const low = this.mem.getUint32(addr + 0, true); - const high = this.mem.getInt32(addr + 4, true); - return low + high * 4294967296; - } - const loadValue = (addr) => { - const f = this.mem.getFloat64(addr, true); - if (f === 0) { - return undefined; - } - if (!isNaN(f)) { - return f; - } - const id = this.mem.getUint32(addr, true); - return this._values[id]; - } - const storeValue = (addr, v) => { - const nanHead = 0x7FF80000; - if (typeof v === "number" && v !== 0) { - if (isNaN(v)) { - this.mem.setUint32(addr + 4, nanHead, true); - this.mem.setUint32(addr, 0, true); - return; - } - this.mem.setFloat64(addr, v, true); - return; - } - if (v === undefined) { - this.mem.setFloat64(addr, 0, true); - return; - } - let id = this._ids.get(v); - if (id === undefined) { - id = this._idPool.pop(); - if (id === undefined) { - id = this._values.length; - } - this._values[id] = v; - this._goRefCounts[id] = 0; - this._ids.set(v, id); - } - this._goRefCounts[id]++; - let typeFlag = 0; - switch (typeof v) { - case "object": - if (v !== null) { - typeFlag = 1; - } - break; - case "string": - typeFlag = 2; - break; - case "symbol": - typeFlag = 3; - break; - case "function": - typeFlag = 4; - break; - } - this.mem.setUint32(addr + 4, nanHead | typeFlag, true); - this.mem.setUint32(addr, id, true); - } - const loadSlice = (addr) => { - const array = getInt64(addr + 0); - const len = getInt64(addr + 8); - return new Uint8Array(this._inst.exports.mem.buffer, array, len); - } - const loadSliceOfValues = (addr) => { - const array = getInt64(addr + 0); - const len = getInt64(addr + 8); - const a = new Array(len); - for (let i = 0; i < len; i++) { - a[i] = loadValue(array + i * 8); - } - return a; - } - const loadString = (addr) => { - const saddr = getInt64(addr + 0); - const len = getInt64(addr + 8); - return decoder.decode(new DataView(this._inst.exports.mem.buffer, saddr, len)); - } - const timeOrigin = Date.now() - performance.now(); - this.importObject = { - gojs: { - // Go's SP does not change as long as no Go code is running. Some operations (e.g. calls, getters and setters) - // may synchronously trigger a Go event handler. This makes Go code get executed in the middle of the imported - // function. A goroutine can switch to a new stack if the current stack is too small (see morestack function). - // This changes the SP, thus we have to update the SP used by the imported function. - // func wasmExit(code int32) - "runtime.wasmExit": (sp) => { - sp >>>= 0; - const code = this.mem.getInt32(sp + 8, true); - this.exited = true; - delete this._inst; - delete this._values; - delete this._goRefCounts; - delete this._ids; - delete this._idPool; - this.exit(code); - }, - // func wasmWrite(fd uintptr, p unsafe.Pointer, n int32) - "runtime.wasmWrite": (sp) => { - sp >>>= 0; - const fd = getInt64(sp + 8); - const p = getInt64(sp + 16); - const n = this.mem.getInt32(sp + 24, true); - fs.writeSync(fd, new Uint8Array(this._inst.exports.mem.buffer, p, n)); - }, - // func resetMemoryDataView() - "runtime.resetMemoryDataView": (sp) => { - sp >>>= 0; - this.mem = new DataView(this._inst.exports.mem.buffer); - }, - // func nanotime1() int64 - "runtime.nanotime1": (sp) => { - sp >>>= 0; - setInt64(sp + 8, (timeOrigin + performance.now()) * 1000000); - }, - // func walltime() (sec int64, nsec int32) - "runtime.walltime": (sp) => { - sp >>>= 0; - const msec = (new Date).getTime(); - setInt64(sp + 8, msec / 1000); - this.mem.setInt32(sp + 16, (msec % 1000) * 1000000, true); - }, - // func scheduleTimeoutEvent(delay int64) int32 - "runtime.scheduleTimeoutEvent": (sp) => { - sp >>>= 0; - const id = this._nextCallbackTimeoutID; - this._nextCallbackTimeoutID++; - this._scheduledTimeouts.set(id, setTimeout( - () => { - this._resume(); - while (this._scheduledTimeouts.has(id)) { - // for some reason Go failed to register the timeout event, log and try again - // (temporary workaround for https://github.com/golang/go/issues/28975) - console.warn("scheduleTimeoutEvent: missed timeout event"); - this._resume(); - } - }, - getInt64(sp + 8) + 1, // setTimeout has been seen to fire up to 1 millisecond early - )); - this.mem.setInt32(sp + 16, id, true); - }, - // func clearTimeoutEvent(id int32) - "runtime.clearTimeoutEvent": (sp) => { - sp >>>= 0; - const id = this.mem.getInt32(sp + 8, true); - clearTimeout(this._scheduledTimeouts.get(id)); - this._scheduledTimeouts.delete(id); - }, - // func getRandomData(r []byte) - "runtime.getRandomData": (sp) => { - sp >>>= 0; - crypto.getRandomValues(loadSlice(sp + 8)); - }, - // func finalizeRef(v ref) - "syscall/js.finalizeRef": (sp) => { - sp >>>= 0; - const id = this.mem.getUint32(sp + 8, true); - this._goRefCounts[id]--; - if (this._goRefCounts[id] === 0) { - const v = this._values[id]; - this._values[id] = null; - this._ids.delete(v); - this._idPool.push(id); - } - }, - // func stringVal(value string) ref - "syscall/js.stringVal": (sp) => { - sp >>>= 0; - storeValue(sp + 24, loadString(sp + 8)); - }, - // func valueGet(v ref, p string) ref - "syscall/js.valueGet": (sp) => { - sp >>>= 0; - const result = Reflect.get(loadValue(sp + 8), loadString(sp + 16)); - sp = this._inst.exports.getsp() >>> 0; // see comment above - storeValue(sp + 32, result); - }, - // func valueSet(v ref, p string, x ref) - "syscall/js.valueSet": (sp) => { - sp >>>= 0; - Reflect.set(loadValue(sp + 8), loadString(sp + 16), loadValue(sp + 32)); - }, - // func valueDelete(v ref, p string) - "syscall/js.valueDelete": (sp) => { - sp >>>= 0; - Reflect.deleteProperty(loadValue(sp + 8), loadString(sp + 16)); - }, - // func valueIndex(v ref, i int) ref - "syscall/js.valueIndex": (sp) => { - sp >>>= 0; - storeValue(sp + 24, Reflect.get(loadValue(sp + 8), getInt64(sp + 16))); - }, - // valueSetIndex(v ref, i int, x ref) - "syscall/js.valueSetIndex": (sp) => { - sp >>>= 0; - Reflect.set(loadValue(sp + 8), getInt64(sp + 16), loadValue(sp + 24)); - }, - // func valueCall(v ref, m string, args []ref) (ref, bool) - "syscall/js.valueCall": (sp) => { - sp >>>= 0; - try { - const v = loadValue(sp + 8); - const m = Reflect.get(v, loadString(sp + 16)); - const args = loadSliceOfValues(sp + 32); - const result = Reflect.apply(m, v, args); - sp = this._inst.exports.getsp() >>> 0; // see comment above - storeValue(sp + 56, result); - this.mem.setUint8(sp + 64, 1); - } catch (err) { - sp = this._inst.exports.getsp() >>> 0; // see comment above - storeValue(sp + 56, err); - this.mem.setUint8(sp + 64, 0); - } - }, - // func valueInvoke(v ref, args []ref) (ref, bool) - "syscall/js.valueInvoke": (sp) => { - sp >>>= 0; - try { - const v = loadValue(sp + 8); - const args = loadSliceOfValues(sp + 16); - const result = Reflect.apply(v, undefined, args); - sp = this._inst.exports.getsp() >>> 0; // see comment above - storeValue(sp + 40, result); - this.mem.setUint8(sp + 48, 1); - } catch (err) { - sp = this._inst.exports.getsp() >>> 0; // see comment above - storeValue(sp + 40, err); - this.mem.setUint8(sp + 48, 0); - } - }, - // func valueNew(v ref, args []ref) (ref, bool) - "syscall/js.valueNew": (sp) => { - sp >>>= 0; - try { - const v = loadValue(sp + 8); - const args = loadSliceOfValues(sp + 16); - const result = Reflect.construct(v, args); - sp = this._inst.exports.getsp() >>> 0; // see comment above - storeValue(sp + 40, result); - this.mem.setUint8(sp + 48, 1); - } catch (err) { - sp = this._inst.exports.getsp() >>> 0; // see comment above - storeValue(sp + 40, err); - this.mem.setUint8(sp + 48, 0); - } - }, - // func valueLength(v ref) int - "syscall/js.valueLength": (sp) => { - sp >>>= 0; - setInt64(sp + 16, parseInt(loadValue(sp + 8).length)); - }, - // valuePrepareString(v ref) (ref, int) - "syscall/js.valuePrepareString": (sp) => { - sp >>>= 0; - const str = encoder.encode(String(loadValue(sp + 8))); - storeValue(sp + 16, str); - setInt64(sp + 24, str.length); - }, - // valueLoadString(v ref, b []byte) - "syscall/js.valueLoadString": (sp) => { - sp >>>= 0; - const str = loadValue(sp + 8); - loadSlice(sp + 16).set(str); - }, - // func valueInstanceOf(v ref, t ref) bool - "syscall/js.valueInstanceOf": (sp) => { - sp >>>= 0; - this.mem.setUint8(sp + 24, (loadValue(sp + 8) instanceof loadValue(sp + 16)) ? 1 : 0); - }, - // func copyBytesToGo(dst []byte, src ref) (int, bool) - "syscall/js.copyBytesToGo": (sp) => { - sp >>>= 0; - const dst = loadSlice(sp + 8); - const src = loadValue(sp + 32); - if (!(src instanceof Uint8Array || src instanceof Uint8ClampedArray)) { - this.mem.setUint8(sp + 48, 0); - return; - } - const toCopy = src.subarray(0, dst.length); - dst.set(toCopy); - setInt64(sp + 40, toCopy.length); - this.mem.setUint8(sp + 48, 1); - }, - // func copyBytesToJS(dst ref, src []byte) (int, bool) - "syscall/js.copyBytesToJS": (sp) => { - sp >>>= 0; - const dst = loadValue(sp + 8); - const src = loadSlice(sp + 16); - if (!(dst instanceof Uint8Array || dst instanceof Uint8ClampedArray)) { - this.mem.setUint8(sp + 48, 0); - return; - } - const toCopy = src.subarray(0, dst.length); - dst.set(toCopy); - setInt64(sp + 40, toCopy.length); - this.mem.setUint8(sp + 48, 1); - }, - "debug": (value) => { - console.log(value); - }, - } - }; - // Expose imports under both "gojs" (Go 1.21+) and "go" (Go <1.21) - this.importObject.go = this.importObject.gojs; - } - async run(instance) { - if (!(instance instanceof WebAssembly.Instance)) { - throw new Error("Go.run: WebAssembly.Instance expected"); - } - this._inst = instance; - this.mem = new DataView(this._inst.exports.mem.buffer); - this._values = [ // JS values that Go currently has references to, indexed by reference id - NaN, - 0, - null, - true, - false, - globalThis, - this, - ]; - this._goRefCounts = new Array(this._values.length).fill(Infinity); // number of references that Go has to a JS value, indexed by reference id - this._ids = new Map([ // mapping from JS values to reference ids - [0, 1], - [null, 2], - [true, 3], - [false, 4], - [globalThis, 5], - [this, 6], - ]); - this._idPool = []; // unused ids that have been garbage collected - this.exited = false; // whether the Go program has exited - // Pass command line arguments and environment variables to WebAssembly by writing them to the linear memory. - let offset = 4096; - const strPtr = (str) => { - const ptr = offset; - const bytes = encoder.encode(str + "\0"); - new Uint8Array(this.mem.buffer, offset, bytes.length).set(bytes); - offset += bytes.length; - if (offset % 8 !== 0) { - offset += 8 - (offset % 8); - } - return ptr; - }; - const argc = this.argv.length; - const argvPtrs = []; - this.argv.forEach((arg) => { - argvPtrs.push(strPtr(arg)); - }); - argvPtrs.push(0); - const keys = Object.keys(this.env).sort(); - keys.forEach((key) => { - argvPtrs.push(strPtr(` + "`" + `${key}=${this.env[key]}` + "`" + `)); - }); - argvPtrs.push(0); - const argv = offset; - argvPtrs.forEach((ptr) => { - this.mem.setUint32(offset, ptr, true); - this.mem.setUint32(offset + 4, 0, true); - offset += 8; - }); - // The linker guarantees global data starts from at least wasmMinDataAddr. - // Keep in sync with cmd/link/internal/ld/data.go:wasmMinDataAddr. - const wasmMinDataAddr = 4096 + 8192; - if (offset >= wasmMinDataAddr) { - throw new Error("total length of command line and environment variables exceeds limit"); - } - this._inst.exports.run(argc, argv); - if (this.exited) { - this._resolveExitPromise(); - } - await this._exitPromise; - } - _resume() { - if (this.exited) { - throw new Error("Go program has already exited"); - } - this._inst.exports.resume(); - if (this.exited) { - this._resolveExitPromise(); - } - } - _makeFuncWrapper(id) { - const go = this; - return function () { - const event = { id: id, this: this, args: arguments }; - go._pendingEvent = event; - go._resume(); - return event.result; - }; - } - } -})(); -` diff --git a/pkg/fn/runtime/nodejs.go b/pkg/fn/runtime/nodejs.go deleted file mode 100644 index ed0d9f56b0..0000000000 --- a/pkg/fn/runtime/nodejs.go +++ /dev/null @@ -1,74 +0,0 @@ -// Copyright 2022,2026 The kpt Authors -// -// Licensed under the Apache License, Version 2.0 (the "License"); -// you may not use this file except in compliance with the License. -// You may obtain a copy of the License at -// -// http://www.apache.org/licenses/LICENSE-2.0 -// -// Unless required by applicable law or agreed to in writing, software -// distributed under the License is distributed on an "AS IS" BASIS, -// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -// See the License for the specific language governing permissions and -// limitations under the License. - -package runtime - -import ( - "fmt" - "io" - "os" - "path/filepath" - - fnresultv1 "github.com/kptdev/kpt/api/fnresult/v1" -) - -const ( - WasmPathEnv = "KPT_FN_WASM_PATH" -) - -type WasmNodejsFn struct { - NodeJsRunner *ExecFn - loader WasmLoader -} - -func NewNodejsFn(loader WasmLoader) (*WasmNodejsFn, error) { - cacheDir := filepath.Join(os.TempDir(), "kpt-wasm-fn") - err := os.MkdirAll(cacheDir, 0750) - if err != nil { - return nil, fmt.Errorf("unable to create cache dir: %w", err) - } - tempDir, err := os.MkdirTemp(cacheDir, "nodejs-") - if err != nil { - return nil, fmt.Errorf("unable to create temp dir: %w", err) - } - jsPath := filepath.Join(tempDir, "krm-fn-wasm-glue-runner.js") - if err = os.WriteFile(jsPath, []byte(golangWasmJSCode+glueCode), 0600); err != nil { - return nil, fmt.Errorf("unable to write the js glue code file: %w", err) - } - - wasmFile, err := loader.getFilePath() - if err != nil { - return nil, err - } - - f := &WasmNodejsFn{ - NodeJsRunner: &ExecFn{ - Path: "node", - Args: []string{jsPath}, - Env: map[string]string{ - WasmPathEnv: wasmFile, - }, - FnResult: &fnresultv1.Result{}, - }, - loader: loader, - } - return f, nil -} - -func (f *WasmNodejsFn) Run(r io.Reader, w io.Writer) error { - if err := f.NodeJsRunner.Run(r, w); err != nil { - return fmt.Errorf("failed to run wasm with node.js: %w", err) - } - return f.loader.cleanup() -} diff --git a/pkg/fn/runtime/wasm.go b/pkg/fn/runtime/wasm.go index e3d9378718..aa5d613061 100644 --- a/pkg/fn/runtime/wasm.go +++ b/pkg/fn/runtime/wasm.go @@ -16,6 +16,7 @@ package runtime import ( "context" + "errors" "fmt" "io" "os" @@ -24,60 +25,24 @@ import ( "github.com/kptdev/kpt/pkg/wasm" ) -type WasmRuntime string +// ErrWasmFunctionsRemoved is returned when kpt is asked to run a WASM function. +// Support for WASM functions has been removed from kpt, see +// https://github.com/kptdev/kpt/issues/4881. The WasmFn type and the loaders +// below are kept as stubs in case WASM support is revisited in the future. +var ErrWasmFunctionsRemoved = errors.New("support for WASM functions has been removed from kpt, " + + "run the function as a container image or an executable instead") -const ( - Wasmtime WasmRuntime = "wasmtime" - Nodejs WasmRuntime = "nodejs" +// WasmFn is a stub kept for API compatibility. It can no longer run WASM functions. +type WasmFn struct{} - WasmRuntimeEnv = "KPT_FN_WASM_RUNTIME" - - jsEntrypointFunction = "processResourceList" -) - -type WasmFn struct { - runtimeType WasmRuntime - - wasmtime *WasmtimeFn - nodejs *WasmNodejsFn +// NewWasmFn always returns ErrWasmFunctionsRemoved. +func NewWasmFn(_ WasmLoader) (*WasmFn, error) { + return nil, ErrWasmFunctionsRemoved } -func NewWasmFn(loader WasmLoader) (*WasmFn, error) { - switch os.Getenv(WasmRuntimeEnv) { - case string(Nodejs): - nf, err := NewNodejsFn(loader) - if err != nil { - return nil, err - } - return &WasmFn{ - runtimeType: Nodejs, - nodejs: nf, - }, nil - case "": - fallthrough - case string(Wasmtime): - wf, err := NewWasmtimeFn(loader) - if err != nil { - return nil, err - } - return &WasmFn{ - runtimeType: Wasmtime, - wasmtime: wf, - }, nil - default: - return nil, fmt.Errorf("unsupported wasm runtime: %v", os.Getenv(WasmRuntimeEnv)) - } -} - -func (f *WasmFn) Run(r io.Reader, w io.Writer) error { - switch f.runtimeType { - case Nodejs: - return f.nodejs.Run(r, w) - case Wasmtime: - return f.wasmtime.Run(r, w) - default: - return fmt.Errorf("unknown wasm runtime type: %q", f.runtimeType) - } +// Run always returns ErrWasmFunctionsRemoved. +func (f *WasmFn) Run(_ io.Reader, _ io.Writer) error { + return ErrWasmFunctionsRemoved } type WasmLoader interface { diff --git a/pkg/fn/runtime/wasmtime.go b/pkg/fn/runtime/wasmtime.go deleted file mode 100644 index 67202be6f9..0000000000 --- a/pkg/fn/runtime/wasmtime.go +++ /dev/null @@ -1,223 +0,0 @@ -//go:build cgo - -// Copyright 2022,2026 The kpt Authors -// -// Licensed under the Apache License, Version 2.0 (the "License"); -// you may not use this file except in compliance with the License. -// You may obtain a copy of the License at -// -// http://www.apache.org/licenses/LICENSE-2.0 -// -// Unless required by applicable law or agreed to in writing, software -// distributed under the License is distributed on an "AS IS" BASIS, -// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -// See the License for the specific language governing permissions and -// limitations under the License. - -package runtime - -import ( - "errors" - "fmt" - "io" - "log" - "os" - - "github.com/bytecodealliance/wasmtime-go" - "github.com/prep/wasmexec" - "github.com/prep/wasmexec/wasmtimexec" - "sigs.k8s.io/kustomize/kyaml/yaml" -) - -type WasmtimeFn struct { - wasmexec.Memory - *wasmtime.Instance - store *wasmtime.Store - - gomod *wasmexec.Module - - spFn *wasmtime.Func - resumeFn *wasmtime.Func - - loader WasmLoader -} - -func NewWasmtimeFn(loader WasmLoader) (*WasmtimeFn, error) { - f := &WasmtimeFn{ - loader: loader, - } - wasmFileReadCloser, err := loader.getReadCloser() - if err != nil { - return nil, err - } - defer wasmFileReadCloser.Close() - // Create a module out of the Wasm file. - data, err := io.ReadAll(wasmFileReadCloser) - if err != nil { - return nil, fmt.Errorf("unable to read wasm content from reader: %w", err) - } - - // Create the engine and store. - config := wasmtime.NewConfig() - err = config.CacheConfigLoadDefault() - if err != nil { - return nil, fmt.Errorf("failed to config cache in wasmtime") - } - engine := wasmtime.NewEngineWithConfig(config) - - module, err := wasmtime.NewModule(engine, data) - if err != nil { - return nil, err - } - f.store = wasmtime.NewStore(engine) - - linker := wasmtime.NewLinker(engine) - f.gomod, err = wasmtimexec.Import(f.store, linker, f) - if err != nil { - return nil, err - } - - // Create an instance of the module. - if f.Instance, err = linker.Instantiate(f.store, module); err != nil { - return nil, err - } - - return f, nil -} - -// Run runs the executable file which reads the input from r and -// writes the output to w. -func (f *WasmtimeFn) Run(r io.Reader, w io.Writer) error { - // Fetch the memory export and set it on the instance, making the memory - // accessible by the imports. - ext := f.GetExport(f.store, "mem") - if ext == nil { - return errors.New("unable to find memory export") - } - - mem := ext.Memory() - if mem == nil { - return errors.New("mem: export is not memory") - } - - f.Memory = wasmexec.NewMemory(mem.UnsafeData(f.store)) - - // Fetch the getsp function and reference it on the instance. - spFn := f.GetExport(f.store, "getsp") - if spFn == nil { - return errors.New("getsp: missing export") - } - - if f.spFn = spFn.Func(); f.spFn == nil { - return errors.New("getsp: export is not a function") - } - - // Fetch the resume function and reference it on the instance. - resumeFn := f.GetExport(f.store, "resume") - if resumeFn == nil { - return errors.New("resume: missing export") - } - - if f.resumeFn = resumeFn.Func(); f.resumeFn == nil { - return errors.New("resume: export is not a function") - } - - // Set the args and the environment variables. - argc, argv, err := wasmexec.SetArgs(f.Memory, []string{"krm-fn-wasm-wasmtime"}, []string{}) - if err != nil { - return err - } - - // Fetch the "run" function and call it. This starts the program. - runFn := f.GetFunc(f.store, "run") - if runFn == nil { - return errors.New("run: missing export") - } - - if _, err = runFn.Call(f.store, argc, argv); err != nil { - return err - } - resourceList, err := io.ReadAll(r) - if err != nil { - return err - } - result, err := f.gomod.Call(jsEntrypointFunction, string(resourceList)) - if err != nil { - return fmt.Errorf("unable to invoke %v: %v", jsEntrypointFunction, err) - } - - // We expect `result` to be a *wasmexec.jsString (which is not exportable) with - // the following definition: type jsString struct { data string }. It will look - // like `&{realPayload}` - resultStr := fmt.Sprintf("%s", result) - resultStr = resultStr[2 : len(resultStr)-1] - // Try to parse the output as yaml. - resourceListOutput, err := yaml.Parse(resultStr) - if err != nil { - additionalErrorMessage, errorResultRetrievalErr := retrieveError(f, resourceList) - if errorResultRetrievalErr != nil { - return errorResultRetrievalErr - } - return fmt.Errorf("parsing output resource list with content: %q\n%w\n%s", resultStr, err, additionalErrorMessage) - } - if resourceListOutput.GetKind() != "ResourceList" { - additionalErrorMessage, errorResultRetrievalErr := retrieveError(f, resourceList) - if errorResultRetrievalErr != nil { - return errorResultRetrievalErr - } - return fmt.Errorf("invalid resource list output from wasm library; got %q\n%s", resultStr, additionalErrorMessage) - } - if _, err = w.Write([]byte(resultStr)); err != nil { - return fmt.Errorf("unable to write the output resource list: %w", err) - } - return f.loader.cleanup() -} - -func retrieveError(f *WasmtimeFn, resourceList []byte) (string, error) { - errResult, err := f.gomod.Call(jsEntrypointFunction+"Errors", string(resourceList)) - if err != nil { - return "", fmt.Errorf("unable to retrieve additional error message from function: %w", err) - } - return fmt.Sprintf("%s", errResult), nil -} - -var _ wasmexec.Instance = &WasmtimeFn{} - -func (f *WasmtimeFn) GetSP() (uint32, error) { - val, err := f.spFn.Call(f.store) - if err != nil { - return 0, err - } - - sp, ok := val.(int32) - if !ok { - return 0, fmt.Errorf("getsp: %T: expected an int32 return value", sp) - } - - if sp >= 0 { - return uint32(sp), nil - } - return 0, fmt.Errorf("getsp: %T: expected a positive return value", sp) -} - -func (f *WasmtimeFn) Resume() error { - _, err := f.resumeFn.Call(f.store) - return err -} - -// Write implements the wasmexec.fdWriter interface. -func (f *WasmtimeFn) Write(fd int, b []byte) (n int, err error) { - switch fd { - case 1, 2: - n, err = os.Stdout.Write(b) - default: - err = fmt.Errorf("%d: invalid file descriptor", fd) - } - - return n, err -} - -// Error implements the wasmexec.errorLogger interface -func (f *WasmtimeFn) Error(format string, params ...any) { - log.Printf("ERROR: "+format+"\n", params...) -} diff --git a/pkg/fn/runtime/wasmtime_unsupported.go b/pkg/fn/runtime/wasmtime_unsupported.go deleted file mode 100644 index 95578925fb..0000000000 --- a/pkg/fn/runtime/wasmtime_unsupported.go +++ /dev/null @@ -1,40 +0,0 @@ -//go:build !cgo - -// Copyright 2022,2026 The kpt Authors -// -// Licensed under the Apache License, Version 2.0 (the "License"); -// you may not use this file except in compliance with the License. -// You may obtain a copy of the License at -// -// http://www.apache.org/licenses/LICENSE-2.0 -// -// Unless required by applicable law or agreed to in writing, software -// distributed under the License is distributed on an "AS IS" BASIS, -// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -// See the License for the specific language governing permissions and -// limitations under the License. - -package runtime - -// Stub functions for running without wasmtime support compiled in. -// wasmtime requires cgo, which is not always a viable option. - -import ( - "fmt" - "io" -) - -const ( - msg = "wasmtime support is not compiled into this binary. Binaries with wasmtime is available at github.com/kptdev/kpt" -) - -type WasmtimeFn struct { -} - -func NewWasmtimeFn(loader WasmLoader) (*WasmtimeFn, error) { - return nil, fmt.Errorf("%s", msg) -} - -func (f *WasmtimeFn) Run(r io.Reader, w io.Writer) error { - return fmt.Errorf("%s", msg) -}