Skip to content

Commit 7bdd8c2

Browse files
author
Mengqi Yu
committed
support compile set-ns to wasm
1 parent e0096a7 commit 7bdd8c2

4 files changed

Lines changed: 109 additions & 4 deletions

File tree

functions/go/set-namespace/main.go

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,13 +15,10 @@ package main
1515

1616
import (
1717
"os"
18-
19-
"github.com/GoogleContainerTools/kpt-functions-catalog/functions/go/set-namespace/transformer"
20-
"github.com/GoogleContainerTools/kpt-functions-sdk/go/fn"
2118
)
2219

2320
func main() {
24-
if err := fn.AsMain(fn.ResourceListProcessorFunc(transformer.Run)); err != nil {
21+
if err := run(); err != nil {
2522
os.Exit(1)
2623
}
2724
}

functions/go/set-namespace/run.go

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
// Copyright 2022 Google LLC
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
//go:build !(js && wasm)
16+
17+
package main
18+
19+
import (
20+
"github.com/GoogleContainerTools/kpt-functions-catalog/functions/go/set-namespace/transformer"
21+
"github.com/GoogleContainerTools/kpt-functions-sdk/go/fn"
22+
)
23+
24+
func run() error {
25+
return fn.AsMain(fn.ResourceListProcessorFunc(transformer.Run))
26+
}
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
// Copyright 2022 Google LLC
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
//go:build js && wasm
16+
17+
package main
18+
19+
import (
20+
// Work around https://github.com/prep/wasmexec/issues/3 until it is resolved upstream.
21+
// This should be the first one among all imports.
22+
_ "github.com/GoogleContainerTools/kpt-functions-catalog/functions/go/set-namespace/setosargs"
23+
24+
"fmt"
25+
"syscall/js"
26+
27+
"github.com/GoogleContainerTools/kpt-functions-catalog/functions/go/set-namespace/transformer"
28+
"github.com/GoogleContainerTools/kpt-functions-sdk/go/fn"
29+
)
30+
31+
func run() error {
32+
js.Global().Set("processResourceList", resourceListProcessorWrapper())
33+
<-make(chan bool)
34+
return nil
35+
}
36+
37+
func transformNamespace(input []byte) ([]byte, error) {
38+
return fn.Run(fn.ResourceListProcessorFunc(transformer.Run), []byte(input))
39+
}
40+
41+
func resourceListProcessorWrapper() js.Func {
42+
// TODO: figure out a better way to surface a golang error to JS environment.
43+
// Currently error is surfaced as a string.
44+
jsonFunc := js.FuncOf(func(this js.Value, args []js.Value) interface{} {
45+
if len(args) != 1 {
46+
return "Invalid number of arguments passed"
47+
}
48+
input := args[0].String()
49+
transformed, err := transformNamespace([]byte(input))
50+
if err != nil {
51+
return fmt.Errorf("unable to process resource list:", err.Error())
52+
}
53+
return string(transformed)
54+
})
55+
return jsonFunc
56+
}
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
// Copyright 2022 Google LLC
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
package setosargs
16+
17+
import (
18+
"os"
19+
)
20+
21+
// Work around https://github.com/prep/wasmexec/issues/3 until it is resolved upstream.
22+
func init() {
23+
if len(os.Args) == 0 {
24+
os.Args = []string{"kpt-fn-wasm"}
25+
}
26+
}

0 commit comments

Comments
 (0)