Skip to content

Commit

Permalink
Prevent multiple custom resource configurations for the same resource
Browse files Browse the repository at this point in the history
  • Loading branch information
chrischdi committed Aug 24, 2022
1 parent 5b017f7 commit ad6a739
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 5 deletions.
10 changes: 6 additions & 4 deletions docs/customresourcestate-metrics.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,14 @@ A YAML configuration file described below is required to define your custom reso

Two flags can be used:

* `--custom-resource-state-config "inline yaml (see example)"` or
* `--custom-resource-state-config-file /path/to/config.yaml`
* `--custom-resource-state-config "inline yaml (see example)"` or
* `--custom-resource-state-config-file /path/to/config.yaml`

If both flags are provided, the inline configuration will take precedence.
Both flags can be provided multiple times to load multiple configurations.
When multiple entries for the same resource exist, kube-state-metrics will refuse to start.
This includes configuration which refers to a different API version.

In addition to specifying one of `--custom-resource-state-config*` flags, you should also add the custom resource *Kind*s in plural form to the list of exposed resources in the `--resources` flag. If you don't specify `--resources`, then all known custom resources configured in `--custom-resource-state-config-*` and all available default kubernetes objects will be taken into account by kube-state-metrics.
In addition to specifying one of `--custom-resource-state-config*` flags, you should also add the custom resource *Kind*s in plural form to the list of exposed resources in the `--resources` flag. If you don't specify `--resources`, then all known custom resources configured in `--custom-resource-state-config*` and all available default kubernetes objects will be taken into account by kube-state-metrics.

```yaml
apiVersion: apps/v1
Expand Down
8 changes: 7 additions & 1 deletion pkg/customresourcestate/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -162,8 +162,10 @@ type ConfigDecoder interface {
}

// FromConfig decodes a configuration source into a slice of customresource.RegistryFactory that are ready to use.
func FromConfig(decoder ConfigDecoder) (factories []customresource.RegistryFactory, err error) {
func FromConfig(decoder ConfigDecoder) ([]customresource.RegistryFactory, error) {
var crconfig Metrics
var factories []customresource.RegistryFactory
factoriesIndex := map[string]bool{}
if err := decoder.Decode(&crconfig); err != nil {
return nil, fmt.Errorf("failed to parse Custom Resource State metrics: %w", err)
}
Expand All @@ -172,6 +174,10 @@ func FromConfig(decoder ConfigDecoder) (factories []customresource.RegistryFacto
if err != nil {
return nil, fmt.Errorf("failed to create metrics factory for %s: %w", resource.GroupVersionKind, err)
}
if _, ok := factoriesIndex[factory.Name()]; ok {
return nil, fmt.Errorf("found multiple custom resource configurations for the same resource %s", factory.Name())
}
factoriesIndex[factory.Name()] = true
factories = append(factories, factory)
}
return factories, nil
Expand Down

0 comments on commit ad6a739

Please sign in to comment.