Skip to content

Commit

Permalink
Fix initiated (#81)
Browse files Browse the repository at this point in the history
Co-authored-by: Hardik Choksi <[email protected]>
  • Loading branch information
bhogayatakb and hc-mw committed Dec 5, 2024
1 parent 8f898bb commit bdc1c25
Show file tree
Hide file tree
Showing 12 changed files with 74 additions and 37 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -172,7 +172,6 @@ func (a *metricDataAccumulator) containerStats(sPod stats.PodStats, s stats.Cont

resourceKey := sPod.PodRef.UID + s.Name
currentTime := pcommon.NewTimestampFromTime(a.time)
resourceKey := sPod.PodRef.UID + s.Name
addUptimeMetric(a.mbs.ContainerMetricsBuilder, metadata.ContainerUptimeMetrics.Uptime, s.StartTime, currentTime)
addCPUMetrics(a.mbs.ContainerMetricsBuilder, metadata.ContainerCPUMetrics, s.CPU, currentTime, a.metadata.containerResources[resourceKey], a.metadata.nodeCapacity.CPUCapacity)
addMemoryMetrics(a.mbs.ContainerMetricsBuilder, metadata.ContainerMemoryMetrics, s.Memory, currentTime, a.metadata.containerResources[resourceKey], a.metadata.nodeCapacity.MemoryCapacity)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -275,7 +275,7 @@ func TestGetServiceName(t *testing.T) {
acc := metricDataAccumulator{
metadata: NewMetadata([]MetadataLabel{MetadataLabelContainerID}, &v1.PodList{
Items: pods,
}, nil, nil),
}, &v1.NodeList{Items: []v1.Node{}}, NodeLimits{}, nil),
}

// Create a Service with the same labels as the Pod
Expand Down Expand Up @@ -324,7 +324,7 @@ func TestGetServiceAccountName(t *testing.T) {
acc := metricDataAccumulator{
metadata: NewMetadata([]MetadataLabel{MetadataLabelContainerID}, &v1.PodList{
Items: pods,
}, nil, nil),
}, &v1.NodeList{Items: []v1.Node{}}, NodeLimits{}, nil),
}

// Call the getServiceName method
Expand Down Expand Up @@ -358,7 +358,7 @@ func TestGetJobInfo(t *testing.T) {
acc := metricDataAccumulator{
metadata: NewMetadata([]MetadataLabel{MetadataLabelContainerID}, &v1.PodList{
Items: pods,
}, nil, nil),
}, &v1.NodeList{Items: []v1.Node{}}, NodeLimits{}, nil),
}

// Create a Job with the same labels as the Pod
Expand Down
11 changes: 8 additions & 3 deletions receiver/kubeletstatsreceiver/internal/kubelet/cpu.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,15 +25,20 @@ func addCPUMetrics(
usageCores := float64(*s.UsageNanoCores) / 1_000_000_000
cpuMetrics.Usage(mb, currentTime, usageCores)
addCPUUtilizationMetrics(mb, cpuMetrics, usageCores, currentTime, r, nodeCPULimit)
addCPUUsageMetric(mb, cpuMetrics, s, currentTime, r)
addCPUUsageMetric(mb, cpuMetrics, s, currentTime, r, nodeCPULimit)
}
addCPUTimeMetric(mb, cpuMetrics.Time, s, currentTime)
}

