-
Notifications
You must be signed in to change notification settings - Fork 2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1759 from kaitoii11/roles
Add support for role and cluster role
- Loading branch information
Showing
15 changed files
with
594 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
# ClusterRole Metrics | ||
|
||
| Metric name| Metric type | Labels/tags | Status | | ||
| ---------- | ----------- | ----------- | ----------- | | ||
| kube_clusterrole_annotations | Gauge | `clusterrole`=<clusterrole-name> | EXPERIMENTAL | ||
| kube_clusterrole_labels | Gauge | `clusterrole`=<clusterrole-name> | EXPERIMENTAL | ||
| kube_clusterrole_info | Gauge | `clusterrole`=<clusterrole-name> | EXPERIMENTAL | | ||
| kube_clusterrole_created | Gauge | `clusterrole`=<clusterrole-name> | EXPERIMENTAL | | ||
| kube_clusterrole_metadata_resource_version | Gauge | `clusterrole`=<clusterrole-name> | EXPERIMENTAL | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
# Role Metrics | ||
|
||
| Metric name| Metric type | Labels/tags | Status | | ||
| ---------- | ----------- | ----------- | ----------- | | ||
| kube_role_annotations | Gauge | `role`=<role-name> <br> `namespace`=<role-namespace> | EXPERIMENTAL | ||
| kube_role_labels | Gauge | `role`=<role-name> <br> `namespace`=<role-namespace> | EXPERIMENTAL | ||
| kube_role_info | Gauge | `role`=<role-name> <br> `namespace`=<role-namespace> | EXPERIMENTAL | ||
| kube_role_created | Gauge | `role`=<role-name> <br> `namespace`=<role-namespace> | EXPERIMENTAL | | ||
| kube_role_metadata_resource_version | Gauge | `role`=<role-name> <br> `namespace`=<role-namespace> | EXPERIMENTAL | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,154 @@ | ||
/* | ||
Copyright 2018 The Kubernetes Authors All rights reserved. | ||
Licensed under the Apache License, Version 2.0 (the "License"); | ||
you may not use this file except in compliance with the License. | ||
You may obtain a copy of the License at | ||
http://www.apache.org/licenses/LICENSE-2.0 | ||
Unless required by applicable law or agreed to in writing, software | ||
distributed under the License is distributed on an "AS IS" BASIS, | ||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
See the License for the specific language governing permissions and | ||
limitations under the License. | ||
*/ | ||
|
||
package store | ||
|
||
import ( | ||
"context" | ||
|
||
rbacv1 "k8s.io/api/rbac/v1" | ||
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" | ||
"k8s.io/apimachinery/pkg/runtime" | ||
"k8s.io/apimachinery/pkg/watch" | ||
clientset "k8s.io/client-go/kubernetes" | ||
"k8s.io/client-go/tools/cache" | ||
|
||
"k8s.io/kube-state-metrics/v2/pkg/metric" | ||
generator "k8s.io/kube-state-metrics/v2/pkg/metric_generator" | ||
) | ||
|
||
var ( | ||
descClusterRoleAnnotationsName = "kube_clusterrole_annotations" | ||
descClusterRoleAnnotationsHelp = "Kubernetes annotations converted to Prometheus labels." | ||
descClusterRoleLabelsName = "kube_clusterrole_labels" | ||
descClusterRoleLabelsHelp = "Kubernetes labels converted to Prometheus labels." | ||
descClusterRoleLabelsDefaultLabels = []string{"clusterrole"} | ||
) | ||
|
||
func clusterRoleMetricFamilies(allowAnnotationsList, allowLabelsList []string) []generator.FamilyGenerator { | ||
return []generator.FamilyGenerator{ | ||
*generator.NewFamilyGenerator( | ||
descClusterRoleAnnotationsName, | ||
descClusterRoleAnnotationsHelp, | ||
metric.Gauge, | ||
"", | ||
wrapClusterRoleFunc(func(r *rbacv1.ClusterRole) *metric.Family { | ||
annotationKeys, annotationValues := createPrometheusLabelKeysValues("annotation", r.Annotations, allowAnnotationsList) | ||
return &metric.Family{ | ||
Metrics: []*metric.Metric{ | ||
{ | ||
LabelKeys: annotationKeys, | ||
LabelValues: annotationValues, | ||
Value: 1, | ||
}, | ||
}, | ||
} | ||
}), | ||
), | ||
*generator.NewFamilyGenerator( | ||
descClusterRoleLabelsName, | ||
descClusterRoleLabelsHelp, | ||
metric.Gauge, | ||
"", | ||
wrapClusterRoleFunc(func(r *rbacv1.ClusterRole) *metric.Family { | ||
labelKeys, labelValues := createPrometheusLabelKeysValues("label", r.Labels, allowLabelsList) | ||
return &metric.Family{ | ||
Metrics: []*metric.Metric{ | ||
{ | ||
LabelKeys: labelKeys, | ||
LabelValues: labelValues, | ||
Value: 1, | ||
}, | ||
}, | ||
} | ||
}), | ||
), | ||
*generator.NewFamilyGenerator( | ||
"kube_clusterrole_info", | ||
"Information about cluster role.", | ||
metric.Gauge, | ||
"", | ||
wrapClusterRoleFunc(func(r *rbacv1.ClusterRole) *metric.Family { | ||
return &metric.Family{ | ||
Metrics: []*metric.Metric{{ | ||
LabelKeys: []string{}, | ||
LabelValues: []string{}, | ||
Value: 1, | ||
}}, | ||
} | ||
}), | ||
), | ||
*generator.NewFamilyGenerator( | ||
"kube_clusterrole_created", | ||
"Unix creation timestamp", | ||
metric.Gauge, | ||
"", | ||
wrapClusterRoleFunc(func(r *rbacv1.ClusterRole) *metric.Family { | ||
ms := []*metric.Metric{} | ||
|
||
if !r.CreationTimestamp.IsZero() { | ||
ms = append(ms, &metric.Metric{ | ||
LabelKeys: []string{}, | ||
LabelValues: []string{}, | ||
Value: float64(r.CreationTimestamp.Unix()), | ||
}) | ||
} | ||
|
||
return &metric.Family{ | ||
Metrics: ms, | ||
} | ||
}), | ||
), | ||
*generator.NewFamilyGenerator( | ||
"kube_clusterrole_metadata_resource_version", | ||
"Resource version representing a specific version of the cluster role.", | ||
metric.Gauge, | ||
"", | ||
wrapClusterRoleFunc(func(r *rbacv1.ClusterRole) *metric.Family { | ||
return &metric.Family{ | ||
Metrics: resourceVersionMetric(r.ObjectMeta.ResourceVersion), | ||
} | ||
}), | ||
), | ||
} | ||
} | ||
|
||
func createClusterRoleListWatch(kubeClient clientset.Interface, ns string, fieldSelector string) cache.ListerWatcher { | ||
return &cache.ListWatch{ | ||
ListFunc: func(opts metav1.ListOptions) (runtime.Object, error) { | ||
opts.FieldSelector = fieldSelector | ||
return kubeClient.RbacV1().ClusterRoles().List(context.TODO(), opts) | ||
}, | ||
WatchFunc: func(opts metav1.ListOptions) (watch.Interface, error) { | ||
opts.FieldSelector = fieldSelector | ||
return kubeClient.RbacV1().ClusterRoles().Watch(context.TODO(), opts) | ||
}, | ||
} | ||
} | ||
|
||
func wrapClusterRoleFunc(f func(*rbacv1.ClusterRole) *metric.Family) func(interface{}) *metric.Family { | ||
return func(obj interface{}) *metric.Family { | ||
clusterrole := obj.(*rbacv1.ClusterRole) | ||
|
||
metricFamily := f(clusterrole) | ||
|
||
for _, m := range metricFamily.Metrics { | ||
m.LabelKeys, m.LabelValues = mergeKeyValues(descClusterRoleLabelsDefaultLabels, []string{clusterrole.Name}, m.LabelKeys, m.LabelValues) | ||
} | ||
|
||
return metricFamily | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,103 @@ | ||
/* | ||
Copyright 2012 The Kubernetes Authors All rights reserved. | ||
Licensed under the Apache License, Version 2.0 (the "License"); | ||
you may not use this file except in compliance with the License. | ||
You may obtain a copy of the License at | ||
http://www.apache.org/licenses/LICENSE-2.0 | ||
Unless required by applicable law or agreed to in writing, software | ||
distributed under the License is distributed on an "AS IS" BASIS, | ||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
See the License for the specific language governing permissions and | ||
limitations under the License. | ||
*/ | ||
|
||
package store | ||
|
||
import ( | ||
"testing" | ||
|
||
rbacv1 "k8s.io/api/rbac/v1" | ||
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" | ||
|
||
generator "k8s.io/kube-state-metrics/v2/pkg/metric_generator" | ||
) | ||
|
||
func TestClusterRoleStore(t *testing.T) { | ||
startTime := 1501569018 | ||
metav1StartTime := metav1.Unix(int64(startTime), 0) | ||
|
||
cases := []generateMetricsTestCase{ | ||
{ | ||
AllowAnnotationsList: []string{ | ||
"app.k8s.io/owner", | ||
}, | ||
AllowLabelsList: []string{ | ||
"app", | ||
}, | ||
Obj: &rbacv1.ClusterRole{ | ||
ObjectMeta: metav1.ObjectMeta{ | ||
Name: "role1", | ||
ResourceVersion: "BBBBB", | ||
Annotations: map[string]string{ | ||
"app": "mysql-server", | ||
"app.k8s.io/owner": "@foo", | ||
}, | ||
Labels: map[string]string{ | ||
"excluded": "me", | ||
"app": "mysql-server", | ||
}, | ||
}, | ||
}, | ||
Want: ` | ||
# HELP kube_clusterrole_annotations Kubernetes annotations converted to Prometheus labels. | ||
# HELP kube_clusterrole_labels Kubernetes labels converted to Prometheus labels. | ||
# HELP kube_clusterrole_info Information about cluster role. | ||
# HELP kube_clusterrole_metadata_resource_version Resource version representing a specific version of the cluster role. | ||
# TYPE kube_clusterrole_annotations gauge | ||
# TYPE kube_clusterrole_labels gauge | ||
# TYPE kube_clusterrole_info gauge | ||
# TYPE kube_clusterrole_metadata_resource_version gauge | ||
kube_clusterrole_annotations{annotation_app_k8s_io_owner="@foo",clusterrole="role1"} 1 | ||
kube_clusterrole_labels{clusterrole="role1",label_app="mysql-server"} 1 | ||
kube_clusterrole_info{clusterrole="role1"} 1 | ||
`, | ||
MetricNames: []string{ | ||
"kube_clusterrole_annotations", | ||
"kube_clusterrole_labels", | ||
"kube_clusterrole_info", | ||
"kube_clusterrole_metadata_resource_version", | ||
}, | ||
}, | ||
{ | ||
Obj: &rbacv1.ClusterRole{ | ||
ObjectMeta: metav1.ObjectMeta{ | ||
Name: "role2", | ||
CreationTimestamp: metav1StartTime, | ||
ResourceVersion: "10596", | ||
}, | ||
}, | ||
Want: ` | ||
# HELP kube_clusterrole_created Unix creation timestamp | ||
# HELP kube_clusterrole_info Information about cluster role. | ||
# HELP kube_clusterrole_metadata_resource_version Resource version representing a specific version of the cluster role. | ||
# TYPE kube_clusterrole_created gauge | ||
# TYPE kube_clusterrole_info gauge | ||
# TYPE kube_clusterrole_metadata_resource_version gauge | ||
kube_clusterrole_info{clusterrole="role2"} 1 | ||
kube_clusterrole_created{clusterrole="role2"} 1.501569018e+09 | ||
kube_clusterrole_metadata_resource_version{clusterrole="role2"} 10596 | ||
`, | ||
MetricNames: []string{"kube_clusterrole_info", "kube_clusterrole_created", "kube_clusterrole_metadata_resource_version"}, | ||
}, | ||
} | ||
for i, c := range cases { | ||
c.Func = generator.ComposeMetricGenFuncs(clusterRoleMetricFamilies(c.AllowAnnotationsList, c.AllowLabelsList)) | ||
c.Headers = generator.ExtractMetricFamilyHeaders(clusterRoleMetricFamilies(c.AllowAnnotationsList, c.AllowLabelsList)) | ||
if err := c.run(); err != nil { | ||
t.Errorf("unexpected collecting result in %vth run:\n%s", i, err) | ||
} | ||
} | ||
} |
Oops, something went wrong.