Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix topology-updater cpu report #1979

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
42 changes: 20 additions & 22 deletions pkg/resourcemonitor/podresourcesscanner.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ func (resMon *PodResourcesScanner) isWatchable(podNamespace string, podName stri
return false, false, err
}

isIntegralGuaranteed := hasExclusiveCPUs(pod)
isIntegralGuaranteed := pod.Status.QOSClass == corev1.PodQOSGuaranteed && hasExclusiveCPUs(pod)

if resMon.namespace == "*" && (isIntegralGuaranteed || hasDevice) {
return true, isIntegralGuaranteed, nil
Expand All @@ -85,9 +85,9 @@ func hasExclusiveCPUs(pod *corev1.Pod) bool {
continue
}
totalCPU += cpuQuantity.Value()
isInitContainerGuaranteed := hasIntegralCPUs(pod, &container)
if !isInitContainerGuaranteed {
return false
isInitContainerGuaranteed := hasIntegralCPUs(&container)
AllenXu93 marked this conversation as resolved.
Show resolved Hide resolved
if isInitContainerGuaranteed {
return true
}
}
for _, container := range pod.Spec.Containers {
Expand All @@ -96,18 +96,18 @@ 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
}
}

//No CPUs requested in all the containers in the pod
return totalCPU != 0
return false
}

// 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 @@ -165,19 +165,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 && isIntegralGuaranteed {
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
148 changes: 148 additions & 0 deletions pkg/resourcemonitor/podresourcesscanner_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -165,6 +165,9 @@ func TestPodScanner(t *testing.T) {
},
},
},
Status: corev1.PodStatus{
QOSClass: corev1.PodQOSGuaranteed,
},
}

fakeCli := fakeclient.NewSimpleClientset(pod)
Expand Down Expand Up @@ -280,6 +283,9 @@ func TestPodScanner(t *testing.T) {
},
},
},
Status: corev1.PodStatus{
QOSClass: corev1.PodQOSGuaranteed,
},
}

fakeCli = fakeclient.NewSimpleClientset(pod)
Expand Down Expand Up @@ -368,6 +374,9 @@ func TestPodScanner(t *testing.T) {
},
},
},
Status: corev1.PodStatus{
QOSClass: corev1.PodQOSGuaranteed,
},
}
fakeCli = fakeclient.NewSimpleClientset(pod)
resScan.(*PodResourcesScanner).k8sClient = fakeCli
Expand Down Expand Up @@ -458,6 +467,9 @@ func TestPodScanner(t *testing.T) {
},
},
},
Status: corev1.PodStatus{
QOSClass: corev1.PodQOSGuaranteed,
},
}
fakeCli = fakeclient.NewSimpleClientset(pod)
resScan.(*PodResourcesScanner).k8sClient = fakeCli
Expand Down Expand Up @@ -628,6 +640,9 @@ func TestPodScanner(t *testing.T) {
},
},
},
Status: corev1.PodStatus{
QOSClass: corev1.PodQOSGuaranteed,
},
}
fakeCli = fakeclient.NewSimpleClientset(pod)
resScan.(*PodResourcesScanner).k8sClient = fakeCli
Expand Down Expand Up @@ -825,6 +840,9 @@ func TestPodScanner(t *testing.T) {
},
},
},
Status: corev1.PodStatus{
QOSClass: corev1.PodQOSGuaranteed,
},
}
fakeCli = fakeclient.NewSimpleClientset(pod)
resScan.(*PodResourcesScanner).k8sClient = fakeCli
Expand Down Expand Up @@ -1029,6 +1047,9 @@ func TestPodScanner(t *testing.T) {
},
},
},
Status: corev1.PodStatus{
QOSClass: corev1.PodQOSGuaranteed,
},
}
fakeCli = fakeclient.NewSimpleClientset(pod)
resScan.(*PodResourcesScanner).k8sClient = fakeCli
Expand Down Expand Up @@ -1113,6 +1134,120 @@ func TestPodScanner(t *testing.T) {
},
},
},
Status: corev1.PodStatus{
QOSClass: corev1.PodQOSGuaranteed,
},
}
fakeCli = fakeclient.NewSimpleClientset(pod)
resScan.(*PodResourcesScanner).k8sClient = fakeCli
res, err := resScan.Scan()

