From 2afc8463c5d669cd776bd4bed91ce3c16a23f223 Mon Sep 17 00:00:00 2001 From: Sanjay Suthar Date: Fri, 5 Jan 2024 15:25:33 +0530 Subject: [PATCH] Test-cases for ENG-1076 & previous functions --- .../k8sclusterreceiver/internal/pod/pods.go | 42 ++--- .../internal/pod/pods_test.go | 102 ++++++++++ .../internal/kubelet/accumulator.go | 43 ++--- .../internal/kubelet/accumulator_test.go | 174 +++++++++++++++++- .../internal/kubelet/metadata.go | 4 +- .../internal/kubelet/metadata_test.go | 20 +- .../internal/kubelet/metrics_test.go | 2 +- .../internal/kubelet/volume_test.go | 20 +- 8 files changed, 322 insertions(+), 85 deletions(-) diff --git a/receiver/k8sclusterreceiver/internal/pod/pods.go b/receiver/k8sclusterreceiver/internal/pod/pods.go index 0b4cae7bf29d..3a65b294f593 100644 --- a/receiver/k8sclusterreceiver/internal/pod/pods.go +++ b/receiver/k8sclusterreceiver/internal/pod/pods.go @@ -7,6 +7,7 @@ import ( "context" "github.com/open-telemetry/opentelemetry-collector-contrib/internal/k8sconfig" "k8s.io/apimachinery/pkg/labels" + k8s "k8s.io/client-go/kubernetes" "strings" "time" @@ -72,7 +73,14 @@ func Transform(pod *corev1.Pod) *corev1.Pod { } func RecordMetrics(logger *zap.Logger, mb *metadata.MetricsBuilder, pod *corev1.Pod, ts pcommon.Timestamp) { - jobInfo := getJobInfoForPod(pod) + client, err := k8sconfig.MakeClient(k8sconfig.APIConfig{ + AuthType: k8sconfig.AuthTypeServiceAccount, + }) + if err != nil { + logger.Error(err.Error()) + } + + jobInfo := getJobInfoForPod(client, pod) mb.RecordK8sPodPhaseDataPoint(ts, int64(phaseToInt(pod.Status.Phase))) mb.RecordK8sPodStatusReasonDataPoint(ts, int64(reasonToInt(pod.Status.Reason))) @@ -83,10 +91,10 @@ func RecordMetrics(logger *zap.Logger, mb *metadata.MetricsBuilder, pod *corev1. rb.SetK8sPodUID(string(pod.UID)) rb.SetK8sPodStartTime(pod.GetCreationTimestamp().String()) rb.SetOpencensusResourcetype("k8s") - rb.SetK8sServiceName(getServiceNameForPod(pod)) + rb.SetK8sServiceName(getServiceNameForPod(client, pod)) rb.SetK8sJobName(jobInfo.Name) rb.SetK8sJobUID(string(jobInfo.UID)) - rb.SetK8sServiceAccountName(getServiceAccountNameForPod(pod)) + rb.SetK8sServiceAccountName(getServiceAccountNameForPod(client, pod)) rb.SetK8sClusterName("unknown") mb.EmitForResource(metadata.WithResource(rb.Emit())) @@ -95,16 +103,9 @@ func RecordMetrics(logger *zap.Logger, mb *metadata.MetricsBuilder, pod *corev1. } } -func getServiceNameForPod(pod *corev1.Pod) string { +func getServiceNameForPod(client k8s.Interface, pod *corev1.Pod) string { var serviceName string - client, err := k8sconfig.MakeClient(k8sconfig.APIConfig{ - AuthType: k8sconfig.AuthTypeServiceAccount, - }) - if err != nil { - return "" - } - serviceList, err := client.CoreV1().Services(pod.Namespace).List(context.TODO(), v1.ListOptions{}) if err != nil { return "" @@ -127,14 +128,7 @@ type JobInfo struct { UID types.UID } -func getJobInfoForPod(pod *corev1.Pod) JobInfo { - client, err := k8sconfig.MakeClient(k8sconfig.APIConfig{ - AuthType: k8sconfig.AuthTypeServiceAccount, - }) - if err != nil { - return JobInfo{} - } - +func getJobInfoForPod(client k8s.Interface, pod *corev1.Pod) JobInfo { podSelector := labels.Set(pod.Labels) jobList, err := client.BatchV1().Jobs(pod.Namespace).List(context.TODO(), v1.ListOptions{ LabelSelector: podSelector.AsSelector().String(), @@ -153,17 +147,9 @@ func getJobInfoForPod(pod *corev1.Pod) JobInfo { return JobInfo{} } -func getServiceAccountNameForPod(pod *corev1.Pod) string { +func getServiceAccountNameForPod(client k8s.Interface, pod *corev1.Pod) string { var serviceAccountName string - client, err := k8sconfig.MakeClient(k8sconfig.APIConfig{ - AuthType: k8sconfig.AuthTypeServiceAccount, - }) - if err != nil { - panic(err) - return "" - } - podDetails, err := client.CoreV1().Pods(pod.Namespace).Get(context.TODO(), pod.Name, v1.GetOptions{}) if err != nil { return "" diff --git a/receiver/k8sclusterreceiver/internal/pod/pods_test.go b/receiver/k8sclusterreceiver/internal/pod/pods_test.go index 7c5e40f8ed85..122341906c11 100644 --- a/receiver/k8sclusterreceiver/internal/pod/pods_test.go +++ b/receiver/k8sclusterreceiver/internal/pod/pods_test.go @@ -4,6 +4,7 @@ package pod import ( + "context" "fmt" "path/filepath" "strings" @@ -21,6 +22,7 @@ import ( "k8s.io/apimachinery/pkg/api/resource" v1 "k8s.io/apimachinery/pkg/apis/meta/v1" "k8s.io/apimachinery/pkg/types" + "k8s.io/client-go/kubernetes/fake" "github.com/open-telemetry/opentelemetry-collector-contrib/internal/coreinternal/golden" "github.com/open-telemetry/opentelemetry-collector-contrib/pkg/experimentalmetricmetadata" @@ -474,3 +476,103 @@ func TestTransform(t *testing.T) { } assert.Equal(t, wantPod, Transform(originalPod)) } + +func TestGetServiceNameForPod(t *testing.T) { + // Create a fake Kubernetes client + client := fake.NewSimpleClientset() + + // Create a Pod with labels + pod := testutils.NewPodWithContainer( + "1", + testutils.NewPodSpecWithContainer("container-name"), + testutils.NewPodStatusWithContainer("container-name", containerIDWithPreifx("container-id")), + ) + + // Create a Service with the same labels as the Pod + service := &corev1.Service{ + ObjectMeta: v1.ObjectMeta{ + Name: "test-service", + Namespace: "test-namespace", + }, + Spec: corev1.ServiceSpec{ + Selector: map[string]string{ + "foo": "bar", + "foo1": "", + }, + }, + } + + // Create the Pod and Service in the fake client + _, err := client.CoreV1().Pods(pod.Namespace).Create(context.TODO(), pod, v1.CreateOptions{}) + assert.NoError(t, err) + + _, err = client.CoreV1().Services(service.Namespace).Create(context.TODO(), service, v1.CreateOptions{}) + assert.NoError(t, err) + + // Call the function + serviceName := getServiceNameForPod(client, pod) + + // Verify the result + expectedServiceName := "test-service" + + assert.Equal(t, expectedServiceName, serviceName) +} + +func TestGetServiceAccountNameForPod(t *testing.T) { + // Create a fake Kubernetes client + client := fake.NewSimpleClientset() + + // Create a Pod with labels + pod := testutils.NewPodWithContainer( + "1", + &corev1.PodSpec{ + ServiceAccountName: "test-service-account", + }, + testutils.NewPodStatusWithContainer("container-name", containerIDWithPreifx("container-id")), + ) + + // Create the Pod in the fake client + _, err := client.CoreV1().Pods(pod.Namespace).Create(context.TODO(), pod, v1.CreateOptions{}) + assert.NoError(t, err) + + // Call the function + serviceAccountName := getServiceAccountNameForPod(client, pod) + + // Verify the result + expectedServiceAccountName := "test-service-account" + + assert.Equal(t, expectedServiceAccountName, serviceAccountName) +} + +func TestGetJobInfoForPod(t *testing.T) { + // Create a fake Kubernetes client + client := fake.NewSimpleClientset() + + // Create a Pod with labels + pod := testutils.NewPodWithContainer( + "1", + testutils.NewPodSpecWithContainer("container-name"), + testutils.NewPodStatusWithContainer("container-name", containerIDWithPreifx("container-id")), + ) + + // Create a Job with the same labels as the Pod + job := testutils.NewJob("1") + + // Create the Pod and Job in the fake client + _, err := client.CoreV1().Pods(pod.Namespace).Create(context.TODO(), pod, v1.CreateOptions{}) + assert.NoError(t, err) + + _, err = client.BatchV1().Jobs(job.Namespace).Create(context.TODO(), job, v1.CreateOptions{}) + assert.NoError(t, err) + + // Call the function + jobInfo := getJobInfoForPod(client, pod) + + // Verify the result + expectedJobInfo := JobInfo{ + Name: "test-job-1", + UID: job.UID, + } + + assert.Equal(t, expectedJobInfo, jobInfo) +} diff --git a/receiver/kubeletstatsreceiver/internal/kubelet/accumulator.go b/receiver/kubeletstatsreceiver/internal/kubelet/accumulator.go index 6dd881f50749..1540d80bd4a0 100644 --- a/receiver/kubeletstatsreceiver/internal/kubelet/accumulator.go +++ b/receiver/kubeletstatsreceiver/internal/kubelet/accumulator.go @@ -4,7 +4,7 @@ package kubelet // import "github.com/open-telemetry/opentelemetry-collector-contrib/receiver/kubeletstatsreceiver/internal/kubelet" import ( - "log" + k8s "k8s.io/client-go/kubernetes" "time" "github.com/open-telemetry/opentelemetry-collector-contrib/internal/k8sconfig" @@ -78,7 +78,7 @@ func (a *metricDataAccumulator) nodeStats(s stats.NodeStats) { func (a *metricDataAccumulator) getNodeUID(nodeName string) string { uid, err := a.metadata.getNodeUID(nodeName) if err != nil { - log.Println(err.Error()) + a.logger.Error(err.Error()) return "" } return uid @@ -96,8 +96,15 @@ func (a *metricDataAccumulator) podStats(s stats.PodStats) { addFilesystemMetrics(a.mbs.PodMetricsBuilder, metadata.PodFilesystemMetrics, s.EphemeralStorage, currentTime) addNetworkMetrics(a.mbs.PodMetricsBuilder, metadata.PodNetworkMetrics, s.Network, currentTime) - serviceName := a.getServiceName(s.PodRef.UID) - jobInfo := a.getJobInfo(s.PodRef.UID) + client, err := k8sconfig.MakeClient(k8sconfig.APIConfig{ + AuthType: k8sconfig.AuthTypeServiceAccount, + }) + if err != nil { + a.logger.Error(err.Error()) + } + + serviceName := a.getServiceName(client, s.PodRef.UID) + jobInfo := a.getJobInfo(client, s.PodRef.UID) serviceAccountName := a.getServiceAccountName(s.PodRef.UID) rb := a.mbs.PodMetricsBuilder.NewResourceBuilder() @@ -117,34 +124,20 @@ func (a *metricDataAccumulator) podStats(s stats.PodStats) { } // getch k8s service name from metadata -func (a *metricDataAccumulator) getServiceName(podUID string) string { - k8sAPIClient, err := k8sconfig.MakeClient(k8sconfig.APIConfig{ - AuthType: k8sconfig.AuthTypeServiceAccount, - }) +func (a *metricDataAccumulator) getServiceName(client k8s.Interface, podUID string) string { + name, err := a.metadata.getServiceName(client, podUID) if err != nil { - return "" - } - - name, err := a.metadata.getServiceName(podUID, k8sAPIClient) - if err != nil { - log.Println(err.Error()) + a.logger.Error(err.Error()) return "" } return name } // getch k8s job uid from metadata -func (a *metricDataAccumulator) getJobInfo(podUID string) JobInfo { - k8sAPIClient, err := k8sconfig.MakeClient(k8sconfig.APIConfig{ - AuthType: k8sconfig.AuthTypeServiceAccount, - }) - if err != nil { - return JobInfo{} - } - - jobInfo, err := a.metadata.getJobInfo(podUID, k8sAPIClient) +func (a *metricDataAccumulator) getJobInfo(client k8s.Interface, podUID string) JobInfo { + jobInfo, err := a.metadata.getJobInfo(client, podUID) if err != nil { - log.Println(err.Error()) + a.logger.Error(err.Error()) return JobInfo{} } return jobInfo @@ -154,7 +147,7 @@ func (a *metricDataAccumulator) getJobInfo(podUID string) JobInfo { func (a *metricDataAccumulator) getServiceAccountName(podUID string) string { name, err := a.metadata.getServiceAccountName(podUID) if err != nil { - log.Println(err.Error()) + a.logger.Error(err.Error()) return "" } return name diff --git a/receiver/kubeletstatsreceiver/internal/kubelet/accumulator_test.go b/receiver/kubeletstatsreceiver/internal/kubelet/accumulator_test.go index 5068e0f23ffa..92f86ec76c32 100644 --- a/receiver/kubeletstatsreceiver/internal/kubelet/accumulator_test.go +++ b/receiver/kubeletstatsreceiver/internal/kubelet/accumulator_test.go @@ -4,18 +4,22 @@ package kubelet import ( + "context" "errors" - "testing" - "github.com/stretchr/testify/assert" "github.com/stretchr/testify/require" "go.opentelemetry.io/collector/receiver/receivertest" "go.uber.org/zap" "go.uber.org/zap/zapcore" "go.uber.org/zap/zaptest/observer" + batchv1 "k8s.io/api/batch/v1" v1 "k8s.io/api/core/v1" metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" + vone "k8s.io/apimachinery/pkg/apis/meta/v1" + "k8s.io/apimachinery/pkg/types" + "k8s.io/client-go/kubernetes/fake" stats "k8s.io/kubelet/pkg/apis/stats/v1alpha1" + "testing" "github.com/open-telemetry/opentelemetry-collector-contrib/receiver/kubeletstatsreceiver/internal/metadata" ) @@ -31,7 +35,7 @@ func TestMetadataErrorCases(t *testing.T) { numMDs int numLogs int logMessages []string - detailedPVCLabelsSetterOverride func(rb *metadata.ResourceBuilder, volCacheID, volumeClaim, namespace string) error + detailedPVCLabelsSetterOverride func(rb *metadata.ResourceBuilder, volCacheID, volumeClaim, namespace string) ([]metadata.ResourceMetricsOption, error) }{ { name: "Fails to get container metadata", @@ -44,6 +48,13 @@ func TestMetadataErrorCases(t *testing.T) { ObjectMeta: metav1.ObjectMeta{ UID: "pod-uid-123", }, + Spec: v1.PodSpec{ + Containers: []v1.Container{ + { + Name: "container2", + }, + }, + }, Status: v1.PodStatus{ ContainerStatuses: []v1.ContainerStatus{ { @@ -55,7 +66,7 @@ func TestMetadataErrorCases(t *testing.T) { }, }, }, - }, nil), + }, nil, nil), testScenario: func(acc metricDataAccumulator) { now := metav1.Now() podStats := stats.PodStats{ @@ -81,7 +92,7 @@ func TestMetadataErrorCases(t *testing.T) { metricGroupsToCollect: map[MetricGroup]bool{ VolumeMetricGroup: true, }, - metadata: NewMetadata([]MetadataLabel{MetadataLabelVolumeType}, nil, nil), + metadata: NewMetadata([]MetadataLabel{MetadataLabelVolumeType}, nil, nil, nil), testScenario: func(acc metricDataAccumulator) { podStats := stats.PodStats{ PodRef: stats.PodReference{ @@ -123,7 +134,7 @@ func TestMetadataErrorCases(t *testing.T) { }, }, }, - }, nil), + }, nil, nil), testScenario: func(acc metricDataAccumulator) { podStats := stats.PodStats{ PodRef: stats.PodReference{ @@ -167,10 +178,10 @@ func TestMetadataErrorCases(t *testing.T) { }, }, }, - }, nil), - detailedPVCLabelsSetterOverride: func(rb *metadata.ResourceBuilder, volCacheID, volumeClaim, namespace string) error { + }, nil, nil), + detailedPVCLabelsSetterOverride: func(rb *metadata.ResourceBuilder, volCacheID, volumeClaim, namespace string) ([]metadata.ResourceMetricsOption, error) { // Mock failure cases. - return errors.New("") + return nil, errors.New("") }, testScenario: func(acc metricDataAccumulator) { podStats := stats.PodStats{ @@ -249,3 +260,148 @@ func TestNilHandling(t *testing.T) { acc.volumeStats(stats.PodStats{}, stats.VolumeStats{}) }) } + +func TestGetServiceName(t *testing.T) { + // Create a fake Kubernetes client + client := fake.NewSimpleClientset() + + // Create a Pod with labels + var pods []v1.Pod + pods = append(pods, v1.Pod{ + ObjectMeta: metav1.ObjectMeta{ + UID: "test-pod-uid-123", + Name: "test-pod-1", + Namespace: "test-namespace", + Labels: map[string]string{ + "foo": "bar", + "foo1": "", + }, + }, + Spec: v1.PodSpec{}, + }) + + acc := metricDataAccumulator{ + metadata: NewMetadata([]MetadataLabel{MetadataLabelContainerID}, &v1.PodList{ + Items: pods, + }, nil, nil), + } + + // Create a Service with the same labels as the Pod + service := &v1.Service{ + ObjectMeta: vone.ObjectMeta{ + Name: "test-service", + Namespace: "test-namespace", + }, + Spec: v1.ServiceSpec{ + Selector: map[string]string{ + "foo": "bar", + "foo1": "", + }, + }, + } + + // Create the Service in the fake client + _, err := client.CoreV1().Services(service.Namespace).Create(context.TODO(), service, vone.CreateOptions{}) + assert.NoError(t, err) + + // Call the getServiceName method + result := acc.getServiceName(client, string(pods[0].UID)) + + // Verify the result + assert.Equal(t, service.Name, result) +} + +func TestGetServiceAccountName(t *testing.T) { + // Create a Pod with labels + var pods []v1.Pod + pods = append(pods, v1.Pod{ + ObjectMeta: metav1.ObjectMeta{ + UID: "test-pod-uid-123", + Name: "test-pod-1", + Namespace: "test-namespace", + Labels: map[string]string{ + "foo": "bar", + "foo1": "", + }, + }, + Spec: v1.PodSpec{ + ServiceAccountName: "test-service-account", + }, + }) + + acc := metricDataAccumulator{ + metadata: NewMetadata([]MetadataLabel{MetadataLabelContainerID}, &v1.PodList{ + Items: pods, + }, nil, nil), + } + + // Call the getServiceName method + result := acc.getServiceAccountName(string(pods[0].UID)) + + // Verify the result + expectedServiceAccountName := "test-service-account" + + assert.Equal(t, expectedServiceAccountName, result) +} + +func TestGetJobInfo(t *testing.T) { + // Create a fake Kubernetes client + client := fake.NewSimpleClientset() + + // Create a Pod with labels + var pods []v1.Pod + pods = append(pods, v1.Pod{ + ObjectMeta: metav1.ObjectMeta{ + UID: "test-pod-uid-123", + Name: "test-pod-1", + Namespace: "test-namespace", + Labels: map[string]string{ + "foo": "bar", + "foo1": "", + }, + }, + Spec: v1.PodSpec{}, + }) + + acc := metricDataAccumulator{ + metadata: NewMetadata([]MetadataLabel{MetadataLabelContainerID}, &v1.PodList{ + Items: pods, + }, nil, nil), + } + + // Create a Job with the same labels as the Pod + job := &batchv1.Job{ + ObjectMeta: vone.ObjectMeta{ + Name: "test-job-1", + Namespace: "test-namespace", + UID: types.UID("test-job-1-uid"), + Labels: map[string]string{ + "foo": "bar", + "foo1": "", + }, + }, + Spec: batchv1.JobSpec{ + Selector: &metav1.LabelSelector{ + MatchLabels: map[string]string{ + "foo": "bar", + "foo1": "", + }, + }, + }, + } + + // Create the Job in the fake client + _, err := client.BatchV1().Jobs(job.Namespace).Create(context.TODO(), job, vone.CreateOptions{}) + assert.NoError(t, err) + + // Call the getJobInfo method + jobInfo := acc.getJobInfo(client, string(pods[0].UID)) + + // Verify the result + expectedJobInfo := JobInfo{ + Name: "test-job-1", + UID: job.UID, + } + + assert.Equal(t, expectedJobInfo, jobInfo) +} diff --git a/receiver/kubeletstatsreceiver/internal/kubelet/metadata.go b/receiver/kubeletstatsreceiver/internal/kubelet/metadata.go index 187d64d2b241..834985537c07 100644 --- a/receiver/kubeletstatsreceiver/internal/kubelet/metadata.go +++ b/receiver/kubeletstatsreceiver/internal/kubelet/metadata.go @@ -136,7 +136,7 @@ func (m *Metadata) getNodeUID(nodeName string) (string, error) { // getServiceName retrieves k8s.service.name from metadata for given pod uid, // returns an error if no service found in the metadata that matches the requirements. -func (m *Metadata) getServiceName(podUID string, client k8s.Interface) (string, error) { +func (m *Metadata) getServiceName(client k8s.Interface, podUID string) (string, error) { if m.PodsMetadata == nil { return "", errors.New("pods metadata were not fetched") } @@ -168,7 +168,7 @@ type JobInfo struct { // getJobInfo retrieves k8s.job.name & k8s.job.uid from metadata for given pod uid, // returns an error if no job found in the metadata that matches the requirements. -func (m *Metadata) getJobInfo(podUID string, client k8s.Interface) (JobInfo, error) { +func (m *Metadata) getJobInfo(client k8s.Interface, podUID string) (JobInfo, error) { if m.PodsMetadata == nil { return JobInfo{}, errors.New("pods metadata were not fetched") } diff --git a/receiver/kubeletstatsreceiver/internal/kubelet/metadata_test.go b/receiver/kubeletstatsreceiver/internal/kubelet/metadata_test.go index 396c4a32c73e..b02298ea1d79 100644 --- a/receiver/kubeletstatsreceiver/internal/kubelet/metadata_test.go +++ b/receiver/kubeletstatsreceiver/internal/kubelet/metadata_test.go @@ -69,7 +69,7 @@ func TestSetExtraLabels(t *testing.T) { }{ { name: "no_labels", - metadata: NewMetadata([]MetadataLabel{}, nil, nil), + metadata: NewMetadata([]MetadataLabel{}, nil, nil, nil), args: []string{"uid", "container.id", "container"}, want: map[string]interface{}{}, }, @@ -97,7 +97,7 @@ func TestSetExtraLabels(t *testing.T) { }, }, }, - }, nil), + }, nil, nil), args: []string{"uid-1234", "container.id", "container1"}, want: map[string]interface{}{ string(MetadataLabelContainerID): "test-container", @@ -127,7 +127,7 @@ func TestSetExtraLabels(t *testing.T) { }, }, }, - }, nil), + }, nil, nil), args: []string{"uid-1234", "container.id", "init-container1"}, want: map[string]interface{}{ string(MetadataLabelContainerID): "test-init-container", @@ -135,7 +135,7 @@ func TestSetExtraLabels(t *testing.T) { }, { name: "set_container_id_no_metadata", - metadata: NewMetadata([]MetadataLabel{MetadataLabelContainerID}, nil, nil), + metadata: NewMetadata([]MetadataLabel{MetadataLabelContainerID}, nil, nil, nil), args: []string{"uid-1234", "container.id", "container1"}, wantError: "pods metadata were not fetched", }, @@ -157,7 +157,7 @@ func TestSetExtraLabels(t *testing.T) { }, }, }, - }, nil), + }, nil, nil), args: []string{"uid-1234", "container.id", "container1"}, wantError: "pod \"uid-1234\" with container \"container1\" not found in the fetched metadata", }, @@ -179,13 +179,13 @@ func TestSetExtraLabels(t *testing.T) { }, }, }, - }, nil), + }, nil, 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, nil), + metadata: NewMetadata([]MetadataLabel{MetadataLabelVolumeType}, nil, nil, nil), args: []string{"uid-1234", "k8s.volume.type", "volume0"}, wantError: "pods metadata were not fetched", }, @@ -207,7 +207,7 @@ func TestSetExtraLabels(t *testing.T) { }, }, }, - }, nil), + }, nil, nil), args: []string{"uid-1234", "k8s.volume.type", "volume1"}, wantError: "pod \"uid-1234\" with volume \"volume1\" not found in the fetched metadata", }, @@ -375,8 +375,8 @@ func TestSetExtraLabelsForVolumeTypes(t *testing.T) { }, }, }, - }, func(rb *metadata.ResourceBuilder, volCacheID, volumeClaim, namespace string) error { - return nil + }, nil, func(rb *metadata.ResourceBuilder, volCacheID, volumeClaim, namespace string) ([]metadata.ResourceMetricsOption, error) { + return nil, nil }) rb := metadata.NewResourceBuilder(metadata.DefaultResourceAttributesConfig()) err := md.setExtraResources(rb, stats.PodReference{UID: tt.args[0]}, MetadataLabel(tt.args[1]), volName) diff --git a/receiver/kubeletstatsreceiver/internal/kubelet/metrics_test.go b/receiver/kubeletstatsreceiver/internal/kubelet/metrics_test.go index 22b9ee9b681a..712c51cd1f5c 100644 --- a/receiver/kubeletstatsreceiver/internal/kubelet/metrics_test.go +++ b/receiver/kubeletstatsreceiver/internal/kubelet/metrics_test.go @@ -33,7 +33,7 @@ func TestMetricAccumulator(t *testing.T) { summary, _ := statsProvider.StatsSummary() metadataProvider := NewMetadataProvider(rc) podsMetadata, _ := metadataProvider.Pods() - k8sMetadata := NewMetadata([]MetadataLabel{MetadataLabelContainerID}, podsMetadata, nil) + k8sMetadata := NewMetadata([]MetadataLabel{MetadataLabelContainerID}, podsMetadata, nil, nil) mbs := &metadata.MetricsBuilders{ NodeMetricsBuilder: metadata.NewMetricsBuilder(metadata.DefaultMetricsBuilderConfig(), receivertest.NewNopCreateSettings()), PodMetricsBuilder: metadata.NewMetricsBuilder(metadata.DefaultMetricsBuilderConfig(), receivertest.NewNopCreateSettings()), diff --git a/receiver/kubeletstatsreceiver/internal/kubelet/volume_test.go b/receiver/kubeletstatsreceiver/internal/kubelet/volume_test.go index bc0ef5dfc300..f4f3252fb875 100644 --- a/receiver/kubeletstatsreceiver/internal/kubelet/volume_test.go +++ b/receiver/kubeletstatsreceiver/internal/kubelet/volume_test.go @@ -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]interface{} }{ { @@ -40,7 +40,7 @@ func TestDetailedPVCLabels(t *testing.T) { }, }, pod: pod{uid: "uid-1234", name: "pod-name", namespace: "pod-namespace"}, - detailedPVCLabelsSetterOverride: func(rb *metadata.ResourceBuilder, volCacheID, volumeClaim, namespace string) error { + detailedPVCLabelsSetterOverride: func(rb *metadata.ResourceBuilder, volCacheID, volumeClaim, namespace string) ([]metadata.ResourceMetricsOption, error) { SetPersistentVolumeLabels(rb, v1.PersistentVolumeSource{ AWSElasticBlockStore: &v1.AWSElasticBlockStoreVolumeSource{ VolumeID: "volume_id", @@ -48,7 +48,7 @@ func TestDetailedPVCLabels(t *testing.T) { Partition: 10, }, }) - return nil + return nil, nil }, want: map[string]interface{}{ "k8s.volume.name": "volume0", @@ -71,7 +71,7 @@ func TestDetailedPVCLabels(t *testing.T) { }, }, pod: pod{uid: "uid-1234", name: "pod-name", namespace: "pod-namespace"}, - detailedPVCLabelsSetterOverride: func(rb *metadata.ResourceBuilder, volCacheID, volumeClaim, namespace string) error { + detailedPVCLabelsSetterOverride: func(rb *metadata.ResourceBuilder, volCacheID, volumeClaim, namespace string) ([]metadata.ResourceMetricsOption, error) { SetPersistentVolumeLabels(rb, v1.PersistentVolumeSource{ GCEPersistentDisk: &v1.GCEPersistentDiskVolumeSource{ PDName: "pd_name", @@ -79,7 +79,7 @@ func TestDetailedPVCLabels(t *testing.T) { Partition: 10, }, }) - return nil + return nil, nil }, want: map[string]interface{}{ "k8s.volume.name": "volume0", @@ -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, volCacheID, volumeClaim, namespace string) error { + detailedPVCLabelsSetterOverride: func(rb *metadata.ResourceBuilder, volCacheID, volumeClaim, namespace string) ([]metadata.ResourceMetricsOption, error) { SetPersistentVolumeLabels(rb, v1.PersistentVolumeSource{ Glusterfs: &v1.GlusterfsPersistentVolumeSource{ EndpointsName: "endpoints_name", Path: "path", }, }) - return nil + return nil, nil }, want: map[string]interface{}{ "k8s.volume.name": "volume0", @@ -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, volCacheID, volumeClaim, namespace string) error { + detailedPVCLabelsSetterOverride: func(rb *metadata.ResourceBuilder, volCacheID, volumeClaim, namespace string) ([]metadata.ResourceMetricsOption, error) { SetPersistentVolumeLabels(rb, v1.PersistentVolumeSource{ Local: &v1.LocalVolumeSource{ Path: "path", }, }) - return nil + return nil, nil }, want: map[string]interface{}{ "k8s.volume.name": "volume0", @@ -177,7 +177,7 @@ func TestDetailedPVCLabels(t *testing.T) { }, }, }, - }, nil) + }, nil, nil) metadata.DetailedPVCResourceSetter = tt.detailedPVCLabelsSetterOverride res, err := getVolumeResourceOptions(rb, podStats, stats.VolumeStats{Name: tt.volumeName}, metadata)