2017-07-20 05:49:39 +00:00
/ *
Copyright 2017 The Kubernetes Authors .
Licensed under the Apache License , Version 2.0 ( the "License" ) ;
you may not use this file except in compliance with the License .
You may obtain a copy of the License at
http : //www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing , software
distributed under the License is distributed on an "AS IS" BASIS ,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND , either express or implied .
See the License for the specific language governing permissions and
limitations under the License .
* /
2018-01-31 02:10:29 +00:00
package priority
2017-07-20 05:49:39 +00:00
import (
"fmt"
"io"
"k8s.io/apimachinery/pkg/api/errors"
2018-06-28 18:11:22 +00:00
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
2017-07-20 05:49:39 +00:00
"k8s.io/apimachinery/pkg/labels"
"k8s.io/apiserver/pkg/admission"
2017-07-25 01:14:56 +00:00
utilfeature "k8s.io/apiserver/pkg/util/feature"
2017-11-08 22:34:54 +00:00
api "k8s.io/kubernetes/pkg/apis/core"
2017-07-20 05:49:39 +00:00
"k8s.io/kubernetes/pkg/apis/scheduling"
"k8s.io/kubernetes/pkg/client/clientset_generated/internalclientset"
informers "k8s.io/kubernetes/pkg/client/informers/informers_generated/internalversion"
schedulinglisters "k8s.io/kubernetes/pkg/client/listers/scheduling/internalversion"
2017-07-25 01:14:56 +00:00
"k8s.io/kubernetes/pkg/features"
2017-07-20 05:49:39 +00:00
kubeapiserveradmission "k8s.io/kubernetes/pkg/kubeapiserver/admission"
2018-02-12 19:04:19 +00:00
kubelettypes "k8s.io/kubernetes/pkg/kubelet/types"
2017-07-20 05:49:39 +00:00
)
const (
2018-01-15 06:58:57 +00:00
// PluginName indicates name of admission plugin.
PluginName = "Priority"
2017-07-20 05:49:39 +00:00
)
// Register registers a plugin
func Register ( plugins * admission . Plugins ) {
2018-01-15 06:58:57 +00:00
plugins . Register ( PluginName , func ( config io . Reader ) ( admission . Interface , error ) {
2018-01-31 02:10:29 +00:00
return newPlugin ( ) , nil
2017-07-20 05:49:39 +00:00
} )
}
2018-01-31 02:10:29 +00:00
// priorityPlugin is an implementation of admission.Interface.
type priorityPlugin struct {
2017-07-20 05:49:39 +00:00
* admission . Handler
client internalclientset . Interface
lister schedulinglisters . PriorityClassLister
}
2018-01-31 02:10:29 +00:00
var _ admission . MutationInterface = & priorityPlugin { }
var _ admission . ValidationInterface = & priorityPlugin { }
var _ = kubeapiserveradmission . WantsInternalKubeInformerFactory ( & priorityPlugin { } )
var _ = kubeapiserveradmission . WantsInternalKubeClientSet ( & priorityPlugin { } )
2017-07-20 05:49:39 +00:00
// NewPlugin creates a new priority admission plugin.
2018-01-31 02:10:29 +00:00
func newPlugin ( ) * priorityPlugin {
return & priorityPlugin {
2017-07-25 01:14:56 +00:00
Handler : admission . NewHandler ( admission . Create , admission . Update , admission . Delete ) ,
2017-07-20 05:49:39 +00:00
}
}
2017-10-27 15:09:39 +00:00
// ValidateInitialization implements the InitializationValidator interface.
2018-01-31 02:10:29 +00:00
func ( p * priorityPlugin ) ValidateInitialization ( ) error {
2017-07-20 05:49:39 +00:00
if p . client == nil {
2018-01-15 06:58:57 +00:00
return fmt . Errorf ( "%s requires a client" , PluginName )
2017-07-20 05:49:39 +00:00
}
if p . lister == nil {
2018-01-15 06:58:57 +00:00
return fmt . Errorf ( "%s requires a lister" , PluginName )
2017-07-20 05:49:39 +00:00
}
return nil
}
2017-10-30 13:20:40 +00:00
// SetInternalKubeClientSet implements the WantsInternalKubeClientSet interface.
2018-01-31 02:10:29 +00:00
func ( p * priorityPlugin ) SetInternalKubeClientSet ( client internalclientset . Interface ) {
2017-07-20 05:49:39 +00:00
p . client = client
}
2017-10-30 13:20:40 +00:00
// SetInternalKubeInformerFactory implements the WantsInternalKubeInformerFactory interface.
2018-01-31 02:10:29 +00:00
func ( p * priorityPlugin ) SetInternalKubeInformerFactory ( f informers . SharedInformerFactory ) {
2017-07-20 05:49:39 +00:00
priorityInformer := f . Scheduling ( ) . InternalVersion ( ) . PriorityClasses ( )
p . lister = priorityInformer . Lister ( )
p . SetReadyFunc ( priorityInformer . Informer ( ) . HasSynced )
}
var (
podResource = api . Resource ( "pods" )
2017-10-27 10:02:48 +00:00
priorityClassResource = scheduling . Resource ( "priorityclasses" )
2017-07-20 05:49:39 +00:00
)
2017-11-02 17:11:11 +00:00
// Admit checks Pods and admits or rejects them. It also resolves the priority of pods based on their PriorityClass.
2017-11-10 07:45:38 +00:00
// Note that pod validation mechanism prevents update of a pod priority.
2018-01-31 02:10:29 +00:00
func ( p * priorityPlugin ) Admit ( a admission . Attributes ) error {
2017-07-20 05:49:39 +00:00
operation := a . GetOperation ( )
2017-11-02 17:11:11 +00:00
// Ignore all calls to subresources
2017-07-25 01:14:56 +00:00
if len ( a . GetSubresource ( ) ) != 0 {
2017-07-20 05:49:39 +00:00
return nil
}
switch a . GetResource ( ) . GroupResource ( ) {
case podResource :
2017-11-10 07:45:38 +00:00
if operation == admission . Create {
2017-07-25 01:14:56 +00:00
return p . admitPod ( a )
}
return nil
2017-07-20 05:49:39 +00:00
2017-11-02 17:11:11 +00:00
default :
return nil
}
}
// Validate checks PriorityClasses and admits or rejects them.
2018-01-31 02:10:29 +00:00
func ( p * priorityPlugin ) Validate ( a admission . Attributes ) error {
2017-11-02 17:11:11 +00:00
operation := a . GetOperation ( )
// Ignore all calls to subresources
if len ( a . GetSubresource ( ) ) != 0 {
return nil
}
switch a . GetResource ( ) . GroupResource ( ) {
2017-07-20 05:49:39 +00:00
case priorityClassResource :
2017-07-25 01:14:56 +00:00
if operation == admission . Create || operation == admission . Update {
2017-11-02 17:11:11 +00:00
return p . validatePriorityClass ( a )
2017-07-25 01:14:56 +00:00
}
return nil
2017-07-20 05:49:39 +00:00
default :
return nil
}
}
2017-07-25 01:14:56 +00:00
// admitPod makes sure a new pod does not set spec.Priority field. It also makes sure that the PriorityClassName exists if it is provided and resolves the pod priority from the PriorityClassName.
2018-01-31 02:10:29 +00:00
func ( p * priorityPlugin ) admitPod ( a admission . Attributes ) error {
2017-07-20 05:49:39 +00:00
operation := a . GetOperation ( )
pod , ok := a . GetObject ( ) . ( * api . Pod )
if ! ok {
2017-07-25 01:14:56 +00:00
return errors . NewBadRequest ( "resource was marked with kind Pod but was unable to be converted" )
2017-07-20 05:49:39 +00:00
}
2018-01-18 23:55:36 +00:00
2017-07-25 01:14:56 +00:00
// Make sure that the client has not set `priority` at the time of pod creation.
2017-07-20 05:49:39 +00:00
if operation == admission . Create && pod . Spec . Priority != nil {
2017-07-25 01:14:56 +00:00
return admission . NewForbidden ( a , fmt . Errorf ( "the integer value of priority must not be provided in pod spec. Priority admission controller populates the value from the given PriorityClass name" ) )
2017-07-20 05:49:39 +00:00
}
2017-07-25 01:14:56 +00:00
if utilfeature . DefaultFeatureGate . Enabled ( features . PodPriority ) {
var priority int32
2018-02-12 19:04:19 +00:00
// TODO: @ravig - This is for backwards compatibility to ensure that critical pods with annotations just work fine.
// Remove when no longer needed.
if len ( pod . Spec . PriorityClassName ) == 0 &&
utilfeature . DefaultFeatureGate . Enabled ( features . ExperimentalCriticalPodAnnotation ) &&
kubelettypes . IsCritical ( a . GetNamespace ( ) , pod . Annotations ) {
2018-02-23 01:38:17 +00:00
pod . Spec . PriorityClassName = scheduling . SystemClusterCritical
2018-02-12 19:04:19 +00:00
}
2017-07-25 01:14:56 +00:00
if len ( pod . Spec . PriorityClassName ) == 0 {
var err error
priority , err = p . getDefaultPriority ( )
2017-07-20 05:49:39 +00:00
if err != nil {
2017-07-25 01:14:56 +00:00
return fmt . Errorf ( "failed to get default priority class: %v" , err )
2017-07-20 05:49:39 +00:00
}
2017-07-25 01:14:56 +00:00
} else {
2018-06-28 18:11:22 +00:00
pcName := pod . Spec . PriorityClassName
// Only allow system priorities in the system namespace. This is to prevent abuse or incorrect
// usage of these priorities. Pods created at these priorities could preempt system critical
// components.
for _ , spc := range scheduling . SystemPriorityClasses ( ) {
if spc . Name == pcName && a . GetNamespace ( ) != metav1 . NamespaceSystem {
return admission . NewForbidden ( a , fmt . Errorf ( "pods with %v priorityClass can only be created in %v namespace" , spc . Name , metav1 . NamespaceSystem ) )
}
}
2018-02-23 01:38:17 +00:00
// Try resolving the priority class name.
pc , err := p . lister . Get ( pod . Spec . PriorityClassName )
if err != nil {
if errors . IsNotFound ( err ) {
return admission . NewForbidden ( a , fmt . Errorf ( "no PriorityClass with name %v was found" , pod . Spec . PriorityClassName ) )
2017-07-25 01:14:56 +00:00
}
2017-11-02 06:56:55 +00:00
2018-02-23 01:38:17 +00:00
return fmt . Errorf ( "failed to get PriorityClass with name %s: %v" , pod . Spec . PriorityClassName , err )
2017-07-20 05:49:39 +00:00
}
2018-02-23 01:38:17 +00:00
priority = pc . Value
2017-07-20 05:49:39 +00:00
}
2017-07-25 01:14:56 +00:00
pod . Spec . Priority = & priority
2017-07-20 05:49:39 +00:00
}
return nil
}
2017-11-02 17:11:11 +00:00
// validatePriorityClass ensures that the value field is not larger than the highest user definable priority. If the GlobalDefault is set, it ensures that there is no other PriorityClass whose GlobalDefault is set.
2018-01-31 02:10:29 +00:00
func ( p * priorityPlugin ) validatePriorityClass ( a admission . Attributes ) error {
2017-07-20 05:49:39 +00:00
operation := a . GetOperation ( )
pc , ok := a . GetObject ( ) . ( * scheduling . PriorityClass )
if ! ok {
2017-07-25 01:14:56 +00:00
return errors . NewBadRequest ( "resource was marked with kind PriorityClass but was unable to be converted" )
2017-07-20 05:49:39 +00:00
}
// If the new PriorityClass tries to be the default priority, make sure that no other priority class is marked as default.
if pc . GlobalDefault {
2017-07-25 01:14:56 +00:00
dpc , err := p . getDefaultPriorityClass ( )
2017-07-20 05:49:39 +00:00
if err != nil {
2017-07-25 01:14:56 +00:00
return fmt . Errorf ( "failed to get default priority class: %v" , err )
2017-07-20 05:49:39 +00:00
}
if dpc != nil {
// Throw an error if a second default priority class is being created, or an existing priority class is being marked as default, while another default already exists.
if operation == admission . Create || ( operation == admission . Update && dpc . GetName ( ) != pc . GetName ( ) ) {
return admission . NewForbidden ( a , fmt . Errorf ( "PriorityClass %v is already marked as default. Only one default can exist" , dpc . GetName ( ) ) )
}
}
}
return nil
}
2018-01-31 02:10:29 +00:00
func ( p * priorityPlugin ) getDefaultPriorityClass ( ) ( * scheduling . PriorityClass , error ) {
2017-07-20 05:49:39 +00:00
list , err := p . lister . List ( labels . Everything ( ) )
if err != nil {
return nil , err
}
2018-02-16 22:35:50 +00:00
// In case more than one global default priority class is added as a result of a race condition,
// we pick the one with the lowest priority value.
var defaultPC * scheduling . PriorityClass
2017-07-20 05:49:39 +00:00
for _ , pci := range list {
if pci . GlobalDefault {
2018-02-16 22:35:50 +00:00
if defaultPC == nil || defaultPC . Value > pci . Value {
defaultPC = pci
}
2017-07-20 05:49:39 +00:00
}
}
2018-02-16 22:35:50 +00:00
return defaultPC , nil
2017-07-20 05:49:39 +00:00
}
2017-07-25 01:14:56 +00:00
2018-01-31 02:10:29 +00:00
func ( p * priorityPlugin ) getDefaultPriority ( ) ( int32 , error ) {
2017-07-25 01:14:56 +00:00
dpc , err := p . getDefaultPriorityClass ( )
if err != nil {
return 0 , err
}
if dpc != nil {
2018-02-11 03:15:31 +00:00
return dpc . Value , nil
2017-07-25 01:14:56 +00:00
}
2018-02-11 03:15:31 +00:00
return int32 ( scheduling . DefaultPriorityWhenNoDefaultClassExists ) , nil
2017-07-25 01:14:56 +00:00
}