Skip to content

Commit

Permalink
fix: fix panic bug when volumeHealthStatus is nil
Browse files Browse the repository at this point in the history
  • Loading branch information
fengzixu authored and muyangren2 committed Jul 14, 2022
1 parent f2ce8b7 commit aa3d030
Show file tree
Hide file tree
Showing 2 changed files with 90 additions and 0 deletions.
3 changes: 3 additions & 0 deletions pkg/kubelet/metrics/collectors/volume_stats.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
}
Expand Down
87 changes: 87 additions & 0 deletions pkg/kubelet/metrics/collectors/volume_stats_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
}

0 comments on commit aa3d030

Please sign in to comment.