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 #41 from wanghaoran1988/add_group
Add collector for group
- Loading branch information
Showing
27 changed files
with
3,547 additions
and
1 deletion.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
# Group Metrics | ||
|
||
| Metric name| Metric type | Labels/tags | Status | | ||
| ---------- | ----------- | ----------- | ----------- | | ||
| openshift_group_created | Gauge | `group`=<group-name>| STABLE | | ||
| openshift_group_user_account | Gauge | `group`=<group-name> | STABLE | |
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,105 @@ | ||
package collectors | ||
|
||
import ( | ||
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" | ||
"k8s.io/apimachinery/pkg/runtime" | ||
"k8s.io/apimachinery/pkg/watch" | ||
"k8s.io/client-go/tools/cache" | ||
"k8s.io/client-go/tools/clientcmd" | ||
"k8s.io/kube-state-metrics/pkg/metric" | ||
"k8s.io/kube-state-metrics/pkg/version" | ||
|
||
"github.com/golang/glog" | ||
|
||
"github.com/openshift/api/user/v1" | ||
groupclient "github.com/openshift/client-go/user/clientset/versioned" | ||
) | ||
|
||
var ( | ||
descGroupLabelsName = "openshift_group_labels" | ||
descGroupLabelsHelp = "Kubernetes labels converted to Prometheus labels." | ||
descGroupLabelsDefaultLabels = []string{"group"} | ||
|
||
groupMetricFamilies = []metric.FamilyGenerator{ | ||
metric.FamilyGenerator{ | ||
Name: "openshift_group_created", | ||
Type: metric.MetricTypeGauge, | ||
Help: "Unix creation timestamp", | ||
GenerateFunc: wrapGroupFunc(func(d *v1.Group) metric.Family { | ||
f := metric.Family{} | ||
|
||
if !d.CreationTimestamp.IsZero() { | ||
f.Metrics = append(f.Metrics, &metric.Metric{ | ||
Value: float64(d.CreationTimestamp.Unix()), | ||
}) | ||
} | ||
|
||
return f | ||
}), | ||
}, | ||
metric.FamilyGenerator{ | ||
Name: "openshift_group_user_account", | ||
Type: metric.MetricTypeGauge, | ||
Help: "User account in a group.", | ||
GenerateFunc: wrapGroupFunc(func(d *v1.Group) metric.Family { | ||
f := metric.Family{} | ||
if len(d.Users) > 0 { | ||
for _, user := range d.Users { | ||
f.Metrics = append(f.Metrics, &metric.Metric{ | ||
LabelKeys: []string{"user"}, | ||
LabelValues: []string{user}, | ||
Value: 1, | ||
}) | ||
} | ||
|
||
} | ||
return f | ||
}), | ||
}, | ||
} | ||
) | ||
|
||
func wrapGroupFunc(f func(group *v1.Group) metric.Family) func(interface{}) metric.Family { | ||
return func(obj interface{}) metric.Family { | ||
group := obj.(*v1.Group) | ||
|
||
metricFamily := f(group) | ||
|
||
for _, m := range metricFamily.Metrics { | ||
m.LabelKeys = append(descGroupLabelsDefaultLabels, m.LabelKeys...) | ||
m.LabelValues = append([]string{group.Name}, m.LabelValues...) | ||
} | ||
|
||
return metricFamily | ||
} | ||
} | ||
|
||
func createGroupListWatch(apiserver string, kubeconfig string, ns string) cache.ListWatch { | ||
groupclient, err := createGroupClient(apiserver, kubeconfig) | ||
if err != nil { | ||
glog.Fatalf("cannot create Group client: %v", err) | ||
} | ||
return cache.ListWatch{ | ||
ListFunc: func(opts metav1.ListOptions) (runtime.Object, error) { | ||
return groupclient.UserV1().Groups().List(opts) | ||
}, | ||
WatchFunc: func(opts metav1.ListOptions) (watch.Interface, error) { | ||
return groupclient.UserV1().Groups().Watch(opts) | ||
}, | ||
} | ||
} | ||
|
||
func createGroupClient(apiserver string, kubeconfig string) (*groupclient.Clientset, error) { | ||
config, err := clientcmd.BuildConfigFromFlags(apiserver, kubeconfig) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
config.UserAgent = version.GetVersion().String() | ||
config.AcceptContentTypes = "application/vnd.kubernetes.protobuf,application/json" | ||
config.ContentType = "application/vnd.kubernetes.protobuf" | ||
|
||
client, err := groupclient.NewForConfig(config) | ||
return client, err | ||
|
||
} |
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,44 @@ | ||
package collectors | ||
|
||
import ( | ||
"testing" | ||
"time" | ||
|
||
"github.com/openshift/api/user/v1" | ||
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" | ||
"k8s.io/kube-state-metrics/pkg/metric" | ||
) | ||
|
||
func TestGroupCollector(t *testing.T) { | ||
// Fixed metadata on type and help text. We prepend this to every expected | ||
// output so we only have to modify a single place when doing adjustments. | ||
const metadata = ` | ||
# HELP openshift_group_created Unix creation timestamp | ||
# TYPE openshift_group_created gauge | ||
# HELP openshift_group_user_account User account in a group. | ||
# TYPE openshift_group_user_account gauge | ||
` | ||
cases := []generateMetricsTestCase{ | ||
{ | ||
Obj: &v1.Group{ | ||
ObjectMeta: metav1.ObjectMeta{ | ||
Name: "group", | ||
CreationTimestamp: metav1.Time{Time: time.Unix(1500000000, 0)}, | ||
}, | ||
Users: []string{"user1"}, | ||
}, | ||
Want: ` | ||
openshift_group_created{group="group"} 1.5e+09 | ||
openshift_group_user_account{group="group",user="user1"} 1 | ||
`, | ||
MetricNames: []string{"openshift_group_created", "openshift_group_user_account"}, | ||
}, | ||
} | ||
|
||
for i, c := range cases { | ||
c.Func = metric.ComposeMetricGenFuncs(groupMetricFamilies) | ||
if err := c.run(); err != nil { | ||
t.Errorf("unexpected collecting result in %vth run:\n%s", i, err) | ||
} | ||
} | ||
} |
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
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Oops, something went wrong.