func addCPUUsageMetric(mb *metadata.MetricsBuilder, cpuMetrics metadata.CPUMetrics, s *stats.CPUStats, currentTime pcommon.Timestamp, r resources) {
func addCPUUsageMetric(mb *metadata.MetricsBuilder, cpuMetrics metadata.CPUMetrics, s *stats.CPUStats, currentTime pcommon.Timestamp, r resources, nodeCPULimit float64) {
if s == nil {
return
}

value := float64(*s.UsageNanoCores) / 1_000_000_000
cpuMetrics.Utilization(mb, currentTime, value)
if nodeCPULimit > 0 {
if nodeCPULimit > 0 && s.UsageNanoCores != nil {
usageCores := float64(*s.UsageNanoCores) / 1_000_000_000
cpuMetrics.NodeUtilization(mb, currentTime, usageCores/nodeCPULimit)
}
if r.cpuLimit > 0 {
Expand Down
4 changes: 2 additions & 2 deletions receiver/kubeletstatsreceiver/internal/kubelet/metadata.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ type Metadata struct {
Labels map[MetadataLabel]bool
PodsMetadata *v1.PodList
NodesMetadata *v1.NodeList
DetailedPVCResourceSetter func(rb *metadata.ResourceBuilder, volCacheID, volumeClaim, namespace string) error
DetailedPVCResourceSetter func(rb *metadata.ResourceBuilder, volCacheID, volumeClaim, namespace string) ([]metadata.ResourceMetricsOption, error)
podResources map[string]resources
containerResources map[string]resources
nodeCapacity NodeCapacity
Expand Down Expand Up @@ -90,7 +90,7 @@ func getContainerResources(r *v1.ResourceRequirements) resources {
}
}

func NewMetadata(labels []MetadataLabel, podsMetadata *v1.PodList, nodeCap NodeCapacity,
func NewMetadata(labels []MetadataLabel, podsMetadata *v1.PodList, nodesMetadata *v1.NodeList, nodeCap NodeCapacity,
detailedPVCResourceSetter func(rb *metadata.ResourceBuilder, volCacheID, volumeClaim, namespace string) error,
) Metadata {
m := Metadata{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,3 +32,16 @@ func (p *MetadataProvider) Pods() (*v1.PodList, error) {
}
return &out, nil
}

func (p *MetadataProvider) Nodes() (*v1.NodeList, error) {
nodes, err := p.rc.Nodes()
if err != nil {
return nil, err
}
var out v1.NodeList
err = json.Unmarshal(nodes, &out)
if err != nil {
return nil, err
}
return &out, nil
}
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,10 @@ func (f testRestClient) Pods() ([]byte, error) {
return os.ReadFile("../../testdata/pods.json")
}

func (f testRestClient) Nodes() ([]byte, error) {
return []byte{}, nil
}

func TestPods(t *testing.T) {
tests := []struct {
name string
Expand Down
34 changes: 17 additions & 17 deletions receiver/kubeletstatsreceiver/internal/kubelet/metadata_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ func TestSetExtraLabels(t *testing.T) {
}{
{
name: "no_labels",
metadata: NewMetadata([]MetadataLabel{}, nil, NodeCapacity{}, nil),
metadata: NewMetadata([]MetadataLabel{}, nil, nil, NodeCapacity{}, nil),
args: []string{"uid", "container.id", "container"},
want: map[string]any{},
},
Expand Down Expand Up @@ -98,7 +98,7 @@ func TestSetExtraLabels(t *testing.T) {
},
},
},
}, NodeCapacity{}, nil),
}, &v1.NodeList{Items: []v1.Node{}}, NodeCapacity{}, nil),
args: []string{"uid-1234", "container.id", "container1"},
want: map[string]any{
string(MetadataLabelContainerID): "test-container",
Expand Down Expand Up @@ -128,15 +128,15 @@ func TestSetExtraLabels(t *testing.T) {
},
},
},
}, NodeCapacity{}, nil),
}, &v1.NodeList{Items: []v1.Node{}}, NodeCapacity{}, nil),
args: []string{"uid-1234", "container.id", "init-container1"},
want: map[string]any{
string(MetadataLabelContainerID): "test-init-container",
},
},
{
name: "set_container_id_no_metadata",
metadata: NewMetadata([]MetadataLabel{MetadataLabelContainerID}, nil, NodeCapacity{}, nil),
metadata: NewMetadata([]MetadataLabel{MetadataLabelContainerID}, nil, nil, NodeCapacity{}, nil),
args: []string{"uid-1234", "container.id", "container1"},
wantError: "pods metadata were not fetched",
},
Expand All @@ -158,7 +158,7 @@ func TestSetExtraLabels(t *testing.T) {
},
},
},
}, NodeCapacity{}, nil),
}, &v1.NodeList{Items: []v1.Node{}}, NodeCapacity{}, nil),
args: []string{"uid-1234", "container.id", "container1"},
wantError: "pod \"uid-1234\" with container \"container1\" not found in the fetched metadata",
},
Expand All @@ -180,13 +180,13 @@ func TestSetExtraLabels(t *testing.T) {
},
},
},
}, NodeCapacity{}, nil),
}, &v1.NodeList{Items: []v1.Node{}}, NodeCapacity{}, nil),
args: []string{"uid-1234", "container.id", "container1"},
wantError: "pod \"uid-1234\" with container \"container1\" has an empty containerID",
},
{
name: "set_volume_type_no_metadata",
metadata: NewMetadata([]MetadataLabel{MetadataLabelVolumeType}, nil, NodeCapacity{}, nil),
metadata: NewMetadata([]MetadataLabel{MetadataLabelVolumeType}, nil, nil, NodeCapacity{}, nil),
args: []string{"uid-1234", "k8s.volume.type", "volume0"},
wantError: "pods metadata were not fetched",
},
Expand All @@ -208,7 +208,7 @@ func TestSetExtraLabels(t *testing.T) {
},
},
},
}, NodeCapacity{}, nil),
}, &v1.NodeList{Items: []v1.Node{}}, NodeCapacity{}, nil),
args: []string{"uid-1234", "k8s.volume.type", "volume1"},
wantError: "pod \"uid-1234\" with volume \"volume1\" not found in the fetched metadata",
},
Expand Down Expand Up @@ -376,8 +376,8 @@ func TestSetExtraLabelsForVolumeTypes(t *testing.T) {
},
},
},
}, NodeCapacity{}, func(*metadata.ResourceBuilder, string, string, string) error {
return nil
}, &v1.NodeList{Items: []v1.Node{}}, NodeCapacity{}, func(*metadata.ResourceBuilder, string, string, string) error {
return return []metadata.ResourceMetricsOption{}, nil
})
rb := metadata.NewResourceBuilder(metadata.DefaultResourceAttributesConfig())
err := md.setExtraResources(rb, stats.PodReference{UID: tt.args[0]}, MetadataLabel(tt.args[1]), volName)
Expand Down Expand Up @@ -406,7 +406,7 @@ func TestCpuAndMemoryGetters(t *testing.T) {
}{
{
name: "no metadata",
metadata: NewMetadata([]MetadataLabel{}, nil, NodeCapacity{}, nil),
metadata: NewMetadata([]MetadataLabel{}, nil, nil, NodeCapacity{}, nil),
},
{
name: "pod happy path",
Expand Down Expand Up @@ -448,7 +448,7 @@ func TestCpuAndMemoryGetters(t *testing.T) {
},
},
},
}, NodeCapacity{}, nil),
}, &v1.NodeList{Items: []v1.Node{}}, NodeCapacity{}, nil),
podUID: "uid-1234",
containerName: "container-2",
wantPodCPULimit: 2.1,
Expand Down Expand Up @@ -500,7 +500,7 @@ func TestCpuAndMemoryGetters(t *testing.T) {
},
},
},
}, NodeCapacity{}, nil),
}, &v1.NodeList{Items: []v1.Node{}}, NodeCapacity{}, nil),
podUID: "uid-12345",
},
{
Expand Down Expand Up @@ -543,7 +543,7 @@ func TestCpuAndMemoryGetters(t *testing.T) {
},
},
},
}, NodeCapacity{}, nil),
}, &v1.NodeList{Items: []v1.Node{}}, NodeCapacity{}, nil),
podUID: "uid-1234",
containerName: "container-3",
wantPodCPULimit: 0.7,
Expand Down Expand Up @@ -583,7 +583,7 @@ func TestCpuAndMemoryGetters(t *testing.T) {
},
},
},
}, NodeCapacity{}, nil),
}, &v1.NodeList{Items: []v1.Node{}}, NodeCapacity{}, nil),
podUID: "uid-1234",
containerName: "container-2",
wantPodCPURequest: 2,
Expand Down Expand Up @@ -623,7 +623,7 @@ func TestCpuAndMemoryGetters(t *testing.T) {
},
},
},
}, NodeCapacity{}, nil),
}, &v1.NodeList{Items: []v1.Node{}}, NodeCapacity{}, nil),
podUID: "uid-1234",
containerName: "container-2",
wantPodCPULimit: 2,
Expand Down Expand Up @@ -661,7 +661,7 @@ func TestCpuAndMemoryGetters(t *testing.T) {
},
},
},
}, NodeCapacity{}, nil),
}, &v1.NodeList{Items: []v1.Node{}}, NodeCapacity{}, nil),
podUID: "uid-1234",
containerName: "container-1",
wantContainerCPULimit: 1,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,13 +26,18 @@ func (f fakeRestClient) Pods() ([]byte, error) {
return os.ReadFile("../../testdata/pods.json")
}

