Skip to content

Commit a8648fd

Browse files
committed
kubernetes#2501 Count volumes attached to a node
1 parent ba23b77 commit a8648fd

File tree

2 files changed

+77
-0
lines changed

2 files changed

+77
-0
lines changed

internal/store/node.go

+40
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,8 @@ func nodeMetricFamilies(allowAnnotationsList, allowLabelsList []string) []genera
5656
createNodeStatusCapacityFamilyGenerator(),
5757
createNodeStatusConditionFamilyGenerator(),
5858
createNodeStateAddressFamilyGenerator(),
59+
createNodeVolumeCountGenerator(),
60+
createNodeVolumeInUseGenerator(),
5961
}
6062
}
6163

@@ -506,6 +508,44 @@ func createNodeStatusConditionFamilyGenerator() generator.FamilyGenerator {
506508
)
507509
}
508510

511+
func createNodeVolumeCountGenerator() generator.FamilyGenerator {
512+
return *generator.NewFamilyGeneratorWithStability(
513+
"kube_node_volumes_attached_count",
514+
"Number of volumes attached to the node",
515+
metric.Gauge,
516+
basemetrics.STABLE,
517+
"",
518+
wrapNodeFunc(func(n *v1.Node) *metric.Family {
519+
return &metric.Family{
520+
Metrics: []*metric.Metric{
521+
{
522+
Value: float64(len(n.Status.VolumesAttached)),
523+
},
524+
},
525+
}
526+
}),
527+
)
528+
}
529+
530+
func createNodeVolumeInUseGenerator() generator.FamilyGenerator {
531+
return *generator.NewFamilyGeneratorWithStability(
532+
"kube_node_volumes_in_use_count",
533+
"Number of volumes in use on the node",
534+
metric.Gauge,
535+
basemetrics.STABLE,
536+
"",
537+
wrapNodeFunc(func(n *v1.Node) *metric.Family {
538+
return &metric.Family{
539+
Metrics: []*metric.Metric{
540+
{
541+
Value: float64(len(n.Status.VolumesInUse)),
542+
},
543+
},
544+
}
545+
}),
546+
)
547+
}
548+
509549
func wrapNodeFunc(f func(*v1.Node) *metric.Family) func(interface{}) *metric.Family {
510550
return func(obj interface{}) *metric.Family {
511551
node := obj.(*v1.Node)

internal/store/node_test.go

+37
Original file line numberDiff line numberDiff line change
@@ -298,6 +298,43 @@ func TestNodeStore(t *testing.T) {
298298
`,
299299
MetricNames: []string{"kube_node_status_addresses"},
300300
},
301+
{
302+
Obj: &v1.Node{
303+
ObjectMeta: metav1.ObjectMeta{
304+
Name: "127.0.0.1",
305+
},
306+
Status: v1.NodeStatus{
307+
VolumesAttached: []v1.AttachedVolume{
308+
{Name: "volume1", DevicePath: "/dev/sda1"},
309+
{Name: "volume2", DevicePath: "/dev/sda2"},
310+
},
311+
},
312+
},
313+
Want: `
314+
# HELP kube_node_volumes_attached_count [STABLE] Number of volumes attached to the node
315+
# TYPE kube_node_volumes_attached_count gauge
316+
kube_node_volumes_attached_count{node="127.0.0.1"} 2
317+
`,
318+
MetricNames: []string{"kube_node_volumes_attached_count"},
319+
},
320+
{
321+
Obj: &v1.Node{
322+
ObjectMeta: metav1.ObjectMeta{
323+
Name: "127.0.0.1",
324+
},
325+
Status: v1.NodeStatus{
326+
VolumesInUse: []v1.UniqueVolumeName{
327+
"volume1",
328+
},
329+
},
330+
},
331+
Want: `
332+
# HELP kube_node_volumes_in_use_count [STABLE] Number of volumes in use on the node
333+
# TYPE kube_node_volumes_in_use_count gauge
334+
kube_node_volumes_in_use_count{node="127.0.0.1"} 1
335+
`,
336+
MetricNames: []string{"kube_node_volumes_in_use_count"},
337+
},
301338
}
302339
for i, c := range cases {
303340
c.Func = generator.ComposeMetricGenFuncs(nodeMetricFamilies(nil, nil))

0 commit comments

Comments
 (0)