Convey("Error is nil", func() {
So(err, ShouldBeNil)
})
Convey("Return PodResources should have values", func() {
So(len(res.PodResources), ShouldBeGreaterThan, 0)
})

expected := []PodResources{
{
Name: "test-pod-0",
Namespace: "pod-res-test",
Containers: []ContainerResources{
{
Name: "test-cnt-0",
Resources: []ResourceInfo{
{
Name: "fake.io/resource",
Data: []string{"devA"},
},
},
},
},
},
}
So(reflect.DeepEqual(res.PodResources, expected), ShouldBeTrue)
})

Convey("When I successfully get valid response for guaranteed pods with not cpu pin containers", func() {
resp := &v1.ListPodResourcesResponse{
PodResources: []*v1.PodResources{
{
Name: "test-pod-0",
Namespace: "pod-res-test",
Containers: []*v1.ContainerResources{
{
Name: "test-cnt-0",
CpuIds: []int64{0, 1},
Devices: []*v1.ContainerDevices{
{
ResourceName: "fake.io/resource",
DeviceIds: []string{"devA"},
},
},
},
{
Name: "test-cnt-1",
Devices: []*v1.ContainerDevices{
{
ResourceName: "fake.io/resource",
DeviceIds: []string{"devA"},
},
},
},
},
},
},
}
mockPodResClient.On("List", mock.AnythingOfType("*context.timerCtx"), mock.AnythingOfType("*v1.ListPodResourcesRequest")).Return(resp, nil)
pod := &corev1.Pod{
TypeMeta: metav1.TypeMeta{
Kind: "Pod",
APIVersion: "v1",
},
ObjectMeta: metav1.ObjectMeta{
Name: "test-pod-0",
Namespace: "pod-res-test",
},
Spec: corev1.PodSpec{
Containers: []corev1.Container{
{
Name: "test-cnt-0",
Resources: corev1.ResourceRequirements{
Requests: corev1.ResourceList{

corev1.ResourceName("fake.io/resource"): *resource.NewQuantity(1, resource.DecimalSI),
corev1.ResourceMemory: *resource.NewQuantity(100, resource.DecimalSI),
corev1.ResourceCPU: resource.MustParse("2"),
},
Limits: corev1.ResourceList{
corev1.ResourceName("fake.io/resource"): *resource.NewQuantity(1, resource.DecimalSI),
corev1.ResourceMemory: *resource.NewQuantity(100, resource.DecimalSI),
corev1.ResourceCPU: resource.MustParse("2"),
},
},
},
{
Name: "test-cnt-1",
Resources: corev1.ResourceRequirements{
Requests: corev1.ResourceList{

corev1.ResourceName("fake.io/resource"): *resource.NewQuantity(1, resource.DecimalSI),
corev1.ResourceMemory: *resource.NewQuantity(100, resource.DecimalSI),
corev1.ResourceCPU: resource.MustParse("1500m"),
},
Limits: corev1.ResourceList{
corev1.ResourceName("fake.io/resource"): *resource.NewQuantity(1, resource.DecimalSI),
corev1.ResourceMemory: *resource.NewQuantity(100, resource.DecimalSI),
corev1.ResourceCPU: resource.MustParse("1500m"),
},
},
},
},
},
Status: corev1.PodStatus{
QOSClass: corev1.PodQOSGuaranteed,
},
}
fakeCli = fakeclient.NewSimpleClientset(pod)
resScan.(*PodResourcesScanner).k8sClient = fakeCli
Expand All @@ -1132,6 +1267,19 @@ func TestPodScanner(t *testing.T) {
Containers: []ContainerResources{
{
Name: "test-cnt-0",
Resources: []ResourceInfo{
{
Name: corev1.ResourceCPU,
Data: []string{"0", "1"},
},
{
Name: "fake.io/resource",
Data: []string{"devA"},
},
},
},
{
Name: "test-cnt-1",
Resources: []ResourceInfo{
{
Name: "fake.io/resource",
Expand Down
3 changes: 3 additions & 0 deletions test/e2e/utils/pod/pod.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,9 @@ func GuaranteedSleeper(opts ...func(pod *corev1.Pod)) *corev1.Pod {
},
},
},
Status: corev1.PodStatus{
QOSClass: corev1.PodQOSGuaranteed,
},
}
for _, o := range opts {
o(p)
Expand Down