Skip to content

Commit

Permalink
use json
Browse files Browse the repository at this point in the history
  • Loading branch information
Gekko0114 committed Nov 19, 2023
1 parent 24ec928 commit 3bfbab0
Show file tree
Hide file tree
Showing 23 changed files with 44 additions and 797 deletions.
2 changes: 0 additions & 2 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -85,8 +85,6 @@ submodule-update:
.PHONY: update-kubernetes-proto
update-kubernetes-proto: proto-tools
echo "Regenerate the Go protobuf code."
protoc ./kubernetes/proto/nodetostatus/api.proto --go-plugin_out=./kubernetes/proto/nodetostatus \
--go-plugin_opt=Mkubernetes/proto/nodetostatus/api.proto=.; \
cd kubernetes/kubernetes/staging/src/; \
protoc ./k8s.io/apimachinery/pkg/api/resource/generated.proto --go-plugin_out=../../../proto \
--go-plugin_opt=Mk8s.io/apimachinery/pkg/api/resource/generated.proto=./resource; \
Expand Down
2 changes: 1 addition & 1 deletion guest/api/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -115,5 +115,5 @@ type NodeInfo interface {
}

type NodeToStatusMap interface {
NodeToStatusMap() map[string]*Status
NodeToStatusMap() map[string]int
}
16 changes: 12 additions & 4 deletions guest/internal/imports/host.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
package imports

import (
"encoding/json"
"runtime"

"sigs.k8s.io/kube-scheduler-wasm-extension/guest/api"
Expand Down Expand Up @@ -67,9 +68,16 @@ func Pod(updater func([]byte) error) error {
}, updater)
}

