1
16
17 package framework
18
19 import (
20 "context"
21 "fmt"
22 "sync/atomic"
23 "time"
24
25 v1 "k8s.io/api/core/v1"
26 "k8s.io/apimachinery/pkg/runtime"
27 "k8s.io/kubernetes/pkg/scheduler/framework"
28 frameworkruntime "k8s.io/kubernetes/pkg/scheduler/framework/runtime"
29 )
30
31
32 const ErrReasonFake = "Nodes failed the fake plugin"
33
34
35 type FalseFilterPlugin struct{}
36
37
38 func (pl *FalseFilterPlugin) Name() string {
39 return "FalseFilter"
40 }
41
42
43 func (pl *FalseFilterPlugin) Filter(_ context.Context, _ *framework.CycleState, pod *v1.Pod, nodeInfo *framework.NodeInfo) *framework.Status {
44 return framework.NewStatus(framework.Unschedulable, ErrReasonFake)
45 }
46
47
48 func NewFalseFilterPlugin(_ context.Context, _ runtime.Object, _ framework.Handle) (framework.Plugin, error) {
49 return &FalseFilterPlugin{}, nil
50 }
51
52
53 type TrueFilterPlugin struct{}
54
55
56 func (pl *TrueFilterPlugin) Name() string {
57 return "TrueFilter"
58 }
59
60
61 func (pl *TrueFilterPlugin) Filter(_ context.Context, _ *framework.CycleState, pod *v1.Pod, nodeInfo *framework.NodeInfo) *framework.Status {
62 return nil
63 }
64
65
66 func NewTrueFilterPlugin(_ context.Context, _ runtime.Object, _ framework.Handle) (framework.Plugin, error) {
67 return &TrueFilterPlugin{}, nil
68 }
69
70 type FakePreFilterAndFilterPlugin struct {
71 *FakePreFilterPlugin
72 *FakeFilterPlugin
73 }
74
75
76 func (pl FakePreFilterAndFilterPlugin) Name() string {
77 return "FakePreFilterAndFilterPlugin"
78 }
79
80
81
82 type FakeFilterPlugin struct {
83 NumFilterCalled int32
84 FailedNodeReturnCodeMap map[string]framework.Code
85 }
86
87
88 func (pl *FakeFilterPlugin) Name() string {
89 return "FakeFilter"
90 }
91
92
93 func (pl *FakeFilterPlugin) Filter(_ context.Context, _ *framework.CycleState, pod *v1.Pod, nodeInfo *framework.NodeInfo) *framework.Status {
94 atomic.AddInt32(&pl.NumFilterCalled, 1)
95
96 if returnCode, ok := pl.FailedNodeReturnCodeMap[nodeInfo.Node().Name]; ok {
97 return framework.NewStatus(returnCode, fmt.Sprintf("injecting failure for pod %v", pod.Name))
98 }
99
100 return nil
101 }
102
103
104 func NewFakeFilterPlugin(failedNodeReturnCodeMap map[string]framework.Code) frameworkruntime.PluginFactory {
105 return func(_ context.Context, _ runtime.Object, _ framework.Handle) (framework.Plugin, error) {
106 return &FakeFilterPlugin{
107 FailedNodeReturnCodeMap: failedNodeReturnCodeMap,
108 }, nil
109 }
110 }
111
112
113
114 type MatchFilterPlugin struct{}
115
116
117 func (pl *MatchFilterPlugin) Name() string {
118 return "MatchFilter"
119 }
120
121
122 func (pl *MatchFilterPlugin) Filter(_ context.Context, _ *framework.CycleState, pod *v1.Pod, nodeInfo *framework.NodeInfo) *framework.Status {
123 node := nodeInfo.Node()
124 if node == nil {
125 return framework.NewStatus(framework.Error, "node not found")
126 }
127 if pod.Name == node.Name {
128 return nil
129 }
130 return framework.NewStatus(framework.Unschedulable, ErrReasonFake)
131 }
132
133
134 func NewMatchFilterPlugin(_ context.Context, _ runtime.Object, _ framework.Handle) (framework.Plugin, error) {
135 return &MatchFilterPlugin{}, nil
136 }
137
138
139 type FakePreFilterPlugin struct {
140 Result *framework.PreFilterResult
141 Status *framework.Status
142 name string
143 }
144
145
146 func (pl *FakePreFilterPlugin) Name() string {
147 return pl.name
148 }
149
150
151 func (pl *FakePreFilterPlugin) PreFilter(_ context.Context, _ *framework.CycleState, pod *v1.Pod) (*framework.PreFilterResult, *framework.Status) {
152 return pl.Result, pl.Status
153 }
154
155
156 func (pl *FakePreFilterPlugin) PreFilterExtensions() framework.PreFilterExtensions {
157 return nil
158 }
159
160
161 func NewFakePreFilterPlugin(name string, result *framework.PreFilterResult, status *framework.Status) frameworkruntime.PluginFactory {
162 return func(_ context.Context, _ runtime.Object, _ framework.Handle) (framework.Plugin, error) {
163 return &FakePreFilterPlugin{
164 Result: result,
165 Status: status,
166 name: name,
167 }, nil
168 }
169 }
170
171
172 type FakeReservePlugin struct {
173 Status *framework.Status
174 }
175
176
177 func (pl *FakeReservePlugin) Name() string {
178 return "FakeReserve"
179 }
180
181
182 func (pl *FakeReservePlugin) Reserve(_ context.Context, _ *framework.CycleState, _ *v1.Pod, _ string) *framework.Status {
183 return pl.Status
184 }
185
186
187 func (pl *FakeReservePlugin) Unreserve(_ context.Context, _ *framework.CycleState, _ *v1.Pod, _ string) {
188 }
189
190
191 func NewFakeReservePlugin(status *framework.Status) frameworkruntime.PluginFactory {
192 return func(_ context.Context, _ runtime.Object, _ framework.Handle) (framework.Plugin, error) {
193 return &FakeReservePlugin{
194 Status: status,
195 }, nil
196 }
197 }
198
199
200 type FakePreBindPlugin struct {
201 Status *framework.Status
202 }
203
204
205 func (pl *FakePreBindPlugin) Name() string {
206 return "FakePreBind"
207 }
208
209
210 func (pl *FakePreBindPlugin) PreBind(_ context.Context, _ *framework.CycleState, _ *v1.Pod, _ string) *framework.Status {
211 return pl.Status
212 }
213
214
215 func NewFakePreBindPlugin(status *framework.Status) frameworkruntime.PluginFactory {
216 return func(_ context.Context, _ runtime.Object, _ framework.Handle) (framework.Plugin, error) {
217 return &FakePreBindPlugin{
218 Status: status,
219 }, nil
220 }
221 }
222
223
224 type FakePermitPlugin struct {
225 Status *framework.Status
226 Timeout time.Duration
227 }
228
229
230 func (pl *FakePermitPlugin) Name() string {
231 return "FakePermit"
232 }
233
234
235 func (pl *FakePermitPlugin) Permit(_ context.Context, _ *framework.CycleState, _ *v1.Pod, _ string) (*framework.Status, time.Duration) {
236 return pl.Status, pl.Timeout
237 }
238
239
240 func NewFakePermitPlugin(status *framework.Status, timeout time.Duration) frameworkruntime.PluginFactory {
241 return func(_ context.Context, _ runtime.Object, _ framework.Handle) (framework.Plugin, error) {
242 return &FakePermitPlugin{
243 Status: status,
244 Timeout: timeout,
245 }, nil
246 }
247 }
248
249 type FakePreScoreAndScorePlugin struct {
250 name string
251 score int64
252 preScoreStatus *framework.Status
253 scoreStatus *framework.Status
254 }
255
256
257 func (pl *FakePreScoreAndScorePlugin) Name() string {
258 return pl.name
259 }
260
261 func (pl *FakePreScoreAndScorePlugin) Score(ctx context.Context, state *framework.CycleState, p *v1.Pod, nodeName string) (int64, *framework.Status) {
262 return pl.score, pl.scoreStatus
263 }
264
265 func (pl *FakePreScoreAndScorePlugin) ScoreExtensions() framework.ScoreExtensions {
266 return nil
267 }
268
269 func (pl *FakePreScoreAndScorePlugin) PreScore(ctx context.Context, state *framework.CycleState, pod *v1.Pod, nodes []*framework.NodeInfo) *framework.Status {
270 return pl.preScoreStatus
271 }
272
273 func NewFakePreScoreAndScorePlugin(name string, score int64, preScoreStatus, scoreStatus *framework.Status) frameworkruntime.PluginFactory {
274 return func(_ context.Context, _ runtime.Object, _ framework.Handle) (framework.Plugin, error) {
275 return &FakePreScoreAndScorePlugin{
276 name: name,
277 score: score,
278 preScoreStatus: preScoreStatus,
279 scoreStatus: scoreStatus,
280 }, nil
281 }
282 }
283
284
285 func NewEqualPrioritizerPlugin() frameworkruntime.PluginFactory {
286 return func(_ context.Context, _ runtime.Object, _ framework.Handle) (framework.Plugin, error) {
287 return &FakePreScoreAndScorePlugin{
288 name: "EqualPrioritizerPlugin",
289 score: 1,
290 }, nil
291 }
292 }
293
View as plain text