Merge pull request #104909 from pacoxu/kubectl-qos

kubectl: include init containers when determining pod QoS
This commit is contained in:
Kubernetes Prow Robot
2021-11-01 20:00:58 -07:00
committed by GitHub
2 changed files with 15 additions and 11 deletions

View File

@@ -34,6 +34,7 @@ func isSupportedQoSComputeResource(name core.ResourceName) bool {
// A pod is besteffort if none of its containers have specified any requests or limits. // A pod is besteffort if none of its containers have specified any requests or limits.
// A pod is guaranteed only when requests and limits are specified for all the containers and they are equal. // A pod is guaranteed only when requests and limits are specified for all the containers and they are equal.
// A pod is burstable if limits and requests do not match across all containers. // A pod is burstable if limits and requests do not match across all containers.
// When this function is updated please also update staging/src/k8s.io/kubectl/pkg/util/qos/qos.go
func GetPodQOS(pod *core.Pod) core.PodQOSClass { func GetPodQOS(pod *core.Pod) core.PodQOSClass {
requests := core.ResourceList{} requests := core.ResourceList{}
limits := core.ResourceList{} limits := core.ResourceList{}

View File

@@ -17,14 +17,14 @@ limitations under the License.
package qos package qos
import ( import (
corev1 "k8s.io/api/core/v1" core "k8s.io/api/core/v1"
"k8s.io/apimachinery/pkg/api/resource" "k8s.io/apimachinery/pkg/api/resource"
"k8s.io/apimachinery/pkg/util/sets" "k8s.io/apimachinery/pkg/util/sets"
) )
var supportedQoSComputeResources = sets.NewString(string(corev1.ResourceCPU), string(corev1.ResourceMemory)) var supportedQoSComputeResources = sets.NewString(string(core.ResourceCPU), string(core.ResourceMemory))
func isSupportedQoSComputeResource(name corev1.ResourceName) bool { func isSupportedQoSComputeResource(name core.ResourceName) bool {
return supportedQoSComputeResources.Has(string(name)) return supportedQoSComputeResources.Has(string(name))
} }
@@ -32,12 +32,15 @@ func isSupportedQoSComputeResource(name corev1.ResourceName) bool {
// A pod is besteffort if none of its containers have specified any requests or limits. // A pod is besteffort if none of its containers have specified any requests or limits.
// A pod is guaranteed only when requests and limits are specified for all the containers and they are equal. // A pod is guaranteed only when requests and limits are specified for all the containers and they are equal.
// A pod is burstable if limits and requests do not match across all containers. // A pod is burstable if limits and requests do not match across all containers.
func GetPodQOS(pod *corev1.Pod) corev1.PodQOSClass { func GetPodQOS(pod *core.Pod) core.PodQOSClass {
requests := corev1.ResourceList{} requests := core.ResourceList{}
limits := corev1.ResourceList{} limits := core.ResourceList{}
zeroQuantity := resource.MustParse("0") zeroQuantity := resource.MustParse("0")
isGuaranteed := true isGuaranteed := true
for _, container := range pod.Spec.Containers { allContainers := []core.Container{}
allContainers = append(allContainers, pod.Spec.Containers...)
allContainers = append(allContainers, pod.Spec.InitContainers...)
for _, container := range allContainers {
// process requests // process requests
for name, quantity := range container.Resources.Requests { for name, quantity := range container.Resources.Requests {
if !isSupportedQoSComputeResource(name) { if !isSupportedQoSComputeResource(name) {
@@ -71,12 +74,12 @@ func GetPodQOS(pod *corev1.Pod) corev1.PodQOSClass {
} }
} }
if !qosLimitsFound.HasAll(string(corev1.ResourceMemory), string(corev1.ResourceCPU)) { if !qosLimitsFound.HasAll(string(core.ResourceMemory), string(core.ResourceCPU)) {
isGuaranteed = false isGuaranteed = false
} }
} }
if len(requests) == 0 && len(limits) == 0 { if len(requests) == 0 && len(limits) == 0 {
return corev1.PodQOSBestEffort return core.PodQOSBestEffort
} }
// Check is requests match limits for all resources. // Check is requests match limits for all resources.
if isGuaranteed { if isGuaranteed {
@@ -89,7 +92,7 @@ func GetPodQOS(pod *corev1.Pod) corev1.PodQOSClass {
} }
if isGuaranteed && if isGuaranteed &&
len(requests) == len(limits) { len(requests) == len(limits) {
return corev1.PodQOSGuaranteed return core.PodQOSGuaranteed
} }
return corev1.PodQOSBurstable return core.PodQOSBurstable
} }