generated from kubernetes/kubernetes-template-project
-
Notifications
You must be signed in to change notification settings - Fork 22
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
20 changed files
with
531 additions
and
41 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
//go:build tinygo.wasm | ||
|
||
/* | ||
Copyright 2023 The Kubernetes 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 postfilter | ||
|
||
//go:wasmimport k8s.io/scheduler result.nominated_node_name | ||
func setNominatedNodeNameResult(ptr, size uint32) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
//go:build !tinygo.wasm | ||
|
||
/* | ||
Copyright 2023 The Kubernetes 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 postfilter | ||
|
||
// setNominatedNodeNameResult is stubbed for compilation outside TinyGo. | ||
func setNominatedNodeNameResult(uint32, uint32) {} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,74 @@ | ||
/* | ||
Copyright 2023 The Kubernetes 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 postfilter is defined internally so that it can export Pod as | ||
// cyclestate.Pod, without circular dependencies or exporting it publicly. | ||
package postfilter | ||
|
||
import ( | ||
"runtime" | ||
"unsafe" | ||
|
||
"sigs.k8s.io/kube-scheduler-wasm-extension/guest/api" | ||
"sigs.k8s.io/kube-scheduler-wasm-extension/guest/internal/imports" | ||
"sigs.k8s.io/kube-scheduler-wasm-extension/guest/internal/plugin" | ||
) | ||
|
||
// TODO: should postfilter be defined internally? | ||
// postfilter is the current plugin assigned with SetPlugin. | ||
var postfilter api.PostFilterPlugin | ||
|
||
// SetPlugin is exposed to prevent package cycles. | ||
func SetPlugin(postfilterPlugin api.PostFilterPlugin) { | ||
if postfilterPlugin == nil { | ||
panic("nil postfilterPlugin") | ||
} | ||
postfilter = postfilterPlugin | ||
plugin.MustSet(postfilterPlugin) | ||
} | ||
|
||
// prevent unused lint errors (lint is run with normal go). | ||
var _ func() uint64 = _postfilter | ||
|
||
// _postfilter is only exported to the host. | ||
// | ||
//export postfilter | ||
func _postfilter() uint64 { //nolint | ||
// This function begins a new scheduling cycle: zero out any cycle state. | ||
currentPod = nil | ||
currentCycleState = map[string]any{} | ||
currentNodeToStatusMap = map[string]any{} | ||
|
||
if postfilter == nil { // Then, the user didn't define one. | ||
// Unlike most plugins we always export postfilter so that we can reset | ||
// the cycle state: return success to avoid no-op overhead. | ||
return 0 | ||
} | ||
|
||
// The parameters passed are lazy with regard to host functions. This means | ||
// a no-op plugin should not have any unmarshal penalty. | ||
nominatedNodeName, nominatingMode, status := postfilter.PostFilter(CycleState, Pod, nil) | ||
|
||
cString := []byte(nominatedNodeName) | ||
if cString != nil { | ||
ptr := uint32(uintptr(unsafe.Pointer(&cString[0]))) | ||
size := uint32(len(cString)) | ||
setNominatedNodeNameResult(ptr, size) | ||
runtime.KeepAlive(cString) // until ptr is no longer needed. | ||
} | ||
|
||
return (uint64(nominatingMode) << uint64(32)) | uint64(imports.StatusToCode(status)) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
package postfilter | ||
|
||
import ( | ||
"sigs.k8s.io/kube-scheduler-wasm-extension/guest/api" | ||
"sigs.k8s.io/kube-scheduler-wasm-extension/guest/api/proto" | ||
"sigs.k8s.io/kube-scheduler-wasm-extension/guest/internal/imports" | ||
internalproto "sigs.k8s.io/kube-scheduler-wasm-extension/guest/internal/proto" | ||
protoapi "sigs.k8s.io/kube-scheduler-wasm-extension/kubernetes/proto/api" | ||
) | ||
|
||
// Pod is exposed for the cyclestate package. | ||
var Pod proto.Pod = pod{} | ||
|
||
// CycleState is exposed for the cyclestate package. | ||
var CycleState api.CycleState = cycleState{} | ||
|
||
var currentCycleState = map[string]any{} | ||
|
||
var currentNodeToStatusMap = map[string]any{} | ||
|
||
type cycleState struct{} | ||
|
||
func (cycleState) Read(key string) (val any, ok bool) { | ||
val, ok = currentCycleState[key] | ||
return | ||
} | ||
|
||
func (cycleState) Write(key string, val any) { | ||
currentCycleState[key] = val | ||
} | ||
|
||
func (cycleState) Delete(key string) { | ||
delete(currentCycleState, key) | ||
} | ||
|
||
type pod struct{} | ||
|
||
func (pod) GetName() string { | ||
return internalproto.GetName(lazyPod()) | ||
} | ||
|
||
func (pod) GetNamespace() string { | ||
return internalproto.GetNamespace(lazyPod()) | ||
} | ||
|
||
func (pod) GetUid() string { | ||
return internalproto.GetUid(lazyPod()) | ||
} | ||
|
||
func (pod) Spec() *protoapi.PodSpec { | ||
return lazyPod().Spec | ||
} | ||
|
||
func (pod) Status() *protoapi.PodStatus { | ||
return lazyPod().Status | ||
} | ||
|
||
var currentPod *protoapi.Pod | ||
|
||
// lazyPod lazy initializes currentPod from imports.Pod. | ||
func lazyPod() *protoapi.Pod { | ||
if pod := currentPod; pod != nil { | ||
return pod | ||
} | ||
|
||
var msg protoapi.Pod | ||
if err := imports.Pod(msg.UnmarshalVT); err != nil { | ||
panic(err.Error()) | ||
} | ||
currentPod = &msg | ||
return currentPod | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
/* | ||
Copyright 2023 The Kubernetes 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 postfilter exports an api.PostFilterPlugin to the host. | ||
package postfilter | ||
|
||
import ( | ||
"sigs.k8s.io/kube-scheduler-wasm-extension/guest/api" | ||
internalpostfilter "sigs.k8s.io/kube-scheduler-wasm-extension/guest/internal/postfilter" | ||
) | ||
|
||
// TODO fix explanation | ||
// SetPlugin should be called in `main` to assign an api.PostFilterPlugin | ||
// instance. | ||
// | ||
// For example: | ||
// | ||
// func main() { | ||
// plugin := filterPlugin{} | ||
// prefilter.SetPlugin(plugin) | ||
// filter.SetPlugin(plugin) | ||
// } | ||
// | ||
// type filterPlugin struct{} | ||
// | ||
// func (filterPlugin) PreFilter(state api.CycleState, pod proto.Pod, nodeList proto.NodeList) { | ||
// // Write state you need on Filter | ||
// } | ||
// | ||
// func (filterPlugin) Filter(state api.CycleState, pod api.Pod, nodeInfo api.NodeInfo) (status *api.Status) { | ||
// var Filter int32 | ||
// // Derive Filter for the node name using state set on PreFilter! | ||
// return Filter, nil | ||
// } | ||
// | ||
// Note: This may be set without filter.SetPlugin, if the pre-filter plugin has | ||
// the only filtering logic, or only used to configure api.CycleState. | ||
func SetPlugin(postfilterPlugin api.PostFilterPlugin) { | ||
internalpostfilter.SetPlugin(postfilterPlugin) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.