Skip to content

Commit 052f572

Browse files
committed
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 <[email protected]>
1 parent 7343894 commit 052f572

File tree

3 files changed

+11
-3
lines changed

3 files changed

+11
-3
lines changed

internal/store/builder.go

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -200,10 +200,16 @@ func (b *Builder) WithAllowAnnotations(annotations map[string][]string) {
200200
}
201201

202202
// WithAllowLabels configures which labels can be returned for metrics
203-
func (b *Builder) WithAllowLabels(labels map[string][]string) {
203+
func (b *Builder) WithAllowLabels(labels map[string][]string) error {
204204
if len(labels) > 0 {
205+
for label := range labels {
206+
if !resourceExists(label) {
207+
return fmt.Errorf("resource %s does not exist. Available resources: %s", label, strings.Join(availableResources(), ","))
208+
}
209+
}
205210
b.allowLabelsList = labels
206211
}
212+
return nil
207213
}
208214

209215
// Build initializes and registers all enabled stores.

pkg/app/server.go

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -149,7 +149,9 @@ func RunKubeStateMetrics(ctx context.Context, opts *options.Options, factories .
149149
storeBuilder.WithCustomResourceClients(customResourceClients)
150150
storeBuilder.WithSharding(opts.Shard, opts.TotalShards)
151151
storeBuilder.WithAllowAnnotations(opts.AnnotationsAllowList)
152-
storeBuilder.WithAllowLabels(opts.LabelsAllowList)
152+
if err := storeBuilder.WithAllowLabels(opts.LabelsAllowList); err != nil {
153+
return fmt.Errorf("failed to set up labels allowlist: %v", err)
154+
}
153155

154156
ksmMetricsRegistry.MustRegister(
155157
collectors.NewProcessCollector(collectors.ProcessCollectorOpts{}),

pkg/builder/types/interfaces.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ type BuilderInterface interface {
4444
WithUsingAPIServerCache(u bool)
4545
WithFamilyGeneratorFilter(l generator.FamilyGeneratorFilter)
4646
WithAllowAnnotations(a map[string][]string)
47-
WithAllowLabels(l map[string][]string)
47+
WithAllowLabels(l map[string][]string) error
4848
WithGenerateStoresFunc(f BuildStoresFunc)
4949
WithGenerateCustomResourceStoresFunc(f BuildCustomResourceStoresFunc)
5050
DefaultGenerateStoresFunc() BuildStoresFunc

0 commit comments

Comments
 (0)