node_name.go 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  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 nodename
  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. "k8s.io/kubernetes/pkg/scheduler/nodeinfo"
  20. )
  21. // NodeName is a plugin that checks if a pod spec node name matches the current node.
  22. type NodeName struct{}
  23. var _ framework.FilterPlugin = &NodeName{}
  24. const (
  25. // Name is the name of the plugin used in the plugin registry and configurations.
  26. Name = "NodeName"
  27. // ErrReason returned when node name doesn't match.
  28. ErrReason = "node(s) didn't match the requested hostname"
  29. )
  30. // Name returns name of the plugin. It is used in logs, etc.
  31. func (pl *NodeName) Name() string {
  32. return Name
  33. }
  34. // Filter invoked at the filter extension point.
  35. func (pl *NodeName) Filter(ctx context.Context, _ *framework.CycleState, pod *v1.Pod, nodeInfo *nodeinfo.NodeInfo) *framework.Status {
  36. if nodeInfo.Node() == nil {
  37. return framework.NewStatus(framework.Error, "node not found")
  38. }
  39. if !Fits(pod, nodeInfo) {
  40. return framework.NewStatus(framework.UnschedulableAndUnresolvable, ErrReason)
  41. }
  42. return nil
  43. }
  44. // Fits actually checks if the pod fits the node.
  45. func Fits(pod *v1.Pod, nodeInfo *nodeinfo.NodeInfo) bool {
  46. return len(pod.Spec.NodeName) == 0 || pod.Spec.NodeName == nodeInfo.Node().Name
  47. }
  48. // New initializes a new plugin and returns it.
  49. func New(_ *runtime.Unknown, _ framework.FrameworkHandle) (framework.Plugin, error) {
  50. return &NodeName{}, nil
  51. }