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

feat: #2501 Count volumes attached to a node #2507

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
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
2 changes: 2 additions & 0 deletions docs/metrics/cluster/node-metrics.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,3 +14,5 @@
| kube_node_status_condition | Gauge | The condition of a cluster node | | `node`=&lt;node-address&gt; <br> `condition`=&lt;node-condition&gt; <br> `status`=&lt;true\|false\|unknown&gt; | STABLE |
| kube_node_created | Gauge | Unix creation timestamp | seconds | `node`=&lt;node-address&gt; | STABLE |
| kube_node_deletion_timestamp | Gauge | Unix deletion timestamp | seconds | `node`=&lt;node-address&gt; | EXPERIMENTAL |
| kube_node_volumes_in_use_count | Gauge | Number of volumes in use on the node | | `node`=&lt;node-address&gt; | STABLE |
| kube_node_volumes_attached_count | Gauge | Number of volumes attached to the node | | `node`=&lt;node-address&gt; | STABLE |
40 changes: 40 additions & 0 deletions internal/store/node.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,8 @@ func nodeMetricFamilies(allowAnnotationsList, allowLabelsList []string) []genera
createNodeStatusCapacityFamilyGenerator(),
createNodeStatusConditionFamilyGenerator(),
createNodeStateAddressFamilyGenerator(),
createNodeVolumeCountGenerator(),
createNodeVolumeInUseGenerator(),
}
}

Expand Down Expand Up @@ -506,6 +508,44 @@ func createNodeStatusConditionFamilyGenerator() generator.FamilyGenerator {
)
}

func createNodeVolumeCountGenerator() generator.FamilyGenerator {
return *generator.NewFamilyGeneratorWithStability(
"kube_node_volumes_attached_count",
"Number of volumes attached to the node",
metric.Gauge,
basemetrics.STABLE,
"",
wrapNodeFunc(func(n *v1.Node) *metric.Family {
return &metric.Family{
Metrics: []*metric.Metric{
{
Value: float64(len(n.Status.VolumesAttached)),
},
},
}
}),
)
}

func createNodeVolumeInUseGenerator() generator.FamilyGenerator {
return *generator.NewFamilyGeneratorWithStability(
"kube_node_volumes_in_use_count",
"Number of volumes in use on the node",
metric.Gauge,
basemetrics.STABLE,
"",
wrapNodeFunc(func(n *v1.Node) *metric.Family {
return &metric.Family{
Metrics: []*metric.Metric{
{
Value: float64(len(n.Status.VolumesInUse)),
},
},
}
}),
)
}

func wrapNodeFunc(f func(*v1.Node) *metric.Family) func(interface{}) *metric.Family {
return func(obj interface{}) *metric.Family {
node := obj.(*v1.Node)
Expand Down
37 changes: 37 additions & 0 deletions internal/store/node_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -386,6 +386,43 @@ func TestNodeStore(t *testing.T) {
"kube_node_created",
},
},
{
Obj: &v1.Node{
ObjectMeta: metav1.ObjectMeta{
Name: "127.0.0.1",
},
Status: v1.NodeStatus{
VolumesAttached: []v1.AttachedVolume{
{Name: "volume1", DevicePath: "/dev/sda1"},
{Name: "volume2", DevicePath: "/dev/sda2"},
},
},
},
Want: `
# HELP kube_node_volumes_attached_count [STABLE] Number of volumes attached to the node
# TYPE kube_node_volumes_attached_count gauge
kube_node_volumes_attached_count{node="127.0.0.1"} 2
`,
MetricNames: []string{"kube_node_volumes_attached_count"},
},
{
Obj: &v1.Node{
ObjectMeta: metav1.ObjectMeta{
Name: "127.0.0.1",
},
Status: v1.NodeStatus{
VolumesInUse: []v1.UniqueVolumeName{
"volume1",
},
},
},
Want: `
# HELP kube_node_volumes_in_use_count [STABLE] Number of volumes in use on the node
# TYPE kube_node_volumes_in_use_count gauge
kube_node_volumes_in_use_count{node="127.0.0.1"} 1
`,
MetricNames: []string{"kube_node_volumes_in_use_count"},
},
}
for i, c := range cases {
c.Func = generator.ComposeMetricGenFuncs(nodeMetricFamilies(nil, nil))
Expand Down
Loading