Skip to content

Commit f9d29ba

Browse files
Refactoring
1 parent d523f8e commit f9d29ba

File tree

7 files changed

+47
-40
lines changed

7 files changed

+47
-40
lines changed

docs/cli-arguments.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ Usage of ./kube-state-metrics:
4646
--metric-opt-in-list string Comma-separated list of metrics which are opt-in and not enabled by default. This is in addition to the metric allow- and denylists
4747
--namespaces string Comma-separated list of namespaces to be enabled. Defaults to ""
4848
--namespaces-denylist string Comma-separated list of namespaces not to be enabled. If namespaces and namespaces-denylist are both set, only namespaces that are excluded in namespaces-denylist will be used.
49-
--nodename string Set spec.nodeName=nodename when watching resources. Only available for resources which support nodeName filter.
49+
--nodename string Name of the node that contains the kube-state-metrics pod, only available for resources (pod metrics) that support spec.nodeName fieldSelector. Each kube-state-metrics pod will only exposes metrics related to this node.
5050
--one_output If true, only write logs to their native severity level (vs also writing to each lower severity level; no effect when -logtostderr=true)
5151
--pod string Name of the pod that contains the kube-state-metrics container. When set, it is expected that --pod and --pod-namespace are both set. Most likely this should be passed via the downward API. This is used for auto-detecting sharding. If set, this has preference over statically configured sharding. This is experimental, it may be removed without notice.
5252
--pod-namespace string Name of the namespace of the pod specified by --pod. When set, it is expected that --pod and --pod-namespace are both set. Most likely this should be passed via the downward API. This is used for auto-detecting sharding. If set, this has preference over statically configured sharding. This is experimental, it may be removed without notice.

internal/store/builder.go

