diff --git a/internal/store/builder.go b/internal/store/builder.go index b8d3c47c..c47701ef 100644 --- a/internal/store/builder.go +++ b/internal/store/builder.go @@ -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. diff --git a/pkg/app/server.go b/pkg/app/server.go index a4a20234..1cb05fe3 100644 --- a/pkg/app/server.go +++ b/pkg/app/server.go @@ -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{}), diff --git a/pkg/builder/types/interfaces.go b/pkg/builder/types/interfaces.go index 21b657a2..ea37e941 100644 --- a/pkg/builder/types/interfaces.go +++ b/pkg/builder/types/interfaces.go @@ -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