Skip to content

Commit

Permalink
Merge pull request #793 from monzo/fix-default-available-collectors
Browse files Browse the repository at this point in the history
builder/main: allow collectors not enabled by default
  • Loading branch information
k8s-ci-robot authored Jun 18, 2019
2 parents 4ee79c5 + e1b45de commit 82ab559
Show file tree
Hide file tree
Showing 5 changed files with 29 additions and 18 deletions.
3 changes: 0 additions & 3 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -74,9 +74,7 @@ github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UV
golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2 h1:VklqNMn3ovrHsnt90PveolxSbWFaJdECFbxSq0Mqo2M=
golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w=
golang.org/x/net v0.0.0-20180724234803-3673e40ba225/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4=
golang.org/x/net v0.0.0-20180906233101-161cd47e91fd h1:nTDtHvHSdCn1m6ITfMRqtOd/9+7a3s8RBNOZ3eYZzJA=
golang.org/x/net v0.0.0-20180906233101-161cd47e91fd/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4=
golang.org/x/net v0.0.0-20181201002055-351d144fa1fc h1:a3CU5tJYVj92DY2LaA1kUkrsqD5/3mLDhx2NcNqyW+0=
golang.org/x/net v0.0.0-20181201002055-351d144fa1fc/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4=
golang.org/x/net v0.0.0-20190311183353-d8887717615a h1:oWX7TPOiFAMXLq8o0ikBYfCJVlRHBcsciT5bXOrH628=
golang.org/x/net v0.0.0-20190311183353-d8887717615a/go.mod h1:t9HGtf8HONx5eT2rtn7q6eTqICYqUVnKs3thJo3Qplg=
Expand All @@ -86,7 +84,6 @@ golang.org/x/sync v0.0.0-20180314180146-1d60e4601c6f/go.mod h1:RxMgew5VJxzue5/jJ
golang.org/x/sync v0.0.0-20181108010431-42b317875d0f/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
golang.org/x/sync v0.0.0-20190423024810-112230192c58 h1:8gQV6CLnAEikrhgkHFbMAEhagSSnXWGV915qUMm9mrU=
golang.org/x/sync v0.0.0-20190423024810-112230192c58/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
golang.org/x/sys v0.0.0-20180909124046-d0be0721c37e h1:o3PsSEY8E4eXWkXrIP9YJALUkVZqzHJT5DOasTyn8Vs=
golang.org/x/sys v0.0.0-20180909124046-d0be0721c37e/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a h1:1BGLXjeY4akVXGgbC9HugT3Jv3hCI0z56oJR5vAMgBU=
golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
Expand Down
23 changes: 22 additions & 1 deletion internal/store/builder.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import (
"sort"
"strings"

"github.com/pkg/errors"
"k8s.io/klog"

"golang.org/x/net/context"
Expand Down Expand Up @@ -64,13 +65,20 @@ func NewBuilder(
}

// WithEnabledResources sets the enabledResources property of a Builder.
func (b *Builder) WithEnabledResources(c []string) {
func (b *Builder) WithEnabledResources(c []string) error {
for _, col := range c {
if !collectorExists(col) {
return errors.Errorf("collector %s does not exist. Available collectors: %s", col, strings.Join(availableCollectors(), ","))
}
}

var copy []string
copy = append(copy, c...)

sort.Strings(copy)

b.enabledResources = copy
return nil
}

// WithNamespaces sets the namespaces property of a Builder.
Expand Down Expand Up @@ -138,6 +146,19 @@ var availableStores = map[string]func(f *Builder) *metricsstore.MetricsStore{
"storageclasses": func(b *Builder) *metricsstore.MetricsStore { return b.buildStorageClassStore() },
}

func collectorExists(name string) bool {
_, ok := availableStores[name]
return ok
}

func availableCollectors() []string {
c := []string{}
for name := range availableStores {
c = append(c, name)
}
return c
}

func (b *Builder) buildConfigMapStore() *metricsstore.MetricsStore {
return b.buildStore(configMapMetricFamilies, &v1.ConfigMap{}, createConfigMapListWatch)
}
Expand Down
9 changes: 7 additions & 2 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -81,12 +81,17 @@ func main() {

storeBuilder := store.NewBuilder(ctx)

var collectors []string
if len(opts.Collectors) == 0 {
klog.Info("Using default collectors")
storeBuilder.WithEnabledResources(options.DefaultCollectors.AsSlice())
collectors = options.DefaultCollectors.AsSlice()
} else {
klog.Infof("Using collectors %s", opts.Collectors.String())
storeBuilder.WithEnabledResources(opts.Collectors.AsSlice())
collectors = opts.Collectors.AsSlice()
}

if err := storeBuilder.WithEnabledResources(collectors); err != nil {
klog.Fatalf("Failed to set up collectors: %v", err)
}

if len(opts.Namespaces) == 0 {
Expand Down
6 changes: 0 additions & 6 deletions pkg/options/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,6 @@ import (
"sort"
"strings"

"github.com/pkg/errors"

metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
)

Expand Down Expand Up @@ -84,10 +82,6 @@ func (c *CollectorSet) Set(value string) error {
for _, col := range cols {
col = strings.TrimSpace(col)
if len(col) != 0 {
_, ok := DefaultCollectors[col]
if !ok {
return errors.Errorf("collector \"%s\" does not exist", col)
}
s[col] = struct{}{}
}
}
Expand Down
6 changes: 0 additions & 6 deletions pkg/options/types_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,12 +45,6 @@ func TestCollectorSetSet(t *testing.T) {
}),
WantedError: false,
},
{
Desc: "none exist collectors",
Value: "none-exists",
Wanted: CollectorSet{},
WantedError: true,
},
}

for _, test := range tests {
Expand Down

0 comments on commit 82ab559

Please sign in to comment.