...

Source file src/k8s.io/kubectl/pkg/util/qos/qos.go

Documentation: k8s.io/kubectl/pkg/util/qos

     1  /*
     2  Copyright 2015 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 qos
    18  
    19  import (
    20  	core "k8s.io/api/core/v1"
    21  	"k8s.io/apimachinery/pkg/api/resource"
    22  	"k8s.io/apimachinery/pkg/util/sets"
    23  )
    24  
    25  var supportedQoSComputeResources = sets.NewString(string(core.ResourceCPU), string(core.ResourceMemory))
    26  
    27  func isSupportedQoSComputeResource(name core.ResourceName) bool {
    28  	return supportedQoSComputeResources.Has(string(name))
    29  }
    30  
    31  // GetPodQOS returns the QoS class of a pod persisted in the PodStatus.QOSClass field.
    32  // If PodStatus.QOSClass is empty, it returns value of ComputePodQOS() which evaluates pod's QoS class.
    33  func GetPodQOS(pod *core.Pod) core.PodQOSClass {
    34  	if pod.Status.QOSClass != "" {
    35  		return pod.Status.QOSClass
    36  	}
    37  	return ComputePodQOS(pod)
    38  }
    39  
    40  // ComputePodQOS evaluates the list of containers to determine a pod's QoS class. This function is more
    41  // expensive than GetPodQOS which should be used for pods having a non-empty .Status.QOSClass.
    42  // A pod is besteffort if none of its containers have specified any requests or limits.
    43  // A pod is guaranteed only when requests and limits are specified for all the containers and they are equal.
    44  // A pod is burstable if limits and requests do not match across all containers.
    45  func ComputePodQOS(pod *core.Pod) core.PodQOSClass {
    46  	requests := core.ResourceList{}
    47  	limits := core.ResourceList{}
    48  	zeroQuantity := resource.MustParse("0")
    49  	isGuaranteed := true
    50  	// note, ephemeral containers are not considered for QoS as they cannot define resources
    51  	allContainers := []core.Container{}
    52  	allContainers = append(allContainers, pod.Spec.Containers...)
    53  	allContainers = append(allContainers, pod.Spec.InitContainers...)
    54  	for _, container := range allContainers {
    55  		// process requests
    56  		for name, quantity := range container.Resources.Requests {
    57  			if !isSupportedQoSComputeResource(name) {
    58  				continue
    59  			}
    60  			if quantity.Cmp(zeroQuantity) == 1 {
    61  				delta := quantity.DeepCopy()
    62  				if _, exists := requests[name]; !exists {
    63  					requests[name] = delta
    64  				} else {
    65  					delta.Add(requests[name])
    66  					requests[name] = delta
    67  				}
    68  			}
    69  		}
    70  		// process limits
    71  		qosLimitsFound := sets.NewString()
    72  		for name, quantity := range container.Resources.Limits {
    73  			if !isSupportedQoSComputeResource(name) {
    74  				continue
    75  			}
    76  			if quantity.Cmp(zeroQuantity) == 1 {
    77  				qosLimitsFound.Insert(string(name))
    78  				delta := quantity.DeepCopy()
    79  				if _, exists := limits[name]; !exists {
    80  					limits[name] = delta
    81  				} else {
    82  					delta.Add(limits[name])
    83  					limits[name] = delta
    84  				}
    85  			}
    86  		}
    87  
    88  		if !qosLimitsFound.HasAll(string(core.ResourceMemory), string(core.ResourceCPU)) {
    89  			isGuaranteed = false
    90  		}
    91  	}
    92  	if len(requests) == 0 && len(limits) == 0 {
    93  		return core.PodQOSBestEffort
    94  	}
    95  	// Check is requests match limits for all resources.
    96  	if isGuaranteed {
    97  		for name, req := range requests {
    98  			if lim, exists := limits[name]; !exists || lim.Cmp(req) != 0 {
    99  				isGuaranteed = false
   100  				break
   101  			}
   102  		}
   103  	}
   104  	if isGuaranteed &&
   105  		len(requests) == len(limits) {
   106  		return core.PodQOSGuaranteed
   107  	}
   108  	return core.PodQOSBurstable
   109  }
   110  

View as plain text