...

Source file src/k8s.io/kubernetes/pkg/scheduler/framework/plugins/nodename/node_name.go

Documentation: k8s.io/kubernetes/pkg/scheduler/framework/plugins/nodename

     1  /*
     2  Copyright 2019 The Kubernetes Authors.
     3  
     4  Licensed under the Apache License, Version 2.0 (the "License");
     5  you may not use this file except in compliance with the License.
     6  You may obtain a copy of the License at
     7  
     8      http://www.apache.org/licenses/LICENSE-2.0
     9  
    10  Unless required by applicable law or agreed to in writing, software
    11  distributed under the License is distributed on an "AS IS" BASIS,
    12  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    13  See the License for the specific language governing permissions and
    14  limitations under the License.
    15  */
    16  
    17  package nodename
    18  
    19  import (
    20  	"context"
    21  
    22  	v1 "k8s.io/api/core/v1"
    23  	"k8s.io/apimachinery/pkg/runtime"
    24  	"k8s.io/kubernetes/pkg/scheduler/framework"
    25  	"k8s.io/kubernetes/pkg/scheduler/framework/plugins/names"
    26  )
    27  
    28  // NodeName is a plugin that checks if a pod spec node name matches the current node.
    29  type NodeName struct{}
    30  
    31  var _ framework.FilterPlugin = &NodeName{}
    32  var _ framework.EnqueueExtensions = &NodeName{}
    33  
    34  const (
    35  	// Name is the name of the plugin used in the plugin registry and configurations.
    36  	Name = names.NodeName
    37  
    38  	// ErrReason returned when node name doesn't match.
    39  	ErrReason = "node(s) didn't match the requested node name"
    40  )
    41  
    42  // EventsToRegister returns the possible events that may make a Pod
    43  // failed by this plugin schedulable.
    44  func (pl *NodeName) EventsToRegister() []framework.ClusterEventWithHint {
    45  	return []framework.ClusterEventWithHint{
    46  		{Event: framework.ClusterEvent{Resource: framework.Node, ActionType: framework.Add | framework.Update}},
    47  	}
    48  }
    49  
    50  // Name returns name of the plugin. It is used in logs, etc.
    51  func (pl *NodeName) Name() string {
    52  	return Name
    53  }
    54  
    55  // Filter invoked at the filter extension point.
    56  func (pl *NodeName) Filter(ctx context.Context, _ *framework.CycleState, pod *v1.Pod, nodeInfo *framework.NodeInfo) *framework.Status {
    57  
    58  	if !Fits(pod, nodeInfo) {
    59  		return framework.NewStatus(framework.UnschedulableAndUnresolvable, ErrReason)
    60  	}
    61  	return nil
    62  }
    63  
    64  // Fits actually checks if the pod fits the node.
    65  func Fits(pod *v1.Pod, nodeInfo *framework.NodeInfo) bool {
    66  	return len(pod.Spec.NodeName) == 0 || pod.Spec.NodeName == nodeInfo.Node().Name
    67  }
    68  
    69  // New initializes a new plugin and returns it.
    70  func New(_ context.Context, _ runtime.Object, _ framework.Handle) (framework.Plugin, error) {
    71  	return &NodeName{}, nil
    72  }
    73  

View as plain text