# Substring search for node affinity

**URL:** <https://discuss.kubernetes.io/t/substring-search-for-node-affinity/25384>\
**Category:** General Discussions\
**Tags:** development\
**Created:** [September 9, 2023, 10:58pm UTC](https://discuss.kubernetes.io/t/substring-search-for-node-affinity/25384 "2023-09-09T22:58:49Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

**Author:** ![David\_Thrower](https://sea2.discourse-cdn.com/flex016/user_avatar/discuss.kubernetes.io/david_thrower/32/12721_2.png) [@David\_Thrower](https://discuss.kubernetes.io/u/David_Thrower)\
**Post date:** [September 9, 2023, 10:58pm UTC](https://discuss.kubernetes.io/t/substring-search-for-node-affinity/25384/1 "2023-09-09T22:58:49Z")

</div>

### Cluster information: K3s v1.27.4+k3s1

Kubernetes version: 1.27  
Cloud being used: OVH  
Installation method: shell installation of K3s  
Host OS: Ubuntu 23.04  
CNI and version: Flannel (Version unknown)  
CRI and version: containerd://1.7.3-k3s1

I want to set a node affinity such that a node name containing the sub-string “hpc” will be the only scheduling target of the pods of a given deployment. For example, pods for the deployment “foo” will be scheduled on the node named node-0001-hpc-bar and node-0002-hpc-baz, but not on node-0003-web, nor the node node-0004-web.

Basically I wanted to know if there was a way to set a node affinity by means of the boolean: “is [this string] a sub-string of the property [[kubernetes.io/hostname](http://kubernetes.io/hostname)]”. I have tried numerous permutations of ways I can configure a node affinity on a deployment, but nothing works.

The configuration an LLM suggested in response to a prompt asking how to do this suggested:

```auto
apiVersion: apps/v1
kind: Deployment
metadata:
  name: bar-deployment
  namespace: [redacted]
spec:
  replicas: 1
  selector:
    matchLabels:
      app: bar
  template:
    metadata:
      labels:
        app: bar
    spec:
      affinity:
        nodeAffinity:
          requiredDuringSchedulingIgnoredDuringExecution:
            nodeSelectorTerms:
              - matchExpressions:
                  - key: kubernetes.io/hostname
                    operator: In
                    values:
                      - "*hpc*"
      containers:
# ...

```

This never scheduled, and `kubectl -n [redacted] describe [unscheduled pod]` returns:

```auto
...
Warning FailedScheduling ... default-scheduler 0/[redacted] nodes are available: [redacted] node(s) didn't match Pod's node affinity/selector. preemption: 0/[redacted] nodes are available: [redacted] Preemption is not helpful for scheduling..

```

This try returned an error that the key didn’t begin with an alphanumeric character:

```auto
apiVersion: apps/v1
kind: Deployment
metadata:
  name: bar-deployment
  namespace: [redacted]
spec:
  replicas: 1
  selector:
    matchLabels:
      app: bar
  template:
    metadata:
      labels:
        app: bar
    spec:
      affinity:
        nodeAffinity:
          requiredDuringSchedulingIgnoredDuringExecution:
            nodeSelectorTerms:
              - matchExpressions:
                  - key: kubernetes.io/hostname
                    operator: In
                    values:
                      - *hpc*
      containers:
...

```

This didn’t match either

```auto
apiVersion: apps/v1
kind: Deployment
metadata:
  name: bar-deployment
  namespace: [redacted]
spec:
  replicas: 1
  selector:
    matchLabels:
      app: bar
  template:
    metadata:
      labels:
        app: bar
    spec:
      affinity:
        nodeAffinity:
          requiredDuringSchedulingIgnoredDuringExecution:
            nodeSelectorTerms:
              - matchExpressions:
                  - key: kubernetes.io/hostname
                    operator: In
                    values:
                      - hpc
      containers:
...

```

Is there a way to do this, or am I stuck adding additional non-default labels to the nodes?
