From aa3d03002ba8fd49cc96e3df83c4415a32bb49e1 Mon Sep 17 00:00:00 2001 From: fengzixu Date: Thu, 17 Mar 2022 00:21:12 +0000 Subject: [PATCH] fix: fix panic bug when volumeHealthStatus is nil --- .../metrics/collectors/volume_stats.go | 3 + .../metrics/collectors/volume_stats_test.go | 87 +++++++++++++++++++ 2 files changed, 90 insertions(+) diff --git a/pkg/kubelet/metrics/collectors/volume_stats.go b/pkg/kubelet/metrics/collectors/volume_stats.go index 8ff14a0b2acd2..dae751c2dadbf 100644 --- a/pkg/kubelet/metrics/collectors/volume_stats.go +++ b/pkg/kubelet/metrics/collectors/volume_stats.go @@ -120,6 +120,9 @@ func (collector *volumeStatsCollector) CollectWithStability(ch chan<- metrics.Me addGauge(volumeStatsInodesDesc, pvcRef, float64(*volumeStat.Inodes)) addGauge(volumeStatsInodesFreeDesc, pvcRef, float64(*volumeStat.InodesFree)) addGauge(volumeStatsInodesUsedDesc, pvcRef, float64(*volumeStat.InodesUsed)) + if volumeStat.VolumeHealthStats != nil { + addGauge(volumeStatsHealthAbnormalDesc, pvcRef, convertBoolToFloat64(volumeStat.VolumeHealthStats.Abnormal)) + } allPVCs.Insert(pvcUniqStr) } } diff --git a/pkg/kubelet/metrics/collectors/volume_stats_test.go b/pkg/kubelet/metrics/collectors/volume_stats_test.go index 54f07178c6d15..0e335a4d74325 100644 --- a/pkg/kubelet/metrics/collectors/volume_stats_test.go +++ b/pkg/kubelet/metrics/collectors/volume_stats_test.go @@ -140,3 +140,90 @@ func TestVolumeStatsCollector(t *testing.T) { t.Errorf("unexpected collecting result:\n%s", err) } } + +func TestVolumeStatsCollectorWithNullVolumeStatus(t *testing.T) { + // Fixed metadata on type and help text. We prepend this to every expected + // output so we only have to modify a single place when doing adjustments. + const metadata = ` + # HELP kubelet_volume_stats_available_bytes [ALPHA] Number of available bytes in the volume + # TYPE kubelet_volume_stats_available_bytes gauge + # HELP kubelet_volume_stats_capacity_bytes [ALPHA] Capacity in bytes of the volume + # TYPE kubelet_volume_stats_capacity_bytes gauge + # HELP kubelet_volume_stats_inodes [ALPHA] Maximum number of inodes in the volume + # TYPE kubelet_volume_stats_inodes gauge + # HELP kubelet_volume_stats_inodes_free [ALPHA] Number of free inodes in the volume + # TYPE kubelet_volume_stats_inodes_free gauge + # HELP kubelet_volume_stats_inodes_used [ALPHA] Number of used inodes in the volume + # TYPE kubelet_volume_stats_inodes_used gauge + # HELP kubelet_volume_stats_used_bytes [ALPHA] Number of used bytes in the volume + # TYPE kubelet_volume_stats_used_bytes gauge + ` + + var ( + podStats = []statsapi.PodStats{ + { + PodRef: statsapi.PodReference{Name: "test-pod", Namespace: "test-namespace", UID: "UID_test-pod"}, + StartTime: metav1.Now(), + VolumeStats: []statsapi.VolumeStats{ + { + FsStats: statsapi.FsStats{ + Time: metav1.Now(), + AvailableBytes: newUint64Pointer(5.663154176e+09), + CapacityBytes: newUint64Pointer(1.0434699264e+10), + UsedBytes: newUint64Pointer(4.21789696e+09), + InodesFree: newUint64Pointer(655344), + Inodes: newUint64Pointer(655360), + InodesUsed: newUint64Pointer(16), + }, + Name: "test", + PVCRef: nil, + }, + { + FsStats: statsapi.FsStats{ + Time: metav1.Now(), + AvailableBytes: newUint64Pointer(5.663154176e+09), + CapacityBytes: newUint64Pointer(1.0434699264e+10), + UsedBytes: newUint64Pointer(4.21789696e+09), + InodesFree: newUint64Pointer(655344), + Inodes: newUint64Pointer(655360), + InodesUsed: newUint64Pointer(16), + }, + Name: "test", + PVCRef: &statsapi.PVCReference{ + Name: "testpvc", + Namespace: "testns", + }, + }, + }, + }, + } + + want = metadata + ` + kubelet_volume_stats_available_bytes{namespace="testns",persistentvolumeclaim="testpvc"} 5.663154176e+09 + kubelet_volume_stats_capacity_bytes{namespace="testns",persistentvolumeclaim="testpvc"} 1.0434699264e+10 + kubelet_volume_stats_inodes{namespace="testns",persistentvolumeclaim="testpvc"} 655360 + kubelet_volume_stats_inodes_free{namespace="testns",persistentvolumeclaim="testpvc"} 655344 + kubelet_volume_stats_inodes_used{namespace="testns",persistentvolumeclaim="testpvc"} 16 + kubelet_volume_stats_used_bytes{namespace="testns",persistentvolumeclaim="testpvc"} 4.21789696e+09 + ` + + metrics = []string{ + "kubelet_volume_stats_available_bytes", + "kubelet_volume_stats_capacity_bytes", + "kubelet_volume_stats_inodes", + "kubelet_volume_stats_inodes_free", + "kubelet_volume_stats_inodes_used", + "kubelet_volume_stats_used_bytes", + } + ) + + mockCtrl := gomock.NewController(t) + defer mockCtrl.Finish() + mockStatsProvider := statstest.NewMockProvider(mockCtrl) + + mockStatsProvider.EXPECT().ListPodStats().Return(podStats, nil).AnyTimes() + mockStatsProvider.EXPECT().ListPodStatsAndUpdateCPUNanoCoreUsage().Return(podStats, nil).AnyTimes() + if err := testutil.CustomCollectAndCompare(&volumeStatsCollector{statsProvider: mockStatsProvider}, strings.NewReader(want), metrics...); err != nil { + t.Errorf("unexpected collecting result:\n%s", err) + } +}