Skip to content

Commit

Permalink
Fix --nodeName= doesn't select unscheduable pods
Browse files Browse the repository at this point in the history
pods
  • Loading branch information
CatherineF-dev committed Jul 24, 2024
1 parent a3e9266 commit b45913d
Show file tree
Hide file tree
Showing 6 changed files with 67 additions and 15 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -282,7 +282,7 @@ spec:
fieldPath: spec.nodeName
```

To track metrics for unassigned pods, you need to add an additional deployment and set `--node=""`, as shown in the following example:
To track metrics for unassigned pods, you need to add an additional deployment and set `--node=`, as shown in the following example:

```
apiVersion: apps/v1
Expand Down
2 changes: 1 addition & 1 deletion README.md.tpl
Original file line number Diff line number Diff line change
Expand Up @@ -283,7 +283,7 @@ spec:
fieldPath: spec.nodeName
```

To track metrics for unassigned pods, you need to add an additional deployment and set `--node=""`, as shown in the following example:
To track metrics for unassigned pods, you need to add an additional deployment and set `--node=`, as shown in the following example:

```
apiVersion: apps/v1
Expand Down
20 changes: 20 additions & 0 deletions examples/daemonsetsharding/deployment-no-node-pods-service.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
apiVersion: v1
kind: Service
metadata:
labels:
app.kubernetes.io/component: exporter
app.kubernetes.io/name: kube-state-metrics-no-node-pods
app.kubernetes.io/version: 2.12.0
name: kube-state-metrics-no-node-pods
namespace: kube-system
spec:
clusterIP: None
ports:
- name: http-metrics
port: 8080
targetPort: http-metrics
- name: telemetry
port: 8081
targetPort: telemetry
selector:
app.kubernetes.io/name: kube-state-metrics-no-node-pods
10 changes: 5 additions & 5 deletions examples/daemonsetsharding/deployment-no-node-pods.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -3,20 +3,20 @@ kind: Deployment
metadata:
labels:
app.kubernetes.io/component: exporter
app.kubernetes.io/name: kube-state-metrics-pods
app.kubernetes.io/name: kube-state-metrics-no-node-pods
app.kubernetes.io/version: 2.13.0
name: kube-state-metrics-pods
name: kube-state-metrics-no-node-pods
namespace: kube-system
spec:
replicas: 1
selector:
matchLabels:
app.kubernetes.io/name: kube-state-metrics
app.kubernetes.io/name: kube-state-metrics-no-node-pods
template:
metadata:
labels:
app.kubernetes.io/component: exporter
app.kubernetes.io/name: kube-state-metrics
app.kubernetes.io/name: kube-state-metrics-no-node-pods
app.kubernetes.io/version: 2.13.0
spec:
automountServiceAccountToken: true
Expand All @@ -31,7 +31,7 @@ spec:
port: http-metrics
initialDelaySeconds: 5
timeoutSeconds: 5
name: kube-state-metrics
name: kube-state-metrics-no-node-pods
ports:
- containerPort: 8080
name: http-metrics
Expand Down
32 changes: 31 additions & 1 deletion jsonnet/kube-state-metrics/kube-state-metrics.libsonnet
Original file line number Diff line number Diff line change
Expand Up @@ -379,16 +379,25 @@
'--resources=pods',
'--node=""',
],
name: shardksmname,
};
local shardksmname = ksm.name + "-pods";
local shardksmname = ksm.name + "-no-node-pods";
std.mergePatch(ksm.deployment,
{
metadata: {
name: shardksmname,
labels: {'app.kubernetes.io/name': shardksmname}
},
spec: {
selector{
matchLabels: {app.kubernetes.io/name': shardksmname}
}
template: {
metadata: {
labels: {
app.kubernetes.io/name': shardksmname
}
}
spec: {
containers: [c],
},
Expand All @@ -397,6 +406,27 @@
},
),

deploymentNoNodePodsService:
local c = ksm.deployment.spec.template.spec.containers[0] {
args: [
'--resources=pods',
'--node=""',
],
};
local shardksmname = ksm.name + "-no-node-pods";
std.mergePatch(ksm.service,
{
metadata: {
name: shardksmname,
labels: {'app.kubernetes.io/name': shardksmname}
},
spec: {
selector: {
'app.kubernetes.io/name': shardksmname
}
}
}
),
daemonset:
// extending the default container from above
local c0 = ksm.deployment.spec.template.spec.containers[0] {
Expand Down
16 changes: 9 additions & 7 deletions pkg/options/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ package options

import (
"errors"
"regexp"
"sort"
"strings"

Expand Down Expand Up @@ -141,15 +140,18 @@ func (n *NodeType) Type() string {

// GetNodeFieldSelector returns a nodename field selector.
func (n *NodeType) GetNodeFieldSelector() string {
if nil == n || len(*n) == 0 {
if nil == n {
klog.InfoS("Using node type is nil")
return EmptyFieldSelector()
}
pattern := "[^a-zA-Z0-9_,-]+"
re := regexp.MustCompile(pattern)
result := re.ReplaceAllString(n.String(), "")
klog.InfoS("Using node type", "node", result)
return fields.OneTermEqualSelector("spec.nodeName", result).String()
nodeName := n.String()
// `--node=""` find pods without node name assigned which uses fieldselector spec.nodeName=""
klog.InfoS("Using node name", nodeName)
if nodeName == "" {
klog.InfoS("Using spec.nodeName= to select unscheduable pods without node")
return "spec.nodeName="
}
return fields.OneTermEqualSelector("spec.nodeName", nodeName).String()

}

Expand Down

0 comments on commit b45913d

Please sign in to comment.