Skip to content

Commit 15c1f05

Browse files
committed
fix(kep-3633): redesign matchLabelKeys as matchLabelSelectors to introduce Operator field
1 parent ea5255c commit 15c1f05

File tree

2 files changed

+94
-35
lines changed

2 files changed

+94
-35
lines changed

keps/sig-scheduling/3633-matchlabelkeys-to-podaffinity/README.md renamed to keps/sig-scheduling/3633-matchlabelselectors-to-podaffinity/README.md

Lines changed: 89 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ If none of those approvers are still appropriate, then changes to that list
5858
should be approved by the remaining approvers and/or the owning SIG (or
5959
SIG Architecture for cross-cutting KEPs).
6060
-->
61-
# KEP-3633: Introduce MatchLabelKeys to PodAffinity and PodAntiAffinity
61+
# KEP-3633: Introduce MatchLabelSelectors to PodAffinity and PodAntiAffinity
6262

6363
<!--
6464
This is the title of your KEP. Keep it short, simple, and descriptive. A good
@@ -85,6 +85,7 @@ tags, and then generate with `hack/update-toc.sh`.
8585
- [Proposal](#proposal)
8686
- [User Stories (Optional)](#user-stories-optional)
8787
- [Story 1](#story-1)
88+
- [Story 2](#story-2)
8889
- [Notes/Constraints/Caveats (Optional)](#notesconstraintscaveats-optional)
8990
- [Risks and Mitigations](#risks-and-mitigations)
9091
- [Design Details](#design-details)
@@ -175,7 +176,7 @@ updates.
175176
[documentation style guide]: https://github.com/kubernetes/community/blob/master/contributors/guide/style-guide.md
176177
-->
177178

178-
This KEP proposes introducing a complementary field `MatchLabelKeys` to `PodAffinityTerm`.
179+
This KEP proposes introducing a complementary field `MatchLabelSelectors` to `PodAffinityTerm`.
179180
This enables users to finely control the scope where Pods are expected to co-exist (PodAffinity)
180181
or not (PodAntiAffinity), on top of the existing `LabelSelector`.
181182

@@ -207,7 +208,7 @@ The same issue applies to other scheduling directives as well. For example, Matc
207208

208209
### Goals
209210

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.
211212

212213
### Non-Goals
213214

@@ -216,7 +217,7 @@ What is out of scope for this KEP? Listing non-goals helps to focus discussion
216217
and make progress.
217218
-->
218219

219-
- Apply additional internal labels when evaluating `MatchLabelKeys`
220+
- Apply additional internal labels when evaluating `MatchLabelSelectors`
220221

221222
## Proposal
222223

@@ -245,7 +246,7 @@ and they want only replicas from the same replicaset to be evaluated.
245246

246247
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.
247248

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.
249250

250251
```yaml
251252
apiVersion: apps/v1
@@ -263,10 +264,51 @@ metadata:
263264
values:
264265
- database
265266
topologyKey: topology.kubernetes.io/zone
266-
matchlabelKeys: # ADDED
267-
- pod-template-hash
267+
matchLabelSelectors: # ADDED
268+
- matchLabelKeys: pod-template-hash
269+
operator: In
268270
```
269271
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+
270312
### Notes/Constraints/Caveats (Optional)
271313

272314
<!--
@@ -297,7 +339,7 @@ Consider including folks who also work outside the SIG or subproject.
297339
-->
298340

299341
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
301343
which pods should be grouped. If so, the user needs to ensure that it is
302344
correct and not duplicated with other unrelated workloads.
303345

@@ -310,30 +352,44 @@ required) or even code snippets. If there's any ambiguity about HOW your
310352
proposal will be implemented, this is the place to discuss them.
311353
-->
312354

313-
A new optional field `MatchLabelKeys` is introduced to `PodAffinityTerm`.
355+
A new optional field `MatchLabelSelectors` is introduced to `PodAffinityTerm`.
314356

315357
```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+
316373
type PodAffinityTerm struct {
317374
LabelSelector *metav1.LabelSelector
318375
Namespaces []string
319376
TopologyKey string
320377
NamespaceSelector *metav1.LabelSelector
321378

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.
328382
// +optional
329-
MatchLabelKeys []string
383+
MatchLabelSelectors []strinMatchLabelSelectorg
330384
}
331385
```
332386

333387
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.
337393

338394
### Test Plan
339395

@@ -394,9 +450,9 @@ https://storage.googleapis.com/k8s-triage/index.html
394450
-->
395451

396452
- These tests will be added.
397-
- `MatchLabelKeys` in `PodAffinity` (both in Filter and Score) works as expected.
398-
- `MatchLabelKeys` in `PodAntiAffinity` (both in Filter and Score) works as expected.
399-
- `MatchLabelKeys` with the feature gate enabled/disabled.
453+
- `MatchLabelSelectors` in `PodAffinity` (both in Filter and Score) works as expected.
454+
- `MatchLabelSelectors` in `PodAntiAffinity` (both in Filter and Score) works as expected.
455+
- `MatchLabelSelectors` with the feature gate enabled/disabled.
400456

401457
**Filter**
402458
- `k8s.io/kubernetes/test/integration/scheduler/filters/filters_test.go`: https://storage.googleapis.com/k8s-triage/index.html?test=TestPodTopologySpreadFilter
@@ -420,9 +476,12 @@ https://storage.googleapis.com/k8s-triage/index.html
420476
We expect no non-infra related flakes in the last month as a GA graduation criteria.
421477
-->
422478

423-
- These e2e tests will be added.
424-
- `MatchLabelKeys: ["pod-template-hash"]` in `PodAffinity` (both in Filter and Score) works as expected with a rolling upgrade scenario.
425-
- `MatchLabelKeys: ["pod-template-hash"]` in `PodAntiAffinity` (both in Filter and Score) works as expected with a rolling upgrade scenario.
479+
N/A
480+
481+
--
482+
483+
This feature doesn't introduce any new API endpoints and doesn't interact with other components.
484+
So, E2E tests doesn't add extra value to integration tests.
426485

427486
### Graduation Criteria
428487

@@ -491,7 +550,7 @@ in back-to-back releases.
491550
#### Alpha
492551

493552
- Feature implemented behind a feature flag
494-
- Unit tests and e2e tests are implemented
553+
- Unit tests and integration tests are implemented
495554
- No significant performance degradation is observed from the benchmark test
496555

497556
#### Beta
@@ -523,11 +582,11 @@ The previous PodAffinity/PodAntiAffinity behavior will not be broken. Users can
523582
their Pod specs as it is.
524583

525584
To use this enhancement, users need to enable the feature gate (during this feature is in the alpha.),
526-
and add `MatchLabelKeys` on their PodAffinity/PodAntiAffinity.
585+
and add `MatchLabelSelectors` on their PodAffinity/PodAntiAffinity.
527586

528587
**Downgrade**
529588

530-
kube-apiserver will ignore `MatchLabelKeys` in PodAffinity/PodAntiAffinity,
589+
kube-apiserver will ignore `MatchLabelSelectors` in PodAffinity/PodAntiAffinity,
531590
and thus, kube-scheduler will also do nothing with it.
532591

533592
### Version Skew Strategy
@@ -590,7 +649,7 @@ well as the [existing list] of feature gates.
590649
-->
591650

592651
- [x] Feature gate (also fill in values in `kep.yaml`)
593-
- Feature gate name: `MatchLabelKeysInPodAffinityAndPodAntiAffinity`
652+
- Feature gate name: `MatchLabelSelectorsInPodAffinity`
594653
- Components depending on the feature gate: `kube-scheduler`, `kube-apiserver`
595654
- [ ] Other
596655

@@ -619,7 +678,7 @@ NOTE: Also set `disable-supported` to `true` or `false` in `kep.yaml`.
619678
The feature can be disabled in Alpha and Beta versions
620679
by restarting kube-apiserver and kube-scheduler with the feature-gate off.
621680
In terms of Stable versions, users can choose to opt-out by not setting the
622-
`MatchLabelKeys` field.
681+
`MatchLabelSelectors` field.
623682

624683

625684
###### What happens if we reenable the feature if it was previously rolled back?

keps/sig-scheduling/3633-matchlabelkeys-to-podaffinity/kep.yaml renamed to keps/sig-scheduling/3633-matchlabelselectors-to-podaffinity/kep.yaml

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -15,15 +15,15 @@ see-also:
1515

1616
stage: alpha
1717

18-
latest-milestone: "v1.27"
18+
latest-milestone: "v1.28"
1919

2020
milestone:
21-
alpha: "v1.27"
22-
beta: "v1.28"
23-
stable: "v1.30"
21+
alpha: "v1.28"
22+
beta: "v1.29"
23+
stable: "v1.31"
2424

2525
feature-gates:
26-
- name: MatchLabelKeysInPodAffinity
26+
- name: MatchLabelSelectorsInPodAffinity
2727
components:
2828
- kube-scheduler
2929
disable-supported: true

0 commit comments

Comments
 (0)