plugin.go 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  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 podtopologyspread
  14. import (
  15. "fmt"
  16. "k8s.io/apimachinery/pkg/runtime"
  17. framework "k8s.io/kubernetes/pkg/scheduler/framework/v1alpha1"
  18. schedulerlisters "k8s.io/kubernetes/pkg/scheduler/listers"
  19. )
  20. const (
  21. // ErrReasonConstraintsNotMatch is used for PodTopologySpread filter error.
  22. ErrReasonConstraintsNotMatch = "node(s) didn't match pod topology spread constraints"
  23. )
  24. // PodTopologySpread is a plugin that ensures pod's topologySpreadConstraints is satisfied.
  25. type PodTopologySpread struct {
  26. sharedLister schedulerlisters.SharedLister
  27. }
  28. var _ framework.PreFilterPlugin = &PodTopologySpread{}
  29. var _ framework.FilterPlugin = &PodTopologySpread{}
  30. var _ framework.PreScorePlugin = &PodTopologySpread{}
  31. var _ framework.ScorePlugin = &PodTopologySpread{}
  32. const (
  33. // Name is the name of the plugin used in the plugin registry and configurations.
  34. Name = "PodTopologySpread"
  35. )
  36. // Name returns name of the plugin. It is used in logs, etc.
  37. func (pl *PodTopologySpread) Name() string {
  38. return Name
  39. }
  40. // New initializes a new plugin and returns it.
  41. func New(_ *runtime.Unknown, h framework.FrameworkHandle) (framework.Plugin, error) {
  42. if h.SnapshotSharedLister() == nil {
  43. return nil, fmt.Errorf("SnapshotSharedlister is nil")
  44. }
  45. return &PodTopologySpread{sharedLister: h.SnapshotSharedLister()}, nil
  46. }