volume_binding.go 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  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 volumebinding
  14. import (
  15. "context"
  16. v1 "k8s.io/api/core/v1"
  17. "k8s.io/apimachinery/pkg/runtime"
  18. framework "k8s.io/kubernetes/pkg/scheduler/framework/v1alpha1"
  19. schedulernodeinfo "k8s.io/kubernetes/pkg/scheduler/nodeinfo"
  20. "k8s.io/kubernetes/pkg/scheduler/volumebinder"
  21. )
  22. // VolumeBinding is a plugin that binds pod volumes in scheduling.
  23. type VolumeBinding struct {
  24. binder *volumebinder.VolumeBinder
  25. }
  26. var _ framework.FilterPlugin = &VolumeBinding{}
  27. // Name is the name of the plugin used in Registry and configurations.
  28. const Name = "VolumeBinding"
  29. const (
  30. // ErrReasonBindConflict is used for VolumeBindingNoMatch predicate error.
  31. ErrReasonBindConflict = "node(s) didn't find available persistent volumes to bind"
  32. // ErrReasonNodeConflict is used for VolumeNodeAffinityConflict predicate error.
  33. ErrReasonNodeConflict = "node(s) had volume node affinity conflict"
  34. )
  35. // Name returns name of the plugin. It is used in logs, etc.
  36. func (pl *VolumeBinding) Name() string {
  37. return Name
  38. }
  39. func podHasPVCs(pod *v1.Pod) bool {
  40. for _, vol := range pod.Spec.Volumes {
  41. if vol.PersistentVolumeClaim != nil {
  42. return true
  43. }
  44. }
  45. return false
  46. }
  47. // Filter invoked at the filter extension point.
  48. // It evaluates if a pod can fit due to the volumes it requests,
  49. // for both bound and unbound PVCs.
  50. //
  51. // For PVCs that are bound, then it checks that the corresponding PV's node affinity is
  52. // satisfied by the given node.
  53. //
  54. // For PVCs that are unbound, it tries to find available PVs that can satisfy the PVC requirements
  55. // and that the PV node affinity is satisfied by the given node.
  56. //
  57. // The predicate returns true if all bound PVCs have compatible PVs with the node, and if all unbound
  58. // PVCs can be matched with an available and node-compatible PV.
  59. func (pl *VolumeBinding) Filter(ctx context.Context, cs *framework.CycleState, pod *v1.Pod, nodeInfo *schedulernodeinfo.NodeInfo) *framework.Status {
  60. node := nodeInfo.Node()
  61. if node == nil {
  62. return framework.NewStatus(framework.Error, "node not found")
  63. }
  64. // If pod does not request any PVC, we don't need to do anything.
  65. if !podHasPVCs(pod) {
  66. return nil
  67. }
  68. unboundSatisfied, boundSatisfied, err := pl.binder.Binder.FindPodVolumes(pod, node)
  69. if err != nil {
  70. return framework.NewStatus(framework.Error, err.Error())
  71. }
  72. if !boundSatisfied || !unboundSatisfied {
  73. status := framework.NewStatus(framework.UnschedulableAndUnresolvable)
  74. if !boundSatisfied {
  75. status.AppendReason(ErrReasonNodeConflict)
  76. }
  77. if !unboundSatisfied {
  78. status.AppendReason(ErrReasonBindConflict)
  79. }
  80. return status
  81. }
  82. return nil
  83. }
  84. // New initializes a new plugin with volume binder and returns it.
  85. func New(_ *runtime.Unknown, fh framework.FrameworkHandle) (framework.Plugin, error) {
  86. return &VolumeBinding{
  87. binder: fh.VolumeBinder(),
  88. }, nil
  89. }