Merge pull request #119665 from vinaykul/getpodqos-optimization

Perf optimization: GetPodQOS() returns persisted value of PodStatus.QOSClass, if set.

Kubernetes-commit: a2cc9db02fa6420c7905ef372cae6695fddb1120
This commit is contained in:
Kubernetes Publisher 2023-10-12 06:48:26 +02:00
commit 7f2868f3ed
4 changed files with 21 additions and 15 deletions

8
go.mod
View File

@ -30,10 +30,10 @@ require (
github.com/stretchr/testify v1.8.3
golang.org/x/sys v0.13.0
gopkg.in/yaml.v2 v2.4.0
k8s.io/api v0.0.0-20231011070906-133964c1133a
k8s.io/api v0.0.0-20231012030921-012f0adb0d62
k8s.io/apimachinery v0.0.0-20231011070637-1ca6c696c8e4
k8s.io/cli-runtime v0.0.0-20231011193854-a96869e0c2be
k8s.io/client-go v0.0.0-20231011150411-57d597c1d633
k8s.io/client-go v0.0.0-20231012031300-d02858f5197d
k8s.io/component-base v0.0.0-20231011071914-d2d2799059b6
k8s.io/component-helpers v0.0.0-20231011072036-2cfdfb492663
k8s.io/klog/v2 v2.100.1
@ -96,10 +96,10 @@ require (
)
replace (
k8s.io/api => k8s.io/api v0.0.0-20231011070906-133964c1133a
k8s.io/api => k8s.io/api v0.0.0-20231012030921-012f0adb0d62
k8s.io/apimachinery => k8s.io/apimachinery v0.0.0-20231011070637-1ca6c696c8e4
k8s.io/cli-runtime => k8s.io/cli-runtime v0.0.0-20231011193854-a96869e0c2be
k8s.io/client-go => k8s.io/client-go v0.0.0-20231011150411-57d597c1d633
k8s.io/client-go => k8s.io/client-go v0.0.0-20231012031300-d02858f5197d
k8s.io/code-generator => k8s.io/code-generator v0.0.0-20231011070418-d14db18ca33c
k8s.io/component-base => k8s.io/component-base v0.0.0-20231011071914-d2d2799059b6
k8s.io/component-helpers => k8s.io/component-helpers v0.0.0-20231011072036-2cfdfb492663

8
go.sum
View File

@ -276,14 +276,14 @@ gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
honnef.co/go/tools v0.0.0-20190102054323-c2f93a96b099/go.mod h1:rf3lG4BRIbNafJWhAfAdb/ePZxsR/4RtNHQocxwk9r4=
honnef.co/go/tools v0.0.0-20190523083050-ea95bdfd59fc/go.mod h1:rf3lG4BRIbNafJWhAfAdb/ePZxsR/4RtNHQocxwk9r4=
k8s.io/api v0.0.0-20231011070906-133964c1133a h1:ewt1Wxw8kmOLVJfAatcAQhH1e8p/RrQGvwZzWhndWIs=
k8s.io/api v0.0.0-20231011070906-133964c1133a/go.mod h1:fu7/5TpQZY/iFHh5+Ss8NiQgwuMNFajKRNuU00g+a6U=
k8s.io/api v0.0.0-20231012030921-012f0adb0d62 h1:MdBY0DIozFuPf2IXOGBkpo2XelUqIPv9WgDpTRwpmqU=
k8s.io/api v0.0.0-20231012030921-012f0adb0d62/go.mod h1:fu7/5TpQZY/iFHh5+Ss8NiQgwuMNFajKRNuU00g+a6U=
k8s.io/apimachinery v0.0.0-20231011070637-1ca6c696c8e4 h1:B+3yeqV++bsLepyIyjsMGN4EJOxwykKC/vT5aGQjYjk=
k8s.io/apimachinery v0.0.0-20231011070637-1ca6c696c8e4/go.mod h1:CaWZ0SJfeNC59+1gyc+rnythykg+OHqDPEGPQmVnTiE=
k8s.io/cli-runtime v0.0.0-20231011193854-a96869e0c2be h1:8UBiMQn87pMU6yoFM/WXkO7R/m5IJQvQOfiNmfc0H0M=
k8s.io/cli-runtime v0.0.0-20231011193854-a96869e0c2be/go.mod h1:L/ZADS+dmmZguNL+w4PdIjg/ytMzAu3+1qDxy9cL7GY=
k8s.io/client-go v0.0.0-20231011150411-57d597c1d633 h1:r21mAcOlMrjKXvltpLFxAmQpr5zVCmX0CGfuC1oX070=
k8s.io/client-go v0.0.0-20231011150411-57d597c1d633/go.mod h1:4zPB+NUOQERJBGKdq7X91PGpvAI/jiK0NZEmq5c8wx4=
k8s.io/client-go v0.0.0-20231012031300-d02858f5197d h1:FFDrvDEd9uVYCbvsmFJjj+apanVgWMUUi0VBso0+Eyo=
k8s.io/client-go v0.0.0-20231012031300-d02858f5197d/go.mod h1:vtjzldTFMeBQFt3R4QmwbwddWAIHun2SgsQKhpxkbOA=
k8s.io/component-base v0.0.0-20231011071914-d2d2799059b6 h1:DaLh50HBfqyagz99VKlApWGGTQjwTZYneZh309zm40M=
k8s.io/component-base v0.0.0-20231011071914-d2d2799059b6/go.mod h1:4eHXzsEQqrbZP52drXln0tPwtwp0YyajVy/n8vp6VGI=
k8s.io/component-helpers v0.0.0-20231011072036-2cfdfb492663 h1:I5tqhXbxypHvWjTc2oDErCjA1B/vvmLwRtjFRuizOVI=

View File

@ -871,11 +871,7 @@ func describePod(pod *corev1.Pod, events *corev1.EventList) (string, error) {
}
}
describeVolumes(pod.Spec.Volumes, w, "")
if pod.Status.QOSClass != "" {
w.Write(LEVEL_0, "QoS Class:\t%s\n", pod.Status.QOSClass)
} else {
w.Write(LEVEL_0, "QoS Class:\t%s\n", qos.GetPodQOS(pod))
}
w.Write(LEVEL_0, "QoS Class:\t%s\n", qos.GetPodQOS(pod))
printLabelsMultiline(w, "Node-Selectors", pod.Spec.NodeSelector)
printPodTolerationsMultiline(w, "Tolerations", pod.Spec.Tolerations)
describeTopologySpreadConstraints(pod.Spec.TopologySpreadConstraints, w, "")

View File

@ -28,11 +28,21 @@ func isSupportedQoSComputeResource(name core.ResourceName) bool {
return supportedQoSComputeResources.Has(string(name))
}
// GetPodQOS returns the QoS class of a pod.
// GetPodQOS returns the QoS class of a pod persisted in the PodStatus.QOSClass field.
// If PodStatus.QOSClass is empty, it returns value of ComputePodQOS() which evaluates pod's QoS class.
func GetPodQOS(pod *core.Pod) core.PodQOSClass {
if pod.Status.QOSClass != "" {
return pod.Status.QOSClass
}
return ComputePodQOS(pod)
}
// ComputePodQOS evaluates the list of containers to determine a pod's QoS class. This function is more
// expensive than GetPodQOS which should be used for pods having a non-empty .Status.QOSClass.
// 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 burstable if limits and requests do not match across all containers.
func GetPodQOS(pod *core.Pod) core.PodQOSClass {
func ComputePodQOS(pod *core.Pod) core.PodQOSClass {
requests := core.ResourceList{}
limits := core.ResourceList{}
zeroQuantity := resource.MustParse("0")