mirror of
https://github.com/Telecominfraproject/wlan-cloud-kube-state-metrics.git
synced 2025-11-01 19:17:54 +00:00
Add CompositeFamilyGeneratorFilter struct for combining multiple filters
This commit is contained in:
6
main.go
6
main.go
@@ -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)
|
||||
|
||||
|
||||
10
main_test.go
10
main_test.go
@@ -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{})
|
||||
|
||||
@@ -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"
|
||||
)
|
||||
|
||||
|
||||
@@ -18,6 +18,7 @@ package builder
|
||||
|
||||
import (
|
||||
"context"
|
||||
|
||||
generator "k8s.io/kube-state-metrics/v2/pkg/metric_generator"
|
||||
|
||||
"github.com/prometheus/client_golang/prometheus"
|
||||
|
||||
@@ -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)
|
||||
|
||||
|
||||
@@ -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}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user