stateful.go 2.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  1. /*
  2. Copyright 2019 The Kubernetes Authors.
  3. Licensed under the Apache License, Version 2.0 (the "License");
  4. you may not use this file except in compliance with the License.
  5. You may obtain a copy of the License at
  6. http://www.apache.org/licenses/LICENSE-2.0
  7. Unless required by applicable law or agreed to in writing, software
  8. distributed under the License is distributed on an "AS IS" BASIS,
  9. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  10. See the License for the specific language governing permissions and
  11. limitations under the License.
  12. */
  13. package stateful
  14. import (
  15. "context"
  16. "fmt"
  17. "sync"
  18. v1 "k8s.io/api/core/v1"
  19. "k8s.io/apimachinery/pkg/runtime"
  20. "k8s.io/klog"
  21. framework "k8s.io/kubernetes/pkg/scheduler/framework/v1alpha1"
  22. )
  23. // MultipointExample is an example plugin that is executed at multiple extension points.
  24. // This plugin is stateful. It receives arguments at initialization (NewMultipointPlugin)
  25. // and changes its state when it is executed.
  26. type MultipointExample struct {
  27. mpState map[int]string
  28. numRuns int
  29. mu sync.RWMutex
  30. }
  31. var _ framework.ReservePlugin = &MultipointExample{}
  32. var _ framework.PreBindPlugin = &MultipointExample{}
  33. // Name is the name of the plug used in Registry and configurations.
  34. const Name = "multipoint-plugin-example"
  35. // Name returns name of the plugin. It is used in logs, etc.
  36. func (mp *MultipointExample) Name() string {
  37. return Name
  38. }
  39. // Reserve is the functions invoked by the framework at "reserve" extension point.
  40. func (mp *MultipointExample) Reserve(ctx context.Context, state *framework.CycleState, pod *v1.Pod, nodeName string) *framework.Status {
  41. // Reserve is not called concurrently, and so we don't need to lock.
  42. mp.numRuns++
  43. return nil
  44. }
  45. // PreBind is the functions invoked by the framework at "prebind" extension point.
  46. func (mp *MultipointExample) PreBind(ctx context.Context, state *framework.CycleState, pod *v1.Pod, nodeName string) *framework.Status {
  47. // PreBind could be called concurrently for different pods.
  48. mp.mu.Lock()
  49. defer mp.mu.Unlock()
  50. mp.numRuns++
  51. if pod == nil {
  52. return framework.NewStatus(framework.Error, "pod must not be nil")
  53. }
  54. return nil
  55. }
  56. // New initializes a new plugin and returns it.
  57. func New(config *runtime.Unknown, _ framework.FrameworkHandle) (framework.Plugin, error) {
  58. if config == nil {
  59. klog.Error("MultipointExample configuration cannot be empty")
  60. return nil, fmt.Errorf("MultipointExample configuration cannot be empty")
  61. }
  62. mp := MultipointExample{
  63. mpState: make(map[int]string),
  64. }
  65. return &mp, nil
  66. }