Skip to content

Commit

Permalink
fix topology-updater cpu report
Browse files Browse the repository at this point in the history
  • Loading branch information
AllenXu93 committed Dec 13, 2024
1 parent 86d2809 commit a694d91
Showing 1 changed file with 24 additions and 21 deletions.
45 changes: 24 additions & 21 deletions pkg/resourcemonitor/podresourcesscanner.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,11 @@ func (resMon *PodResourcesScanner) isWatchable(podNamespace string, podName stri
return false, false, err
}

// Check Pod is guaranteed QOS class and has exclusive CPUs or devices
if pod.Status.QOSClass != corev1.PodQOSGuaranteed {
return false, false, nil
}

isIntegralGuaranteed := hasExclusiveCPUs(pod)

if resMon.namespace == "*" && (isIntegralGuaranteed || hasDevice) {
Expand All @@ -85,9 +90,9 @@ func hasExclusiveCPUs(pod *corev1.Pod) bool {
continue
}
totalCPU += cpuQuantity.Value()
isInitContainerGuaranteed := hasIntegralCPUs(pod, &container)
if !isInitContainerGuaranteed {
return false
isInitContainerGuaranteed := hasIntegralCPUs(&container)
if isInitContainerGuaranteed {
return true
}
}
for _, container := range pod.Spec.Containers {
Expand All @@ -96,9 +101,9 @@ func hasExclusiveCPUs(pod *corev1.Pod) bool {
continue
}
totalCPU += cpuQuantity.Value()
isAppContainerGuaranteed := hasIntegralCPUs(pod, &container)
if !isAppContainerGuaranteed {
return false
isAppContainerGuaranteed := hasIntegralCPUs(&container)
if isAppContainerGuaranteed {
return true
}
}

Expand All @@ -107,7 +112,7 @@ func hasExclusiveCPUs(pod *corev1.Pod) bool {
}

// hasIntegralCPUs returns true if a container in pod is requesting integral CPUs else returns false
func hasIntegralCPUs(pod *corev1.Pod, container *corev1.Container) bool {
func hasIntegralCPUs(container *corev1.Container) bool {
cpuQuantity := container.Resources.Requests[corev1.ResourceCPU]
return cpuQuantity.Value()*1000 == cpuQuantity.MilliValue()
}
Expand Down Expand Up @@ -147,7 +152,7 @@ func (resMon *PodResourcesScanner) Scan() (ScanResponse, error) {
for _, podResource := range respPodResources {
klog.InfoS("scanning pod", "podName", podResource.GetName())
hasDevice := hasDevice(podResource)
isWatchable, isIntegralGuaranteed, err := resMon.isWatchable(podResource.GetNamespace(), podResource.GetName(), hasDevice)
isWatchable, _, err := resMon.isWatchable(podResource.GetNamespace(), podResource.GetName(), hasDevice)
if err != nil {
return ScanResponse{}, fmt.Errorf("checking if pod in a namespace is watchable, namespace:%v, pod name %v: %w", podResource.GetNamespace(), podResource.GetName(), err)
}
Expand All @@ -165,19 +170,17 @@ func (resMon *PodResourcesScanner) Scan() (ScanResponse, error) {
Name: container.Name,
}

if isIntegralGuaranteed {
cpuIDs := container.GetCpuIds()
if len(cpuIDs) > 0 {
var resCPUs []string
for _, cpuID := range container.GetCpuIds() {
resCPUs = append(resCPUs, strconv.FormatInt(cpuID, 10))
}
contRes.Resources = []ResourceInfo{
{
Name: corev1.ResourceCPU,
Data: resCPUs,
},
}
cpuIDs := container.GetCpuIds()
if len(cpuIDs) > 0 {
var resCPUs []string
for _, cpuID := range container.GetCpuIds() {
resCPUs = append(resCPUs, strconv.FormatInt(cpuID, 10))
}
contRes.Resources = []ResourceInfo{
{
Name: corev1.ResourceCPU,
Data: resCPUs,
},
}
}

Expand Down

0 comments on commit a694d91

Please sign in to comment.