|
| 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 reserve exports an api.ReservePlugin to the host. |
| 18 | +package reserve |
| 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 | +// reserve is the current plugin assigned with SetPlugin. |
| 28 | +var reserve api.ReservePlugin |
| 29 | + |
| 30 | +// SetPlugin should be called in `main` to assign an api.ReservePlugin instance. |
| 31 | +// |
| 32 | +// For example: |
| 33 | +// |
| 34 | +// func main() { |
| 35 | +// plugin := reservePlugin{} |
| 36 | +// reserve.SetPlugin(plugin) |
| 37 | +// } |
| 38 | +// |
| 39 | +// type reservePlugin struct{} |
| 40 | +// |
| 41 | +// func (reservePlugin) Reserve(state api.CycleState, pod proto.Pod, nodeName string) (status *api.Status) { |
| 42 | +// // Write state you need on Reserve |
| 43 | +// } |
| 44 | +// |
| 45 | +// func (reservePlugin) Unreserve(state api.CycleState, pod proto.Pod, nodeName string) { |
| 46 | +// // Write state you need on Unreserve |
| 47 | +// } |
| 48 | +func SetPlugin(reservePlugin api.ReservePlugin) { |
| 49 | + if reservePlugin == nil { |
| 50 | + panic("nil reservePlugin") |
| 51 | + } |
| 52 | + reserve = reservePlugin |
| 53 | + plugin.MustSet(reserve) |
| 54 | +} |
| 55 | + |
| 56 | +// prevent unused lint errors (lint is run with normal go). |
| 57 | +var ( |
| 58 | + _ func() uint32 = _reserve |
| 59 | + _ func() = _unreserve |
| 60 | +) |
| 61 | + |
| 62 | +// _reserve is only exported to the host. |
| 63 | +// |
| 64 | +//export reserve |
| 65 | +func _reserve() uint32 { //nolint |
| 66 | + if reserve == nil { // Then, the user didn't define one. |
| 67 | + // Unlike most plugins we always export reserve so that we can reset |
| 68 | + // the cycle state: return success to avoid no-op overhead. |
| 69 | + return 0 |
| 70 | + } |
| 71 | + |
| 72 | + nodeName := imports.NodeName() |
| 73 | + status := reserve.Reserve(cyclestate.Values, cyclestate.Pod, nodeName) |
| 74 | + |
| 75 | + return imports.StatusToCode(status) |
| 76 | +} |
| 77 | + |
| 78 | +// _unreserve is only exported to the host. |
| 79 | +// |
| 80 | +//export unreserve |
| 81 | +func _unreserve() { //nolint |
| 82 | + if reserve == nil { // Then, the user didn't define one. |
| 83 | + // Unlike most plugins we always export unreserve so that we can reset |
| 84 | + // the cycle state: return success to avoid no-op overhead. |
| 85 | + return |
| 86 | + } |
| 87 | + |
| 88 | + nodeName := imports.NodeName() |
| 89 | + reserve.Unreserve(cyclestate.Values, cyclestate.Pod, nodeName) |
| 90 | +} |
0 commit comments