Skip to content

Commit 435c072

Browse files
author
Mengqi Yu
committed
support compile set-ns and set-labels to wasm
1 parent 3baa902 commit 435c072

6 files changed

Lines changed: 163 additions & 8 deletions

File tree

functions/go/set-labels/main.go

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,14 +16,11 @@ package main
1616

1717
import (
1818
"os"
19-
20-
"github.com/GoogleContainerTools/kpt-functions-catalog/functions/go/set-labels/transformer"
21-
"github.com/GoogleContainerTools/kpt-functions-sdk/go/fn"
2219
)
2320

2421
//nolint
2522
func main() {
26-
if err := fn.AsMain(fn.ResourceListProcessorFunc(transformer.SetLabels)); err != nil {
23+
if err := run(); err != nil {
2724
os.Exit(1)
2825
}
2926
}

functions/go/set-labels/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-labels/transformer"
21+
"github.com/GoogleContainerTools/kpt-functions-sdk/go/fn"
22+
)
23+
24+
func run() error {
25+
return fn.AsMain(fn.ResourceListProcessorFunc(transformer.SetLabels))
26+
}

functions/go/set-labels/run_js.go

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
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+
"fmt"
21+
"syscall/js"
22+
23+
"github.com/GoogleContainerTools/kpt-functions-catalog/functions/go/set-labels/transformer"
24+
"github.com/GoogleContainerTools/kpt-functions-sdk/go/fn"
25+
)
26+
27+
func run() error {
28+
// Register js function processResourceList to the globals.
29+
js.Global().Set("processResourceList", resourceListProcessorWrapper())
30+
// We need to ensure that the Go program is running when JavaScript calls it.
31+
// Otherwise, it will complain the Go program has already exited.
32+
return nil
33+
}
34+
35+
func transformLabels(input []byte) ([]byte, error) {
36+
return fn.Run(fn.ResourceListProcessorFunc(transformer.SetLabels), []byte(input))
37+
}
38+
39+
func resourceListProcessorWrapper() js.Func {
40+
// TODO: figure out a better way to surface a golang error to JS environment.
41+
// Currently error is surfaced as a string.
42+
jsonFunc := js.FuncOf(func(this js.Value, args []js.Value) interface{} {
43+
if len(args) != 1 {
44+
return "Invalid number of arguments passed"
45+
}
46+
input := args[0].String()
47+
transformed, err := transformLabels([]byte(input))
48+
if err != nil {
49+
return fmt.Errorf("unable to process resource list:", err.Error())
50+
}
51+
return string(transformed)
52+
})
53+
return jsonFunc
54+
}

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: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
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+
"fmt"
21+
"syscall/js"
22+
23+
"github.com/GoogleContainerTools/kpt-functions-catalog/functions/go/set-namespace/transformer"
24+
"github.com/GoogleContainerTools/kpt-functions-sdk/go/fn"
25+
)
26+
27+
func run() error {
28+
// Register js function processResourceList to the globals.
29+
js.Global().Set("processResourceList", resourceListProcessorWrapper())
30+
// We need to ensure that the Go program is running when JavaScript calls it.
31+
// Otherwise, it will complain the Go program has already exited.
32+
<-make(chan bool)
33+
return nil
34+
}
35+
36+
func transformNamespace(input []byte) ([]byte, error) {
37+
return fn.Run(fn.ResourceListProcessorFunc(transformer.Run), []byte(input))
38+
}
39+
40+
func resourceListProcessorWrapper() js.Func {
41+
// TODO: figure out a better way to surface a golang error to JS environment.
42+
// Currently error is surfaced as a string.
43+
jsonFunc := js.FuncOf(func(this js.Value, args []js.Value) interface{} {
44+
if len(args) != 1 {
45+
return "Invalid number of arguments passed"
46+
}
47+
input := args[0].String()
48+
transformed, err := transformNamespace([]byte(input))
49+
if err != nil {
50+
return fmt.Errorf("unable to process resource list:", err.Error())
51+
}
52+
return string(transformed)
53+
})
54+
return jsonFunc
55+
}

0 commit comments

Comments
 (0)