plugin.go 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  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. "k8s.io/apimachinery/pkg/util/validation/field"
  19. framework "k8s.io/kubernetes/pkg/scheduler/framework/v1alpha1"
  20. schedulerlisters "k8s.io/kubernetes/pkg/scheduler/listers"
  21. "k8s.io/utils/pointer"
  22. )
  23. const (
  24. // Name is the name of the plugin used in the plugin registry and configurations.
  25. Name = "InterPodAffinity"
  26. // DefaultHardPodAffinityWeight is the default HardPodAffinityWeight.
  27. DefaultHardPodAffinityWeight int32 = 1
  28. // MinHardPodAffinityWeight is the minimum HardPodAffinityWeight.
  29. MinHardPodAffinityWeight int32 = 0
  30. // MaxHardPodAffinityWeight is the maximum HardPodAffinityWeight.
  31. MaxHardPodAffinityWeight int32 = 100
  32. )
  33. // Args holds the args that are used to configure the plugin.
  34. type Args struct {
  35. // HardPodAffinityWeight is the scoring weight for existing pods with a
  36. // matching hard affinity to the incoming pod.
  37. HardPodAffinityWeight *int32 `json:"hardPodAffinityWeight,omitempty"`
  38. }
  39. var _ framework.PreFilterPlugin = &InterPodAffinity{}
  40. var _ framework.FilterPlugin = &InterPodAffinity{}
  41. var _ framework.PreScorePlugin = &InterPodAffinity{}
  42. var _ framework.ScorePlugin = &InterPodAffinity{}
  43. // InterPodAffinity is a plugin that checks inter pod affinity
  44. type InterPodAffinity struct {
  45. Args
  46. sharedLister schedulerlisters.SharedLister
  47. sync.Mutex
  48. }
  49. // Name returns name of the plugin. It is used in logs, etc.
  50. func (pl *InterPodAffinity) Name() string {
  51. return Name
  52. }
  53. // BuildArgs returns the args that were used to build the plugin.
  54. func (pl *InterPodAffinity) BuildArgs() interface{} {
  55. return pl.Args
  56. }
  57. // New initializes a new plugin and returns it.
  58. func New(plArgs *runtime.Unknown, h framework.FrameworkHandle) (framework.Plugin, error) {
  59. if h.SnapshotSharedLister() == nil {
  60. return nil, fmt.Errorf("SnapshotSharedlister is nil")
  61. }
  62. pl := &InterPodAffinity{
  63. sharedLister: h.SnapshotSharedLister(),
  64. }
  65. if err := framework.DecodeInto(plArgs, &pl.Args); err != nil {
  66. return nil, err
  67. }
  68. if err := validateArgs(&pl.Args); err != nil {
  69. return nil, err
  70. }
  71. if pl.HardPodAffinityWeight == nil {
  72. pl.HardPodAffinityWeight = pointer.Int32Ptr(DefaultHardPodAffinityWeight)
  73. }
  74. return pl, nil
  75. }
  76. func validateArgs(args *Args) error {
  77. if args.HardPodAffinityWeight == nil {
  78. return nil
  79. }
  80. return ValidateHardPodAffinityWeight(field.NewPath("hardPodAffinityWeight"), *args.HardPodAffinityWeight)
  81. }
  82. // ValidateHardPodAffinityWeight validates that weight is within allowed range.
  83. func ValidateHardPodAffinityWeight(path *field.Path, w int32) error {
  84. if w < MinHardPodAffinityWeight || w > MaxHardPodAffinityWeight {
  85. msg := fmt.Sprintf("not in valid range [%d-%d]", MinHardPodAffinityWeight, MaxHardPodAffinityWeight)
  86. return field.Invalid(path, w, msg)
  87. }
  88. return nil
  89. }