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

Bug 1755757: store: add kube_node_role metric #17

Merged
merged 1 commit into from
Oct 15, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
1 change: 1 addition & 0 deletions Documentation/node-metrics.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
| ---------- | ----------- | ----------- | ----------- |
| kube_node_info | Gauge | `node`=&lt;node-address&gt; <br> `kernel_version`=&lt;kernel-version&gt; <br> `os_image`=&lt;os-image-name&gt; <br> `container_runtime_version`=&lt;container-runtime-and-version-combination&gt; <br> `kubelet_version`=&lt;kubelet-version&gt; <br> `kubeproxy_version`=&lt;kubeproxy-version&gt; <br> `provider_id`=&lt;provider-id&gt; | STABLE |
| kube_node_labels | Gauge | `node`=&lt;node-address&gt; <br> `label_NODE_LABEL`=&lt;NODE_LABEL&gt; | STABLE |
| kube_node_role | Gauge | `node`=&lt;node-address&gt; <br> `role`=&lt;NODE_ROLE&gt; | EXPERIMENTAL |
| kube_node_spec_unschedulable | Gauge | `node`=&lt;node-address&gt;|
| kube_node_spec_taint | Gauge | `node`=&lt;node-address&gt; <br> `key`=&lt;taint-key&gt; <br> `value=`&lt;taint-value&gt; <br> `effect=`&lt;taint-effect&gt; | STABLE |
| kube_node_status_phase| Gauge | `node`=&lt;node-address&gt; <br> `phase`=&lt;Pending\|Running\|Terminated&gt; | STABLE |
Expand Down
24 changes: 23 additions & 1 deletion pkg/collectors/node.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,12 @@ limitations under the License.
package collectors

import (
"strings"

"k8s.io/kube-state-metrics/pkg/constant"
"k8s.io/kube-state-metrics/pkg/metrics"

"k8s.io/api/core/v1"
v1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/runtime"
"k8s.io/apimachinery/pkg/watch"
Expand Down Expand Up @@ -93,6 +95,26 @@ var (
}}
}),
},
metrics.FamilyGenerator{
Name: "kube_node_role",
Type: metrics.MetricTypeGauge,
Help: "The role of a cluster node.",
GenerateFunc: wrapNodeFunc(func(n *v1.Node) metrics.Family {
const prefix = "node-role.kubernetes.io/"
ms := metrics.Family{}
for lbl := range n.Labels {
if strings.HasPrefix(lbl, prefix) {
ms = append(ms, &metrics.Metric{
Name: "kube_node_role",
LabelKeys: []string{"role"},
LabelValues: []string{strings.TrimPrefix(lbl, prefix)},
Value: float64(1),
})
}
}
return ms
}),
},
metrics.FamilyGenerator{
Name: "kube_node_spec_unschedulable",
Type: metrics.MetricTypeGauge,
Expand Down
22 changes: 19 additions & 3 deletions pkg/collectors/node_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ func TestNodeCollector(t *testing.T) {
Name: "127.0.0.1",
CreationTimestamp: metav1.Time{Time: time.Unix(1500000000, 0)},
Labels: map[string]string{
"type": "master",
"node-role.kubernetes.io/master": "",
},
},
Spec: v1.NodeSpec{
Expand Down Expand Up @@ -127,9 +127,10 @@ func TestNodeCollector(t *testing.T) {
},
},
Want: `
kube_node_created{node="127.0.0.1"} 1.5e+09
kube_node_created{node="127.0.0.1"} 1.5e+09
kube_node_info{container_runtime_version="rkt",kernel_version="kernel",kubelet_version="kubelet",kubeproxy_version="kubeproxy",node="127.0.0.1",os_image="osimage",provider_id="provider://i-randomidentifier"} 1
kube_node_labels{label_type="master",node="127.0.0.1"} 1
kube_node_labels{label_node_role_kubernetes_io_master="",node="127.0.0.1"} 1
kube_node_role{node="127.0.0.1",role="master"} 1
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: Are we missing a gofmt run here?

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is to sync with the upstream repo, if we were to make the change here we would diverge with the upstream branch.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

okay!

kube_node_spec_unschedulable{node="127.0.0.1"} 1
kube_node_status_allocatable_cpu_cores{node="127.0.0.1"} 3
kube_node_status_allocatable_memory_bytes{node="127.0.0.1"} 1e+09
Expand All @@ -150,6 +151,21 @@ func TestNodeCollector(t *testing.T) {
kube_node_status_capacity{node="127.0.0.1",resource="pods",unit="integer"} 1000
kube_node_status_capacity{node="127.0.0.1",resource="storage",unit="byte"} 3e+09
`,
MetricNames: []string{
"kube_node_status_capacity",
"kube_node_status_capacity_pods",
"kube_node_status_capacity_memory_bytes",
"kube_node_status_capacity_cpu_cores",
"kube_node_status_allocatable",
"kube_node_status_allocatable_pods",
"kube_node_status_allocatable_memory_bytes",
"kube_node_status_allocatable_cpu_cores",
"kube_node_spec_unschedulable",
"kube_node_labels",
"kube_node_role",
"kube_node_info",
"kube_node_created",
},
},
// Verify phase enumerations.
{
Expand Down