func (f fakeRestClient) Nodes() ([]byte, error) {
return os.ReadFile("../../testdata/nodes.json")
}

func TestMetricAccumulator(t *testing.T) {
rc := &fakeRestClient{}
statsProvider := NewStatsProvider(rc)
summary, _ := statsProvider.StatsSummary()
metadataProvider := NewMetadataProvider(rc)
podsMetadata, _ := metadataProvider.Pods()
k8sMetadata := NewMetadata([]MetadataLabel{MetadataLabelContainerID}, podsMetadata, NodeCapacity{}, nil)
nodesMetadata, _ := metadataProvider.Nodes()
k8sMetadata := NewMetadata([]MetadataLabel{MetadataLabelContainerID}, podsMetadata, nodesMetadata, NodeCapacity{}, nil)
mbs := &metadata.MetricsBuilders{
NodeMetricsBuilder: metadata.NewMetricsBuilder(metadata.DefaultMetricsBuilderConfig(), receivertest.NewNopSettings()),
PodMetricsBuilder: metadata.NewMetricsBuilder(metadata.DefaultMetricsBuilderConfig(), receivertest.NewNopSettings()),
Expand Down
5 changes: 5 additions & 0 deletions receiver/kubeletstatsreceiver/internal/kubelet/rest_client.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import (
type RestClient interface {
StatsSummary() ([]byte, error)
Pods() ([]byte, error)
Nodes() ([]byte, error)
}

// HTTPRestClient is a thin wrapper around a kubelet client, encapsulating endpoints
Expand All @@ -32,3 +33,7 @@ func (c *HTTPRestClient) StatsSummary() ([]byte, error) {
func (c *HTTPRestClient) Pods() ([]byte, error) {
return c.client.Get("/pods")
}

func (c *HTTPRestClient) Nodes() ([]byte, error) {
return c.client.Get("/nodes")
}
22 changes: 12 additions & 10 deletions receiver/kubeletstatsreceiver/internal/kubelet/volume_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ func TestDetailedPVCLabels(t *testing.T) {
volumeName string
volumeSource v1.VolumeSource
pod pod
detailedPVCLabelsSetterOverride func(rb *metadata.ResourceBuilder, volCacheID, volumeClaim, namespace string) error
detailedPVCLabelsSetterOverride func(rb *metadata.ResourceBuilder, volCacheID, volumeClaim, namespace string) ([]metadata.ResourceMetricsOption, error)
want map[string]any
}{
{
Expand All @@ -40,15 +40,15 @@ func TestDetailedPVCLabels(t *testing.T) {
},
},
pod: pod{uid: "uid-1234", name: "pod-name", namespace: "pod-namespace"},
detailedPVCLabelsSetterOverride: func(rb *metadata.ResourceBuilder, _, _, _ string) error {
detailedPVCLabelsSetterOverride: func(rb *metadata.ResourceBuilder, _, _, _ string) ([]metadata.ResourceMetricsOption, error) {
SetPersistentVolumeLabels(rb, v1.PersistentVolumeSource{
AWSElasticBlockStore: &v1.AWSElasticBlockStoreVolumeSource{
VolumeID: "volume_id",
FSType: "fs_type",
Partition: 10,
},
})
return nil
return []metadata.ResourceMetricsOption{}, nil
},
want: map[string]any{
"k8s.volume.name": "volume0",
Expand All @@ -71,15 +71,15 @@ func TestDetailedPVCLabels(t *testing.T) {
},
},
pod: pod{uid: "uid-1234", name: "pod-name", namespace: "pod-namespace"},
detailedPVCLabelsSetterOverride: func(rb *metadata.ResourceBuilder, _, _, _ string) error {
detailedPVCLabelsSetterOverride: func(rb *metadata.ResourceBuilder, _, _, _ string) ([]metadata.ResourceMetricsOption, error) {
SetPersistentVolumeLabels(rb, v1.PersistentVolumeSource{
GCEPersistentDisk: &v1.GCEPersistentDiskVolumeSource{
PDName: "pd_name",
FSType: "fs_type",
Partition: 10,
},
})
return nil
return []metadata.ResourceMetricsOption{}, nil
},
want: map[string]any{
"k8s.volume.name": "volume0",
Expand All @@ -102,14 +102,14 @@ func TestDetailedPVCLabels(t *testing.T) {
},
},
pod: pod{uid: "uid-1234", name: "pod-name", namespace: "pod-namespace"},
detailedPVCLabelsSetterOverride: func(rb *metadata.ResourceBuilder, _, _, _ string) error {
detailedPVCLabelsSetterOverride: func(rb *metadata.ResourceBuilder, _, _, _ string) ([]metadata.ResourceMetricsOption, error) {
SetPersistentVolumeLabels(rb, v1.PersistentVolumeSource{
Glusterfs: &v1.GlusterfsPersistentVolumeSource{
EndpointsName: "endpoints_name",
Path: "path",
},
})
return nil
return []metadata.ResourceMetricsOption{}, nil
},
want: map[string]any{
"k8s.volume.name": "volume0",
Expand All @@ -131,13 +131,13 @@ func TestDetailedPVCLabels(t *testing.T) {
},
},
pod: pod{uid: "uid-1234", name: "pod-name", namespace: "pod-namespace"},
detailedPVCLabelsSetterOverride: func(rb *metadata.ResourceBuilder, _, _, _ string) error {
detailedPVCLabelsSetterOverride: func(rb *metadata.ResourceBuilder, _, _, _ string) ([]metadata.ResourceMetricsOption, error) {
SetPersistentVolumeLabels(rb, v1.PersistentVolumeSource{
Local: &v1.LocalVolumeSource{
Path: "path",
},
})
return nil
return []metadata.ResourceMetricsOption{}, nil
},
want: map[string]any{
"k8s.volume.name": "volume0",
Expand Down Expand Up @@ -177,7 +177,9 @@ func TestDetailedPVCLabels(t *testing.T) {
},
},
},
}, NodeCapacity{}, nil)
}, &v1.NodeList{
Items: []v1.Node{},
}, NodeCapacity{}, nil)
metadata.DetailedPVCResourceSetter = tt.detailedPVCLabelsSetterOverride

res, err := getVolumeResourceOptions(rb, podStats, stats.VolumeStats{Name: tt.volumeName}, metadata)
Expand Down
4 changes: 4 additions & 0 deletions receiver/kubeletstatsreceiver/scraper_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -838,3 +838,7 @@ func (f *fakeRestClient) Pods() ([]byte, error) {
}
return os.ReadFile("testdata/pods.json")
}

func (f *fakeRestClient) Nodes() ([]byte, error) {
return []byte{}, nil
}
Empty file.

0 comments on commit bdc1c25

Please sign in to comment.