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

[receiver/kubeletstats] Refactor setting of volume labels #9745

Merged
merged 1 commit into from
May 10, 2022
Merged
Show file tree
Hide file tree
Changes from all 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
28 changes: 19 additions & 9 deletions receiver/kubeletstatsreceiver/internal/kubelet/metadata.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import (
conventions "go.opentelemetry.io/collector/semconv/v1.6.1"
v1 "k8s.io/api/core/v1"
"k8s.io/apimachinery/pkg/types"
stats "k8s.io/kubelet/pkg/apis/stats/v1alpha1"
)

type MetadataLabel string
Expand Down Expand Up @@ -79,7 +80,7 @@ func getLabelsMap(metadataLabels []MetadataLabel) map[MetadataLabel]bool {

// setExtraLabels sets extra labels in `labels` map based on provided metadata label.
func (m *Metadata) setExtraLabels(
labels map[string]string, podUID string,
labels map[string]string, podRef stats.PodReference,
extraMetadataLabel MetadataLabel, extraMetadataFrom string) error {
// Ensure MetadataLabel exists before proceeding.
if !m.Labels[extraMetadataLabel] || len(m.Labels) == 0 {
Expand All @@ -93,16 +94,27 @@ func (m *Metadata) setExtraLabels(

switch extraMetadataLabel {
case MetadataLabelContainerID:
containerID, err := m.getContainerID(podUID, extraMetadataFrom)
containerID, err := m.getContainerID(podRef.UID, extraMetadataFrom)
if err != nil {
return err
}
labels[conventions.AttributeContainerID] = containerID
case MetadataLabelVolumeType:
err := m.setExtraVolumeMetadata(podUID, extraMetadataFrom, labels)
volume, err := m.getPodVolume(podRef.UID, extraMetadataFrom)
if err != nil {
return err
}

getLabelsFromVolume(volume, labels)

// Get more labels from PersistentVolumeClaim volume type.
if volume.PersistentVolumeClaim != nil {
volCacheID := fmt.Sprintf("%s/%s", podRef.UID, extraMetadataFrom)
if err := m.DetailedPVCLabelsSetter(volCacheID, labels[labelPersistentVolumeClaimName], podRef.Namespace,
labels); err != nil {
return fmt.Errorf("failed to set labels from volume claim: %w", err)
}
}
}
return nil
}
Expand Down Expand Up @@ -131,18 +143,16 @@ func stripContainerID(id string) string {
return containerSchemeRegexp.ReplaceAllString(id, "")
}

func (m *Metadata) setExtraVolumeMetadata(podUID string, volumeName string, labels map[string]string) error {
uid := types.UID(podUID)
func (m *Metadata) getPodVolume(podUID string, volumeName string) (v1.Volume, error) {
for _, pod := range m.PodsMetadata.Items {
if pod.UID == uid {
if pod.UID == types.UID(podUID) {
for _, volume := range pod.Spec.Volumes {
if volumeName == volume.Name {
getLabelsFromVolume(volume, labels)
return nil
return volume, nil
}
}
}
}

return fmt.Errorf("pod %q with volume %q not found in the fetched metadata", podUID, volumeName)
return v1.Volume{}, fmt.Errorf("pod %q with volume %q not found in the fetched metadata", podUID, volumeName)
dmitryax marked this conversation as resolved.
Show resolved Hide resolved
}
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import (
"github.com/stretchr/testify/require"
v1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
stats "k8s.io/kubelet/pkg/apis/stats/v1alpha1"
)

func TestValidateMetadataLabelsConfig(t *testing.T) {
Expand Down Expand Up @@ -165,7 +166,7 @@ func TestSetExtraLabels(t *testing.T) {
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
fields := map[string]string{}
err := tt.metadata.setExtraLabels(fields, tt.args[0], MetadataLabel(tt.args[1]), tt.args[2])
err := tt.metadata.setExtraLabels(fields, stats.PodReference{UID: tt.args[0]}, MetadataLabel(tt.args[1]), tt.args[2])
if tt.wantError == "" {
require.NoError(t, err)
assert.EqualValues(t, tt.want, fields)
Expand Down Expand Up @@ -323,8 +324,10 @@ func TestSetExtraLabelsForVolumeTypes(t *testing.T) {
},
},
},
}, nil)
metadata.setExtraLabels(fields, tt.args[0], MetadataLabel(tt.args[1]), volName)
}, func(volCacheID, volumeClaim, namespace string, labels map[string]string) error {
return nil
})
metadata.setExtraLabels(fields, stats.PodReference{UID: tt.args[0]}, MetadataLabel(tt.args[1]), volName)
assert.Equal(t, tt.want, fields)
})
}
Expand Down
11 changes: 2 additions & 9 deletions receiver/kubeletstatsreceiver/internal/kubelet/resource.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ func fillContainerResource(dest pcommon.Resource, sPod stats.PodStats, sContaine
conventions.AttributeK8SNamespaceName: sPod.PodRef.Namespace,
conventions.AttributeK8SContainerName: sContainer.Name,
}
if err := metadata.setExtraLabels(labels, sPod.PodRef.UID, MetadataLabelContainerID, sContainer.Name); err != nil {
if err := metadata.setExtraLabels(labels, sPod.PodRef, MetadataLabelContainerID, sContainer.Name); err != nil {
return fmt.Errorf("failed to set extra labels from metadata: %w", err)
}
for k, v := range labels {
Expand All @@ -56,17 +56,10 @@ func fillVolumeResource(dest pcommon.Resource, sPod stats.PodStats, vs stats.Vol
labelVolumeName: vs.Name,
}

if err := metadata.setExtraLabels(labels, sPod.PodRef.UID, MetadataLabelVolumeType, vs.Name); err != nil {
if err := metadata.setExtraLabels(labels, sPod.PodRef, MetadataLabelVolumeType, vs.Name); err != nil {
return fmt.Errorf("failed to set extra labels from metadata: %w", err)
}

if labels[labelVolumeType] == labelValuePersistentVolumeClaim {
volCacheID := fmt.Sprintf("%s/%s", sPod.PodRef.UID, vs.Name)
if err := metadata.DetailedPVCLabelsSetter(volCacheID, labels[labelPersistentVolumeClaimName], sPod.PodRef.Namespace, labels); err != nil {
return fmt.Errorf("failed to set labels from volume claim: %w", err)
}
}

for k, v := range labels {
dest.Attributes().UpsertString(k, v)
}
Expand Down