diff --git a/README.md b/README.md index 60f0199902..44cb24f309 100644 --- a/README.md +++ b/README.md @@ -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` @@ -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. @@ -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: @@ -268,7 +268,7 @@ spec: containers: - name: kube-state-metrics args: - - '--collectors=pods' + - '--resources=pods' - '--namespace=project1' ``` diff --git a/docs/cli-arguments.md b/docs/cli-arguments.md index 9995387ee5..1ca8b44ed4 100644 --- a/docs/cli-arguments.md +++ b/docs/cli-arguments.md @@ -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") @@ -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 diff --git a/docs/developer/guide.md b/docs/developer/guide.md index b728f7e6a0..c0295569a8 100644 --- a/docs/developer/guide.md +++ b/docs/developer/guide.md @@ -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 `.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. diff --git a/internal/store/builder.go b/internal/store/builder.go index 93174863cf..88e6cfdb4a 100644 --- a/internal/store/builder.go +++ b/internal/store/builder.go @@ -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) @@ -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 } @@ -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) diff --git a/main.go b/main.go index b9de39a7a4..a3728a2a6b 100644 --- a/main.go +++ b/main.go @@ -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 { diff --git a/main_test.go b/main_test.go index 023190445e..7a4b9f4853 100644 --- a/main_test.go +++ b/main_test.go @@ -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) @@ -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()) @@ -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) @@ -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) @@ -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) diff --git a/pkg/options/options.go b/pkg/options/options.go index 60ef382f6b..e8edeeb4d2 100644 --- a/pkg/options/options.go +++ b/pkg/options/options.go @@ -35,7 +35,7 @@ type Options struct { Host string TelemetryPort int TelemetryHost string - Collectors CollectorSet + Resources ResourceSet Namespaces NamespaceList Shard int32 TotalShards int @@ -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{}, } @@ -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.") diff --git a/pkg/options/options_test.go b/pkg/options/options_test.go index f0e63064e1..e6f0bf0a8c 100644 --- a/pkg/options/options_test.go +++ b/pkg/options/options_test.go @@ -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, }, { diff --git a/pkg/options/collector.go b/pkg/options/resource.go similarity index 94% rename from pkg/options/collector.go rename to pkg/options/resource.go index 97232214d0..09e0e0400c 100644 --- a/pkg/options/collector.go +++ b/pkg/options/resource.go @@ -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{}{}, diff --git a/pkg/options/types.go b/pkg/options/types.go index 914519f625..e153aed520 100644 --- a/pkg/options/types.go +++ b/pkg/options/types.go @@ -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) @@ -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" } diff --git a/pkg/options/types_test.go b/pkg/options/types_test.go index 53b3400a49..fb5eecbfc5 100644 --- a/pkg/options/types_test.go +++ b/pkg/options/types_test.go @@ -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": {}, @@ -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)