@@ -57,18 +57,19 @@ var _ ksmtypes.BuilderInterface = &Builder{}
5757// Builder helps to build store. It follows the builder pattern
5858// (https://en.wikipedia.org/wiki/Builder_pattern).
5959type Builder struct {
60- kubeClient clientset.Interface
61- vpaClient vpaclientset.Interface
62- namespaces options.NamespaceList
63- ctx context.Context
64- enabledResources []string
65- allowDenyList ksmtypes.AllowDenyLister
66- listWatchMetrics * watch.ListWatchMetrics
67- shardingMetrics * sharding.Metrics
68- shard int32
69- totalShards int
70- buildStoresFunc ksmtypes.BuildStoresFunc
71- allowLabelsList map [string ][]string
60+ kubeClient clientset.Interface
61+ vpaClient vpaclientset.Interface
62+ namespaces options.NamespaceList
63+ ctx context.Context
64+ enabledResources []string
65+ allowDenyList ksmtypes.AllowDenyLister
66+ listWatchMetrics * watch.ListWatchMetrics
67+ shardingMetrics * sharding.Metrics
68+ shard int32
69+ totalShards int
70+ buildStoresFunc ksmtypes.BuildStoresFunc
71+ allowLabelsList map [string ][]string
72+ useAPIServerCache bool
7273}
7374
7475// NewBuilder returns a new builder.
@@ -137,8 +138,9 @@ func (b *Builder) WithAllowDenyList(l ksmtypes.AllowDenyLister) {
137138}
138139
139140// WithGenerateStoresFunc configures a custom generate store function
140- func (b * Builder ) WithGenerateStoresFunc (f ksmtypes.BuildStoresFunc ) {
141+ func (b * Builder ) WithGenerateStoresFunc (f ksmtypes.BuildStoresFunc , u bool ) {
141142 b .buildStoresFunc = f
143+ b .useAPIServerCache = u
142144}
143145
144146// DefaultGenerateStoresFunc returns default buildStores function
@@ -228,125 +230,126 @@ func availableResources() []string {
228230}
229231
230232func (b * Builder ) buildConfigMapStores () []* metricsstore.MetricsStore {
231- return b .buildStoresFunc (configMapMetricFamilies , & v1.ConfigMap {}, createConfigMapListWatch )
233+ return b .buildStoresFunc (configMapMetricFamilies , & v1.ConfigMap {}, createConfigMapListWatch , b . useAPIServerCache )
232234}
233235
234236func (b * Builder ) buildCronJobStores () []* metricsstore.MetricsStore {
235- return b .buildStoresFunc (cronJobMetricFamilies (b .allowLabelsList ["cronjobs" ]), & batchv1beta1.CronJob {}, createCronJobListWatch )
237+ return b .buildStoresFunc (cronJobMetricFamilies (b .allowLabelsList ["cronjobs" ]), & batchv1beta1.CronJob {}, createCronJobListWatch , b . useAPIServerCache )
236238}
237239
238240func (b * Builder ) buildDaemonSetStores () []* metricsstore.MetricsStore {
239- return b .buildStoresFunc (daemonSetMetricFamilies (b .allowLabelsList ["daemonsets" ]), & appsv1.DaemonSet {}, createDaemonSetListWatch )
241+ return b .buildStoresFunc (daemonSetMetricFamilies (b .allowLabelsList ["daemonsets" ]), & appsv1.DaemonSet {}, createDaemonSetListWatch , b . useAPIServerCache )
240242}
241243
242244func (b * Builder ) buildDeploymentStores () []* metricsstore.MetricsStore {
243- return b .buildStoresFunc (deploymentMetricFamilies (b .allowLabelsList ["deployments" ]), & appsv1.Deployment {}, createDeploymentListWatch )
245+ return b .buildStoresFunc (deploymentMetricFamilies (b .allowLabelsList ["deployments" ]), & appsv1.Deployment {}, createDeploymentListWatch , b . useAPIServerCache )
244246}
245247
246248func (b * Builder ) buildEndpointsStores () []* metricsstore.MetricsStore {
247- return b .buildStoresFunc (endpointMetricFamilies (b .allowLabelsList ["endpoints" ]), & v1.Endpoints {}, createEndpointsListWatch )
249+ return b .buildStoresFunc (endpointMetricFamilies (b .allowLabelsList ["endpoints" ]), & v1.Endpoints {}, createEndpointsListWatch , b . useAPIServerCache )
248250}
249251
250252func (b * Builder ) buildHPAStores () []* metricsstore.MetricsStore {
251- return b .buildStoresFunc (hpaMetricFamilies (b .allowLabelsList ["horizontalpodautoscalers" ]), & autoscaling.HorizontalPodAutoscaler {}, createHPAListWatch )
253+ return b .buildStoresFunc (hpaMetricFamilies (b .allowLabelsList ["horizontalpodautoscalers" ]), & autoscaling.HorizontalPodAutoscaler {}, createHPAListWatch , b . useAPIServerCache )
252254}
253255
254256func (b * Builder ) buildIngressStores () []* metricsstore.MetricsStore {
255- return b .buildStoresFunc (ingressMetricFamilies (b .allowLabelsList ["ingresses" ]), & networkingv1.Ingress {}, createIngressListWatch )
257+ return b .buildStoresFunc (ingressMetricFamilies (b .allowLabelsList ["ingresses" ]), & networkingv1.Ingress {}, createIngressListWatch , b . useAPIServerCache )
256258}
257259
258260func (b * Builder ) buildJobStores () []* metricsstore.MetricsStore {
259- return b .buildStoresFunc (jobMetricFamilies (b .allowLabelsList ["jobs" ]), & batchv1.Job {}, createJobListWatch )
261+ return b .buildStoresFunc (jobMetricFamilies (b .allowLabelsList ["jobs" ]), & batchv1.Job {}, createJobListWatch , b . useAPIServerCache )
260262}
261263
262264func (b * Builder ) buildLimitRangeStores () []* metricsstore.MetricsStore {
263- return b .buildStoresFunc (limitRangeMetricFamilies , & v1.LimitRange {}, createLimitRangeListWatch )
265+ return b .buildStoresFunc (limitRangeMetricFamilies , & v1.LimitRange {}, createLimitRangeListWatch , b . useAPIServerCache )
264266}
265267
266268func (b * Builder ) buildMutatingWebhookConfigurationStores () []* metricsstore.MetricsStore {
267- return b .buildStoresFunc (mutatingWebhookConfigurationMetricFamilies , & admissionregistrationv1.MutatingWebhookConfiguration {}, createMutatingWebhookConfigurationListWatch )
269+ return b .buildStoresFunc (mutatingWebhookConfigurationMetricFamilies , & admissionregistrationv1.MutatingWebhookConfiguration {}, createMutatingWebhookConfigurationListWatch , b . useAPIServerCache )
268270}
269271
270272func (b * Builder ) buildNamespaceStores () []* metricsstore.MetricsStore {
271- return b .buildStoresFunc (namespaceMetricFamilies (b .allowLabelsList ["namespaces" ]), & v1.Namespace {}, createNamespaceListWatch )
273+ return b .buildStoresFunc (namespaceMetricFamilies (b .allowLabelsList ["namespaces" ]), & v1.Namespace {}, createNamespaceListWatch , b . useAPIServerCache )
272274}
273275
274276func (b * Builder ) buildNetworkPolicyStores () []* metricsstore.MetricsStore {
275- return b .buildStoresFunc (networkPolicyMetricFamilies (b .allowLabelsList ["networkpolicies" ]), & networkingv1.NetworkPolicy {}, createNetworkPolicyListWatch )
277+ return b .buildStoresFunc (networkPolicyMetricFamilies (b .allowLabelsList ["networkpolicies" ]), & networkingv1.NetworkPolicy {}, createNetworkPolicyListWatch , b . useAPIServerCache )
276278}
277279
278280func (b * Builder ) buildNodeStores () []* metricsstore.MetricsStore {
279- return b .buildStoresFunc (nodeMetricFamilies (b .allowLabelsList ["nodes" ]), & v1.Node {}, createNodeListWatch )
281+ return b .buildStoresFunc (nodeMetricFamilies (b .allowLabelsList ["nodes" ]), & v1.Node {}, createNodeListWatch , b . useAPIServerCache )
280282}
281283
282284func (b * Builder ) buildPersistentVolumeClaimStores () []* metricsstore.MetricsStore {
283- return b .buildStoresFunc (persistentVolumeClaimMetricFamilies (b .allowLabelsList ["persistentvolumeclaims" ]), & v1.PersistentVolumeClaim {}, createPersistentVolumeClaimListWatch )
285+ return b .buildStoresFunc (persistentVolumeClaimMetricFamilies (b .allowLabelsList ["persistentvolumeclaims" ]), & v1.PersistentVolumeClaim {}, createPersistentVolumeClaimListWatch , b . useAPIServerCache )
284286}
285287
286288func (b * Builder ) buildPersistentVolumeStores () []* metricsstore.MetricsStore {
287- return b .buildStoresFunc (persistentVolumeMetricFamilies (b .allowLabelsList ["persistentvolumes" ]), & v1.PersistentVolume {}, createPersistentVolumeListWatch )
289+ return b .buildStoresFunc (persistentVolumeMetricFamilies (b .allowLabelsList ["persistentvolumes" ]), & v1.PersistentVolume {}, createPersistentVolumeListWatch , b . useAPIServerCache )
288290}
289291
290292func (b * Builder ) buildPodDisruptionBudgetStores () []* metricsstore.MetricsStore {
291- return b .buildStoresFunc (podDisruptionBudgetMetricFamilies , & policy.PodDisruptionBudget {}, createPodDisruptionBudgetListWatch )
293+ return b .buildStoresFunc (podDisruptionBudgetMetricFamilies , & policy.PodDisruptionBudget {}, createPodDisruptionBudgetListWatch , b . useAPIServerCache )
292294}
293295
294296func (b * Builder ) buildReplicaSetStores () []* metricsstore.MetricsStore {
295- return b .buildStoresFunc (replicaSetMetricFamilies (b .allowLabelsList ["replicasets" ]), & appsv1.ReplicaSet {}, createReplicaSetListWatch )
297+ return b .buildStoresFunc (replicaSetMetricFamilies (b .allowLabelsList ["replicasets" ]), & appsv1.ReplicaSet {}, createReplicaSetListWatch , b . useAPIServerCache )
296298}
297299
298300func (b * Builder ) buildReplicationControllerStores () []* metricsstore.MetricsStore {
299- return b .buildStoresFunc (replicationControllerMetricFamilies , & v1.ReplicationController {}, createReplicationControllerListWatch )
301+ return b .buildStoresFunc (replicationControllerMetricFamilies , & v1.ReplicationController {}, createReplicationControllerListWatch , b . useAPIServerCache )
300302}
301303
302304func (b * Builder ) buildResourceQuotaStores () []* metricsstore.MetricsStore {
303- return b .buildStoresFunc (resourceQuotaMetricFamilies , & v1.ResourceQuota {}, createResourceQuotaListWatch )
305+ return b .buildStoresFunc (resourceQuotaMetricFamilies , & v1.ResourceQuota {}, createResourceQuotaListWatch , b . useAPIServerCache )
304306}
305307
306308func (b * Builder ) buildSecretStores () []* metricsstore.MetricsStore {
307- return b .buildStoresFunc (secretMetricFamilies (b .allowLabelsList ["secrets" ]), & v1.Secret {}, createSecretListWatch )
309+ return b .buildStoresFunc (secretMetricFamilies (b .allowLabelsList ["secrets" ]), & v1.Secret {}, createSecretListWatch , b . useAPIServerCache )
308310}
309311
310312func (b * Builder ) buildServiceStores () []* metricsstore.MetricsStore {
311- return b .buildStoresFunc (serviceMetricFamilies (b .allowLabelsList ["services" ]), & v1.Service {}, createServiceListWatch )
313+ return b .buildStoresFunc (serviceMetricFamilies (b .allowLabelsList ["services" ]), & v1.Service {}, createServiceListWatch , b . useAPIServerCache )
312314}
313315
314316func (b * Builder ) buildStatefulSetStores () []* metricsstore.MetricsStore {
315- return b .buildStoresFunc (statefulSetMetricFamilies (b .allowLabelsList ["statefulsets" ]), & appsv1.StatefulSet {}, createStatefulSetListWatch )
317+ return b .buildStoresFunc (statefulSetMetricFamilies (b .allowLabelsList ["statefulsets" ]), & appsv1.StatefulSet {}, createStatefulSetListWatch , b . useAPIServerCache )
316318}
317319
318320func (b * Builder ) buildStorageClassStores () []* metricsstore.MetricsStore {
319- return b .buildStoresFunc (storageClassMetricFamilies (b .allowLabelsList ["storageclasses" ]), & storagev1.StorageClass {}, createStorageClassListWatch )
321+ return b .buildStoresFunc (storageClassMetricFamilies (b .allowLabelsList ["storageclasses" ]), & storagev1.StorageClass {}, createStorageClassListWatch , b . useAPIServerCache )
320322}
321323
322324func (b * Builder ) buildPodStores () []* metricsstore.MetricsStore {
323- return b .buildStoresFunc (podMetricFamilies (b .allowLabelsList ["pods" ]), & v1.Pod {}, createPodListWatch )
325+ return b .buildStoresFunc (podMetricFamilies (b .allowLabelsList ["pods" ]), & v1.Pod {}, createPodListWatch , b . useAPIServerCache )
324326}
325327
326328func (b * Builder ) buildCsrStores () []* metricsstore.MetricsStore {
327- return b .buildStoresFunc (csrMetricFamilies (b .allowLabelsList ["certificatesigningrequests" ]), & certv1.CertificateSigningRequest {}, createCSRListWatch )
329+ return b .buildStoresFunc (csrMetricFamilies (b .allowLabelsList ["certificatesigningrequests" ]), & certv1.CertificateSigningRequest {}, createCSRListWatch , b . useAPIServerCache )
328330}
329331
330332func (b * Builder ) buildValidatingWebhookConfigurationStores () []* metricsstore.MetricsStore {
331- return b .buildStoresFunc (validatingWebhookConfigurationMetricFamilies , & admissionregistrationv1.ValidatingWebhookConfiguration {}, createValidatingWebhookConfigurationListWatch )
333+ return b .buildStoresFunc (validatingWebhookConfigurationMetricFamilies , & admissionregistrationv1.ValidatingWebhookConfiguration {}, createValidatingWebhookConfigurationListWatch , b . useAPIServerCache )
332334}
333335
334336func (b * Builder ) buildVolumeAttachmentStores () []* metricsstore.MetricsStore {
335- return b .buildStoresFunc (volumeAttachmentMetricFamilies , & storagev1.VolumeAttachment {}, createVolumeAttachmentListWatch )
337+ return b .buildStoresFunc (volumeAttachmentMetricFamilies , & storagev1.VolumeAttachment {}, createVolumeAttachmentListWatch , b . useAPIServerCache )
336338}
337339
338340func (b * Builder ) buildVPAStores () []* metricsstore.MetricsStore {
339- return b .buildStoresFunc (vpaMetricFamilies (b .allowLabelsList ["verticalpodautoscalers" ]), & vpaautoscaling.VerticalPodAutoscaler {}, createVPAListWatchFunc (b .vpaClient ))
341+ return b .buildStoresFunc (vpaMetricFamilies (b .allowLabelsList ["verticalpodautoscalers" ]), & vpaautoscaling.VerticalPodAutoscaler {}, createVPAListWatchFunc (b .vpaClient ), b . useAPIServerCache )
340342}
341343
342344func (b * Builder ) buildLeasesStores () []* metricsstore.MetricsStore {
343- return b .buildStoresFunc (leaseMetricFamilies , & coordinationv1.Lease {}, createLeaseListWatch )
345+ return b .buildStoresFunc (leaseMetricFamilies , & coordinationv1.Lease {}, createLeaseListWatch , b . useAPIServerCache )
344346}
345347
346348func (b * Builder ) buildStores (
347349 metricFamilies []generator.FamilyGenerator ,
348350 expectedType interface {},
349351 listWatchFunc func (kubeClient clientset.Interface , ns string ) cache.ListerWatcher ,
352+ useAPIServerCache bool ,
350353) []* metricsstore.MetricsStore {
351354 metricFamilies = generator .FilterMetricFamilies (b .allowDenyList , metricFamilies )
352355 composedMetricGenFuncs := generator .ComposeMetricGenFuncs (metricFamilies )
@@ -358,7 +361,7 @@ func (b *Builder) buildStores(
358361 composedMetricGenFuncs ,
359362 )
360363 listWatcher := listWatchFunc (b .kubeClient , v1 .NamespaceAll )
361- b .startReflector (expectedType , store , listWatcher )
364+ b .startReflector (expectedType , store , listWatcher , useAPIServerCache )
362365 return []* metricsstore.MetricsStore {store }
363366 }
364367
@@ -369,7 +372,7 @@ func (b *Builder) buildStores(
369372 composedMetricGenFuncs ,
370373 )
371374 listWatcher := listWatchFunc (b .kubeClient , ns )
372- b .startReflector (expectedType , store , listWatcher )
375+ b .startReflector (expectedType , store , listWatcher , useAPIServerCache )
373376 stores = append (stores , store )
374377 }
375378
@@ -382,8 +385,9 @@ func (b *Builder) startReflector(
382385 expectedType interface {},
383386 store cache.Store ,
384387 listWatcher cache.ListerWatcher ,
388+ useAPIServerCache bool ,
385389) {
386- instrumentedListWatch := watch .NewInstrumentedListerWatcher (listWatcher , b .listWatchMetrics , reflect .TypeOf (expectedType ).String ())
390+ instrumentedListWatch := watch .NewInstrumentedListerWatcher (listWatcher , b .listWatchMetrics , reflect .TypeOf (expectedType ).String (), useAPIServerCache )
387391 reflector := cache .NewReflector (sharding .NewShardedListWatch (b .shard , b .totalShards , instrumentedListWatch ), expectedType , store , 0 )
388392 go reflector .Run (b .ctx .Done ())
389393}
0 commit comments