Skip to content

Commit 5d40f79

Browse files
committed
rewrite isWatchable in pod scan
1 parent f740e1d commit 5d40f79

File tree

1 file changed

+21
-31
lines changed

1 file changed

+21
-31
lines changed

pkg/resourcemonitor/podresourcesscanner.go

+21-31
Original file line numberDiff line numberDiff line change
@@ -19,10 +19,10 @@ package resourcemonitor
1919
import (
2020
"context"
2121
"fmt"
22+
"k8s.io/kubernetes/pkg/apis/core/v1/helper/qos"
2223
"strconv"
2324

2425
corev1 "k8s.io/api/core/v1"
25-
"k8s.io/apimachinery/pkg/api/resource"
2626
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
2727
client "k8s.io/client-go/kubernetes"
2828
"k8s.io/klog/v2"
@@ -57,58 +57,49 @@ func NewPodResourcesScanner(namespace string, podResourceClient podresourcesapi.
5757
}
5858

5959
// isWatchable tells if the the given namespace should be watched.
60-
func (resMon *PodResourcesScanner) isWatchable(podNamespace string, podName string, hasDevice bool) (bool, bool, error) {
61-
pod, err := resMon.k8sClient.CoreV1().Pods(podNamespace).Get(context.TODO(), podName, metav1.GetOptions{})
60+
// In Scan(), if watchable is false, this pods scan will skip
61+
// so we can return directly if pod's namespace is not watchable
62+
func (resMon *PodResourcesScanner) isWatchable(podResource *podresourcesapi.PodResources) (bool, bool, error) {
63+
if resMon.namespace != "*" && resMon.namespace != podResource.Namespace {
64+
return false, false, nil
65+
}
66+
67+
pod, err := resMon.k8sClient.CoreV1().Pods(podResource.Namespace).Get(context.TODO(), podResource.Name, metav1.GetOptions{})
6268
if err != nil {
6369
return false, false, err
6470
}
6571

66-
isIntegralGuaranteed := pod.Status.QOSClass == corev1.PodQOSGuaranteed && hasExclusiveCPUs(pod)
72+
podHasExclusiveCPUs := hasExclusiveCPUs(pod)
73+
isPodGuaranteed := qos.GetPodQOS(pod) == corev1.PodQOSGuaranteed
6774

68-
if resMon.namespace == "*" && (isIntegralGuaranteed || hasDevice) {
69-
return true, isIntegralGuaranteed, nil
70-
}
71-
// TODO: add an explicit check for guaranteed pods and pods with devices
72-
return resMon.namespace == podNamespace && (isIntegralGuaranteed || hasDevice), isIntegralGuaranteed, nil
75+
return isPodGuaranteed || hasDevice(podResource), podHasExclusiveCPUs, nil
7376
}
7477

7578
// hasExclusiveCPUs returns true if a guaranteed pod is allocated exclusive CPUs else returns false.
7679
// In isWatchable() function we check for the pod QoS and proceed if it is guaranteed (i.e. request == limit)
7780
// and hence we only check for request in the function below.
7881
func hasExclusiveCPUs(pod *corev1.Pod) bool {
79-
var totalCPU int64
80-
var cpuQuantity resource.Quantity
8182
for _, container := range pod.Spec.InitContainers {
82-
83-
var ok bool
84-
if cpuQuantity, ok = container.Resources.Requests[corev1.ResourceCPU]; !ok {
85-
continue
86-
}
87-
totalCPU += cpuQuantity.Value()
88-
isInitContainerGuaranteed := hasIntegralCPUs(&container)
89-
if isInitContainerGuaranteed {
83+
if hasIntegralCPUs(&container) {
9084
return true
9185
}
9286
}
9387
for _, container := range pod.Spec.Containers {
94-
var ok bool
95-
if cpuQuantity, ok = container.Resources.Requests[corev1.ResourceCPU]; !ok {
96-
continue
97-
}
98-
totalCPU += cpuQuantity.Value()
99-
isAppContainerGuaranteed := hasIntegralCPUs(&container)
100-
if isAppContainerGuaranteed {
88+
if hasIntegralCPUs(&container) {
10189
return true
10290
}
10391
}
10492

105-
//No CPUs requested in all the containers in the pod
93+
//No integralCPUs requested in all the containers of the pod
10694
return false
10795
}
10896

10997
// hasIntegralCPUs returns true if a container in pod is requesting integral CPUs else returns false
11098
func hasIntegralCPUs(container *corev1.Container) bool {
111-
cpuQuantity := container.Resources.Requests[corev1.ResourceCPU]
99+
cpuQuantity, ok := container.Resources.Requests[corev1.ResourceCPU]
100+
if !ok {
101+
return false
102+
}
112103
return cpuQuantity.Value()*1000 == cpuQuantity.MilliValue()
113104
}
114105

@@ -146,8 +137,7 @@ func (resMon *PodResourcesScanner) Scan() (ScanResponse, error) {
146137

147138
for _, podResource := range respPodResources {
148139
klog.InfoS("scanning pod", "podName", podResource.GetName())
149-
hasDevice := hasDevice(podResource)
150-
isWatchable, isIntegralGuaranteed, err := resMon.isWatchable(podResource.GetNamespace(), podResource.GetName(), hasDevice)
140+
isWatchable, isExclusiveCPUs, err := resMon.isWatchable(podResource)
151141
if err != nil {
152142
return ScanResponse{}, fmt.Errorf("checking if pod in a namespace is watchable, namespace:%v, pod name %v: %w", podResource.GetNamespace(), podResource.GetName(), err)
153143
}
@@ -166,7 +156,7 @@ func (resMon *PodResourcesScanner) Scan() (ScanResponse, error) {
166156
}
167157

168158
cpuIDs := container.GetCpuIds()
169-
if len(cpuIDs) > 0 && isIntegralGuaranteed {
159+
if len(cpuIDs) > 0 && isExclusiveCPUs {
170160
var resCPUs []string
171161
for _, cpuID := range container.GetCpuIds() {
172162
resCPUs = append(resCPUs, strconv.FormatInt(cpuID, 10))

0 commit comments

Comments
 (0)