Skip to content

Commit 44831f7

Browse files
committed
Prevent multiple custom resource configurations for the same resource
1 parent 5b017f7 commit 44831f7

File tree

2 files changed

+12
-4
lines changed

2 files changed

+12
-4
lines changed

docs/customresourcestate-metrics.md

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,12 +9,14 @@ A YAML configuration file described below is required to define your custom reso
99

1010
Two flags can be used:
1111

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

1515
If both flags are provided, the inline configuration will take precedence.
16+
When multiple entries for the same resource exist, kube-state-metrics will exit with an error.
17+
This includes configuration which refers to a different API version.
1618

17-
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.
19+
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.
1820

1921
```yaml
2022
apiVersion: apps/v1

pkg/customresourcestate/config.go

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -162,8 +162,10 @@ type ConfigDecoder interface {
162162
}
163163

164164
// FromConfig decodes a configuration source into a slice of customresource.RegistryFactory that are ready to use.
165-
func FromConfig(decoder ConfigDecoder) (factories []customresource.RegistryFactory, err error) {
165+
func FromConfig(decoder ConfigDecoder) ([]customresource.RegistryFactory, error) {
166166
var crconfig Metrics
167+
var factories []customresource.RegistryFactory
168+
factoriesIndex := map[string]bool{}
167169
if err := decoder.Decode(&crconfig); err != nil {
168170
return nil, fmt.Errorf("failed to parse Custom Resource State metrics: %w", err)
169171
}
@@ -172,6 +174,10 @@ func FromConfig(decoder ConfigDecoder) (factories []customresource.RegistryFacto
172174
if err != nil {
173175
return nil, fmt.Errorf("failed to create metrics factory for %s: %w", resource.GroupVersionKind, err)
174176
}
177+
if _, ok := factoriesIndex[factory.Name()]; ok {
178+
return nil, fmt.Errorf("found multiple custom resource configurations for the same resource %s", factory.Name())
179+
}
180+
factoriesIndex[factory.Name()] = true
175181
factories = append(factories, factory)
176182
}
177183
return factories, nil

0 commit comments

Comments
 (0)