Skip to content

Commit cb38b92

Browse files
committed
feat: implement Golang SDK
1 parent 06c9f99 commit cb38b92

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

46 files changed

+43
-53
lines changed

Makefile

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,10 @@ gosimports := github.com/rinchsan/gosimports/cmd/[email protected]
33
golangci_lint := github.com/golangci/golangci-lint/cmd/[email protected]
44

55
examples/advanced/main.wasm: examples/advanced/main.go
6-
@(cd $(@D); tinygo build -o main.wasm -gc=custom -tags=custommalloc -scheduler=none --no-debug -target=wasi .)
6+
@(cd $(@D); GOARCH=wasm GOOS=wasip1 gotip build -buildmode=c-shared -tags wasm -o main.wasm .)
77

88
%/main.wasm: %/main.go
9-
@(cd $(@D); tinygo build -o main.wasm -scheduler=none --no-debug -target=wasi .)
9+
@(cd $(@D); GOARCH=wasm GOOS=wasip1 gotip build -buildmode=c-shared -tags wasm -o main.wasm .)
1010

1111
.PHONY: build-tinygo
1212
build-tinygo: examples/nodenumber/main.wasm examples/advanced/main.wasm guest/testdata/cyclestate/main.wasm guest/testdata/filter/main.wasm guest/testdata/score/main.wasm \

examples/advanced/go.mod

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,15 +3,11 @@ module sigs.k8s.io/kube-scheduler-wasm-extension/examples/advanced
33
go 1.20
44

55
require (
6-
github.com/wasilibs/nottinygc v0.4.0
76
sigs.k8s.io/kube-scheduler-wasm-extension/guest v0.0.0-00010101000000-000000000000
87
sigs.k8s.io/kube-scheduler-wasm-extension/kubernetes/proto v0.0.0-00010101000000-000000000000
98
)
109

11-
require (
12-
github.com/magefile/mage v1.14.0 // indirect
13-
google.golang.org/protobuf v1.30.0 // indirect
14-
)
10+
require google.golang.org/protobuf v1.30.0 // indirect
1511

1612
replace sigs.k8s.io/kube-scheduler-wasm-extension/guest => ./../../guest
1713

examples/advanced/go.sum

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,6 @@
11
github.com/golang/protobuf v1.5.0/go.mod h1:FsONVRAS9T7sI+LIUmWTfcYkHO4aIWwzhcaSAoJOfIk=
22
github.com/google/go-cmp v0.5.5/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE=
33
github.com/google/go-cmp v0.5.9 h1:O2Tfq5qg4qc4AmwVlvv0oLiVAGB7enBSJ2x2DqQFi38=
4-
github.com/magefile/mage v1.14.0 h1:6QDX3g6z1YvJ4olPhT1wksUcSa/V0a1B+pJb73fBjyo=
5-
github.com/magefile/mage v1.14.0/go.mod h1:z5UZb/iS3GoOSn0JgWuiw7dxlurVYTu+/jHXqQg881A=
6-
github.com/wasilibs/nottinygc v0.4.0 h1:h1TJMihMC4neN6Zq+WKpLxgd9xCFMw7O9ETLwY2exJQ=
7-
github.com/wasilibs/nottinygc v0.4.0/go.mod h1:oDcIotskuYNMpqMF23l7Z8uzD4TC0WXHK8jetlB3HIo=
84
golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
95
google.golang.org/protobuf v1.26.0-rc.1/go.mod h1:jlhhOSvTdKEhbULTjvd4ARK9grFBp09yW+WbY/TyQbw=
106
google.golang.org/protobuf v1.30.0 h1:kPPoIgf3TsEvrm0PFe15JQ+570QVxYzEvvHqChK+cng=

examples/advanced/main.go

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
//go:build tinygo.wasm
2-
31
/*
42
Copyright 2023 The Kubernetes Authors.
53
@@ -20,11 +18,7 @@
2018
// 'tinygo build -target=wasi'. See /guest/RATIONALE.md for details.
2119
package main
2220

23-
// Override the default GC with a more performant one.
24-
// Note: this requires tinygo flags: -gc=custom -tags=custommalloc
2521
import (
26-
_ "github.com/wasilibs/nottinygc"
27-
2822
"sigs.k8s.io/kube-scheduler-wasm-extension/examples/advanced/plugin"
2923
"sigs.k8s.io/kube-scheduler-wasm-extension/guest/config"
3024
"sigs.k8s.io/kube-scheduler-wasm-extension/guest/enqueue"
@@ -36,7 +30,7 @@ import (
3630

3731
// main is compiled to an exported Wasm function named "_start", called by the
3832
// Wasm scheduler plugin during initialization.
39-
func main() {
33+
func init() {
4034
// The plugin package uses only normal Go code, which allows it to be
4135
// unit testable via `tinygo test -target=wasi` as well normal `go test`.
4236
//
@@ -53,3 +47,5 @@ func main() {
5347
prescore.SetPlugin(plugin)
5448
score.SetPlugin(plugin)
5549
}
50+
51+
func main() {}

examples/advanced/main.wasm

100755100644
4.06 MB
Binary file not shown.

examples/nodenumber/main.go

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,14 +33,16 @@ import (
3333

3434
// main is compiled to a WebAssembly function named "_start", called by the
3535
// wasm scheduler plugin during initialization.
36-
func main() {
36+
func init() {
3737
p, err := New(klog.Get(), config.Get())
3838
if err != nil {
3939
panic(err)
4040
}
4141
plugin.Set(p)
4242
}
4343

44+
func main() {}
45+
4446
func New(klog klogapi.Klog, jsonConfig []byte) (api.Plugin, error) {
4547
var args nodeNumberArgs
4648
if jsonConfig != nil {

examples/nodenumber/main.wasm

100755100644
4.23 MB
Binary file not shown.

guest/bind/bind.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ var _ func() uint32 = _bind
5252

5353
// _bind is only exported to the host.
5454
//
55-
//export bind
55+
//go:wasmexport bind
5656
func _bind() uint32 { //nolint
5757
if bind == nil { // Then, the user didn't define one.
5858
// This is likely caused by use of plugin.Set(p), where 'p' didn't

guest/config/imports.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
//go:build tinygo.wasm
1+
//go:build wasm
22

33
/*
44
Copyright 2023 The Kubernetes Authors.

guest/config/imports_stub.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
//go:build !tinygo.wasm
1+
//go:build !wasm
22

33
/*
44
Copyright 2023 The Kubernetes Authors.

0 commit comments

Comments
 (0)