Handle singular labels in allowlist

Handle singular labels in allowlist failing when such a label is
supplied, in order to keep the behaviour in sync with --resources.

Signed-off-by: Pranshu Srivastava <rexagod@gmail.com>
This commit is contained in:
Pranshu Srivastava 2022-09-03 02:17:28 +05:30
parent 734389481a
commit 052f572ae7
No known key found for this signature in database
GPG Key ID: 3CDBD4D4CEF89B86
3 changed files with 11 additions and 3 deletions

View File

@ -200,10 +200,16 @@ func (b *Builder) WithAllowAnnotations(annotations map[string][]string) {
}
// WithAllowLabels configures which labels can be returned for metrics
func (b *Builder) WithAllowLabels(labels map[string][]string) {
func (b *Builder) WithAllowLabels(labels map[string][]string) error {
if len(labels) > 0 {
for label := range labels {
if !resourceExists(label) {
return fmt.Errorf("resource %s does not exist. Available resources: %s", label, strings.Join(availableResources(), ","))
}
}
b.allowLabelsList = labels
}
return nil
}
// Build initializes and registers all enabled stores.

View File

@ -149,7 +149,9 @@ func RunKubeStateMetrics(ctx context.Context, opts *options.Options, factories .
storeBuilder.WithCustomResourceClients(customResourceClients)
storeBuilder.WithSharding(opts.Shard, opts.TotalShards)
storeBuilder.WithAllowAnnotations(opts.AnnotationsAllowList)
storeBuilder.WithAllowLabels(opts.LabelsAllowList)
if err := storeBuilder.WithAllowLabels(opts.LabelsAllowList); err != nil {
return fmt.Errorf("failed to set up labels allowlist: %v", err)
}
ksmMetricsRegistry.MustRegister(
collectors.NewProcessCollector(collectors.ProcessCollectorOpts{}),

View File

@ -44,7 +44,7 @@ type BuilderInterface interface {
WithUsingAPIServerCache(u bool)
WithFamilyGeneratorFilter(l generator.FamilyGeneratorFilter)
WithAllowAnnotations(a map[string][]string)
WithAllowLabels(l map[string][]string)
WithAllowLabels(l map[string][]string) error
WithGenerateStoresFunc(f BuildStoresFunc)
WithGenerateCustomResourceStoresFunc(f BuildCustomResourceStoresFunc)
DefaultGenerateStoresFunc() BuildStoresFunc