Add CompositeFamilyGeneratorFilter struct for combining multiple filters

This commit is contained in:
Julian van den Berkmortel
2021-11-16 08:52:07 +01:00
parent d767e99c98
commit f78c8eb1c6
6 changed files with 38 additions and 9 deletions

View File

@@ -26,6 +26,8 @@ import (
"strconv"
"time"
generator "k8s.io/kube-state-metrics/v2/pkg/metric_generator"
"github.com/oklog/run"
"github.com/pkg/errors"
"github.com/prometheus/client_golang/prometheus"
@@ -130,7 +132,9 @@ func main() {
klog.Infof("metric allow-denylisting: %v", allowDenyList.Status())
storeBuilder.WithFamilyGeneratorFilter(allowDenyList)
storeBuilder.WithFamilyGeneratorFilter(generator.NewCompositeFamilyGeneratorFilter(
allowDenyList,
))
storeBuilder.WithGenerateStoresFunc(storeBuilder.DefaultGenerateStoresFunc(), opts.UseAPIServerCache)

View File

@@ -28,6 +28,8 @@ import (
"testing"
"time"
generator "k8s.io/kube-state-metrics/v2/pkg/metric_generator"
"github.com/prometheus/client_golang/prometheus"
v1 "k8s.io/api/core/v1"
"k8s.io/apimachinery/pkg/api/resource"
@@ -69,12 +71,14 @@ func BenchmarkKubeStateMetrics(b *testing.B) {
builder.WithNamespaces(options.DefaultNamespaces, "")
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{}{})
allowDenyListFilter, err := allowdenylist.New(map[string]struct{}{}, map[string]struct{}{})
if err != nil {
b.Fatal(err)
}
builder.WithFamilyGeneratorFilter(l)
builder.WithFamilyGeneratorFilter(generator.NewCompositeFamilyGeneratorFilter(
allowDenyListFilter,
))
builder.WithAllowAnnotations(map[string][]string{})
builder.WithAllowLabels(map[string][]string{})

View File

@@ -17,10 +17,11 @@ limitations under the License.
package allowdenylist
import (
generator "k8s.io/kube-state-metrics/v2/pkg/metric_generator"
"regexp"
"strings"
generator "k8s.io/kube-state-metrics/v2/pkg/metric_generator"
"github.com/pkg/errors"
)

View File

@@ -18,6 +18,7 @@ package builder
import (
"context"
generator "k8s.io/kube-state-metrics/v2/pkg/metric_generator"
"github.com/prometheus/client_golang/prometheus"

View File

@@ -23,7 +23,6 @@ import (
clientset "k8s.io/client-go/kubernetes"
"k8s.io/client-go/tools/cache"
"k8s.io/kube-state-metrics/v2/pkg/allowdenylist"
"k8s.io/kube-state-metrics/v2/pkg/builder"
generator "k8s.io/kube-state-metrics/v2/pkg/metric_generator"
)
@@ -40,8 +39,7 @@ var (
// This test case ensures we don't break compatibility for external consumers.
func TestBuilderWithCustomStore(t *testing.T) {
b := builder.NewBuilder()
// TODO: replace with a generic family generator filter which composes both the AllowDenyList and OptInList
b.WithFamilyGeneratorFilter(&allowdenylist.AllowDenyList{})
b.WithFamilyGeneratorFilter(generator.NewCompositeFamilyGeneratorFilter())
b.WithEnabledResources([]string{"pods"})
b.WithGenerateStoresFunc(customStore)

View File

@@ -25,6 +25,22 @@ type FamilyGeneratorFilter interface {
Test(generator FamilyGenerator) bool
}
// CompositeFamilyGeneratorFilter is composite for combining multiple filters
type CompositeFamilyGeneratorFilter struct {
filters []FamilyGeneratorFilter
}
// Test tests the generator by passing it through the filters contained within the composite
// and return false if the generator does not match all the filters
func (composite CompositeFamilyGeneratorFilter) Test(generator FamilyGenerator) bool {
for _, filter := range composite.filters {
if !filter.Test(generator) {
return false
}
}
return true
}
// 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 {
@@ -37,4 +53,9 @@ func FilterFamilyGenerators(filter FamilyGeneratorFilter, families []FamilyGener
}
return filtered
}
}
// NewCompositeFamilyGeneratorFilter combines multiple family generators filters into one composite filter
func NewCompositeFamilyGeneratorFilter(filters ...FamilyGeneratorFilter) CompositeFamilyGeneratorFilter {
return CompositeFamilyGeneratorFilter{filters}
}