Skip to content

Commit

Permalink
Merge pull request kubernetes#1810 from chrischdi/pr-custom-resource-…
Browse files Browse the repository at this point in the history
…flag-repeat

Prevent definition of same gvk in custom resource configuration
  • Loading branch information
k8s-ci-robot authored Aug 24, 2022
2 parents 5b017f7 + 44831f7 commit 4c141d5
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 4 deletions.
8 changes: 5 additions & 3 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.
When multiple entries for the same resource exist, kube-state-metrics will exit with an error.
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 4c141d5

Please sign in to comment.