...

Source file src/k8s.io/kubernetes/plugin/pkg/admission/nodetaint/admission.go

Documentation: k8s.io/kubernetes/plugin/pkg/admission/nodetaint

     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 nodetaint
    18  
    19  import (
    20  	"context"
    21  	"fmt"
    22  	"io"
    23  
    24  	v1 "k8s.io/api/core/v1"
    25  	"k8s.io/apiserver/pkg/admission"
    26  	api "k8s.io/kubernetes/pkg/apis/core"
    27  )
    28  
    29  const (
    30  	// PluginName is the name of the plugin.
    31  	PluginName = "TaintNodesByCondition"
    32  )
    33  
    34  // Register registers a plugin
    35  func Register(plugins *admission.Plugins) {
    36  	plugins.Register(PluginName, func(config io.Reader) (admission.Interface, error) {
    37  		return NewPlugin(), nil
    38  	})
    39  }
    40  
    41  // NewPlugin creates a new NodeTaint admission plugin.
    42  // This plugin identifies requests from nodes
    43  func NewPlugin() *Plugin {
    44  	return &Plugin{
    45  		Handler: admission.NewHandler(admission.Create),
    46  	}
    47  }
    48  
    49  // Plugin holds state for and implements the admission plugin.
    50  type Plugin struct {
    51  	*admission.Handler
    52  }
    53  
    54  var (
    55  	_ = admission.Interface(&Plugin{})
    56  )
    57  
    58  var (
    59  	nodeResource = api.Resource("nodes")
    60  )
    61  
    62  // Admit is the main function that checks node identity and adds taints as needed.
    63  func (p *Plugin) Admit(ctx context.Context, a admission.Attributes, o admission.ObjectInterfaces) error {
    64  	// Our job is just to taint nodes.
    65  	if a.GetResource().GroupResource() != nodeResource || a.GetSubresource() != "" {
    66  		return nil
    67  	}
    68  
    69  	node, ok := a.GetObject().(*api.Node)
    70  	if !ok {
    71  		return admission.NewForbidden(a, fmt.Errorf("unexpected type %T", a.GetObject()))
    72  	}
    73  
    74  	// Taint node with NotReady taint at creation. This is needed to make sure
    75  	// that nodes are added to the cluster with the NotReady taint. Otherwise,
    76  	// a new node may receive the taint with some delay causing pods to be
    77  	// scheduled on a not-ready node. Node controller will remove the taint
    78  	// when the node becomes ready.
    79  	addNotReadyTaint(node)
    80  	return nil
    81  }
    82  
    83  func addNotReadyTaint(node *api.Node) {
    84  	notReadyTaint := api.Taint{
    85  		Key:    v1.TaintNodeNotReady,
    86  		Effect: api.TaintEffectNoSchedule,
    87  	}
    88  	for _, taint := range node.Spec.Taints {
    89  		if taint.MatchTaint(notReadyTaint) {
    90  			// the taint already exists.
    91  			return
    92  		}
    93  	}
    94  	node.Spec.Taints = append(node.Spec.Taints, notReadyTaint)
    95  }
    96  

View as plain text