...

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

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

     1  /*
     2  Copyright 2017 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 extendedresourcetoleration
    18  
    19  import (
    20  	"context"
    21  	"fmt"
    22  	"io"
    23  
    24  	"k8s.io/apimachinery/pkg/api/errors"
    25  	"k8s.io/apimachinery/pkg/util/sets"
    26  	"k8s.io/apiserver/pkg/admission"
    27  	"k8s.io/kubernetes/pkg/apis/core"
    28  	"k8s.io/kubernetes/pkg/apis/core/helper"
    29  )
    30  
    31  // PluginName indicates name of admission plugin.
    32  const PluginName = "ExtendedResourceToleration"
    33  
    34  // Register is called by the apiserver to register the plugin factory.
    35  func Register(plugins *admission.Plugins) {
    36  	plugins.Register(PluginName, func(config io.Reader) (admission.Interface, error) {
    37  		return newExtendedResourceToleration(), nil
    38  	})
    39  }
    40  
    41  // newExtendedResourceToleration creates a new instance of the ExtendedResourceToleration admission controller.
    42  func newExtendedResourceToleration() *plugin {
    43  	return &plugin{
    44  		Handler: admission.NewHandler(admission.Create, admission.Update),
    45  	}
    46  }
    47  
    48  // Make sure we are implementing the interface.
    49  var _ admission.MutationInterface = &plugin{}
    50  
    51  type plugin struct {
    52  	*admission.Handler
    53  }
    54  
    55  // Admit updates the toleration of a pod based on the resources requested by it.
    56  // If an extended resource of name "example.com/device" is requested, it adds
    57  // a toleration with key "example.com/device", operator "Exists" and effect "NoSchedule".
    58  // The rationale for this is described in:
    59  // https://github.com/kubernetes/kubernetes/issues/55080
    60  func (p *plugin) Admit(ctx context.Context, attributes admission.Attributes, o admission.ObjectInterfaces) error {
    61  	// Ignore all calls to subresources or resources other than pods.
    62  	if len(attributes.GetSubresource()) != 0 || attributes.GetResource().GroupResource() != core.Resource("pods") {
    63  		return nil
    64  	}
    65  
    66  	pod, ok := attributes.GetObject().(*core.Pod)
    67  	if !ok {
    68  		return errors.NewBadRequest(fmt.Sprintf("expected *core.Pod but got %T", attributes.GetObject()))
    69  	}
    70  
    71  	resources := sets.String{}
    72  	for _, container := range pod.Spec.Containers {
    73  		for resourceName := range container.Resources.Requests {
    74  			if helper.IsExtendedResourceName(resourceName) {
    75  				resources.Insert(string(resourceName))
    76  			}
    77  		}
    78  	}
    79  	for _, container := range pod.Spec.InitContainers {
    80  		for resourceName := range container.Resources.Requests {
    81  			if helper.IsExtendedResourceName(resourceName) {
    82  				resources.Insert(string(resourceName))
    83  			}
    84  		}
    85  	}
    86  
    87  	// Doing .List() so that we get a stable sorted list.
    88  	// This allows us to test adding tolerations for multiple extended resources.
    89  	for _, resource := range resources.List() {
    90  		helper.AddOrUpdateTolerationInPod(pod, &core.Toleration{
    91  			Key:      resource,
    92  			Operator: core.TolerationOpExists,
    93  			Effect:   core.TaintEffectNoSchedule,
    94  		})
    95  	}
    96  
    97  	return nil
    98  }
    99  

View as plain text