Skip to content

Commit

Permalink
Use better methods
Browse files Browse the repository at this point in the history
  • Loading branch information
TylerHelmuth committed Sep 19, 2023
1 parent 4e3c4c7 commit 6d29614
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 51 deletions.
38 changes: 13 additions & 25 deletions receiver/kubeletstatsreceiver/internal/kubelet/metadata.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ import (
"errors"
"fmt"
"regexp"
"strconv"
"strings"

conventions "go.opentelemetry.io/collector/semconv/v1.6.1"
Expand Down Expand Up @@ -56,34 +55,23 @@ type Metadata struct {
}

type resources struct {
cpuLimit float64
cpuRequest float64
memoryLimit float64
memoryRequest float64
cpuLimit float64
memoryRequest int64
memoryLimit int64
}

func getContainerResources(r *v1.ResourceRequirements) resources {
if r == nil {
return resources{}
}
var containerResource resources
cpuLimit, err := strconv.ParseFloat(r.Limits.Cpu().AsDec().String(), 64)
if err == nil {
containerResource.cpuLimit = cpuLimit
}
cpuRequest, err := strconv.ParseFloat(r.Requests.Cpu().AsDec().String(), 64)
if err == nil {
containerResource.cpuRequest = cpuRequest
}
memoryLimit, err := strconv.ParseFloat(r.Limits.Memory().AsDec().String(), 64)
if err == nil {
containerResource.memoryLimit = memoryLimit
}
memoryRequest, err := strconv.ParseFloat(r.Requests.Memory().AsDec().String(), 64)
if err == nil {
containerResource.memoryRequest = memoryRequest

return resources{
cpuRequest: r.Requests.Cpu().AsApproximateFloat64(),
cpuLimit: r.Limits.Cpu().AsApproximateFloat64(),
memoryRequest: r.Requests.Memory().Value(),
memoryLimit: r.Limits.Memory().Value(),
}
return containerResource
}

func NewMetadata(labels []MetadataLabel, podsMetadata *v1.PodList,
Expand Down Expand Up @@ -280,7 +268,7 @@ func (m *Metadata) getContainerCPURequest(podUID string, containerName string) *
return nil
}

func (m *Metadata) getPodMemoryLimit(uid string) *float64 {
func (m *Metadata) getPodMemoryLimit(uid string) *int64 {
podResource, ok := m.PodResources[uid]
if !ok {
return nil
Expand All @@ -291,7 +279,7 @@ func (m *Metadata) getPodMemoryLimit(uid string) *float64 {
return nil
}

func (m *Metadata) getContainerMemoryLimit(podUID string, containerName string) *float64 {
func (m *Metadata) getContainerMemoryLimit(podUID string, containerName string) *int64 {
containerResource, ok := m.ContainerResources[podUID+containerName]
if !ok {
return nil
Expand All @@ -302,7 +290,7 @@ func (m *Metadata) getContainerMemoryLimit(podUID string, containerName string)
return nil
}

func (m *Metadata) getPodMemoryRequest(uid string) *float64 {
func (m *Metadata) getPodMemoryRequest(uid string) *int64 {
podResource, ok := m.PodResources[uid]
if !ok {
return nil
Expand All @@ -313,7 +301,7 @@ func (m *Metadata) getPodMemoryRequest(uid string) *float64 {
return nil
}

func (m *Metadata) getContainerMemoryRequest(podUID string, containerName string) *float64 {
func (m *Metadata) getContainerMemoryRequest(podUID string, containerName string) *int64 {
containerResource, ok := m.ContainerResources[podUID+containerName]
if !ok {
return nil
Expand Down
56 changes: 30 additions & 26 deletions receiver/kubeletstatsreceiver/internal/kubelet/metadata_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -388,6 +388,10 @@ func TestSetExtraLabelsForVolumeTypes(t *testing.T) {
}
}

func intp(i int64) *int64 {
return &i
}

func floatp(f float64) *float64 {
return &f
}
Expand All @@ -404,10 +408,10 @@ func TestCpuAndMemoryGetters(t *testing.T) {
wantPodCPURequest *float64
wantContainerCPULimit *float64
wantContainerCPURequest *float64
wantPodMemoryLimit *float64
wantPodMemoryRequest *float64
wantContainerMemoryLimit *float64
wantContainerMemoryRequest *float64
wantPodMemoryLimit *int64
wantPodMemoryRequest *int64
wantContainerMemoryLimit *int64
wantContainerMemoryRequest *int64
}{
{
name: "no metadata",
Expand All @@ -427,11 +431,11 @@ func TestCpuAndMemoryGetters(t *testing.T) {
Name: "container-1",
Resources: v1.ResourceRequirements{
Requests: v1.ResourceList{
v1.ResourceCPU: k8sresource.MustParse("1"),
v1.ResourceCPU: k8sresource.MustParse("100m"),
v1.ResourceMemory: k8sresource.MustParse("1G"),
},
Limits: v1.ResourceList{
v1.ResourceCPU: k8sresource.MustParse("1"),
v1.ResourceCPU: k8sresource.MustParse("100m"),
v1.ResourceMemory: k8sresource.MustParse("1G"),
},
},
Expand All @@ -456,14 +460,14 @@ func TestCpuAndMemoryGetters(t *testing.T) {
}, nil),
podUID: "uid-1234",
containerName: "container-2",
wantPodCPULimit: floatp(3),
wantPodCPURequest: floatp(3),
wantPodCPULimit: floatp(2.1),
wantPodCPURequest: floatp(2.1),
wantContainerCPULimit: floatp(2),
wantContainerCPURequest: floatp(2),
wantPodMemoryLimit: floatp(4000000000),
wantPodMemoryRequest: floatp(4000000000),
wantContainerMemoryLimit: floatp(3000000000),
wantContainerMemoryRequest: floatp(3000000000),
wantPodMemoryLimit: intp(4000000000),
wantPodMemoryRequest: intp(4000000000),
wantContainerMemoryLimit: intp(3000000000),
wantContainerMemoryRequest: intp(3000000000),
},
{
name: "unknown pod",
Expand Down Expand Up @@ -522,11 +526,11 @@ func TestCpuAndMemoryGetters(t *testing.T) {
Name: "container-1",
Resources: v1.ResourceRequirements{
Requests: v1.ResourceList{
v1.ResourceCPU: k8sresource.MustParse("1"),
v1.ResourceCPU: k8sresource.MustParse("300m"),
v1.ResourceMemory: k8sresource.MustParse("1G"),
},
Limits: v1.ResourceList{
v1.ResourceCPU: k8sresource.MustParse("1"),
v1.ResourceCPU: k8sresource.MustParse("300m"),
v1.ResourceMemory: k8sresource.MustParse("1G"),
},
},
Expand All @@ -535,11 +539,11 @@ func TestCpuAndMemoryGetters(t *testing.T) {
Name: "container-2",
Resources: v1.ResourceRequirements{
Requests: v1.ResourceList{
v1.ResourceCPU: k8sresource.MustParse("2"),
v1.ResourceCPU: k8sresource.MustParse("400m"),
v1.ResourceMemory: k8sresource.MustParse("3G"),
},
Limits: v1.ResourceList{
v1.ResourceCPU: k8sresource.MustParse("2"),
v1.ResourceCPU: k8sresource.MustParse("400m"),
v1.ResourceMemory: k8sresource.MustParse("3G"),
},
},
Expand All @@ -551,10 +555,10 @@ func TestCpuAndMemoryGetters(t *testing.T) {
}, nil),
podUID: "uid-1234",
containerName: "container-3",
wantPodCPULimit: floatp(3),
wantPodCPURequest: floatp(3),
wantPodMemoryLimit: floatp(4000000000),
wantPodMemoryRequest: floatp(4000000000),
wantPodCPULimit: floatp(0.7),
wantPodCPURequest: floatp(0.7),
wantPodMemoryLimit: intp(4000000000),
wantPodMemoryRequest: intp(4000000000),
},
{
name: "container limit not set",
Expand Down Expand Up @@ -593,8 +597,8 @@ func TestCpuAndMemoryGetters(t *testing.T) {
containerName: "container-2",
wantPodCPURequest: floatp(2),
wantContainerCPURequest: floatp(1),
wantPodMemoryRequest: floatp(2000000000),
wantContainerMemoryRequest: floatp(1000000000),
wantPodMemoryRequest: intp(2000000000),
wantContainerMemoryRequest: intp(1000000000),
},
{
name: "container request not set",
Expand Down Expand Up @@ -633,8 +637,8 @@ func TestCpuAndMemoryGetters(t *testing.T) {
containerName: "container-2",
wantPodCPULimit: floatp(2),
wantContainerCPULimit: floatp(1),
wantPodMemoryLimit: floatp(2000000000),
wantContainerMemoryLimit: floatp(1000000000),
wantPodMemoryLimit: intp(2000000000),
wantContainerMemoryLimit: intp(1000000000),
},
{
name: "container limit not set but other is",
Expand Down Expand Up @@ -671,8 +675,8 @@ func TestCpuAndMemoryGetters(t *testing.T) {
containerName: "container-1",
wantContainerCPULimit: floatp(1),
wantContainerCPURequest: floatp(1),
wantContainerMemoryLimit: floatp(1000000000),
wantContainerMemoryRequest: floatp(1000000000),
wantContainerMemoryLimit: intp(1000000000),
wantContainerMemoryRequest: intp(1000000000),
},
}
for _, tt := range tests {
Expand Down

0 comments on commit 6d29614

Please sign in to comment.