-
Notifications
You must be signed in to change notification settings - Fork 22
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[WIP] feat: port the NodeUnschedulable in-tree plugin to wasm #123
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -5,13 +5,16 @@ golangci_lint := github.com/golangci/golangci-lint/cmd/[email protected] | |
examples/advanced/main.wasm: examples/advanced/main.go | ||
@(cd $(@D); tinygo build -o main.wasm -gc=custom -tags=custommalloc -scheduler=none --no-debug -target=wasi .) | ||
|
||
examples/nodeunschedulable/main.wasm: examples/nodeunschedulable/main.go | ||
@(cd $(@D); tinygo build -o main.wasm -gc=custom -tags=custommalloc -scheduler=none --no-debug -target=wasi .) | ||
|
||
%/main.wasm: %/main.go | ||
@(cd $(@D); tinygo build -o main.wasm -scheduler=none --no-debug -target=wasi .) | ||
|
||
.PHONY: build-tinygo | ||
build-tinygo: examples/nodenumber/main.wasm examples/advanced/main.wasm examples/imagelocality/main.wasm guest/testdata/cyclestate/main.wasm guest/testdata/filter/main.wasm guest/testdata/score/main.wasm \ | ||
build-tinygo: examples/nodenumber/main.wasm examples/advanced/main.wasm examples/imagelocality/main.wasm examples/nodeunschedulable/main.wasm guest/testdata/cyclestate/main.wasm guest/testdata/filter/main.wasm guest/testdata/score/main.wasm \ | ||
guest/testdata/bind/main.wasm guest/testdata/reserve/main.wasm guest/testdata/handle/main.wasm guest/testdata/permit/main.wasm \ | ||
internal/e2e/scheduler_perf/wasm/nodenumber/main.wasm | ||
internal/e2e/scheduler_perf/wasm/nodenumber/main.wasm internal/e2e/scheduler_perf/wasm/nodeunschedulable/main.wasm | ||
|
||
%/main-debug.wasm: %/main.go | ||
@(cd $(@D); tinygo build -o main-debug.wasm -gc=custom -tags=custommalloc -scheduler=none -target=wasi .) | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
module sigs.k8s.io/kube-scheduler-wasm-extension/examples/nodeunschedulable | ||
|
||
go 1.20 | ||
|
||
replace sigs.k8s.io/kube-scheduler-wasm-extension/guest => ./../../guest | ||
|
||
replace sigs.k8s.io/kube-scheduler-wasm-extension/kubernetes/proto => ./../../kubernetes/proto | ||
|
||
require ( | ||
github.com/wasilibs/nottinygc v0.7.1 | ||
sigs.k8s.io/kube-scheduler-wasm-extension/guest v0.0.0-00010101000000-000000000000 | ||
sigs.k8s.io/kube-scheduler-wasm-extension/kubernetes/proto v0.0.0-00010101000000-000000000000 | ||
) | ||
|
||
require ( | ||
github.com/google/go-cmp v0.6.0 // indirect | ||
github.com/magefile/mage v1.14.0 // indirect | ||
google.golang.org/protobuf v1.34.2 // indirect | ||
) |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
github.com/google/go-cmp v0.6.0 h1:ofyhxvXcZhMsU5ulbFiLKl/XBFqE1GSq7atu8tAmTRI= | ||
github.com/google/go-cmp v0.6.0/go.mod h1:17dUlkBOakJ0+DkrSSNjCkIjxS6bF9zb3elmeNGIjoY= | ||
github.com/magefile/mage v1.14.0 h1:6QDX3g6z1YvJ4olPhT1wksUcSa/V0a1B+pJb73fBjyo= | ||
github.com/magefile/mage v1.14.0/go.mod h1:z5UZb/iS3GoOSn0JgWuiw7dxlurVYTu+/jHXqQg881A= | ||
github.com/wasilibs/nottinygc v0.7.1 h1:rKu19+SFniRNuSo5NX7/wxpSpXmMUmkcyt/YiWLJg8w= | ||
github.com/wasilibs/nottinygc v0.7.1/go.mod h1:oDcIotskuYNMpqMF23l7Z8uzD4TC0WXHK8jetlB3HIo= | ||
google.golang.org/protobuf v1.34.2 h1:6xV6lTsCfpGD21XK49h7MhtcApnLqkfYgPcdHftf6hg= | ||
google.golang.org/protobuf v1.34.2/go.mod h1:qYOHts0dSfpeUzUFpOMr/WGzszTmLH+DiWniOlNbLDw= |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,84 @@ | ||
/* | ||
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 main is the entrypoint of the %.wasm file, compiled with | ||
// '-target=wasi'. See /guest/RATIONALE.md for details. | ||
package main | ||
|
||
import ( | ||
_ "github.com/wasilibs/nottinygc" | ||
|
||
"sigs.k8s.io/kube-scheduler-wasm-extension/guest/api" | ||
"sigs.k8s.io/kube-scheduler-wasm-extension/guest/api/helper" | ||
"sigs.k8s.io/kube-scheduler-wasm-extension/guest/api/proto" | ||
"sigs.k8s.io/kube-scheduler-wasm-extension/guest/config" | ||
"sigs.k8s.io/kube-scheduler-wasm-extension/guest/klog" | ||
klogapi "sigs.k8s.io/kube-scheduler-wasm-extension/guest/klog/api" | ||
"sigs.k8s.io/kube-scheduler-wasm-extension/guest/plugin" | ||
"sigs.k8s.io/kube-scheduler-wasm-extension/guest/util" | ||
k8sproto "sigs.k8s.io/kube-scheduler-wasm-extension/kubernetes/proto/api" | ||
) | ||
|
||
const ( | ||
// ErrReasonUnknownCondition is used for NodeUnknownCondition predicate error. | ||
ErrReasonUnknownCondition = "node(s) had unknown conditions" | ||
// ErrReasonUnschedulable is used for NodeUnschedulable predicate error. | ||
ErrReasonUnschedulable = "node(s) were unschedulable" | ||
) | ||
|
||
// main is compiled to a WebAssembly function named "_start", called by the | ||
// wasm scheduler plugin during initialization. | ||
func main() { | ||
p, err := New(klog.Get(), config.Get()) | ||
if err != nil { | ||
panic(err) | ||
} | ||
plugin.Set(p) | ||
} | ||
|
||
func New(klog klogapi.Klog, jsonConfig []byte) (api.Plugin, error) { | ||
return &NodeUnschedulable{}, nil | ||
} | ||
|
||
// NodeUnschedulable plugin filters nodes that set node.Spec.Unschedulable=true unless | ||
// the pod tolerates {key=node.kubernetes.io/unschedulable, effect:NoSchedule} taint. | ||
type NodeUnschedulable struct{} | ||
|
||
func (p *NodeUnschedulable) Name() string { | ||
return "NodeUnschedulable" | ||
} | ||
|
||
func (p *NodeUnschedulable) Filter(_ api.CycleState, pod proto.Pod, nodeInfo api.NodeInfo) *api.Status { | ||
node := nodeInfo.Node() | ||
if node == nil { | ||
return &api.Status{ | ||
Code: api.StatusCodeUnschedulableAndUnresolvable, | ||
Reason: ErrReasonUnknownCondition, | ||
} | ||
} | ||
// If pod tolerate unschedulable taint, it's also tolerate `node.Spec.Unschedulable`. | ||
podToleratesUnschedulable := helper.TolerationsTolerateTaint(pod.Spec().Tolerations, &k8sproto.Taint{ | ||
Key: util.To(api.TaintNodeUnschedulable), | ||
Effect: util.To(api.TaintEffectNoSchedule), | ||
}) | ||
if *node.Spec().Unschedulable && !podToleratesUnschedulable { | ||
return &api.Status{ | ||
Code: api.StatusCodeUnschedulableAndUnresolvable, | ||
Reason: ErrReasonUnschedulable, | ||
} | ||
} | ||
return nil | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,16 @@ | ||
package api | ||
|
||
type QueueingHint int | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This refers to https://github.com/kubernetes/kubernetes/blob/master/pkg/scheduler/framework/types.go#L256-L263. Where should this be placed? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Why do we need this in the first place? We don't support QueueingHint in the extension yet. |
||
|
||
const ( | ||
// QueueSkip implies that the cluster event has no impact on | ||
// scheduling of the pod. | ||
QueueSkip QueueingHint = iota | ||
|
||
// Queue implies that the Pod may be schedulable by the event. | ||
Queue | ||
) | ||
|
||
// ImageStateSummary provides summarized information about the state of an image. | ||
type ImageStateSummary struct { | ||
// Size of the image | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
package helper | ||
|
||
import ( | ||
"sigs.k8s.io/kube-scheduler-wasm-extension/guest/api" | ||
protoapi "sigs.k8s.io/kube-scheduler-wasm-extension/kubernetes/proto/api" | ||
) | ||
|
||
// TolerationsTolerateTaint checks if taint is tolerated by any of the tolerations. | ||
func TolerationsTolerateTaint(tolerations []*protoapi.Toleration, taint *protoapi.Taint) bool { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This refers to this function https://github.com/kubernetes/kubernetes/blob/master/staging/src/k8s.io/component-helpers/scheduling/corev1/helpers.go#L63-L70. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. You should put it (along with |
||
for i := range tolerations { | ||
if api.ToleratesTaint(tolerations[i], taint) { | ||
return true | ||
} | ||
} | ||
return false | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
package api | ||
|
||
import v1 "sigs.k8s.io/kube-scheduler-wasm-extension/kubernetes/proto/api" | ||
|
||
const ( | ||
// TaintNodeUnschedulable will be added when node becomes unschedulable | ||
// and removed when node becomes schedulable. | ||
TaintNodeUnschedulable string = "node.kubernetes.io/unschedulable" | ||
// Do not allow new pods to schedule onto the node unless they tolerate the taint, | ||
// but allow all pods submitted to Kubelet without going through the scheduler | ||
// to start, and allow all already-running pods to continue running. | ||
// Enforced by the scheduler. | ||
TaintEffectNoSchedule string = "NoSchedule" | ||
TolerationOpExists string = "Exists" | ||
TolerationOpEqual string = "Equal" | ||
) | ||
|
||
// ToleratesTaint checks if the toleration tolerates the taint. | ||
// The matching follows the rules below: | ||
// | ||
// 1. Empty toleration.effect means to match all taint effects, | ||
// otherwise taint effect must equal to toleration.effect. | ||
// 2. If toleration.operator is 'Exists', it means to match all taint values. | ||
// 3. Empty toleration.key means to match all taint keys. | ||
// If toleration.key is empty, toleration.operator must be 'Exists'; | ||
// this combination means to match all taint values and all taint keys. | ||
func ToleratesTaint(toleration *v1.Toleration, taint *v1.Taint) bool { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This refers to this function https://github.com/kubernetes/kubernetes/blob/master/staging/src/k8s.io/api/core/v1/toleration.go#L38-L57. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yes, kubernetes folder is only for generating the object files. Also, we cannot import the things from kubernetes because of the limitation coming from TinyGo. |
||
if len(toleration.GetEffect()) > 0 && toleration.Effect != taint.Effect { | ||
return false | ||
} | ||
|
||
if len(toleration.GetKey()) > 0 && toleration.Key != taint.Key { | ||
return false | ||
} | ||
|
||
// TODO: Use proper defaulting when Toleration becomes a field of PodSpec | ||
switch *toleration.Operator { | ||
// empty operator means Equal | ||
case "", TolerationOpEqual: | ||
return toleration.Value == taint.Value | ||
case TolerationOpExists: | ||
return true | ||
default: | ||
return false | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
package util | ||
|
||
import ( | ||
"fmt" | ||
) | ||
|
||
// As converts two objects to the given type. | ||
// Both objects must be of the same type. If not, an error is returned. | ||
// nil objects are allowed and will be converted to nil. | ||
func As[T any](oldObj, newobj interface{}) (T, T, error) { | ||
var oldTyped T | ||
var newTyped T | ||
var ok bool | ||
if newobj != nil { | ||
newTyped, ok = newobj.(T) | ||
if !ok { | ||
return oldTyped, newTyped, fmt.Errorf("expected %T, but got %T", newTyped, newobj) | ||
} | ||
} | ||
|
||
if oldObj != nil { | ||
oldTyped, ok = oldObj.(T) | ||
if !ok { | ||
return oldTyped, newTyped, fmt.Errorf("expected %T, but got %T", oldTyped, oldObj) | ||
} | ||
} | ||
return oldTyped, newTyped, nil | ||
} | ||
|
||
// To returns a pointer to the given value. | ||
func To[T any](v T) *T { | ||
return &v | ||
} |
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Please revert all the changes in scheduler-perf test cases. |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -5,11 +5,11 @@ profiles: | |
multiPoint: | ||
enabled: | ||
- name: wasm | ||
- name: NodeNumber | ||
- name: NodeUnschedulable | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This was edited so I can test does it work. |
||
pluginConfig: | ||
- name: wasm | ||
args: | ||
guestURL: "file://../../../examples/nodenumber/main.wasm" | ||
guestURL: "file://../../../examples/nodeunschedulable/main.wasm" | ||
extenders: | ||
- urlPrefix: "http://localhost:8080/" | ||
prioritizeVerb: "priorities" | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -4,4 +4,4 @@ profiles: | |
- plugins: | ||
multiPoint: | ||
enabled: | ||
- name: NodeNumber | ||
- name: NodeUnschedulable |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
# NodeUnschedulable Plugin | ||
|
||
This is the nodeunschedulable wasm plugin based on the in-tree, which only implements Filter. |
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Why do we need this? I mean, can we just use the wasm built from examples/nodeunschedulable? |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,84 @@ | ||
/* | ||
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 main is the entrypoint of the %.wasm file, compiled with | ||
// '-target=wasi'. See /guest/RATIONALE.md for details. | ||
package main | ||
|
||
import ( | ||
"sigs.k8s.io/kube-scheduler-wasm-extension/guest/api" | ||
"sigs.k8s.io/kube-scheduler-wasm-extension/guest/api/helper" | ||
"sigs.k8s.io/kube-scheduler-wasm-extension/guest/api/proto" | ||
"sigs.k8s.io/kube-scheduler-wasm-extension/guest/config" | ||
"sigs.k8s.io/kube-scheduler-wasm-extension/guest/klog" | ||
klogapi "sigs.k8s.io/kube-scheduler-wasm-extension/guest/klog/api" | ||
"sigs.k8s.io/kube-scheduler-wasm-extension/guest/plugin" | ||
"sigs.k8s.io/kube-scheduler-wasm-extension/guest/util" | ||
k8sproto "sigs.k8s.io/kube-scheduler-wasm-extension/kubernetes/proto/api" | ||
) | ||
|
||
const ( | ||
// ErrReasonUnknownCondition is used for NodeUnknownCondition predicate error. | ||
ErrReasonUnknownCondition = "node(s) had unknown conditions" | ||
// ErrReasonUnschedulable is used for NodeUnschedulable predicate error. | ||
ErrReasonUnschedulable = "node(s) were unschedulable" | ||
) | ||
|
||
func main() { | ||
p, err := New(klog.Get(), config.Get()) | ||
if err != nil { | ||
panic(err) | ||
} | ||
plugin.Set(p) | ||
} | ||
|
||
func New(klog klogapi.Klog, jsonConfig []byte) (api.Plugin, error) { | ||
return &NodeUnschedulable{}, nil | ||
} | ||
|
||
// NodeUnschedulable plugin filters nodes that set node.Spec.Unschedulable=true unless | ||
// the pod tolerates {key=node.kubernetes.io/unschedulable, effect:NoSchedule} taint. | ||
type NodeUnschedulable struct{} | ||
|
||
func (p *NodeUnschedulable) Name() string { | ||
return "NodeUnschedulable" | ||
} | ||
|
||
func (p *NodeUnschedulable) Filter(_ api.CycleState, pod proto.Pod, nodeInfo api.NodeInfo) *api.Status { | ||
node := nodeInfo.Node() | ||
if node == nil { | ||
return &api.Status{ | ||
Code: api.StatusCodeUnschedulableAndUnresolvable, | ||
Reason: ErrReasonUnknownCondition, | ||
} | ||
} | ||
// If pod tolerate unschedulable taint, it's also tolerate `node.Spec.Unschedulable`. | ||
podToleratesUnschedulable := helper.TolerationsTolerateTaint(pod.Spec().Tolerations, &k8sproto.Taint{ | ||
Key: util.To(api.TaintNodeUnschedulable), | ||
Effect: util.To(api.TaintEffectNoSchedule), | ||
}) | ||
unschedulable := false | ||
if node.Spec().Unschedulable != nil { | ||
unschedulable = *node.Spec().Unschedulable | ||
} | ||
if unschedulable && !podToleratesUnschedulable { | ||
return &api.Status{ | ||
Code: api.StatusCodeUnschedulableAndUnresolvable, | ||
Reason: ErrReasonUnschedulable, | ||
} | ||
} | ||
return nil | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I do not understand why the
internal/e2e/scheduler_perf/wasm/nodeunschedulable
can be compiled successfully with the%/main.wasm
but theexamples/nodeunschedulable
one which is almost identical fails and needs the-gc=custom -tags=custommalloc
flags.