You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
This KEP proposes introducing a complementary field `MatchLabelKeys` to `PodAffinityTerm`.
179
+
This KEP proposes introducing a complementary field `MatchLabelSelectors` to `PodAffinityTerm`.
179
180
This enables users to finely control the scope where Pods are expected to co-exist (PodAffinity)
180
181
or not (PodAntiAffinity), on top of the existing `LabelSelector`.
181
182
@@ -207,7 +208,7 @@ The same issue applies to other scheduling directives as well. For example, Matc
207
208
208
209
### Goals
209
210
210
-
- Introduce `MatchLabelKeys` in `PodAffinityTerm` to let users define the scope where Pods are evaluated in required and preferred Pod(Anti)Affinity.
211
+
- Introduce `MatchLabelSelectors` in `PodAffinityTerm` to let users define the scope where Pods are evaluated in required and preferred Pod(Anti)Affinity.
211
212
212
213
### Non-Goals
213
214
@@ -216,7 +217,7 @@ What is out of scope for this KEP? Listing non-goals helps to focus discussion
216
217
and make progress.
217
218
-->
218
219
219
-
- Apply additional internal labels when evaluating `MatchLabelKeys`
220
+
- Apply additional internal labels when evaluating `MatchLabelSelectors`
220
221
221
222
## Proposal
222
223
@@ -245,7 +246,7 @@ and they want only replicas from the same replicaset to be evaluated.
245
246
246
247
The deployment controller adds [pod-template-hash](https://kubernetes.io/docs/concepts/workloads/controllers/deployment/#pod-template-hash-label) to underlying ReplicaSet and thus every Pod created from Deployment carries the hash string.
247
248
248
-
Therefore, users can use `pod-template-hash` in `MatchlabelKeys` to inform the scheduler to only evaluate Pods with the same `pod-template-hash` value.
249
+
Therefore, users can use `pod-template-hash` in `matchLabelSelector.matchLabelKeys` to inform the scheduler to only evaluate Pods with the same `pod-template-hash` value.
249
250
250
251
```yaml
251
252
apiVersion: apps/v1
@@ -263,10 +264,51 @@ metadata:
263
264
values:
264
265
- database
265
266
topologyKey: topology.kubernetes.io/zone
266
-
matchlabelKeys: # ADDED
267
-
- pod-template-hash
267
+
matchLabelSelectors: # ADDED
268
+
- matchLabelKeys: pod-template-hash
269
+
operator: In
268
270
```
269
271
272
+
#### Story 2
273
+
274
+
When users want to achieve exclusive 1:1 job to domain placement in [kubernetes-sigs/jobset](https://github.com/kubernetes-sigs/jobset).
275
+
Noone knows the job's name until it's created from JobSet, but each Job and Pod will get [`job-name`](https://github.com/kubernetes-sigs/jobset/blob/749a77c2647ac46b309b8227f66293e4116b97ed/api/v1alpha1/jobset_types.go#L26) label on it.
276
+
277
+
Users can use `job-name` label to ensure that the Pods with the same `job-name` will land on the same rack
278
+
and Pods with other `job-name` won't labd on the same rack via `matchLabelSelectors`.
279
+
280
+
```yaml
281
+
apiVersion: jobset.x-k8s.io/v1alpha1
282
+
kind: JobSet
283
+
metadata:
284
+
name: pytorch
285
+
annotations:
286
+
alpha.jobset.sigs.k8s.io/exclusive-topology: rack
287
+
spec:
288
+
replicatedJobs:
289
+
- name: workers
290
+
template:
291
+
...
292
+
affinity:
293
+
podAffinity: # ensures the pods of this job land on the same rack
294
+
requiredDuringSchedulingIgnoredDuringExecution:
295
+
- matchLabelSelectors:
296
+
- matchLabelKey: job-name
297
+
operator: In
298
+
topologyKey: rack
299
+
podAntiAffinity: # ensures only this job lands on the rack
300
+
requiredDuringSchedulingIgnoredDuringExecution:
301
+
- matchLabelSelectors:
302
+
- matchLabelKey: job-name
303
+
operator: NotIn
304
+
- labelSelector:
305
+
matchExpressions:
306
+
- key: job-name
307
+
operator: Exists
308
+
topologyKey: rack
309
+
```
310
+
311
+
270
312
### Notes/Constraints/Caveats (Optional)
271
313
272
314
<!--
@@ -297,7 +339,7 @@ Consider including folks who also work outside the SIG or subproject.
297
339
-->
298
340
299
341
In addition to using `pod-template-hash` added by the Deployment controller,
300
-
users can also provide the customized key in `MatchLabelKeys` to identify
342
+
users can also provide the customized key in `MatchLabelKey` to identify
301
343
which pods should be grouped. If so, the user needs to ensure that it is
302
344
correct and not duplicated with other unrelated workloads.
303
345
@@ -310,30 +352,44 @@ required) or even code snippets. If there's any ambiguity about HOW your
310
352
proposal will be implemented, this is the place to discuss them.
311
353
-->
312
354
313
-
A new optional field `MatchLabelKeys` is introduced to `PodAffinityTerm`.
355
+
A new optional field `MatchLabelSelectors` is introduced to `PodAffinityTerm`.
314
356
315
357
```go
358
+
type LabelSelectorOperator string
359
+
360
+
type MatchLabelSelector struct {
361
+
// MatchLabelKey is used to lookup value from the incoming pod labels,
362
+
// and that key-value label is merged with `LabelSelector`.
363
+
// Key that doesn't exist in the incoming pod labels will be ignored.
364
+
MatchLabelKey string
365
+
// Operator defines how key-value, fetched via the above `MatchLabelKeys`, is merged into LabelSelector.
366
+
// If Operator is `In`, `key in (value)` is merged with LabelSelector.
367
+
// If Operator is `NotIn`, `key notin (value)` is merged with LabelSelector.
368
+
//
369
+
// +optional
370
+
Operator LabelSelectorOperator
371
+
}
372
+
316
373
type PodAffinityTerm struct {
317
374
LabelSelector *metav1.LabelSelector
318
375
Namespaces []string
319
376
TopologyKey string
320
377
NamespaceSelector *metav1.LabelSelector
321
378
322
-
// MatchLabelKeys is a set of pod label keys to select which pods will
323
-
// be taken into consideration. The keys are used to lookup values from the
324
-
// incoming pod labels, those key-value labels are ANDed with `LabelSelector`
325
-
// to select the group of existing pods which pods will be taken into consideration
326
-
// for the incoming pod's pod (anti) affinity. Keys that don't exist in the incoming
327
-
// pod labels will be ignored. The default value is empty.
379
+
// MatchLabelSelectors is a set of pod label keys to select the group of existing pods
380
+
// which pods will be taken into consideration for the incoming pod's pod (anti) affinity.
381
+
// The default value is empty.
328
382
// +optional
329
-
MatchLabelKeys []string
383
+
MatchLabelSelectors []strinMatchLabelSelectorg
330
384
}
331
385
```
332
386
333
387
The inter-Pod Affinity plugin will obtain the labels from the pod
334
-
labels by the keys in `MatchLabelKeys`. The obtained labels will be merged
335
-
to `LabelSelector` of `PodAffinityTerm` to filter and group pods.
336
-
The pods belonging to the same group will be evaluated.
388
+
labels by the key in `MatchLabelKey`.
389
+
390
+
The obtained labels will be merged to `LabelSelector` of `PodAffinityTerm` depending on `Operator`.
391
+
- If Operator is `In`, `key in (value)` is merged with LabelSelector.
392
+
- If Operator is `NotIn`, `key notin (value)` is merged with LabelSelector.
0 commit comments