Skip to content

Commit 43c6073

Browse files
Refactoring
1 parent 813496d commit 43c6073

File tree

6 files changed

+34
-33
lines changed

6 files changed

+34
-33
lines changed

README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -239,7 +239,7 @@ The downside of using an auto-sharded setup comes from the rollout strategy supp
239239
### Daemonset sharding for pod metrics
240240

241241
For pod metrics, they can be sharded per node with the following flag:
242-
* `--nodename`
242+
* `--node`
243243

244244
Each kube-state-metrics pod uses FieldSelector (spec.nodeName) to watch/list pod metrics only on the same node.
245245

@@ -255,7 +255,7 @@ spec:
255255
name: kube-state-metrics
256256
args:
257257
- --resource=pods
258-
- --nodename=$(NODE_NAME)
258+
- --node=$(NODE_NAME)
259259
env:
260260
- name: NODE_NAME
261261
valueFrom:

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 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.
49+
--node string Name of the node that contains the kube-state-metrics pod. Most likely it should be passed via the downward API. This is used for daemonset sharding. Only available for resources (pod metrics) that support spec.nodeName fieldSelector. This is experimental.
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.

pkg/app/server.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -106,8 +106,8 @@ func RunKubeStateMetrics(ctx context.Context, opts *options.Options, factories .
106106

107107
namespaces := opts.Namespaces.GetNamespaces()
108108
nsFieldSelector := namespaces.GetExcludeNSFieldSelector(opts.NamespacesDenylist)
109-
nodeNameFieldSelector := opts.NodeName.GetNodeNameFieldSelector()
110-
merged, err := storeBuilder.MergeFieldSelectors([]string{nsFieldSelector, nodeNameFieldSelector})
109+
nodeFieldSelector := opts.Node.GetNodeFieldSelector()
110+
merged, err := storeBuilder.MergeFieldSelectors([]string{nsFieldSelector, nodeFieldSelector})
111111
if err != nil {
112112
return err
113113
}

pkg/options/options.go

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ type Options struct {
3939
Resources ResourceSet
4040
Namespaces NamespaceList
4141
NamespacesDenylist NamespaceList
42-
NodeName NodeNameType
42+
Node NodeType
4343
Shard int32
4444
TotalShards int
4545
Pod string
@@ -104,7 +104,6 @@ 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", "", "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.")
108107
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.")
109108
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.")
110109
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")
@@ -113,6 +112,8 @@ func (o *Options) AddFlags() {
113112
o.flags.Int32Var(&o.Shard, "shard", int32(0), "The instances shard nominal (zero indexed) within the total number of shards. (default 0)")
114113
o.flags.IntVar(&o.TotalShards, "total-shards", 1, "The total number of shards. Sharding is disabled when total shards is set to 1.")
115114

115+
o.flags.StringVar((*string)(&o.Node), "node", "", "Name of the node that contains the kube-state-metrics pod. Most likely it should be passed via the downward API. This is used for daemonset sharding. Only available for resources (pod metrics) that support spec.nodeName fieldSelector. This is experimental.")
116+
116117
autoshardingNotice := "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."
117118

118119
o.flags.StringVar(&o.Pod, "pod", "", "Name of the pod that contains the kube-state-metrics container. "+autoshardingNotice)
@@ -138,12 +139,12 @@ func (o *Options) Usage() {
138139
// Validate validates arguments
139140
func (o *Options) Validate() error {
140141
shardableResource := "pods"
141-
if o.NodeName == "" {
142+
if o.Node == "" {
142143
return nil
143144
}
144145
for _, x := range o.Resources.AsSlice() {
145146
if x != shardableResource {
146-
return fmt.Errorf("Resource %s can't be sharding by field selector spec.nodeName", x)
147+
return fmt.Errorf("Resource %s can't be sharded by field selector spec.nodeName", x)
147148
}
148149
}
149150
return nil

pkg/options/types.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -104,11 +104,11 @@ func (r *ResourceSet) Type() string {
104104
return "string"
105105
}
106106

107-
// NodeNameType represents a nodeName to query from.
108-
type NodeNameType string
107+
// NodeType represents a nodeName to query from.
108+
type NodeType string
109109

110-
// GetNodeNameFieldSelector returns a nodename field selector.
111-
func (n *NodeNameType) GetNodeNameFieldSelector() string {
110+
// GetNodeFieldSelector returns a nodename field selector.
111+
func (n *NodeType) GetNodeFieldSelector() string {
112112
if string(*n) != "" {
113113
return fields.OneTermEqualSelector("spec.nodeName", string(*n)).String()
114114
}

pkg/options/types_test.go

Lines changed: 20 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -155,27 +155,27 @@ func TestNamespaceList_ExcludeNamespacesFieldSelector(t *testing.T) {
155155
}
156156
}
157157

158-
func TestNodeNameFieldSelector(t *testing.T) {
158+
func TestNodeFieldSelector(t *testing.T) {
159159
tests := []struct {
160-
Desc string
161-
NodeName NodeNameType
162-
Wanted string
160+
Desc string
161+
Node NodeType
162+
Wanted string
163163
}{
164164
{
165-
Desc: "empty node name",
166-
NodeName: "",
167-
Wanted: "",
165+
Desc: "empty node name",
166+
Node: "",
167+
Wanted: "",
168168
},
169169
{
170-
Desc: "with node name",
171-
NodeName: "k8s-node-1",
172-
Wanted: "spec.nodeName=k8s-node-1",
170+
Desc: "with node name",
171+
Node: "k8s-node-1",
172+
Wanted: "spec.nodeName=k8s-node-1",
173173
},
174174
}
175175

176176
for _, test := range tests {
177-
node := test.NodeName
178-
actual := node.GetNodeNameFieldSelector()
177+
node := test.Node
178+
actual := node.GetNodeFieldSelector()
179179
if !reflect.DeepEqual(actual, test.Wanted) {
180180
t.Errorf("Test error for Desc: %s. Want: %+v. Got: %+v.", test.Desc, test.Wanted, actual)
181181
}
@@ -187,49 +187,49 @@ func TestMergeFieldSelectors(t *testing.T) {
187187
Desc string
188188
Namespaces NamespaceList
189189
DeniedNamespaces NamespaceList
190-
NodeName NodeNameType
190+
Node NodeType
191191
Wanted string
192192
}{
193193
{
194194
Desc: "empty DeniedNamespaces",
195195
Namespaces: NamespaceList{"default", "kube-system"},
196196
DeniedNamespaces: NamespaceList{},
197-
NodeName: "",
197+
Node: "",
198198
Wanted: "",
199199
},
200200
{
201201
Desc: "all DeniedNamespaces",
202202
Namespaces: DefaultNamespaces,
203203
DeniedNamespaces: NamespaceList{"some-system"},
204-
NodeName: "",
204+
Node: "",
205205
Wanted: "metadata.namespace!=some-system",
206206
},
207207
{
208208
Desc: "general case",
209209
Namespaces: DefaultNamespaces,
210210
DeniedNamespaces: NamespaceList{"case1-system", "case2-system"},
211-
NodeName: "",
211+
Node: "",
212212
Wanted: "metadata.namespace!=case1-system,metadata.namespace!=case2-system",
213213
},
214214
{
215215
Desc: "empty DeniedNamespaces",
216216
Namespaces: NamespaceList{"default", "kube-system"},
217217
DeniedNamespaces: NamespaceList{},
218-
NodeName: "k8s-node-1",
218+
Node: "k8s-node-1",
219219
Wanted: "spec.nodeName=k8s-node-1",
220220
},
221221
{
222222
Desc: "all DeniedNamespaces",
223223
Namespaces: DefaultNamespaces,
224224
DeniedNamespaces: NamespaceList{"some-system"},
225-
NodeName: "k8s-node-1",
225+
Node: "k8s-node-1",
226226
Wanted: "metadata.namespace!=some-system,spec.nodeName=k8s-node-1",
227227
},
228228
{
229229
Desc: "general case",
230230
Namespaces: DefaultNamespaces,
231231
DeniedNamespaces: NamespaceList{"case1-system", "case2-system"},
232-
NodeName: "k8s-node-1",
232+
Node: "k8s-node-1",
233233
Wanted: "metadata.namespace!=case1-system,metadata.namespace!=case2-system,spec.nodeName=k8s-node-1",
234234
},
235235
}
@@ -238,7 +238,7 @@ func TestMergeFieldSelectors(t *testing.T) {
238238
ns := test.Namespaces
239239
deniedNS := test.DeniedNamespaces
240240
selector1 := ns.GetExcludeNSFieldSelector(deniedNS)
241-
selector2 := test.NodeName.GetNodeNameFieldSelector()
241+
selector2 := test.Node.GetNodeFieldSelector()
242242
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)

0 commit comments

Comments
 (0)