@@ -38,7 +38,9 @@ import (
38
38
policyv1 "k8s.io/api/policy/v1"
39
39
rbacv1 "k8s.io/api/rbac/v1"
40
40
storagev1 "k8s.io/api/storage/v1"
41
+ metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
41
42
clientset "k8s.io/client-go/kubernetes"
43
+ "k8s.io/client-go/metadata"
42
44
"k8s.io/client-go/tools/cache"
43
45
"k8s.io/klog/v2"
44
46
@@ -65,9 +67,10 @@ var _ ksmtypes.BuilderInterface = &Builder{}
65
67
// Builder helps to build store. It follows the builder pattern
66
68
// (https://en.wikipedia.org/wiki/Builder_pattern).
67
69
type Builder struct {
68
- kubeClient clientset.Interface
69
- customResourceClients map [string ]interface {}
70
- namespaces options.NamespaceList
70
+ kubeClient clientset.Interface
71
+ metadataOnlyKubeClient metadata.Interface
72
+ customResourceClients map [string ]interface {}
73
+ namespaces options.NamespaceList
71
74
// namespaceFilter is inside fieldSelectorFilter
72
75
fieldSelectorFilter string
73
76
ctx context.Context
@@ -78,6 +81,7 @@ type Builder struct {
78
81
shard int32
79
82
totalShards int
80
83
buildStoresFunc ksmtypes.BuildStoresFunc
84
+ buildMetadataOnlyStoresFunc ksmtypes.BuildMetadataOnlyStoresFunc
81
85
buildCustomResourceStoresFunc ksmtypes.BuildCustomResourceStoresFunc
82
86
allowAnnotationsList map [string ][]string
83
87
allowLabelsList map [string ][]string
@@ -157,6 +161,11 @@ func (b *Builder) WithKubeClient(c clientset.Interface) {
157
161
b .kubeClient = c
158
162
}
159
163
164
+ // WithMetadataOnlyKubeClient sets the metadataOnlyKubeClient property of a Builder.
165
+ func (b * Builder ) WithMetadataOnlyKubeClient (c metadata.Interface ) {
166
+ b .metadataOnlyKubeClient = c
167
+ }
168
+
160
169
// WithCustomResourceClients sets the customResourceClients property of a Builder.
161
170
func (b * Builder ) WithCustomResourceClients (cs map [string ]interface {}) {
162
171
b .customResourceClients = cs
@@ -178,6 +187,11 @@ func (b *Builder) WithGenerateStoresFunc(f ksmtypes.BuildStoresFunc) {
178
187
b .buildStoresFunc = f
179
188
}
180
189
190
+ // WithGenerateMetadataOnlyStoresFunc configures a custom generate custom resource store function
191
+ func (b * Builder ) WithGenerateMetadataOnlyStoresFunc (f ksmtypes.BuildMetadataOnlyStoresFunc ) {
192
+ b .buildMetadataOnlyStoresFunc = f
193
+ }
194
+
181
195
// WithGenerateCustomResourceStoresFunc configures a custom generate custom resource store function
182
196
func (b * Builder ) WithGenerateCustomResourceStoresFunc (f ksmtypes.BuildCustomResourceStoresFunc ) {
183
197
b .buildCustomResourceStoresFunc = f
@@ -188,6 +202,11 @@ func (b *Builder) DefaultGenerateStoresFunc() ksmtypes.BuildStoresFunc {
188
202
return b .buildStores
189
203
}
190
204
205
+ // DefaultGenerateMetadataOnlyStoresFunc returns default buildStores function
206
+ func (b * Builder ) DefaultGenerateMetadataOnlyStoresFunc () ksmtypes.BuildMetadataOnlyStoresFunc {
207
+ return b .buildMetadataOnlyStores
208
+ }
209
+
191
210
// DefaultGenerateCustomResourceStoresFunc returns default buildCustomResourceStores function
192
211
func (b * Builder ) DefaultGenerateCustomResourceStoresFunc () ksmtypes.BuildCustomResourceStoresFunc {
193
212
return b .buildCustomResourceStores
@@ -362,7 +381,7 @@ func availableResources() []string {
362
381
}
363
382
364
383
func (b * Builder ) buildConfigMapStores () []cache.Store {
365
- return b .buildStoresFunc (configMapMetricFamilies (b .allowAnnotationsList ["configmaps" ], b .allowLabelsList ["configmaps" ]), & v1. ConfigMap {}, createConfigMapListWatch , b .useAPIServerCache )
384
+ return b .buildMetadataOnlyStoresFunc (configMapMetricFamilies (b .allowAnnotationsList ["configmaps" ], b .allowLabelsList ["configmaps" ]), & metav1. PartialObjectMetadata {}, createConfigMapListWatch , b .useAPIServerCache )
366
385
}
367
386
368
387
func (b * Builder ) buildCronJobStores () []cache.Store {
@@ -519,7 +538,8 @@ func (b *Builder) buildStores(
519
538
if b .fieldSelectorFilter != "" {
520
539
klog .InfoS ("FieldSelector is used" , "fieldSelector" , b .fieldSelectorFilter )
521
540
}
522
- listWatcher := listWatchFunc (b .kubeClient , v1 .NamespaceAll , b .fieldSelectorFilter )
541
+ kubeClient := b .kubeClient
542
+ listWatcher := listWatchFunc (kubeClient , v1 .NamespaceAll , b .fieldSelectorFilter )
523
543
b .startReflector (expectedType , store , listWatcher , useAPIServerCache )
524
544
return []cache.Store {store }
525
545
}
@@ -541,6 +561,46 @@ func (b *Builder) buildStores(
541
561
return stores
542
562
}
543
563
564
+ func (b * Builder ) buildMetadataOnlyStores (
565
+ metricFamilies []generator.FamilyGenerator ,
566
+ expectedType interface {},
567
+ listWatchFunc func (kubeClient metadata.Interface , ns string , fieldSelector string ) cache.ListerWatcher ,
568
+ useAPIServerCache bool ,
569
+ ) []cache.Store {
570
+ metricFamilies = generator .FilterFamilyGenerators (b .familyGeneratorFilter , metricFamilies )
571
+ composedMetricGenFuncs := generator .ComposeMetricGenFuncs (metricFamilies )
572
+ familyHeaders := generator .ExtractMetricFamilyHeaders (metricFamilies )
573
+
574
+ if b .namespaces .IsAllNamespaces () {
575
+ store := metricsstore .NewMetricsStore (
576
+ familyHeaders ,
577
+ composedMetricGenFuncs ,
578
+ )
579
+ if b .fieldSelectorFilter != "" {
580
+ klog .InfoS ("FieldSelector is used" , "fieldSelector" , b .fieldSelectorFilter )
581
+ }
582
+ listWatcher := listWatchFunc (b .metadataOnlyKubeClient , v1 .NamespaceAll , b .fieldSelectorFilter )
583
+ b .startReflector (expectedType , store , listWatcher , useAPIServerCache )
584
+ return []cache.Store {store }
585
+ }
586
+
587
+ stores := make ([]cache.Store , 0 , len (b .namespaces ))
588
+ for _ , ns := range b .namespaces {
589
+ store := metricsstore .NewMetricsStore (
590
+ familyHeaders ,
591
+ composedMetricGenFuncs ,
592
+ )
593
+ if b .fieldSelectorFilter != "" {
594
+ klog .InfoS ("FieldSelector is used" , "fieldSelector" , b .fieldSelectorFilter )
595
+ }
596
+ listWatcher := listWatchFunc (b .metadataOnlyKubeClient , ns , b .fieldSelectorFilter )
597
+ b .startReflector (expectedType , store , listWatcher , useAPIServerCache )
598
+ stores = append (stores , store )
599
+ }
600
+
601
+ return stores
602
+ }
603
+
544
604
// TODO(Garrybest): Merge `buildStores` and `buildCustomResourceStores`
545
605
func (b * Builder ) buildCustomResourceStores (resourceName string ,
546
606
metricFamilies []generator.FamilyGenerator ,
0 commit comments