|
| 1 | +/* |
| 2 | + Copyright 2023 The Kubernetes Authors. |
| 3 | +
|
| 4 | + Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + you may not use this file except in compliance with the License. |
| 6 | + You may obtain a copy of the License at |
| 7 | +
|
| 8 | + http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | +
|
| 10 | + Unless required by applicable law or agreed to in writing, software |
| 11 | + distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + See the License for the specific language governing permissions and |
| 14 | + limitations under the License. |
| 15 | +*/ |
| 16 | + |
| 17 | +// Package permit exports an api.PermitPlugin to the host. |
| 18 | +package permit |
| 19 | + |
| 20 | +import ( |
| 21 | + "sigs.k8s.io/kube-scheduler-wasm-extension/guest/api" |
| 22 | + "sigs.k8s.io/kube-scheduler-wasm-extension/guest/internal/cyclestate" |
| 23 | + "sigs.k8s.io/kube-scheduler-wasm-extension/guest/internal/imports" |
| 24 | + "sigs.k8s.io/kube-scheduler-wasm-extension/guest/internal/plugin" |
| 25 | +) |
| 26 | + |
| 27 | +// permit is the current plugin assigned with SetPlugin. |
| 28 | +var permit api.PermitPlugin |
| 29 | + |
| 30 | +// SetPlugin should be called in `main` to assign an api.PermitPlugin |
| 31 | +// instance. |
| 32 | +// |
| 33 | +// For example: |
| 34 | +// |
| 35 | +// func main() { |
| 36 | +// plugin := permitPlugin{} |
| 37 | +// permit.SetPlugin(plugin) |
| 38 | +// } |
| 39 | +// |
| 40 | +// type permitPlugin struct{} |
| 41 | +// |
| 42 | +// func (permitPlugin) Permit(state api.CycleState, p proto.Pod, nodeName string) (status *api.Status, timeout uint32) |
| 43 | +// // Write state you need on Permit |
| 44 | +// } |
| 45 | +func SetPlugin(permitPlugin api.PermitPlugin) { |
| 46 | + if permitPlugin == nil { |
| 47 | + panic("nil permitPlugin") |
| 48 | + } |
| 49 | + permit = permitPlugin |
| 50 | + plugin.MustSet(permit) |
| 51 | +} |
| 52 | + |
| 53 | +// prevent unused lint errors (lint is run with normal go). |
| 54 | +var _ func() uint64 = _permit |
| 55 | + |
| 56 | +// _permit is only exported to the host. |
| 57 | +// |
| 58 | +//export permit |
| 59 | +func _permit() uint64 { |
| 60 | + if permit == nil { // Then, the user didn't define one. |
| 61 | + // Unlike most plugins we always export permit so that we can reset |
| 62 | + // the cycle state: return success to avoid no-op overhead. |
| 63 | + return 0 |
| 64 | + } |
| 65 | + |
| 66 | + pod := cyclestate.Pod |
| 67 | + nodeName := imports.NodeName() |
| 68 | + status, timeout := permit.Permit(cyclestate.Values, pod, nodeName) |
| 69 | + |
| 70 | + // Pack the score and status code into a single WebAssembly 1.0 compatible |
| 71 | + // result |
| 72 | + return (uint64(imports.StatusToCode(status)) << uint64(32)) | uint64(timeout) |
| 73 | +} |
0 commit comments