From 6f7be35ad1ce56ba5d77291eaa6c84e3ef6bdf1c Mon Sep 17 00:00:00 2001 From: David Murphy Date: Sun, 14 May 2023 01:58:17 +0100 Subject: [PATCH 1/2] remove duplicates from resources list --- pkg/app/server.go | 17 +++++++++-------- pkg/util/collections/set.go | 37 +++++++++++++++++++++++++++++++++++++ 2 files changed, 46 insertions(+), 8 deletions(-) create mode 100644 pkg/util/collections/set.go diff --git a/pkg/app/server.go b/pkg/app/server.go index 51d3837fdb..8cc7d0a89a 100644 --- a/pkg/app/server.go +++ b/pkg/app/server.go @@ -53,6 +53,7 @@ import ( "k8s.io/kube-state-metrics/v2/pkg/metricshandler" "k8s.io/kube-state-metrics/v2/pkg/optin" "k8s.io/kube-state-metrics/v2/pkg/options" + "k8s.io/kube-state-metrics/v2/pkg/util/collections" "k8s.io/kube-state-metrics/v2/pkg/util/proc" ) @@ -173,25 +174,25 @@ func RunKubeStateMetrics(ctx context.Context, opts *options.Options) error { } - resources := make([]string, len(factories)) + resources := collections.NewSet[string]() - for i, factory := range factories { - resources[i] = factory.Name() + for _, factory := range factories { + resources.Add(factory.Name()) } switch { case len(opts.Resources) == 0 && !opts.CustomResourcesOnly: - resources = append(resources, options.DefaultResources.AsSlice()...) + resources.Add(options.DefaultResources.AsSlice()...) klog.InfoS("Used default resources") case opts.CustomResourcesOnly: // enable custom resource only - klog.InfoS("Used CRD resources only", "resources", resources) + klog.InfoS("Used CRD resources only", "resources", resources.String()) default: - resources = append(resources, opts.Resources.AsSlice()...) - klog.InfoS("Used resources", "resources", resources) + resources.Add(opts.Resources.AsSlice()...) + klog.InfoS("Used resources", "resources", resources.String()) } - if err := storeBuilder.WithEnabledResources(resources); err != nil { + if err := storeBuilder.WithEnabledResources(resources.AsSlice()); err != nil { return fmt.Errorf("failed to set up resources: %v", err) } diff --git a/pkg/util/collections/set.go b/pkg/util/collections/set.go new file mode 100644 index 0000000000..18f6219260 --- /dev/null +++ b/pkg/util/collections/set.go @@ -0,0 +1,37 @@ +package collections + +import ( + "fmt" + "sort" + "strings" +) + +type Set[T comparable] map[T]struct{} + +func NewSet[T comparable](values ...T) Set[T] { + s := make(Set[T], len(values)) + return s +} + +func (s Set[T]) Add(values ...T) { + for _, val := range values { + s[val] = struct{}{} + } +} + +func (s Set[T]) AsSlice() []T { + slice := make([]T, 0, len(s)) + for val := range s { + slice = append(slice, val) + } + return slice +} + +func (s *Set[T]) String() string { + ss := make([]string, 0, len(*s)) + for val := range *s { + ss = append(ss, fmt.Sprint(val)) + } + sort.Strings(ss) + return strings.Join(ss, ",") +} From 51c8eed25b62e03204f5ad89cc0fddf04d5e3482 Mon Sep 17 00:00:00 2001 From: David Murphy Date: Thu, 11 Jul 2024 15:23:09 +0100 Subject: [PATCH 2/2] update to use k8s.io/apimachinery/pkg/util/sets --- pkg/app/server.go | 16 ++++++++-------- pkg/util/collections/set.go | 37 ------------------------------------- 2 files changed, 8 insertions(+), 45 deletions(-) delete mode 100644 pkg/util/collections/set.go diff --git a/pkg/app/server.go b/pkg/app/server.go index 8cc7d0a89a..e9c97c8c42 100644 --- a/pkg/app/server.go +++ b/pkg/app/server.go @@ -44,6 +44,7 @@ import ( _ "k8s.io/client-go/plugin/pkg/client/auth" // Initialize common client auth plugins. "k8s.io/client-go/tools/clientcmd" "k8s.io/klog/v2" + "k8s.io/apimachinery/pkg/util/sets" "k8s.io/kube-state-metrics/v2/internal/store" "k8s.io/kube-state-metrics/v2/pkg/allowdenylist" @@ -53,7 +54,6 @@ import ( "k8s.io/kube-state-metrics/v2/pkg/metricshandler" "k8s.io/kube-state-metrics/v2/pkg/optin" "k8s.io/kube-state-metrics/v2/pkg/options" - "k8s.io/kube-state-metrics/v2/pkg/util/collections" "k8s.io/kube-state-metrics/v2/pkg/util/proc" ) @@ -174,25 +174,25 @@ func RunKubeStateMetrics(ctx context.Context, opts *options.Options) error { } - resources := collections.NewSet[string]() + resources := sets.New[string]() for _, factory := range factories { - resources.Add(factory.Name()) + resources.Insert(factory.Name()) } switch { case len(opts.Resources) == 0 && !opts.CustomResourcesOnly: - resources.Add(options.DefaultResources.AsSlice()...) + resources.Insert(options.DefaultResources.AsSlice()...) klog.InfoS("Used default resources") case opts.CustomResourcesOnly: // enable custom resource only - klog.InfoS("Used CRD resources only", "resources", resources.String()) + klog.InfoS("Used CRD resources only", "resources", resources) default: - resources.Add(opts.Resources.AsSlice()...) - klog.InfoS("Used resources", "resources", resources.String()) + resources.Insert(opts.Resources.AsSlice()...) + klog.InfoS("Used resources", "resources", resources) } - if err := storeBuilder.WithEnabledResources(resources.AsSlice()); err != nil { + if err := storeBuilder.WithEnabledResources(resources.UnsortedList()); err != nil { return fmt.Errorf("failed to set up resources: %v", err) } diff --git a/pkg/util/collections/set.go b/pkg/util/collections/set.go deleted file mode 100644 index 18f6219260..0000000000 --- a/pkg/util/collections/set.go +++ /dev/null @@ -1,37 +0,0 @@ -package collections - -import ( - "fmt" - "sort" - "strings" -) - -type Set[T comparable] map[T]struct{} - -func NewSet[T comparable](values ...T) Set[T] { - s := make(Set[T], len(values)) - return s -} - -func (s Set[T]) Add(values ...T) { - for _, val := range values { - s[val] = struct{}{} - } -} - -func (s Set[T]) AsSlice() []T { - slice := make([]T, 0, len(s)) - for val := range s { - slice = append(slice, val) - } - return slice -} - -func (s *Set[T]) String() string { - ss := make([]string, 0, len(*s)) - for val := range *s { - ss = append(ss, fmt.Sprint(val)) - } - sort.Strings(ss) - return strings.Join(ss, ",") -}