Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: duplicate custom resources #2066

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 9 additions & 8 deletions pkg/app/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we use sets here instead?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@murphd40 any progress? :)

"k8s.io/kube-state-metrics/v2/pkg/util/proc"
)

Expand Down Expand Up @@ -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)
}

Expand Down
37 changes: 37 additions & 0 deletions pkg/util/collections/set.go
Original file line number Diff line number Diff line change
@@ -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, ",")
}