plugin.go 2.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  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 interpodaffinity
  14. import (
  15. "fmt"
  16. "sync"
  17. "k8s.io/apimachinery/pkg/runtime"
  18. framework "k8s.io/kubernetes/pkg/scheduler/framework/v1alpha1"
  19. schedulerlisters "k8s.io/kubernetes/pkg/scheduler/listers"
  20. )
  21. const (
  22. // Name is the name of the plugin used in the plugin registry and configurations.
  23. Name = "InterPodAffinity"
  24. defaultHardPodAffinityWeight int32 = 1
  25. minHardPodAffinityWeight int32 = 0
  26. maxHardPodAffinityWeight int32 = 100
  27. )
  28. // Args holds the args that are used to configure the plugin.
  29. type Args struct {
  30. HardPodAffinityWeight *int32 `json:"hardPodAffinityWeight,omitempty"`
  31. }
  32. var _ framework.PreFilterPlugin = &InterPodAffinity{}
  33. var _ framework.FilterPlugin = &InterPodAffinity{}
  34. var _ framework.PostFilterPlugin = &InterPodAffinity{}
  35. var _ framework.ScorePlugin = &InterPodAffinity{}
  36. // InterPodAffinity is a plugin that checks inter pod affinity
  37. type InterPodAffinity struct {
  38. sharedLister schedulerlisters.SharedLister
  39. hardPodAffinityWeight int32
  40. sync.Mutex
  41. }
  42. // Name returns name of the plugin. It is used in logs, etc.
  43. func (pl *InterPodAffinity) Name() string {
  44. return Name
  45. }
  46. // New initializes a new plugin and returns it.
  47. func New(plArgs *runtime.Unknown, h framework.FrameworkHandle) (framework.Plugin, error) {
  48. if h.SnapshotSharedLister() == nil {
  49. return nil, fmt.Errorf("SnapshotSharedlister is nil")
  50. }
  51. args := &Args{}
  52. if err := framework.DecodeInto(plArgs, args); err != nil {
  53. return nil, err
  54. }
  55. if err := validateArgs(args); err != nil {
  56. return nil, err
  57. }
  58. pl := &InterPodAffinity{
  59. sharedLister: h.SnapshotSharedLister(),
  60. hardPodAffinityWeight: defaultHardPodAffinityWeight,
  61. }
  62. if args.HardPodAffinityWeight != nil {
  63. pl.hardPodAffinityWeight = *args.HardPodAffinityWeight
  64. }
  65. return pl, nil
  66. }
  67. func validateArgs(args *Args) error {
  68. if args.HardPodAffinityWeight == nil {
  69. return nil
  70. }
  71. weight := *args.HardPodAffinityWeight
  72. if weight < minHardPodAffinityWeight || weight > maxHardPodAffinityWeight {
  73. return fmt.Errorf("invalid args.hardPodAffinityWeight: %d, must be in the range %d-%d", weight, minHardPodAffinityWeight, maxHardPodAffinityWeight)
  74. }
  75. return nil
  76. }