func NodeToStatusMap(updater func([]byte) error) error {
func NodeToStatusMap() map[string]int {
// Wrap to avoid TinyGo 0.28: cannot use an exported function as value
return mem.Update(func(ptr uint32, limit mem.BufLimit) (len uint32) {
return k8sApiNodeToStatusMap(ptr, limit)
}, updater)
jsonStr := mem.GetString(func(ptr uint32, limit mem.BufLimit) (len uint32) {
return k8sSchedulerNodeToStatusMap(ptr, limit)
})
byte := []byte(jsonStr)
var nodeToMap map[string]int
err := json.Unmarshal(byte, &nodeToMap)
if err != nil {
panic(err)
}
return nodeToMap
}
4 changes: 2 additions & 2 deletions guest/internal/imports/imports.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,8 @@ func k8sApiNodeName(ptr uint32, limit mem.BufLimit) (len uint32)
//go:wasmimport k8s.io/api pod
func k8sApiPod(ptr uint32, limit mem.BufLimit) (len uint32)

//go:wasmimport k8s.io/api nodeToStatusMap
func k8sApiNodeToStatusMap(ptr uint32, limit mem.BufLimit) (len uint32)
//go:wasmimport k8s.io/scheduler nodeToStatusMap
func k8sSchedulerNodeToStatusMap(ptr uint32, limit mem.BufLimit) (len uint32)

//go:wasmimport k8s.io/scheduler result.status_reason
func k8sSchedulerResultStatusReason(ptr, size uint32)
4 changes: 2 additions & 2 deletions guest/internal/imports/imports_stub.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,8 @@ func k8sApiNodeName(uint32, mem.BufLimit) (len uint32) { return }
// k8sApiPod is stubbed for compilation outside TinyGo.
func k8sApiPod(uint32, mem.BufLimit) (len uint32) { return }

// k8sApiNodeToStatusMap is stubbed for compilation outside TinyGo.
func k8sApiNodeToStatusMap(uint32, mem.BufLimit) (len uint32) { return }
// k8sSchedulerNodeToStatusMap is stubbed for compilation outside TinyGo.
func k8sSchedulerNodeToStatusMap(uint32, mem.BufLimit) (len uint32) { return }

// k8sSchedulerResultStatusReason is stubbed for compilation outside TinyGo.
func k8sSchedulerResultStatusReason(uint32, uint32) {}
46 changes: 5 additions & 41 deletions guest/postfilter/postfilter.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@ import (
"sigs.k8s.io/kube-scheduler-wasm-extension/guest/internal/imports"
"sigs.k8s.io/kube-scheduler-wasm-extension/guest/internal/mem"
"sigs.k8s.io/kube-scheduler-wasm-extension/guest/internal/plugin"
"sigs.k8s.io/kube-scheduler-wasm-extension/kubernetes/proto/nodetostatus"
)

// postfilter is the current plugin assigned with SetPlugin.
Expand Down Expand Up @@ -86,51 +85,16 @@ func _postfilter() uint64 { //nolint
}

type nodeToStatusMap struct {
statusMap map[string]*api.Status
statusMap map[string]int
}

func (n *nodeToStatusMap) NodeToStatusMap() map[string]*api.Status {
func (n *nodeToStatusMap) NodeToStatusMap() map[string]int {
return n.lazyNodeToStatusMap()
}

// lazyNodeToStatusMap returns NodeToStatusMap from imports.NodeToStatusMap.
func (n *nodeToStatusMap) lazyNodeToStatusMap() map[string]*api.Status {
var msg nodetostatus.NodeToStatusMap
if err := imports.NodeToStatusMap(msg.UnmarshalVT); err != nil {
panic(err.Error())
}
n.statusMap = convertNodeStatusMapType(msg.NodeStatus)
func (n *nodeToStatusMap) lazyNodeToStatusMap() map[string]int {
nodeMap := imports.NodeToStatusMap()
n.statusMap = nodeMap
return n.statusMap
}

func convertNodeStatusMapType(nodeToStatusMap map[string]*nodetostatus.Status) map[string]*api.Status {
converted := make(map[string]*api.Status)

for key, value := range nodeToStatusMap {
convertedStatus := &api.Status{
Code: convertCode(*value.Code),
Reason: *value.Reason,
}
converted[key] = convertedStatus
}
return converted
}

func convertCode(code nodetostatus.StatusCode) api.StatusCode {
switch code {
case nodetostatus.StatusCode_STATUS_CODE_SUCCESS:
return api.StatusCodeSuccess
case nodetostatus.StatusCode_STATUS_CODE_ERROR:
return api.StatusCodeError
case nodetostatus.StatusCode_STATUS_CODE_UNSCHEDULABLE:
return api.StatusCodeUnschedulable
case nodetostatus.StatusCode_STATUS_CODE_UNSCHEDULABLE_AND_UNRESOLVABLE:
return api.StatusCodeUnschedulableAndUnresolvable
case nodetostatus.StatusCode_STATUS_CODE_WAIT:
return api.StatusCodeWait
case nodetostatus.StatusCode_STATUS_CODE_SKIP:
return api.StatusCodeSkip
default:
panic("StatusCode can't be converted")
}
}
4 changes: 2 additions & 2 deletions guest/testdata/cyclestate/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,7 @@ func (statePlugin) PostFilter(state api.CycleState, pod proto.Pod, _ api.NodeToS
}
mustNotScoreState(state)
if val, ok := state.Read(preFilterStateKey); !ok {
panic("didn't propagate state from pre-filter")
panic("didn't propagate post-filter state from pre-filter")
} else if _, ok = val.(preFilterStateVal)["filter"]; !ok {
panic("filter value lost propagating from post-filter")
} else {
Expand All @@ -128,7 +128,7 @@ func (statePlugin) PostFilter(state api.CycleState, pod proto.Pod, _ api.NodeToS

func (statePlugin) PreScore(state api.CycleState, pod proto.Pod, _ proto.NodeList) *api.Status {
if unsafe.Pointer(pod.Spec()) != unsafe.Pointer(podSpec) {
panic("didn't cache pod from post-filter")
panic("didn't cache pod from pre-filter")
}
mustFilterState(state)
if _, ok := state.Read(preScoreStateKey); ok {
Expand Down
Binary file modified guest/testdata/cyclestate/main.wasm
Binary file not shown.
2 changes: 1 addition & 1 deletion guest/testdata/filter/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,7 @@ func (postFilterPlugin) PostFilter(_ api.CycleState, pod proto.Pod, nodeMap api.
}
// If nominatedNodeName is schedulable, pass!
if val, ok := m[podSpecNodeName]; ok {
if val.Code == api.StatusCodeSuccess {
if val == 0 {
return podSpecNodeName, api.ModeOverride, nil
}
}
Expand Down
Binary file modified guest/testdata/filter/main.wasm
Binary file not shown.
1 change: 0 additions & 1 deletion internal/e2e/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -191,7 +191,6 @@ require (
k8s.io/utils v0.0.0-20230209194617-a36077c30491 // indirect
sigs.k8s.io/apiserver-network-proxy/konnectivity-client v0.1.2 // indirect
sigs.k8s.io/json v0.0.0-20221116044647-bc3834ca7abd // indirect
sigs.k8s.io/kube-scheduler-wasm-extension/kubernetes/proto v0.0.0-20230808005812-6708ed44fd99 // indirect
sigs.k8s.io/structured-merge-diff/v4 v4.2.3 // indirect
)

Expand Down
102 changes: 0 additions & 102 deletions kubernetes/proto/nodetostatus/api.pb.go

This file was deleted.

21 changes: 0 additions & 21 deletions kubernetes/proto/nodetostatus/api.proto

This file was deleted.

Loading

0 comments on commit 3bfbab0

Please sign in to comment.