...

Source file src/k8s.io/kubernetes/pkg/scheduler/framework/plugins/nodeunschedulable/node_unschedulable.go

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

     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 nodeunschedulable
    18  
    19  import (
    20  	"context"
    21  
    22  	v1 "k8s.io/api/core/v1"
    23  	"k8s.io/apimachinery/pkg/runtime"
    24  	v1helper "k8s.io/component-helpers/scheduling/corev1"
    25  	"k8s.io/klog/v2"
    26  	"k8s.io/kubernetes/pkg/scheduler/framework"
    27  	"k8s.io/kubernetes/pkg/scheduler/framework/plugins/names"
    28  	"k8s.io/kubernetes/pkg/scheduler/util"
    29  )
    30  
    31  // NodeUnschedulable plugin filters nodes that set node.Spec.Unschedulable=true unless
    32  // the pod tolerates {key=node.kubernetes.io/unschedulable, effect:NoSchedule} taint.
    33  type NodeUnschedulable struct {
    34  }
    35  
    36  var _ framework.FilterPlugin = &NodeUnschedulable{}
    37  var _ framework.EnqueueExtensions = &NodeUnschedulable{}
    38  
    39  // Name is the name of the plugin used in the plugin registry and configurations.
    40  const Name = names.NodeUnschedulable
    41  
    42  const (
    43  	// ErrReasonUnknownCondition is used for NodeUnknownCondition predicate error.
    44  	ErrReasonUnknownCondition = "node(s) had unknown conditions"
    45  	// ErrReasonUnschedulable is used for NodeUnschedulable predicate error.
    46  	ErrReasonUnschedulable = "node(s) were unschedulable"
    47  )
    48  
    49  // EventsToRegister returns the possible events that may make a Pod
    50  // failed by this plugin schedulable.
    51  func (pl *NodeUnschedulable) EventsToRegister() []framework.ClusterEventWithHint {
    52  	return []framework.ClusterEventWithHint{
    53  		{Event: framework.ClusterEvent{Resource: framework.Node, ActionType: framework.Add | framework.Update}, QueueingHintFn: pl.isSchedulableAfterNodeChange},
    54  	}
    55  }
    56  
    57  // isSchedulableAfterNodeChange is invoked for all node events reported by
    58  // an informer. It checks whether that change made a previously unschedulable
    59  // pod schedulable.
    60  func (pl *NodeUnschedulable) isSchedulableAfterNodeChange(logger klog.Logger, pod *v1.Pod, oldObj, newObj interface{}) (framework.QueueingHint, error) {
    61  	_, modifiedNode, err := util.As[*v1.Node](oldObj, newObj)
    62  	if err != nil {
    63  		return framework.Queue, err
    64  	}
    65  
    66  	if !modifiedNode.Spec.Unschedulable {
    67  		logger.V(5).Info("node was created or updated, pod may be schedulable now", "pod", klog.KObj(pod), "node", klog.KObj(modifiedNode))
    68  		return framework.Queue, nil
    69  	}
    70  
    71  	// TODO: also check if the original node meets the pod's requestments once preCheck is completely removed.
    72  	// See: https://github.com/kubernetes/kubernetes/issues/110175
    73  
    74  	logger.V(5).Info("node was created or updated, but it doesn't make this pod schedulable", "pod", klog.KObj(pod), "node", klog.KObj(modifiedNode))
    75  	return framework.QueueSkip, nil
    76  }
    77  
    78  // Name returns name of the plugin. It is used in logs, etc.
    79  func (pl *NodeUnschedulable) Name() string {
    80  	return Name
    81  }
    82  
    83  // Filter invoked at the filter extension point.
    84  func (pl *NodeUnschedulable) Filter(ctx context.Context, _ *framework.CycleState, pod *v1.Pod, nodeInfo *framework.NodeInfo) *framework.Status {
    85  	node := nodeInfo.Node()
    86  
    87  	if !node.Spec.Unschedulable {
    88  		return nil
    89  	}
    90  
    91  	// If pod tolerate unschedulable taint, it's also tolerate `node.Spec.Unschedulable`.
    92  	podToleratesUnschedulable := v1helper.TolerationsTolerateTaint(pod.Spec.Tolerations, &v1.Taint{
    93  		Key:    v1.TaintNodeUnschedulable,
    94  		Effect: v1.TaintEffectNoSchedule,
    95  	})
    96  	if !podToleratesUnschedulable {
    97  		return framework.NewStatus(framework.UnschedulableAndUnresolvable, ErrReasonUnschedulable)
    98  	}
    99  
   100  	return nil
   101  }
   102  
   103  // New initializes a new plugin and returns it.
   104  func New(_ context.Context, _ runtime.Object, _ framework.Handle) (framework.Plugin, error) {
   105  	return &NodeUnschedulable{}, nil
   106  }
   107  

View as plain text