diff --git a/pkg/app/server.go b/pkg/app/server.go index 8dbcf1d409..98bb11df01 100644 --- a/pkg/app/server.go +++ b/pkg/app/server.go @@ -224,7 +224,13 @@ func RunKubeStateMetrics(ctx context.Context, opts *options.Options) error { namespaces := opts.Namespaces.GetNamespaces() nsFieldSelector := namespaces.GetExcludeNSFieldSelector(opts.NamespacesDenylist) - nodeFieldSelector := opts.Node.GetNodeFieldSelector(opts.TrackUnscheduledPods) + var nodeFieldSelector string + if opts.TrackUnscheduledPods { + nodeFieldSelector = "spec.nodeName=" + klog.InfoS("Using spec.nodeName= to select unscheduable pods without node") + } else { + nodeFieldSelector = opts.Node.GetNodeFieldSelector() + } merged, err := storeBuilder.MergeFieldSelectors([]string{nsFieldSelector, nodeFieldSelector}) if err != nil { return err diff --git a/pkg/options/options.go b/pkg/options/options.go index 3e1b67f9cc..d10a12cce2 100644 --- a/pkg/options/options.go +++ b/pkg/options/options.go @@ -60,19 +60,19 @@ type Options struct { NamespacesDenylist NamespaceList `yaml:"namespaces_denylist"` Node NodeType `yaml:"node"` TrackUnscheduledPods bool `yaml:"track_unscheduled_pods"` - Pod string `yaml:"pod"` - Port int `yaml:"port"` - Resources ResourceSet `yaml:"resources"` - Shard int32 `yaml:"shard"` - TLSConfig string `yaml:"tls_config"` - TelemetryHost string `yaml:"telemetry_host"` - TelemetryPort int `yaml:"telemetry_port"` - TotalShards int `yaml:"total_shards"` - UseAPIServerCache bool `yaml:"use_api_server_cache"` - ServerReadTimeout time.Duration `yaml:"server_read_timeout"` - ServerWriteTimeout time.Duration `yaml:"server_write_timeout"` - ServerIdleTimeout time.Duration `yaml:"server_idle_timeout"` - ServerReadHeaderTimeout time.Duration `yaml:"server_read_header_timeout"` + Pod string `yaml:"pod"` + Port int `yaml:"port"` + Resources ResourceSet `yaml:"resources"` + Shard int32 `yaml:"shard"` + TLSConfig string `yaml:"tls_config"` + TelemetryHost string `yaml:"telemetry_host"` + TelemetryPort int `yaml:"telemetry_port"` + TotalShards int `yaml:"total_shards"` + UseAPIServerCache bool `yaml:"use_api_server_cache"` + ServerReadTimeout time.Duration `yaml:"server_read_timeout"` + ServerWriteTimeout time.Duration `yaml:"server_write_timeout"` + ServerIdleTimeout time.Duration `yaml:"server_idle_timeout"` + ServerReadHeaderTimeout time.Duration `yaml:"server_read_header_timeout"` Config string diff --git a/pkg/options/types.go b/pkg/options/types.go index d1173e2eea..93f8a7f505 100644 --- a/pkg/options/types.go +++ b/pkg/options/types.go @@ -107,15 +107,19 @@ func (r *ResourceSet) Type() string { // NodeType represents a nodeName to query from. type NodeType string +// Set sets the node name to NodeType. +func (n *NodeType) Set(value string) error { + *n = NodeType(value) + return nil +} + +// String gets node name. +func (n NodeType) String() string { + return string(n) +} + // GetNodeFieldSelector returns a nodename field selector. -func (n *NodeType) GetNodeFieldSelector(fetchUnscheduledPods bool) string { - if fetchUnscheduledPods { - if string(*n) != "" { - klog.Warningf("spec.nodeName=%s will not be used, because --track-unscheduled-pods is set", string(*n)) - } - klog.InfoS("Using spec.nodeName= to select unscheduable pods without node") - return "spec.nodeName=" - } +func (n *NodeType) GetNodeFieldSelector() string { if string(*n) != "" { return fields.OneTermEqualSelector("spec.nodeName", string(*n)).String() } diff --git a/pkg/options/types_test.go b/pkg/options/types_test.go index 44fb26ec7c..a1b43a2c0b 100644 --- a/pkg/options/types_test.go +++ b/pkg/options/types_test.go @@ -175,30 +175,7 @@ func TestNodeFieldSelector(t *testing.T) { for _, test := range tests { node := test.Node - actual := node.GetNodeFieldSelector(false) - if !reflect.DeepEqual(actual, test.Wanted) { - t.Errorf("Test error for Desc: %s. Want: %+v. Got: %+v.", test.Desc, test.Wanted, actual) - } - } - tests1 := []struct { - Desc string - Node NodeType - Wanted string - }{ - { - Desc: "empty node name", - Node: "", - Wanted: "spec.nodeName=", - }, - { - Desc: "have node name when --track-unscheduled-pods is set", - Node: "", - Wanted: "spec.nodeName=", - }, - } - for _, test := range tests1 { - node := test.Node - actual := node.GetNodeFieldSelector(true) + actual := node.GetNodeFieldSelector() if !reflect.DeepEqual(actual, test.Wanted) { t.Errorf("Test error for Desc: %s. Want: %+v. Got: %+v.", test.Desc, test.Wanted, actual) } @@ -261,7 +238,7 @@ func TestMergeFieldSelectors(t *testing.T) { ns := test.Namespaces deniedNS := test.DeniedNamespaces selector1 := ns.GetExcludeNSFieldSelector(deniedNS) - selector2 := test.Node.GetNodeFieldSelector(false) + selector2 := test.Node.GetNodeFieldSelector() actual, err := MergeFieldSelectors([]string{selector1, selector2}) if err != nil { t.Errorf("Test error for Desc: %s. Can't merge field selector %v.", test.Desc, err)