Skip to content

Commit

Permalink
Clean
Browse files Browse the repository at this point in the history
  • Loading branch information
CatherineF-dev committed Nov 4, 2022
1 parent 3b50fbc commit 87a7793
Show file tree
Hide file tree
Showing 7 changed files with 35 additions and 37 deletions.
2 changes: 1 addition & 1 deletion docs/cli-arguments.md
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ Usage of ./kube-state-metrics:
--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
--namespaces string Comma-separated list of namespaces to be enabled. Defaults to ""
--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.
--nodename string Set spec.nodeName=nodeName when watching resources. Only available for resources which support nodeName filter.
--nodename string Set spec.nodeName=nodename when watching resources. Only available for resources which support nodeName filter.
--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)
--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.
--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.
Expand Down
25 changes: 12 additions & 13 deletions internal/store/builder.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ type Builder struct {
vpaClient vpaclientset.Interface
namespaces options.NamespaceList
namespaceFilter string
nodenameFilter string
nodeNameFilter string
ctx context.Context
enabledResources []string
familyGeneratorFilter generator.FamilyGeneratorFilter
Expand Down Expand Up @@ -113,9 +113,9 @@ func (b *Builder) WithNamespaces(n options.NamespaceList, nsFilter string) {
b.namespaceFilter = nsFilter
}

// WithNodename sets the nodename property of a Builder.
func (b *Builder) WithNodename(nodenameFilter string) {
b.nodenameFilter = nodenameFilter
// WithNodeName sets the nodename property of a Builder.
func (b *Builder) WithNodeName(nodeNameFilter string) {
b.nodeNameFilter = nodeNameFilter
}

// MergeFieldSelector merges two fieldSelectors using AND operator.
Expand Down Expand Up @@ -478,14 +478,13 @@ func (b *Builder) buildStores(
familyHeaders,
composedMetricGenFuncs,
)
merged, err := b.MergeFieldSelector(b.namespaceFilter, b.nodenameFilter)
merged, err := b.MergeFieldSelector(b.namespaceFilter, b.nodeNameFilter)
if err != nil {
panic(fmt.Sprintf("Failed to merge fieldSelector %s and %s", b.namespaceFilter, b.nodenameFilter))
panic(fmt.Sprintf("Failed to merge fieldSelector %s and %s", b.namespaceFilter, b.nodeNameFilter))
}
klog.Infof("FieldSelector is used ", merged)
listWatcher := listWatchFunc(b.kubeClient, v1.NamespaceAll, merged)
b.startReflector(expectedType, store, listWatcher, useAPIServerCache)
b.startReflector(expectedType, store, listWatcher, useAPIServerCache)
return []cache.Store{store}
}

Expand All @@ -495,9 +494,9 @@ func (b *Builder) buildStores(
familyHeaders,
composedMetricGenFuncs,
)
merged, err := b.MergeFieldSelector(b.namespaceFilter, b.nodenameFilter)
merged, err := b.MergeFieldSelector(b.namespaceFilter, b.nodeNameFilter)
if err != nil {
panic(fmt.Sprintf("Failed to merge fieldSelector %s and %s", b.namespaceFilter, b.nodenameFilter))
panic(fmt.Sprintf("Failed to merge fieldSelector %s and %s", b.namespaceFilter, b.nodeNameFilter))
}
klog.Infof("FieldSelector is used ", merged)
listWatcher := listWatchFunc(b.kubeClient, ns, merged)
Expand Down Expand Up @@ -530,9 +529,9 @@ func (b *Builder) buildCustomResourceStores(resourceName string,
familyHeaders,
composedMetricGenFuncs,
)
merged, err := b.MergeFieldSelector(b.namespaceFilter, b.nodenameFilter)
merged, err := b.MergeFieldSelector(b.namespaceFilter, b.nodeNameFilter)
if err != nil {
panic(fmt.Sprintf("Failed to merge fieldSelector %s and %s", b.namespaceFilter, b.nodenameFilter))
panic(fmt.Sprintf("Failed to merge fieldSelector %s and %s", b.namespaceFilter, b.nodeNameFilter))
}
klog.Infof("FieldSelector is used ", merged)
listWatcher := listWatchFunc(customResourceClient, v1.NamespaceAll, merged)
Expand All @@ -546,9 +545,9 @@ func (b *Builder) buildCustomResourceStores(resourceName string,
familyHeaders,
composedMetricGenFuncs,
)
merged, err := b.MergeFieldSelector(b.namespaceFilter, b.nodenameFilter)
merged, err := b.MergeFieldSelector(b.namespaceFilter, b.nodeNameFilter)
if err != nil {
panic(fmt.Sprintf("Failed to merge fieldSelector %s and %s", b.namespaceFilter, b.nodenameFilter))
panic(fmt.Sprintf("Failed to merge fieldSelector %s and %s", b.namespaceFilter, b.nodeNameFilter))
}
klog.Infof("FieldSelector is used ", merged)
listWatcher := listWatchFunc(customResourceClient, ns, merged)
Expand Down
2 changes: 1 addition & 1 deletion main.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ import (

func validate(opts *options.Options) error {
shardableResource := "pods"
if opts.Nodename == "" {
if opts.NodeName == "" {
return nil
}
for _, x := range opts.Resources.AsSlice() {
Expand Down
3 changes: 1 addition & 2 deletions pkg/app/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -107,8 +107,7 @@ func RunKubeStateMetrics(ctx context.Context, opts *options.Options, factories .
namespaces := opts.Namespaces.GetNamespaces()
nsFieldSelector := namespaces.GetExcludeNSFieldSelector(opts.NamespacesDenylist)
storeBuilder.WithNamespaces(namespaces, nsFieldSelector)

storeBuilder.WithNodename(opts.Nodename.GetNodenameFieldSelector())
storeBuilder.WithNodeName(opts.NodeName.GetNodeNameFieldSelector())

allowDenyList, err := allowdenylist.New(opts.MetricAllowlist, opts.MetricDenylist)
if err != nil {
Expand Down
4 changes: 2 additions & 2 deletions pkg/options/options.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ type Options struct {
Resources ResourceSet
Namespaces NamespaceList
NamespacesDenylist NamespaceList
Nodename NodenameType
NodeName NodeNameType
Shard int32
TotalShards int
Pod string
Expand Down Expand Up @@ -104,7 +104,7 @@ func (o *Options) AddFlags() {
o.flags.Var(&o.Resources, "resources", fmt.Sprintf("Comma-separated list of Resources to be enabled. Defaults to %q", &DefaultResources))
o.flags.Var(&o.Namespaces, "namespaces", fmt.Sprintf("Comma-separated list of namespaces to be enabled. Defaults to %q", &DefaultNamespaces))
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.")
o.flags.StringVar((*string)(&o.Nodename), "nodename", "", "Set spec.nodeName=nodeName when watching resources. Only available for resources which support nodeName filter.")
o.flags.StringVar((*string)(&o.NodeName), "nodename", "", "Set spec.nodeName=nodename when watching resources. Only available for resources which support nodeName filter.")
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.")
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.")
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")
Expand Down
8 changes: 4 additions & 4 deletions pkg/options/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -104,11 +104,11 @@ func (r *ResourceSet) Type() string {
return "string"
}

// NodenameType represents a nodeName to query from.
type NodenameType string
// NodeNameType represents a nodeName to query from.
type NodeNameType string

// GetNodenameFieldSelector returns a nodename field selector.
func (n *NodenameType) GetNodenameFieldSelector() string {
// GetNodeNameFieldSelector returns a nodename field selector.
func (n *NodeNameType) GetNodeNameFieldSelector() string {
if string(*n) != "" {
return fields.OneTermEqualSelector("spec.nodeName", string(*n)).String()
}
Expand Down
28 changes: 14 additions & 14 deletions pkg/options/types_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -155,27 +155,27 @@ func TestNamespaceList_ExcludeNamespacesFieldSelector(t *testing.T) {
}
}

func TestNodenameFieldSelector(t *testing.T) {
func TestNodeNameFieldSelector(t *testing.T) {
tests := []struct {
Desc string
Nodename NodenameType
NodeName NodeNameType
Wanted string
}{
{
Desc: "empty node name",
Nodename: "",
NodeName: "",
Wanted: "",
},
{
Desc: "with node name",
Nodename: "k8s-node-1",
NodeName: "k8s-node-1",
Wanted: "spec.nodeName=k8s-node-1",
},
}

for _, test := range tests {
node := test.Nodename
actual := node.GetNodenameFieldSelector()
node := test.NodeName
actual := node.GetNodeNameFieldSelector()
if !reflect.DeepEqual(actual, test.Wanted) {
t.Errorf("Test error for Desc: %s. Want: %+v. Got: %+v.", test.Desc, test.Wanted, actual)
}
Expand All @@ -187,49 +187,49 @@ func TestMergeFieldSelector(t *testing.T) {
Desc string
Namespaces NamespaceList
DeniedNamespaces NamespaceList
Nodename NodenameType
NodeName NodeNameType
Wanted string
}{
{
Desc: "empty DeniedNamespaces",
Namespaces: NamespaceList{"default", "kube-system"},
DeniedNamespaces: NamespaceList{},
Nodename: "",
NodeName: "",
Wanted: "",
},
{
Desc: "all DeniedNamespaces",
Namespaces: DefaultNamespaces,
DeniedNamespaces: NamespaceList{"some-system"},
Nodename: "",
NodeName: "",
Wanted: "metadata.namespace!=some-system",
},
{
Desc: "general case",
Namespaces: DefaultNamespaces,
DeniedNamespaces: NamespaceList{"case1-system", "case2-system"},
Nodename: "",
NodeName: "",
Wanted: "metadata.namespace!=case1-system,metadata.namespace!=case2-system",
},
{
Desc: "empty DeniedNamespaces",
Namespaces: NamespaceList{"default", "kube-system"},
DeniedNamespaces: NamespaceList{},
Nodename: "k8s-node-1",
NodeName: "k8s-node-1",
Wanted: "spec.nodeName=k8s-node-1",
},
{
Desc: "all DeniedNamespaces",
Namespaces: DefaultNamespaces,
DeniedNamespaces: NamespaceList{"some-system"},
Nodename: "k8s-node-1",
NodeName: "k8s-node-1",
Wanted: "metadata.namespace!=some-system,spec.nodeName=k8s-node-1",
},
{
Desc: "general case",
Namespaces: DefaultNamespaces,
DeniedNamespaces: NamespaceList{"case1-system", "case2-system"},
Nodename: "k8s-node-1",
NodeName: "k8s-node-1",
Wanted: "metadata.namespace!=case1-system,metadata.namespace!=case2-system,spec.nodeName=k8s-node-1",
},
}
Expand All @@ -238,7 +238,7 @@ func TestMergeFieldSelector(t *testing.T) {
ns := test.Namespaces
deniedNS := test.DeniedNamespaces
selector1 := ns.GetExcludeNSFieldSelector(deniedNS)
selector2 := test.Nodename.GetNodenameFieldSelector()
selector2 := test.NodeName.GetNodeNameFieldSelector()
actual, err := MergeFieldSelector(selector1, selector2)
if err != nil {
t.Errorf("Test error for Desc: %s. Can't merge field selector %v.", test.Desc, err)
Expand Down

0 comments on commit 87a7793

Please sign in to comment.