Skip to content

Commit

Permalink
feat: Add kube_node_status_images metric
Browse files Browse the repository at this point in the history
  • Loading branch information
Tobias Hauser committed Nov 11, 2024
1 parent 17151ac commit 2117a08
Show file tree
Hide file tree
Showing 3 changed files with 63 additions and 0 deletions.
1 change: 1 addition & 0 deletions docs/metrics/cluster/node-metrics.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,5 +12,6 @@
| kube_node_status_addresses | Gauge | The addresses of a node | | `node`=&lt;node-address&gt; <br> `type`=&lt;address-type&gt; <br> `address`=&lt;address-value&gt; | EXPERIMENTAL |
| kube_node_status_allocatable | Gauge | The amount of resources allocatable for pods (after reserving some for system daemons) | `cpu`=&lt;core&gt; <br> `ephemeral_storage`=&lt;byte&gt; <br> `pods`=&lt;integer&gt; <br> `attachable_volumes_*`=&lt;byte&gt; <br> `hugepages_*`=&lt;byte&gt; <br> `memory`=&lt;byte&gt; | `node`=&lt;node-address&gt; <br> `resource`=&lt;resource-name&gt; <br> `unit`=&lt;resource-unit&gt; | STABLE |
| 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_status_images | Gauge | The Container Images that are present on the Node, including the size in bytes. | bytes | `node`=&lt;node-address&gt; <br> `image_digest`=&lt;image_digest&gt; <br> `image_name`=&lt;image_name&gt; <br> `image_size_bytes`=&lt;image_size_bytes&gt; | EXPERIMENTAL |
| 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 |
35 changes: 35 additions & 0 deletions internal/store/node.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ package store

import (
"context"
"strconv"
"strings"

basemetrics "k8s.io/component-base/metrics"
Expand Down Expand Up @@ -56,9 +57,43 @@ func nodeMetricFamilies(allowAnnotationsList, allowLabelsList []string) []genera
createNodeStatusCapacityFamilyGenerator(),
createNodeStatusConditionFamilyGenerator(),
createNodeStateAddressFamilyGenerator(),
createNodeStatusImagesFamilyGenerator(),
}
}

func createNodeStatusImagesFamilyGenerator() generator.FamilyGenerator {
return *generator.NewFamilyGeneratorWithStability(
"kube_node_status_images",
"Container Images on the Node",
metric.Gauge,
basemetrics.STABLE,
"",
wrapNodeFunc(func(n *v1.Node) *metric.Family {
ms := []*metric.Metric{}
for _, images := range n.Status.Images {
imageDigest := ""
imageName := ""

if len(images.Names) == 2{
imageDigest = images.Names[0]
imageName = images.Names[1]
} else if len(images.Names) == 1 {
imageName = images.Names[0]
}

ms = append(ms, &metric.Metric{
LabelKeys: []string{"image_digest", "image_name", "image_size_bytes"},
LabelValues: []string{imageDigest, imageName, strconv.FormatInt(images.SizeBytes, 10)},
Value: 1,
})
}
return &metric.Family{
Metrics: ms,
}
}),
)
}

func createNodeDeletionTimestampFamilyGenerator() generator.FamilyGenerator {
return *generator.NewFamilyGeneratorWithStability(
"kube_node_deletion_timestamp",
Expand Down
27 changes: 27 additions & 0 deletions internal/store/node_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -386,6 +386,33 @@ func TestNodeStore(t *testing.T) {
"kube_node_created",
},
},
// verify image metrics
{
Obj: &v1.Node{
ObjectMeta: metav1.ObjectMeta{
Name: "127.0.0.1",
},
Status: v1.NodeStatus{
Images: []v1.ContainerImage{
{
Names: []string{"registry.k8s.io/kube-state-metrics/kube-state-metrics:v2.13.0"},
SizeBytes: 253421,
},
{
Names: []string{"registry.k8s.io/busybox/busybox@sha256:5d462a2f5f5eae1e9a9a5dd0f8a0b7d5a5d12", "registry.k8s.io/busybox/busybox:v1.28.0"},
SizeBytes: 23342213,
},
},
},
},
Want: `
# HELP kube_node_status_images [STABLE] Container Images on the Node
# TYPE kube_node_status_images gauge
kube_node_status_images{image_digest="",image_name="registry.k8s.io/kube-state-metrics/kube-state-metrics:v2.13.0",image_size_bytes="253421"} 1
kube_node_status_images{image_digest="registry.k8s.io/busybox/busybox@sha256:5d462a2f5f5eae1e9a9a5dd0f8a0b7d5a5d12",image_name="registry.k8s.io/busybox/busybox:v1.28.0",image_size_bytes="23342213"} 1
`,
MetricNames: []string{"kube_node_status_images"},
},
}
for i, c := range cases {
c.Func = generator.ComposeMetricGenFuncs(nodeMetricFamilies(nil, nil))
Expand Down

0 comments on commit 2117a08

Please sign in to comment.