Skip to content

Commit

Permalink
Fix Nodetype
Browse files Browse the repository at this point in the history
  • Loading branch information
CatherineF-dev committed Jul 24, 2024
1 parent e18b557 commit aeb9e35
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 47 deletions.
8 changes: 7 additions & 1 deletion pkg/app/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
26 changes: 13 additions & 13 deletions pkg/options/options.go
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
20 changes: 12 additions & 8 deletions pkg/options/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -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()
}
Expand Down
27 changes: 2 additions & 25 deletions pkg/options/types_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
Expand Down Expand Up @@ -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)
Expand Down

0 comments on commit aeb9e35

Please sign in to comment.