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

v2: Rename collector to resource #1006

Merged
merged 7 commits into from
Feb 10, 2020
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ those that could be used for actionable alerts. Please contribute PR's for
additional metrics!

> WARNING: THESE METRIC/TAG NAMES ARE UNSTABLE AND MAY CHANGE IN A FUTURE RELEASE.
> For now, the following metrics and collectors
> For now, the following metrics and resources
>
> **metrics**
> * `kube_pod_container_resource_requests_nvidia_gpu_devices`
Expand All @@ -102,7 +102,7 @@ additional metrics!
>
> are removed in kube-state-metrics v1.4.0.
>
> Any collectors and metrics based on alpha Kubernetes APIs are excluded from any stability guarantee,
> Any resources and metrics based on alpha Kubernetes APIs are excluded from any stability guarantee,
> which may be changed at any given release.

See the [`docs`](docs) directory for more information on the exposed metrics.
Expand Down Expand Up @@ -259,7 +259,7 @@ subjects:
namespace: your-namespace-where-kube-state-metrics-will-deployed
```

- then specify a set of namespaces (using the `--namespace` option) and a set of kubernetes objects (using the `--collectors`) that your serviceaccount has access to in the `kube-state-metrics` deployment configuration
- then specify a set of namespaces (using the `--namespace` option) and a set of kubernetes objects (using the `--resources`) that your serviceaccount has access to in the `kube-state-metrics` deployment configuration

```yaml
spec:
Expand All @@ -268,7 +268,7 @@ spec:
containers:
- name: kube-state-metrics
args:
- '--collectors=pods'
- '--resources=pods'
- '--namespace=project1'
```

Expand Down
2 changes: 1 addition & 1 deletion docs/cli-arguments.md
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,6 @@ Usage of ./kube-state-metrics:
--add_dir_header If true, adds the file directory to the header
--alsologtostderr log to standard error as well as files
--apiserver string The URL of the apiserver to use as a master
--collectors string Comma-separated list of collectors to be enabled. Defaults to "certificatesigningrequests,configmaps,cronjobs,daemonsets,deployments,endpoints,horizontalpodautoscalers,ingresses,jobs,leases,limitranges,mutatingwebhookconfigurations,namespaces,networkpolicies,nodes,persistentvolumeclaims,persistentvolumes,poddisruptionbudgets,pods,replicasets,replicationcontrollers,resourcequotas,secrets,services,statefulsets,storageclasses,validatingwebhookconfigurations,volumeattachments"
--enable-gzip-encoding Gzip responses when requested by clients via 'Accept-Encoding: gzip' header.
-h, --help Print Help text
--host string Host to expose metrics on. (default "0.0.0.0")
Expand All @@ -44,6 +43,7 @@ Usage of ./kube-state-metrics:
--pod string Name of the pod that contains the kube-state-metrics container. When set, it is expected that --pod and --pod-namespace are both set. Most likely this should be passed via the downward API. This is used for auto-detecting sharding. If set, this has preference over statically configured sharding. This is experimental, it may be removed without notice.
--pod-namespace string Name of the namespace of the pod specified by --pod. When set, it is expected that --pod and --pod-namespace are both set. Most likely this should be passed via the downward API. This is used for auto-detecting sharding. If set, this has preference over statically configured sharding. This is experimental, it may be removed without notice.
--port int Port to expose metrics on. (default 8080)
--resources string Comma-separated list of Resources to be enabled. Defaults to "certificatesigningrequests,configmaps,cronjobs,daemonsets,deployments,endpoints,horizontalpodautoscalers,ingresses,jobs,leases,limitranges,mutatingwebhookconfigurations,namespaces,networkpolicies,nodes,persistentvolumeclaims,persistentvolumes,poddisruptionbudgets,pods,replicasets,replicationcontrollers,resourcequotas,secrets,services,statefulsets,storageclasses,validatingwebhookconfigurations,volumeattachments"
--shard int32 The instances shard nominal (zero indexed) within the total number of shards. (default 0)
--skip_headers If true, avoid header prefixes in the log messages
--skip_log_headers If true, avoid headers when opening log files
Expand Down
4 changes: 2 additions & 2 deletions docs/developer/guide.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,10 @@ Any contribution to improving this documentation will be appreciated.
The following steps are needed to introduce a new resource and its respective resource metrics.

- Reference your new resource(s) to the [docs/README.md](https://github.com/kubernetes/kube-state-metrics/blob/master/docs/README.md#exposed-metrics).
- Reference your new resource(s) in the [docs/cli-arguments.md](https://github.com/kubernetes/kube-state-metrics/blob/master/docs/cli-arguments.md#available-options) as part of the `--collectors` flag.
- Reference your new resource(s) in the [docs/cli-arguments.md](https://github.com/kubernetes/kube-state-metrics/blob/master/docs/cli-arguments.md#available-options) as part of the `--resources` flag.
- Create a new `<name-of-resource>.md` in the [docs](https://github.com/kubernetes/kube-state-metrics/tree/master/docs) directory to provide documentation on the resource(s) and metrics you implemented. Follow the formatting of all other resources.
- Add the resource(s) you are representing to the [examples/standard/cluster-role.yaml](https://github.com/kubernetes/kube-state-metrics/blob/master/examples/standard/cluster-role.yaml) under the appropriate `apiGroup` using the `verbs`: `list` and `watch`.
- Reference and add build functions for the new resource(s) in [internal/store/builder.go](https://github.com/kubernetes/kube-state-metrics/blob/master/internal/store/builder.go).
- Reference the new resource in [pkg/options/collector.go](https://github.com/kubernetes/kube-state-metrics/blob/master/pkg/options/collector.go).
- Reference the new resource in [pkg/options/resource.go](https://github.com/kubernetes/kube-state-metrics/blob/master/pkg/options/resource.go).
- Add a sample Kubernetes manifest to be used by tests in the [tests/manifests/](https://github.com/kubernetes/kube-state-metrics/tree/master/tests/manifests) directory.
- Lastly, and most importantly, actually implement your new resource(s) and its test binary in [internal/store](https://github.com/kubernetes/kube-state-metrics/tree/master/internal/store). Follow the formatting and structure of other resources.
16 changes: 8 additions & 8 deletions internal/store/builder.go
Original file line number Diff line number Diff line change
Expand Up @@ -78,15 +78,15 @@ func (b *Builder) WithMetrics(r *prometheus.Registry) {
}

// WithEnabledResources sets the enabledResources property of a Builder.
func (b *Builder) WithEnabledResources(c []string) error {
for _, col := range c {
if !collectorExists(col) {
return errors.Errorf("collector %s does not exist. Available collectors: %s", col, strings.Join(availableCollectors(), ","))
func (b *Builder) WithEnabledResources(r []string) error {
for _, col := range r {
if !resourceExists(col) {
return errors.Errorf("resource %s does not exist. Available resources: %s", col, strings.Join(availableResources(), ","))
}
}

var copy []string
copy = append(copy, c...)
copy = append(copy, r...)

sort.Strings(copy)

Expand Down Expand Up @@ -154,7 +154,7 @@ func (b *Builder) Build() []cache.Store {
}
}

klog.Infof("Active collectors: %s", strings.Join(activeStoreNames, ","))
klog.Infof("Active resources: %s", strings.Join(activeStoreNames, ","))

return stores
}
Expand Down Expand Up @@ -191,12 +191,12 @@ var availableStores = map[string]func(f *Builder) cache.Store{
"verticalpodautoscalers": func(b *Builder) cache.Store { return b.buildVPAStore() },
}

func collectorExists(name string) bool {
func resourceExists(name string) bool {
_, ok := availableStores[name]
return ok
}

func availableCollectors() []string {
func availableResources() []string {
c := []string{}
for name := range availableStores {
c = append(c, name)
Expand Down
16 changes: 8 additions & 8 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -81,17 +81,17 @@ func main() {
ksmMetricsRegistry := prometheus.NewRegistry()
storeBuilder.WithMetrics(ksmMetricsRegistry)

var collectors []string
if len(opts.Collectors) == 0 {
klog.Info("Using default collectors")
collectors = options.DefaultCollectors.AsSlice()
var resources []string
if len(opts.Resources) == 0 {
klog.Info("Using default resources")
resources = options.DefaultResources.AsSlice()
} else {
klog.Infof("Using collectors %s", opts.Collectors.String())
collectors = opts.Collectors.AsSlice()
klog.Infof("Using resources %s", opts.Resources.String())
resources = opts.Resources.AsSlice()
}

if err := storeBuilder.WithEnabledResources(collectors); err != nil {
klog.Fatalf("Failed to set up collectors: %v", err)
if err := storeBuilder.WithEnabledResources(resources); err != nil {
klog.Fatalf("Failed to set up resources: %v", err)
}

if len(opts.Namespaces) == 0 {
Expand Down
10 changes: 5 additions & 5 deletions main_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ func BenchmarkKubeStateMetrics(b *testing.B) {

builder := store.NewBuilder()
builder.WithMetrics(reg)
builder.WithEnabledResources(options.DefaultCollectors.AsSlice())
builder.WithEnabledResources(options.DefaultResources.AsSlice())
builder.WithKubeClient(kubeClient)
builder.WithSharding(0, 1)
builder.WithContext(ctx)
Expand Down Expand Up @@ -126,7 +126,7 @@ func TestFullScrapeCycle(t *testing.T) {
reg := prometheus.NewRegistry()
builder := store.NewBuilder()
builder.WithMetrics(reg)
builder.WithEnabledResources(options.DefaultCollectors.AsSlice())
builder.WithEnabledResources(options.DefaultResources.AsSlice())
builder.WithKubeClient(kubeClient)
builder.WithNamespaces(options.DefaultNamespaces)
builder.WithGenerateStoreFunc(builder.DefaultGenerateStoreFunc())
Expand Down Expand Up @@ -355,7 +355,7 @@ func TestShardingEquivalenceScrapeCycle(t *testing.T) {
reg := prometheus.NewRegistry()
unshardedBuilder := store.NewBuilder()
unshardedBuilder.WithMetrics(reg)
unshardedBuilder.WithEnabledResources(options.DefaultCollectors.AsSlice())
unshardedBuilder.WithEnabledResources(options.DefaultResources.AsSlice())
unshardedBuilder.WithKubeClient(kubeClient)
unshardedBuilder.WithNamespaces(options.DefaultNamespaces)
unshardedBuilder.WithAllowDenyList(l)
Expand All @@ -367,7 +367,7 @@ func TestShardingEquivalenceScrapeCycle(t *testing.T) {
regShard1 := prometheus.NewRegistry()
shardedBuilder1 := store.NewBuilder()
shardedBuilder1.WithMetrics(regShard1)
shardedBuilder1.WithEnabledResources(options.DefaultCollectors.AsSlice())
shardedBuilder1.WithEnabledResources(options.DefaultResources.AsSlice())
shardedBuilder1.WithKubeClient(kubeClient)
shardedBuilder1.WithNamespaces(options.DefaultNamespaces)
shardedBuilder1.WithAllowDenyList(l)
Expand All @@ -379,7 +379,7 @@ func TestShardingEquivalenceScrapeCycle(t *testing.T) {
regShard2 := prometheus.NewRegistry()
shardedBuilder2 := store.NewBuilder()
shardedBuilder2.WithMetrics(regShard2)
shardedBuilder2.WithEnabledResources(options.DefaultCollectors.AsSlice())
shardedBuilder2.WithEnabledResources(options.DefaultResources.AsSlice())
shardedBuilder2.WithKubeClient(kubeClient)
shardedBuilder2.WithNamespaces(options.DefaultNamespaces)
shardedBuilder2.WithAllowDenyList(l)
Expand Down
6 changes: 3 additions & 3 deletions pkg/options/options.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ type Options struct {
Host string
TelemetryPort int
TelemetryHost string
Collectors CollectorSet
Resources ResourceSet
Namespaces NamespaceList
Shard int32
TotalShards int
Expand All @@ -53,7 +53,7 @@ type Options struct {
// NewOptions returns a new instance of `Options`.
func NewOptions() *Options {
return &Options{
Collectors: CollectorSet{},
Resources: ResourceSet{},
MetricAllowlist: MetricSet{},
MetricDenylist: MetricSet{},
}
Expand Down Expand Up @@ -82,7 +82,7 @@ func (o *Options) AddFlags() {
o.flags.StringVar(&o.Host, "host", "0.0.0.0", `Host to expose metrics on.`)
o.flags.IntVar(&o.TelemetryPort, "telemetry-port", 8081, `Port to expose kube-state-metrics self metrics on.`)
o.flags.StringVar(&o.TelemetryHost, "telemetry-host", "0.0.0.0", `Host to expose kube-state-metrics self metrics on.`)
o.flags.Var(&o.Collectors, "collectors", fmt.Sprintf("Comma-separated list of collectors to be enabled. Defaults to %q", &DefaultCollectors))
o.flags.Var(&o.Resources, "resources", fmt.Sprintf("Comma-separated list of Resources to be enabled. Defaults to %q", &DefaultResources))
o.flags.Var(&o.Namespaces, "namespace", fmt.Sprintf("Comma-separated list of namespaces to be enabled. Defaults to %q", &DefaultNamespaces))
o.flags.Var(&o.MetricAllowlist, "metric-allowlist", "Comma-separated list of metrics to be exposed. This list comprises of exact metric names and/or regex patterns. The allowlist and denylist are mutually exclusive.")
o.flags.Var(&o.MetricDenylist, "metric-denylist", "Comma-separated list of metrics not to be enabled. This list comprises of exact metric names and/or regex patterns. The allowlist and denylist are mutually exclusive.")
Expand Down
4 changes: 2 additions & 2 deletions pkg/options/options_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,8 @@ func TestOptionsParse(t *testing.T) {
RecoverInvoked bool
}{
{
Desc: "collectors command line argument",
Args: []string{"./kube-state-metrics", "--collectors=configmaps,pods"},
Desc: "resources command line argument",
Args: []string{"./kube-state-metrics", "--resources=configmaps,pods"},
RecoverInvoked: false,
},
{
Expand Down
4 changes: 2 additions & 2 deletions pkg/options/collector.go → pkg/options/resource.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,8 @@ var (
// DefaultNamespaces is the default namespace selector for selecting and filtering across all namespaces.
DefaultNamespaces = NamespaceList{metav1.NamespaceAll}

// DefaultCollectors represents the default set of collectors in kube-state-metrics.
DefaultCollectors = CollectorSet{
// DefaultResources represents the default set of resources in kube-state-metrics.
DefaultResources = ResourceSet{
"certificatesigningrequests": struct{}{},
"configmaps": struct{}{},
"cronjobs": struct{}{},
Expand Down
26 changes: 13 additions & 13 deletions pkg/options/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,19 +60,19 @@ func (ms *MetricSet) Type() string {
return "string"
}

// CollectorSet represents a collection which has a unique set of collectors.
type CollectorSet map[string]struct{}
// ResourceSet represents a collection which has a unique set of resources.
type ResourceSet map[string]struct{}

func (c *CollectorSet) String() string {
s := *c
func (r *ResourceSet) String() string {
s := *r
ss := s.AsSlice()
sort.Strings(ss)
return strings.Join(ss, ",")
}

// Set converts a comma-separated string of collectors into a slice and appends it to the CollectorSet.
func (c *CollectorSet) Set(value string) error {
s := *c
// Set converts a comma-separated string of resources into a slice and appends it to the ResourceSet.
func (r *ResourceSet) Set(value string) error {
s := *r
cols := strings.Split(value, ",")
for _, col := range cols {
col = strings.TrimSpace(col)
Expand All @@ -83,17 +83,17 @@ func (c *CollectorSet) Set(value string) error {
return nil
}

// AsSlice returns the Collector in the form of a plain string slice.
func (c CollectorSet) AsSlice() []string {
cols := make([]string, 0, len(c))
for col := range c {
// AsSlice returns the Resource in the form of a plain string slice.
func (r ResourceSet) AsSlice() []string {
cols := make([]string, 0, len(r))
for col := range r {
cols = append(cols, col)
}
return cols
}

// Type returns a descriptive string about the CollectorSet type.
func (c *CollectorSet) Type() string {
// Type returns a descriptive string about the ResourceSet type.
func (r *ResourceSet) Type() string {
return "string"
}

Expand Down
14 changes: 7 additions & 7 deletions pkg/options/types_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,23 +21,23 @@ import (
"testing"
)

func TestCollectorSetSet(t *testing.T) {
func TestResourceSetSet(t *testing.T) {
tests := []struct {
Desc string
Value string
Wanted CollectorSet
Wanted ResourceSet
WantedError bool
}{
{
Desc: "empty collectors",
Desc: "empty resources",
Value: "",
Wanted: CollectorSet{},
Wanted: ResourceSet{},
WantedError: false,
},
{
Desc: "normal collectors",
Desc: "normal resources",
Value: "configmaps,cronjobs,daemonsets,deployments",
Wanted: CollectorSet(map[string]struct{}{
Wanted: ResourceSet(map[string]struct{}{
"configmaps": {},
"cronjobs": {},
"daemonsets": {},
Expand All @@ -48,7 +48,7 @@ func TestCollectorSetSet(t *testing.T) {
}

for _, test := range tests {
cs := &CollectorSet{}
cs := &ResourceSet{}
gotError := cs.Set(test.Value)
if !(((gotError == nil && !test.WantedError) || (gotError != nil && test.WantedError)) && reflect.DeepEqual(*cs, test.Wanted)) {
t.Errorf("Test error for Desc: %s. Want: %+v. Got: %+v. Wanted Error: %v, Got Error: %v", test.Desc, test.Wanted, *cs, test.WantedError, gotError)
Expand Down