fix: deduplication of cr enabled resources
This commit is contained in:
parent
3d969c5ce9
commit
24c2194e7e
|
|
@ -20,7 +20,7 @@ import (
|
|||
"context"
|
||||
"fmt"
|
||||
"reflect"
|
||||
"sort"
|
||||
"slices"
|
||||
"strconv"
|
||||
"strings"
|
||||
"time"
|
||||
|
|
@ -113,12 +113,10 @@ func (b *Builder) WithEnabledResources(r []string) error {
|
|||
}
|
||||
}
|
||||
|
||||
var sortedResources []string
|
||||
sortedResources = append(sortedResources, r...)
|
||||
b.enabledResources = append(b.enabledResources, r...)
|
||||
slices.Sort(b.enabledResources)
|
||||
b.enabledResources = slices.Compact(b.enabledResources)
|
||||
|
||||
sort.Strings(sortedResources)
|
||||
|
||||
b.enabledResources = append(b.enabledResources, sortedResources...)
|
||||
return nil
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -18,6 +18,7 @@ package store
|
|||
|
||||
import (
|
||||
"reflect"
|
||||
"slices"
|
||||
"testing"
|
||||
|
||||
"k8s.io/kube-state-metrics/v2/pkg/options"
|
||||
|
|
@ -196,3 +197,56 @@ func TestWithAllowAnnotations(t *testing.T) {
|
|||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestWithEnabledResources(t *testing.T) {
|
||||
|
||||
tests := []struct {
|
||||
Desc string
|
||||
EnabledResources []string
|
||||
Wanted []string
|
||||
err expectedError
|
||||
}{
|
||||
{
|
||||
Desc: "sorts enabled resources",
|
||||
EnabledResources: []string{"pods", "cronjobs", "deployments"},
|
||||
Wanted: []string{
|
||||
"cronjobs",
|
||||
"deployments",
|
||||
"pods",
|
||||
},
|
||||
},
|
||||
{
|
||||
Desc: "de-duplicates enabled resources",
|
||||
EnabledResources: []string{"pods", "cronjobs", "deployments", "pods"},
|
||||
Wanted: []string{
|
||||
"cronjobs",
|
||||
"deployments",
|
||||
"pods",
|
||||
},
|
||||
},
|
||||
{
|
||||
Desc: "error if not exist",
|
||||
EnabledResources: []string{"pods", "cronjobs", "deployments", "foo"},
|
||||
Wanted: []string{},
|
||||
err: expectedError{
|
||||
expectedResourceError: true,
|
||||
},
|
||||
},
|
||||
}
|
||||
for _, test := range tests {
|
||||
b := NewBuilder()
|
||||
|
||||
// Set the enabled resources.
|
||||
err := b.WithEnabledResources(test.EnabledResources)
|
||||
if err != nil && !test.err.expectedResourceError {
|
||||
t.Log("Did not expect error while setting resources (--resources).")
|
||||
t.Errorf("Test error for Desc: %s. Got Error: %v", test.Desc, err)
|
||||
}
|
||||
|
||||
// Evaluate.
|
||||
if !slices.Equal(b.enabledResources, test.Wanted) && err == nil {
|
||||
t.Log("Expected enabled resources to be equal.")
|
||||
t.Errorf("Test error for Desc: %s\n Want: \n%+v\n Got: \n%#+v", test.Desc, test.Wanted, b.enabledResources)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in New Issue