Lines changed: 12 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -58,10 +58,11 @@ var _ ksmtypes.BuilderInterface = &Builder{}
5858
// Builder helps to build store. It follows the builder pattern
5959
// (https://en.wikipedia.org/wiki/Builder_pattern).
6060
type Builder struct {
61-
kubeClient clientset.Interface
62-
customResourceClients map[string]interface{}
63-
vpaClient vpaclientset.Interface
64-
namespaces options.NamespaceList
61+
kubeClient clientset.Interface
62+
customResourceClients map[string]interface{}
63+
vpaClient vpaclientset.Interface
64+
namespaces options.NamespaceList
65+
// namespaceFilter is inside fieldSelectorFilter
6566
fieldSelectorFilter string
6667
ctx context.Context
6768
enabledResources []string
@@ -116,17 +117,9 @@ func (b *Builder) WithNamespaces(n options.NamespaceList) {
116117
b.namespaces = n
117118
}
118119

119-
// MergeFieldSelector merges multiple fieldSelectors using AND operator.
120-
func (b *Builder) MergeFieldSelector(selectors []string) (string, error) {
121-
var err error
122-
merged := options.EmptyFieldSelector()
123-
for _, s := range selectors {
124-
merged, err = options.MergeFieldSelector(merged, s)
125-
if err != nil {
126-
return "", err
127-
}
128-
}
129-
return merged, nil
120+
// MergeFieldSelectors merges multiple fieldSelectors using AND operator.
121+
func (b *Builder) MergeFieldSelectors(selectors []string) (string, error) {
122+
return options.MergeFieldSelectors(selectors)
130123
}
131124

132125
// WithSharding sets the shard and totalShards property of a Builder.
@@ -485,7 +478,7 @@ func (b *Builder) buildStores(
485478
composedMetricGenFuncs,
486479
)
487480
if b.fieldSelectorFilter != "" {
488-
klog.Infof("FieldSelector is used ", b.fieldSelectorFilter)
481+
klog.Infof("FieldSelector is used %s", b.fieldSelectorFilter)
489482
}
490483
listWatcher := listWatchFunc(b.kubeClient, v1.NamespaceAll, b.fieldSelectorFilter)
491484
b.startReflector(expectedType, store, listWatcher, useAPIServerCache)
@@ -499,7 +492,7 @@ func (b *Builder) buildStores(
499492
composedMetricGenFuncs,
500493
)
501494
if b.fieldSelectorFilter != "" {
502-
klog.Infof("FieldSelector is used ", b.fieldSelectorFilter)
495+
klog.Infof("FieldSelector is used %s", b.fieldSelectorFilter)
503496
}
504497
listWatcher := listWatchFunc(b.kubeClient, ns, b.fieldSelectorFilter)
505498
b.startReflector(expectedType, store, listWatcher, useAPIServerCache)
@@ -532,7 +525,7 @@ func (b *Builder) buildCustomResourceStores(resourceName string,
532525
composedMetricGenFuncs,
533526
)
534527
if b.fieldSelectorFilter != "" {
535-
klog.Infof("FieldSelector is used ", b.fieldSelectorFilter)
528+
klog.Infof("FieldSelector is used %s", b.fieldSelectorFilter)
536529
}
537530
listWatcher := listWatchFunc(customResourceClient, v1.NamespaceAll, b.fieldSelectorFilter)
538531
b.startReflector(expectedType, store, listWatcher, useAPIServerCache)
@@ -545,7 +538,7 @@ func (b *Builder) buildCustomResourceStores(resourceName string,
545538
familyHeaders,
546539
composedMetricGenFuncs,
547540
)
548-
klog.Infof("FieldSelector is used ", b.fieldSelectorFilter)
541+
klog.Infof("FieldSelector is used %s", b.fieldSelectorFilter)
549542
listWatcher := listWatchFunc(customResourceClient, ns, b.fieldSelectorFilter)
550543
b.startReflector(expectedType, store, listWatcher, useAPIServerCache)
551544
stores = append(stores, store)

main.go

Lines changed: 1 addition & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -34,19 +34,6 @@ import (
3434
"k8s.io/kube-state-metrics/v2/pkg/options"
3535
)
3636

37-
func validate(opts *options.Options) error {
38-
shardableResource := "pods"
39-
if opts.NodeName == "" {
40-
return nil
41-
}
42-
for _, x := range opts.Resources.AsSlice() {
43-
if x != shardableResource {
44-
return fmt.Errorf("Resource %s can't be sharding by field selector nodeName", x)
45-
}
46-
}
47-
return nil
48-
}
49-
5037
func main() {
5138
opts := options.NewOptions()
5239
opts.AddFlags()
@@ -66,7 +53,7 @@ func main() {
6653
os.Exit(0)
6754
}
6855

69-
if err := validate(opts); err != nil {
56+
if err := opts.Validate(); err != nil {
7057
klog.ErrorS(err, "Validating options error")
7158
klog.FlushAndExit(klog.ExitFlushTimeout, 1)
7259
}

pkg/app/server.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -107,7 +107,7 @@ func RunKubeStateMetrics(ctx context.Context, opts *options.Options, factories .
107107
namespaces := opts.Namespaces.GetNamespaces()
108108
nsFieldSelector := namespaces.GetExcludeNSFieldSelector(opts.NamespacesDenylist)
109109
nodeNameFieldSelector := opts.NodeName.GetNodeNameFieldSelector()
110-
merged, err := storeBuilder.MergeFieldSelector([]string{nsFieldSelector, nodeNameFieldSelector})
110+
merged, err := storeBuilder.MergeFieldSelectors([]string{nsFieldSelector, nodeNameFieldSelector})
111111
if err != nil {
112112
return err
113113
}

pkg/options/options.go

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -104,7 +104,7 @@ func (o *Options) AddFlags() {
104104
o.flags.Var(&o.Resources, "resources", fmt.Sprintf("Comma-separated list of Resources to be enabled. Defaults to %q", &DefaultResources))
105105
o.flags.Var(&o.Namespaces, "namespaces", fmt.Sprintf("Comma-separated list of namespaces to be enabled. Defaults to %q", &DefaultNamespaces))
106106
o.flags.Var(&o.NamespacesDenylist, "namespaces-denylist", "Comma-separated list of namespaces not to be enabled. If namespaces and namespaces-denylist are both set, only namespaces that are excluded in namespaces-denylist will be used.")
107-
o.flags.StringVar((*string)(&o.NodeName), "nodename", "", "Set spec.nodeName=nodename when watching resources. Only available for resources which support nodeName filter.")
107+
o.flags.StringVar((*string)(&o.NodeName), "nodename", "", "Name of the node that contains the kube-state-metrics pod, only available for resources (pod metrics) that support spec.nodeName fieldSelector. Each kube-state-metrics pod will only exposes metrics related to this node.")
108108
o.flags.Var(&o.MetricAllowlist, "metric-allowlist", "Comma-separated list of metrics to be exposed. This list comprises of exact metric names and/or regex patterns. The allowlist and denylist are mutually exclusive.")
109109
o.flags.Var(&o.MetricDenylist, "metric-denylist", "Comma-separated list of metrics not to be enabled. This list comprises of exact metric names and/or regex patterns. The allowlist and denylist are mutually exclusive.")
110110
o.flags.Var(&o.MetricOptInList, "metric-opt-in-list", "Comma-separated list of metrics which are opt-in and not enabled by default. This is in addition to the metric allow- and denylists")
@@ -134,3 +134,17 @@ func (o *Options) Parse() error {
134134
func (o *Options) Usage() {
135135
o.flags.Usage()
136136
}
137+
138+
// Validate validates arguments
139+
func (o *Options) Validate() error {
140+
shardableResource := "pods"
141+
if o.NodeName == "" {
142+
return nil
143+
}
144+
for _, x := range o.Resources.AsSlice() {
145+
if x != shardableResource {
146+
return fmt.Errorf("Resource %s can't be sharding by field selector spec.nodeName", x)
147+
}
148+
}
149+
return nil
150+
}

pkg/options/types.go

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -120,8 +120,21 @@ func EmptyFieldSelector() string {
120120
return fields.Nothing().String()
121121
}
122122

123-
// MergeFieldSelector returns AND of two field selectors.
124-
func MergeFieldSelector(s1 string, s2 string) (string, error) {
123+
// MergeFieldSelectors returns AND of a list of field selectors.
124+
func MergeFieldSelectors(selectors []string) (string, error) {
125+
var err error
126+
merged := EmptyFieldSelector()
127+
for _, s := range selectors {
128+
merged, err = MergeTwoFieldSelectors(merged, s)
129+
if err != nil {
130+
return "", err
131+
}
132+
}
133+
return merged, nil
134+
}
135+
136+
// MergeTwoFieldSelectors returns AND of two field selectors.
137+
func MergeTwoFieldSelectors(s1 string, s2 string) (string, error) {
125138
selector1, err := fields.ParseSelector(s1)
126139
if err != nil {
127140
return EmptyFieldSelector(), err

pkg/options/types_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -182,7 +182,7 @@ func TestNodeNameFieldSelector(t *testing.T) {
182182
}
183183
}
184184

185-
func TestMergeFieldSelector(t *testing.T) {
185+
func TestMergeFieldSelectors(t *testing.T) {
186186
tests := []struct {
187187
Desc string
188188
Namespaces NamespaceList
@@ -239,7 +239,7 @@ func TestMergeFieldSelector(t *testing.T) {
239239
deniedNS := test.DeniedNamespaces
240240
selector1 := ns.GetExcludeNSFieldSelector(deniedNS)
241241
selector2 := test.NodeName.GetNodeNameFieldSelector()
242-
actual, err := MergeFieldSelector(selector1, selector2)
242+
actual, err := MergeFieldSelectors([]string{selector1, selector2})
243243
if err != nil {
244244
t.Errorf("Test error for Desc: %s. Can't merge field selector %v.", test.Desc, err)
245245
}

0 commit comments

Comments
 (0)