1 /* 2 Copyright 2019 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 stateful 18 19 import ( 20 "context" 21 "fmt" 22 "sync" 23 24 v1 "k8s.io/api/core/v1" 25 "k8s.io/apimachinery/pkg/runtime" 26 "k8s.io/klog/v2" 27 "k8s.io/kubernetes/pkg/scheduler/framework" 28 ) 29 30 // MultipointExample is an example plugin that is executed at multiple extension points. 31 // This plugin is stateful. It receives arguments at initialization (NewMultipointPlugin) 32 // and changes its state when it is executed. 33 type MultipointExample struct { 34 executionPoints []string 35 mu sync.RWMutex 36 } 37 38 var _ framework.ReservePlugin = &MultipointExample{} 39 var _ framework.PreBindPlugin = &MultipointExample{} 40 41 // Name is the name of the plug used in Registry and configurations. 42 const Name = "multipoint-plugin-example" 43 44 // Name returns name of the plugin. It is used in logs, etc. 45 func (mp *MultipointExample) Name() string { 46 return Name 47 } 48 49 // Reserve is the function invoked by the framework at "reserve" extension 50 // point. In this trivial example, the Reserve method allocates an array of 51 // strings. 52 func (mp *MultipointExample) Reserve(ctx context.Context, state *framework.CycleState, pod *v1.Pod, nodeName string) *framework.Status { 53 // Reserve is not called concurrently, and so we don't need to lock. 54 mp.executionPoints = append(mp.executionPoints, "reserve") 55 return nil 56 } 57 58 // Unreserve is the function invoked by the framework when any error happens 59 // during "reserve" extension point or later. In this example, the Unreserve 60 // method loses its reference to the string slice, allowing it to be garbage 61 // collected, and thereby "unallocating" the reserved resources. 62 func (mp *MultipointExample) Unreserve(ctx context.Context, state *framework.CycleState, pod *v1.Pod, nodeName string) { 63 // Unlike Reserve, the Unreserve method may be called concurrently since 64 // there is no guarantee that there will only one unreserve operation at any 65 // given point in time (for example, during the binding cycle). 66 mp.mu.Lock() 67 defer mp.mu.Unlock() 68 mp.executionPoints = nil 69 } 70 71 // PreBind is the function invoked by the framework at "prebind" extension 72 // point. 73 func (mp *MultipointExample) PreBind(ctx context.Context, state *framework.CycleState, pod *v1.Pod, nodeName string) *framework.Status { 74 // PreBind could be called concurrently for different pods. 75 mp.mu.Lock() 76 defer mp.mu.Unlock() 77 mp.executionPoints = append(mp.executionPoints, "pre-bind") 78 if pod == nil { 79 return framework.NewStatus(framework.Error, "pod must not be nil") 80 } 81 return nil 82 } 83 84 // New initializes a new plugin and returns it. 85 func New(ctx context.Context, config *runtime.Unknown, _ framework.Handle) (framework.Plugin, error) { 86 if config == nil { 87 klog.FromContext(ctx).Error(nil, "MultipointExample configuration cannot be empty") 88 return nil, fmt.Errorf("MultipointExample configuration cannot be empty") 89 } 90 mp := MultipointExample{} 91 return &mp, nil 92 } 93