...

Source file src/k8s.io/kubernetes/pkg/scheduler/testing/framework/fake_plugins.go

Documentation: k8s.io/kubernetes/pkg/scheduler/testing/framework

     1  /*
     2  Copyright 2020 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 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  // ErrReasonFake is a fake error message denotes the filter function errored.
    32  const ErrReasonFake = "Nodes failed the fake plugin"
    33  
    34  // FalseFilterPlugin is a filter plugin which always return Unschedulable when Filter function is called.
    35  type FalseFilterPlugin struct{}
    36  
    37  // Name returns name of the plugin.
    38  func (pl *FalseFilterPlugin) Name() string {
    39  	return "FalseFilter"
    40  }
    41  
    42  // Filter invoked at the filter extension point.
    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  // NewFalseFilterPlugin initializes a FalseFilterPlugin and returns it.
    48  func NewFalseFilterPlugin(_ context.Context, _ runtime.Object, _ framework.Handle) (framework.Plugin, error) {
    49  	return &FalseFilterPlugin{}, nil
    50  }
    51  
    52  // TrueFilterPlugin is a filter plugin which always return Success when Filter function is called.
    53  type TrueFilterPlugin struct{}
    54  
    55  // Name returns name of the plugin.
    56  func (pl *TrueFilterPlugin) Name() string {
    57  	return "TrueFilter"
    58  }
    59  
    60  // Filter invoked at the filter extension point.
    61  func (pl *TrueFilterPlugin) Filter(_ context.Context, _ *framework.CycleState, pod *v1.Pod, nodeInfo *framework.NodeInfo) *framework.Status {
    62  	return nil
    63  }
    64  
    65  // NewTrueFilterPlugin initializes a TrueFilterPlugin and returns it.
    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  // Name returns name of the plugin.
    76  func (pl FakePreFilterAndFilterPlugin) Name() string {
    77  	return "FakePreFilterAndFilterPlugin"
    78  }
    79  
    80  // FakeFilterPlugin is a test filter plugin to record how many times its Filter() function have
    81  // been called, and it returns different 'Code' depending on its internal 'failedNodeReturnCodeMap'.
    82  type FakeFilterPlugin struct {
    83  	NumFilterCalled         int32
    84  	FailedNodeReturnCodeMap map[string]framework.Code
    85  }
    86  
    87  // Name returns name of the plugin.
    88  func (pl *FakeFilterPlugin) Name() string {
    89  	return "FakeFilter"
    90  }
    91  
    92  // Filter invoked at the filter extension point.
    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  // NewFakeFilterPlugin initializes a fakeFilterPlugin and returns it.
   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  // MatchFilterPlugin is a filter plugin which return Success when the evaluated pod and node
   113  // have the same name; otherwise return Unschedulable.
   114  type MatchFilterPlugin struct{}
   115  
   116  // Name returns name of the plugin.
   117  func (pl *MatchFilterPlugin) Name() string {
   118  	return "MatchFilter"
   119  }
   120  
   121  // Filter invoked at the filter extension point.
   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  // NewMatchFilterPlugin initializes a MatchFilterPlugin and returns it.
   134  func NewMatchFilterPlugin(_ context.Context, _ runtime.Object, _ framework.Handle) (framework.Plugin, error) {
   135  	return &MatchFilterPlugin{}, nil
   136  }
   137  
   138  // FakePreFilterPlugin is a test filter plugin.
   139  type FakePreFilterPlugin struct {
   140  	Result *framework.PreFilterResult
   141  	Status *framework.Status
   142  	name   string
   143  }
   144  
   145  // Name returns name of the plugin.
   146  func (pl *FakePreFilterPlugin) Name() string {
   147  	return pl.name
   148  }
   149  
   150  // PreFilter invoked at the PreFilter extension point.
   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  // PreFilterExtensions no extensions implemented by this plugin.
   156  func (pl *FakePreFilterPlugin) PreFilterExtensions() framework.PreFilterExtensions {
   157  	return nil
   158  }
   159  
   160  // NewFakePreFilterPlugin initializes a fakePreFilterPlugin and returns it.
   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  // FakeReservePlugin is a test reserve plugin.
   172  type FakeReservePlugin struct {
   173  	Status *framework.Status
   174  }
   175  
   176  // Name returns name of the plugin.
   177  func (pl *FakeReservePlugin) Name() string {
   178  	return "FakeReserve"
   179  }
   180  
   181  // Reserve invoked at the Reserve extension point.
   182  func (pl *FakeReservePlugin) Reserve(_ context.Context, _ *framework.CycleState, _ *v1.Pod, _ string) *framework.Status {
   183  	return pl.Status
   184  }
   185  
   186  // Unreserve invoked at the Unreserve extension point.
   187  func (pl *FakeReservePlugin) Unreserve(_ context.Context, _ *framework.CycleState, _ *v1.Pod, _ string) {
   188  }
   189  
   190  // NewFakeReservePlugin initializes a fakeReservePlugin and returns it.
   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  // FakePreBindPlugin is a test prebind plugin.
   200  type FakePreBindPlugin struct {
   201  	Status *framework.Status
   202  }
   203  
   204  // Name returns name of the plugin.
   205  func (pl *FakePreBindPlugin) Name() string {
   206  	return "FakePreBind"
   207  }
   208  
   209  // PreBind invoked at the PreBind extension point.
   210  func (pl *FakePreBindPlugin) PreBind(_ context.Context, _ *framework.CycleState, _ *v1.Pod, _ string) *framework.Status {
   211  	return pl.Status
   212  }
   213  
   214  // NewFakePreBindPlugin initializes a fakePreBindPlugin and returns it.
   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  // FakePermitPlugin is a test permit plugin.
   224  type FakePermitPlugin struct {
   225  	Status  *framework.Status
   226  	Timeout time.Duration
   227  }
   228  
   229  // Name returns name of the plugin.
   230  func (pl *FakePermitPlugin) Name() string {
   231  	return "FakePermit"
   232  }
   233  
   234  // Permit invoked at the Permit extension point.
   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  // NewFakePermitPlugin initializes a fakePermitPlugin and returns it.
   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  // Name returns name of the plugin.
   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  // NewEqualPrioritizerPlugin returns a factory function to build equalPrioritizerPlugin.
   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