node_unschedulable.go 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  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 nodeunschedulable
  14. import (
  15. "context"
  16. v1 "k8s.io/api/core/v1"
  17. "k8s.io/apimachinery/pkg/runtime"
  18. v1helper "k8s.io/kubernetes/pkg/apis/core/v1/helper"
  19. framework "k8s.io/kubernetes/pkg/scheduler/framework/v1alpha1"
  20. "k8s.io/kubernetes/pkg/scheduler/nodeinfo"
  21. )
  22. // NodeUnschedulable is a plugin that priorities nodes according to the node annotation
  23. // "scheduler.alpha.kubernetes.io/preferAvoidPods".
  24. type NodeUnschedulable struct {
  25. }
  26. var _ framework.FilterPlugin = &NodeUnschedulable{}
  27. // Name is the name of the plugin used in the plugin registry and configurations.
  28. const Name = "NodeUnschedulable"
  29. const (
  30. // ErrReasonUnknownCondition is used for NodeUnknownCondition predicate error.
  31. ErrReasonUnknownCondition = "node(s) had unknown conditions"
  32. // ErrReasonUnschedulable is used for NodeUnschedulable predicate error.
  33. ErrReasonUnschedulable = "node(s) were unschedulable"
  34. )
  35. // Name returns name of the plugin. It is used in logs, etc.
  36. func (pl *NodeUnschedulable) Name() string {
  37. return Name
  38. }
  39. // Filter invoked at the filter extension point.
  40. func (pl *NodeUnschedulable) Filter(ctx context.Context, _ *framework.CycleState, pod *v1.Pod, nodeInfo *nodeinfo.NodeInfo) *framework.Status {
  41. if nodeInfo == nil || nodeInfo.Node() == nil {
  42. return framework.NewStatus(framework.UnschedulableAndUnresolvable, ErrReasonUnknownCondition)
  43. }
  44. // If pod tolerate unschedulable taint, it's also tolerate `node.Spec.Unschedulable`.
  45. podToleratesUnschedulable := v1helper.TolerationsTolerateTaint(pod.Spec.Tolerations, &v1.Taint{
  46. Key: v1.TaintNodeUnschedulable,
  47. Effect: v1.TaintEffectNoSchedule,
  48. })
  49. // TODO (k82cn): deprecates `node.Spec.Unschedulable` in 1.13.
  50. if nodeInfo.Node().Spec.Unschedulable && !podToleratesUnschedulable {
  51. return framework.NewStatus(framework.UnschedulableAndUnresolvable, ErrReasonUnschedulable)
  52. }
  53. return nil
  54. }
  55. // New initializes a new plugin and returns it.
  56. func New(_ *runtime.Unknown, _ framework.FrameworkHandle) (framework.Plugin, error) {
  57. return &NodeUnschedulable{}, nil
  58. }