mirror of
https://github.com/Telecominfraproject/wlan-cloud-kube-state-metrics.git
synced 2025-11-01 19:17:54 +00:00
Add FamilyGeneratorFilter interface and use it for the allow- / denylist
This commit is contained in:
@@ -57,21 +57,21 @@ var _ ksmtypes.BuilderInterface = &Builder{}
|
|||||||
// Builder helps to build store. It follows the builder pattern
|
// Builder helps to build store. It follows the builder pattern
|
||||||
// (https://en.wikipedia.org/wiki/Builder_pattern).
|
// (https://en.wikipedia.org/wiki/Builder_pattern).
|
||||||
type Builder struct {
|
type Builder struct {
|
||||||
kubeClient clientset.Interface
|
kubeClient clientset.Interface
|
||||||
vpaClient vpaclientset.Interface
|
vpaClient vpaclientset.Interface
|
||||||
namespaces options.NamespaceList
|
namespaces options.NamespaceList
|
||||||
namespaceFilter string
|
namespaceFilter string
|
||||||
ctx context.Context
|
ctx context.Context
|
||||||
enabledResources []string
|
enabledResources []string
|
||||||
allowDenyList ksmtypes.AllowDenyLister
|
familyGeneratorFilter generator.FamilyGeneratorFilter
|
||||||
listWatchMetrics *watch.ListWatchMetrics
|
listWatchMetrics *watch.ListWatchMetrics
|
||||||
shardingMetrics *sharding.Metrics
|
shardingMetrics *sharding.Metrics
|
||||||
shard int32
|
shard int32
|
||||||
totalShards int
|
totalShards int
|
||||||
buildStoresFunc ksmtypes.BuildStoresFunc
|
buildStoresFunc ksmtypes.BuildStoresFunc
|
||||||
allowAnnotationsList map[string][]string
|
allowAnnotationsList map[string][]string
|
||||||
allowLabelsList map[string][]string
|
allowLabelsList map[string][]string
|
||||||
useAPIServerCache bool
|
useAPIServerCache bool
|
||||||
}
|
}
|
||||||
|
|
||||||
// NewBuilder returns a new builder.
|
// NewBuilder returns a new builder.
|
||||||
@@ -134,10 +134,10 @@ func (b *Builder) WithVPAClient(c vpaclientset.Interface) {
|
|||||||
b.vpaClient = c
|
b.vpaClient = c
|
||||||
}
|
}
|
||||||
|
|
||||||
// WithAllowDenyList configures the allow or denylisted metric to be exposed
|
// WithFamilyGeneratorFilter configures the family generator filter which decides which
|
||||||
// by the store build by the Builder.
|
// metrics are to be exposed by the store build by the Builder.
|
||||||
func (b *Builder) WithAllowDenyList(l ksmtypes.AllowDenyLister) {
|
func (b *Builder) WithFamilyGeneratorFilter(l generator.FamilyGeneratorFilter) {
|
||||||
b.allowDenyList = l
|
b.familyGeneratorFilter = l
|
||||||
}
|
}
|
||||||
|
|
||||||
// WithGenerateStoresFunc configures a custom generate store function
|
// WithGenerateStoresFunc configures a custom generate store function
|
||||||
@@ -169,8 +169,8 @@ func (b *Builder) WithAllowLabels(labels map[string][]string) {
|
|||||||
// It returns metrics writers which can be used to write out
|
// It returns metrics writers which can be used to write out
|
||||||
// metrics from the stores.
|
// metrics from the stores.
|
||||||
func (b *Builder) Build() []metricsstore.MetricsWriter {
|
func (b *Builder) Build() []metricsstore.MetricsWriter {
|
||||||
if b.allowDenyList == nil {
|
if b.familyGeneratorFilter == nil {
|
||||||
panic("allowDenyList should not be nil")
|
panic("familyGeneratorFilter should not be nil")
|
||||||
}
|
}
|
||||||
|
|
||||||
var metricsWriters []metricsstore.MetricsWriter
|
var metricsWriters []metricsstore.MetricsWriter
|
||||||
@@ -198,8 +198,8 @@ func (b *Builder) Build() []metricsstore.MetricsWriter {
|
|||||||
// It returns metric stores which can be used to consume
|
// It returns metric stores which can be used to consume
|
||||||
// the generated metrics from the stores.
|
// the generated metrics from the stores.
|
||||||
func (b *Builder) BuildStores() [][]cache.Store {
|
func (b *Builder) BuildStores() [][]cache.Store {
|
||||||
if b.allowDenyList == nil {
|
if b.familyGeneratorFilter == nil {
|
||||||
panic("allowDenyList should not be nil")
|
panic("familyGeneratorFilter should not be nil")
|
||||||
}
|
}
|
||||||
|
|
||||||
var allStores [][]cache.Store
|
var allStores [][]cache.Store
|
||||||
@@ -386,7 +386,7 @@ func (b *Builder) buildStores(
|
|||||||
listWatchFunc func(kubeClient clientset.Interface, ns string, fieldSelector string) cache.ListerWatcher,
|
listWatchFunc func(kubeClient clientset.Interface, ns string, fieldSelector string) cache.ListerWatcher,
|
||||||
useAPIServerCache bool,
|
useAPIServerCache bool,
|
||||||
) []cache.Store {
|
) []cache.Store {
|
||||||
metricFamilies = generator.FilterMetricFamilies(b.allowDenyList, metricFamilies)
|
metricFamilies = generator.FilterFamilyGenerators(b.familyGeneratorFilter, metricFamilies)
|
||||||
composedMetricGenFuncs := generator.ComposeMetricGenFuncs(metricFamilies)
|
composedMetricGenFuncs := generator.ComposeMetricGenFuncs(metricFamilies)
|
||||||
familyHeaders := generator.ExtractMetricFamilyHeaders(metricFamilies)
|
familyHeaders := generator.ExtractMetricFamilyHeaders(metricFamilies)
|
||||||
|
|
||||||
|
|||||||
2
main.go
2
main.go
@@ -130,7 +130,7 @@ func main() {
|
|||||||
|
|
||||||
klog.Infof("metric allow-denylisting: %v", allowDenyList.Status())
|
klog.Infof("metric allow-denylisting: %v", allowDenyList.Status())
|
||||||
|
|
||||||
storeBuilder.WithAllowDenyList(allowDenyList)
|
storeBuilder.WithFamilyGeneratorFilter(allowDenyList)
|
||||||
|
|
||||||
storeBuilder.WithGenerateStoresFunc(storeBuilder.DefaultGenerateStoresFunc(), opts.UseAPIServerCache)
|
storeBuilder.WithGenerateStoresFunc(storeBuilder.DefaultGenerateStoresFunc(), opts.UseAPIServerCache)
|
||||||
|
|
||||||
|
|||||||
11
main_test.go
11
main_test.go
@@ -69,11 +69,12 @@ func BenchmarkKubeStateMetrics(b *testing.B) {
|
|||||||
builder.WithNamespaces(options.DefaultNamespaces, "")
|
builder.WithNamespaces(options.DefaultNamespaces, "")
|
||||||
builder.WithGenerateStoresFunc(builder.DefaultGenerateStoresFunc(), false)
|
builder.WithGenerateStoresFunc(builder.DefaultGenerateStoresFunc(), false)
|
||||||
|
|
||||||
|
// TODO: replace with a generic family generator filter which composes both the AllowDenyList and OptInList
|
||||||
l, err := allowdenylist.New(map[string]struct{}{}, map[string]struct{}{})
|
l, err := allowdenylist.New(map[string]struct{}{}, map[string]struct{}{})
|
||||||
if err != nil {
|
if err != nil {
|
||||||
b.Fatal(err)
|
b.Fatal(err)
|
||||||
}
|
}
|
||||||
builder.WithAllowDenyList(l)
|
builder.WithFamilyGeneratorFilter(l)
|
||||||
|
|
||||||
builder.WithAllowAnnotations(map[string][]string{})
|
builder.WithAllowAnnotations(map[string][]string{})
|
||||||
builder.WithAllowLabels(map[string][]string{})
|
builder.WithAllowLabels(map[string][]string{})
|
||||||
@@ -139,7 +140,7 @@ func TestFullScrapeCycle(t *testing.T) {
|
|||||||
if err != nil {
|
if err != nil {
|
||||||
t.Fatal(err)
|
t.Fatal(err)
|
||||||
}
|
}
|
||||||
builder.WithAllowDenyList(l)
|
builder.WithFamilyGeneratorFilter(l)
|
||||||
builder.WithAllowLabels(map[string][]string{
|
builder.WithAllowLabels(map[string][]string{
|
||||||
"kube_pod_labels": {
|
"kube_pod_labels": {
|
||||||
"namespace",
|
"namespace",
|
||||||
@@ -400,7 +401,7 @@ func TestShardingEquivalenceScrapeCycle(t *testing.T) {
|
|||||||
unshardedBuilder.WithEnabledResources(options.DefaultResources.AsSlice())
|
unshardedBuilder.WithEnabledResources(options.DefaultResources.AsSlice())
|
||||||
unshardedBuilder.WithKubeClient(kubeClient)
|
unshardedBuilder.WithKubeClient(kubeClient)
|
||||||
unshardedBuilder.WithNamespaces(options.DefaultNamespaces, "")
|
unshardedBuilder.WithNamespaces(options.DefaultNamespaces, "")
|
||||||
unshardedBuilder.WithAllowDenyList(l)
|
unshardedBuilder.WithFamilyGeneratorFilter(l)
|
||||||
unshardedBuilder.WithAllowLabels(map[string][]string{})
|
unshardedBuilder.WithAllowLabels(map[string][]string{})
|
||||||
unshardedBuilder.WithGenerateStoresFunc(unshardedBuilder.DefaultGenerateStoresFunc(), false)
|
unshardedBuilder.WithGenerateStoresFunc(unshardedBuilder.DefaultGenerateStoresFunc(), false)
|
||||||
|
|
||||||
@@ -413,7 +414,7 @@ func TestShardingEquivalenceScrapeCycle(t *testing.T) {
|
|||||||
shardedBuilder1.WithEnabledResources(options.DefaultResources.AsSlice())
|
shardedBuilder1.WithEnabledResources(options.DefaultResources.AsSlice())
|
||||||
shardedBuilder1.WithKubeClient(kubeClient)
|
shardedBuilder1.WithKubeClient(kubeClient)
|
||||||
shardedBuilder1.WithNamespaces(options.DefaultNamespaces, "")
|
shardedBuilder1.WithNamespaces(options.DefaultNamespaces, "")
|
||||||
shardedBuilder1.WithAllowDenyList(l)
|
shardedBuilder1.WithFamilyGeneratorFilter(l)
|
||||||
shardedBuilder1.WithAllowLabels(map[string][]string{})
|
shardedBuilder1.WithAllowLabels(map[string][]string{})
|
||||||
shardedBuilder1.WithGenerateStoresFunc(shardedBuilder1.DefaultGenerateStoresFunc(), false)
|
shardedBuilder1.WithGenerateStoresFunc(shardedBuilder1.DefaultGenerateStoresFunc(), false)
|
||||||
|
|
||||||
@@ -426,7 +427,7 @@ func TestShardingEquivalenceScrapeCycle(t *testing.T) {
|
|||||||
shardedBuilder2.WithEnabledResources(options.DefaultResources.AsSlice())
|
shardedBuilder2.WithEnabledResources(options.DefaultResources.AsSlice())
|
||||||
shardedBuilder2.WithKubeClient(kubeClient)
|
shardedBuilder2.WithKubeClient(kubeClient)
|
||||||
shardedBuilder2.WithNamespaces(options.DefaultNamespaces, "")
|
shardedBuilder2.WithNamespaces(options.DefaultNamespaces, "")
|
||||||
shardedBuilder2.WithAllowDenyList(l)
|
shardedBuilder2.WithFamilyGeneratorFilter(l)
|
||||||
shardedBuilder2.WithAllowLabels(map[string][]string{})
|
shardedBuilder2.WithAllowLabels(map[string][]string{})
|
||||||
shardedBuilder2.WithGenerateStoresFunc(shardedBuilder2.DefaultGenerateStoresFunc(), false)
|
shardedBuilder2.WithGenerateStoresFunc(shardedBuilder2.DefaultGenerateStoresFunc(), false)
|
||||||
|
|
||||||
|
|||||||
@@ -17,6 +17,7 @@ limitations under the License.
|
|||||||
package allowdenylist
|
package allowdenylist
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
generator "k8s.io/kube-state-metrics/v2/pkg/metric_generator"
|
||||||
"regexp"
|
"regexp"
|
||||||
"strings"
|
"strings"
|
||||||
|
|
||||||
@@ -134,6 +135,11 @@ func (l *AllowDenyList) Status() string {
|
|||||||
return "Excluding the following lists that were on denylist: " + strings.Join(items, ", ")
|
return "Excluding the following lists that were on denylist: " + strings.Join(items, ", ")
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Test returns if the given family generator passes (is included in) the AllowDenyList
|
||||||
|
func (l *AllowDenyList) Test(generator generator.FamilyGenerator) bool {
|
||||||
|
return l.IsIncluded(generator.Name)
|
||||||
|
}
|
||||||
|
|
||||||
func copyList(l map[string]struct{}) map[string]struct{} {
|
func copyList(l map[string]struct{}) map[string]struct{} {
|
||||||
newList := map[string]struct{}{}
|
newList := map[string]struct{}{}
|
||||||
for k, v := range l {
|
for k, v := range l {
|
||||||
|
|||||||
@@ -18,6 +18,7 @@ package builder
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
|
generator "k8s.io/kube-state-metrics/v2/pkg/metric_generator"
|
||||||
|
|
||||||
"github.com/prometheus/client_golang/prometheus"
|
"github.com/prometheus/client_golang/prometheus"
|
||||||
vpaclientset "k8s.io/autoscaler/vertical-pod-autoscaler/pkg/client/clientset/versioned"
|
vpaclientset "k8s.io/autoscaler/vertical-pod-autoscaler/pkg/client/clientset/versioned"
|
||||||
@@ -79,10 +80,10 @@ func (b *Builder) WithVPAClient(c vpaclientset.Interface) {
|
|||||||
b.internal.WithVPAClient(c)
|
b.internal.WithVPAClient(c)
|
||||||
}
|
}
|
||||||
|
|
||||||
// WithAllowDenyList configures the allow or denylisted metric to be exposed
|
// WithFamilyGeneratorFilter configures the family generator filter which decides which
|
||||||
// by the store build by the Builder.
|
// metrics are to be exposed by the store build by the Builder.
|
||||||
func (b *Builder) WithAllowDenyList(l ksmtypes.AllowDenyLister) {
|
func (b *Builder) WithFamilyGeneratorFilter(l generator.FamilyGeneratorFilter) {
|
||||||
b.internal.WithAllowDenyList(l)
|
b.internal.WithFamilyGeneratorFilter(l)
|
||||||
}
|
}
|
||||||
|
|
||||||
// WithAllowLabels configures which labels can be returned for metrics
|
// WithAllowLabels configures which labels can be returned for metrics
|
||||||
|
|||||||
@@ -40,7 +40,8 @@ var (
|
|||||||
// This test case ensures we don't break compatibility for external consumers.
|
// This test case ensures we don't break compatibility for external consumers.
|
||||||
func TestBuilderWithCustomStore(t *testing.T) {
|
func TestBuilderWithCustomStore(t *testing.T) {
|
||||||
b := builder.NewBuilder()
|
b := builder.NewBuilder()
|
||||||
b.WithAllowDenyList(&allowdenylist.AllowDenyList{})
|
// TODO: replace with a generic family generator filter which composes both the AllowDenyList and OptInList
|
||||||
|
b.WithFamilyGeneratorFilter(&allowdenylist.AllowDenyList{})
|
||||||
b.WithEnabledResources([]string{"pods"})
|
b.WithEnabledResources([]string{"pods"})
|
||||||
b.WithGenerateStoresFunc(customStore)
|
b.WithGenerateStoresFunc(customStore)
|
||||||
|
|
||||||
|
|||||||
@@ -39,7 +39,7 @@ type BuilderInterface interface {
|
|||||||
WithContext(ctx context.Context)
|
WithContext(ctx context.Context)
|
||||||
WithKubeClient(c clientset.Interface)
|
WithKubeClient(c clientset.Interface)
|
||||||
WithVPAClient(c vpaclientset.Interface)
|
WithVPAClient(c vpaclientset.Interface)
|
||||||
WithAllowDenyList(l AllowDenyLister)
|
WithFamilyGeneratorFilter(l generator.FamilyGeneratorFilter)
|
||||||
WithAllowLabels(l map[string][]string)
|
WithAllowLabels(l map[string][]string)
|
||||||
WithGenerateStoresFunc(f BuildStoresFunc, useAPIServerCache bool)
|
WithGenerateStoresFunc(f BuildStoresFunc, useAPIServerCache bool)
|
||||||
DefaultGenerateStoresFunc() BuildStoresFunc
|
DefaultGenerateStoresFunc() BuildStoresFunc
|
||||||
|
|||||||
40
pkg/metric_generator/filter.go
Normal file
40
pkg/metric_generator/filter.go
Normal file
@@ -0,0 +1,40 @@
|
|||||||
|
/*
|
||||||
|
Copyright 2018 The Kubernetes Authors All rights reserved.
|
||||||
|
|
||||||
|
Licensed under the Apache License, Version 2.0 (the "License");
|
||||||
|
you may not use this file except in compliance with the License.
|
||||||
|
You may obtain a copy of the License at
|
||||||
|
|
||||||
|
http://www.apache.org/licenses/LICENSE-2.0
|
||||||
|
|
||||||
|
Unless required by applicable law or agreed to in writing, software
|
||||||
|
distributed under the License is distributed on an "AS IS" BASIS,
|
||||||
|
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||||
|
See the License for the specific language governing permissions and
|
||||||
|
limitations under the License.
|
||||||
|
*/
|
||||||
|
|
||||||
|
package generator
|
||||||
|
|
||||||
|
// FamilyGeneratorFilter represents a filter which decides whether a metric
|
||||||
|
// family is exposed by the store or not
|
||||||
|
type FamilyGeneratorFilter interface {
|
||||||
|
|
||||||
|
// Test returns true if it passes the criteria of the filter, otherwise it
|
||||||
|
// will return false and the metric family is not exposed by the store
|
||||||
|
Test(generator FamilyGenerator) bool
|
||||||
|
}
|
||||||
|
|
||||||
|
// FilterFamilyGenerators filters a given slice of family generators based upon a given filter
|
||||||
|
// and returns a slice containing the family generators which passed the filter criteria
|
||||||
|
func FilterFamilyGenerators(filter FamilyGeneratorFilter, families []FamilyGenerator) []FamilyGenerator {
|
||||||
|
var filtered []FamilyGenerator
|
||||||
|
|
||||||
|
for _, family := range families {
|
||||||
|
if filter.Test(family) {
|
||||||
|
filtered = append(filtered, family)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return filtered
|
||||||
|
}
|
||||||
@@ -101,22 +101,3 @@ func ComposeMetricGenFuncs(familyGens []FamilyGenerator) func(obj interface{}) [
|
|||||||
return families
|
return families
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
type allowDenyLister interface {
|
|
||||||
IsIncluded(string) bool
|
|
||||||
IsExcluded(string) bool
|
|
||||||
}
|
|
||||||
|
|
||||||
// FilterMetricFamilies takes a allow- and a denylist and a slice of metric
|
|
||||||
// families and returns a filtered slice.
|
|
||||||
func FilterMetricFamilies(l allowDenyLister, families []FamilyGenerator) []FamilyGenerator {
|
|
||||||
filtered := []FamilyGenerator{}
|
|
||||||
|
|
||||||
for _, f := range families {
|
|
||||||
if l.IsIncluded(f.Name) {
|
|
||||||
filtered = append(filtered, f)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
return filtered
|
|
||||||
}
|
|
||||||
|
|||||||
Reference in New Issue
Block a user