Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

KEP-3243: Update the design to mutate the label selector based on matchLabelKeys at api-server instead of the scheduler handling it #5033

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,8 @@ tags, and then generate with `hack/update-toc.sh`.
- [Implementation History](#implementation-history)
- [Drawbacks](#drawbacks)
- [Alternatives](#alternatives)
- [use pod generateName](#use-pod-generatename)
- [remove MatchLabelKeys implementation from the scheduler plugin](#remove-matchlabelkeys-implementation-from-the-scheduler-plugin)
- [Infrastructure Needed (Optional)](#infrastructure-needed-optional)
<!-- /toc -->

Expand Down Expand Up @@ -179,10 +181,13 @@ which spreading is applied using a LabelSelector. This means the user should
know the exact label key and value when defining the pod spec.

This KEP proposes a complementary field to LabelSelector named `MatchLabelKeys` in
`TopologySpreadConstraint` which represent a set of label keys only. The scheduler
will use those keys to look up label values from the incoming pod; and those key-value
labels are ANDed with `LabelSelector` to identify the group of existing pods over
which the spreading skew will be calculated.
`TopologySpreadConstraint` which represent a set of label keys only.
kube-apiserver will use those keys to look up label values from the incoming pod
and those labels are merged to `LabelSelector`.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
and those labels are merged to `LabelSelector`.
and those key-value labels are ANDed with `LabelSelector` to identify the group of existing pods over
which the spreading skew will be calculated.

kube-scheduler will also look up the label values from the pod and check if those
labels are included in `LabelSelector`. If not, kube-scheduler will take those labels
and AND with `LabelSelector` to identify the group of existing pods over which the
spreading skew will be calculated.
Comment on lines +187 to +190
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
kube-scheduler will also look up the label values from the pod and check if those
labels are included in `LabelSelector`. If not, kube-scheduler will take those labels
and AND with `LabelSelector` to identify the group of existing pods over which the
spreading skew will be calculated.
kube-scheduler will also handle it if the cluster-level default constraints have the one with `MatchLabelKeys`.


The main case that this new way for identifying pods will enable is constraining
skew spreading calculation to happen at the revision level in Deployments during
Expand Down Expand Up @@ -307,15 +312,10 @@ required) or even code snippets. If there's any ambiguity about HOW your
proposal will be implemented, this is the place to discuss them.
-->

A new field named `MatchLabelKeys` will be added to `TopologySpreadConstraint`.
A new optional field named `MatchLabelKeys` will be introduced to`TopologySpreadConstraint`.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We should keep this part.

Suggested change
A new optional field named `MatchLabelKeys` will be introduced to`TopologySpreadConstraint`.
A new optional field named `MatchLabelKeys` will be introduced to`TopologySpreadConstraint`.
Currently, when scheduling a pod, the `LabelSelector` defined in the pod is used
to identify the group of pods over which spreading will be calculated.
`MatchLabelKeys` adds another constraint to how this group of pods is identified

Currently, when scheduling a pod, the `LabelSelector` defined in the pod is used
to identify the group of pods over which spreading will be calculated.
`MatchLabelKeys` adds another constraint to how this group of pods is identified:
the scheduler will use those keys to look up label values from the incoming pod;
and those key-value labels are ANDed with `LabelSelector` to select the group of
existing pods over which spreading will be calculated.

A new field named `MatchLabelKeys` will be introduced to`TopologySpreadConstraint`:
`MatchLabelKeys` adds another constraint to how this group of pods is identified.
```go
type TopologySpreadConstraint struct {
MaxSkew int32
Expand All @@ -333,27 +333,55 @@ type TopologySpreadConstraint struct {
}
```

Examples of use are as follows:
When a Pod is created, kube-apiserver will obtain the labels from the pod
by the keys in `matchLabelKeys` and `key in (value)` is merged to `LabelSelector`
of `TopologySpreadConstraint`.

For example, when this sample Pod is created,

```yaml
topologySpreadConstraints:
- maxSkew: 1
topologyKey: kubernetes.io/hostname
whenUnsatisfiable: DoNotSchedule
matchLabelKeys:
- app
- pod-template-hash
apiVersion: v1
kind: Pod
metadata:
name: sample
labels:
app: sample
...
topologySpreadConstraints:
- maxSkew: 1
topologyKey: kubernetes.io/hostname
whenUnsatisfiable: DoNotSchedule
labelSelector: {}
matchLabelKeys: # ADDED
- app
```

kube-apiserver modifies the `labelSelector` like the following:

```diff
topologySpreadConstraints:
- maxSkew: 1
topologyKey: kubernetes.io/hostname
whenUnsatisfiable: DoNotSchedule
labelSelector:
+ matchExpressions:
+ - key: app
+ operator: In
+ values:
+ - sample
matchLabelKeys:
- app
```

The scheduler plugin `PodTopologySpread` will obtain the labels from the pod
labels by the keys in `matchLabelKeys`. The obtained labels will be merged
to `labelSelector` of `topologySpreadConstraints` to filter and group pods.
The pods belonging to the same group will be part of the spreading in
`PodTopologySpread`.
kube-scheduler will also be aware of `matchLabelKeys` and gracefully handle the same labels.
This is for the Cluster-level default constraints by
`matchLabelKeys: ["pod-template-hash"]`.([#129198](https://github.com/kubernetes/kubernetes/issues/129198))
Comment on lines +376 to +378
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
kube-scheduler will also be aware of `matchLabelKeys` and gracefully handle the same labels.
This is for the Cluster-level default constraints by
`matchLabelKeys: ["pod-template-hash"]`.([#129198](https://github.com/kubernetes/kubernetes/issues/129198))
Also, kube-scheduler handles `matchLabelKeys` if the cluster-level default constraints is configured with `matchLabelKeys`.


Finally, the feature will be guarded by a new feature flag. If the feature is
disabled, the field `matchLabelKeys` is preserved if it was already set in the
persisted Pod object, otherwise it is silently dropped; moreover, kube-scheduler
will ignore the field and continue to behave as before.
disabled, the field `matchLabelKeys` and corresponding`labelSelector` are preserved
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
disabled, the field `matchLabelKeys` and corresponding`labelSelector` are preserved
disabled, the field `matchLabelKeys` and corresponding `labelSelector` are preserved

if it was already set in the persisted Pod object, otherwise new Pod with the field
creation will be rejected by kube-apiserver; moreover, kube-scheduler will ignore the
field and continue to behave as before.
Comment on lines +383 to +384
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

kube-scheduler cannot determine which label selector(s) is generated by matchLabelKeys at kube-apiserver, and hence it couldn't ignore matchLabelKeys even after the downgrade. the cluster-level default constraints configuration is the exception though

Suggested change
creation will be rejected by kube-apiserver; moreover, kube-scheduler will ignore the
field and continue to behave as before.
creation will be rejected by kube-apiserver.
Also, kube-scheduler will ignore matchLabelKeys in the cluster-level default constraints configuration.


Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

So, I think here you can add another section [v1.33] design change and a safe upgrade path, and describe:

  • For a safe upgrade path from v1.32 to v1.33, kube-scheduler would handle not only matchLabelKeys from the default constraints, but all in-coming pods during v1.33. And, you also need to mention the reason described like my comment.
  • So, matchLabelKeys within in-coming pods are handled by both kube-apiserver and kube-scheduler at v1.33.
  • We'll change kube-scheduler to only concern matchLabelKeys from the default constraints at v1.34 for efficiency, assuming matchLabelKeys of all in-coming pods are handled by kube-apiserver.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks! This is exactly what concerned me while revising the KEP draft.

### Test Plan

Expand Down Expand Up @@ -400,8 +428,9 @@ This can inform certain test coverage improvements that we want to do before
extending the production code to implement this enhancement.
-->

- `k8s.io/kubernetes/pkg/scheduler/framework/plugins/podtopologyspread`: `06-07` - `86%`
- `k8s.io/kubernetes/pkg/scheduler/framework/plugins/podtopologyspread/plugin.go`: `06-07` - `73.1%`
- `k8s.io/kubernetes/pkg/scheduler/framework/plugins/podtopologyspread`: `2025-01-14 JST (The commit hash: ccd2b4e8a719dabe8605b1e6b2e74bb5352696e1)` - `87.5%`
- `k8s.io/kubernetes/pkg/scheduler/framework/plugins/podtopologyspread/plugin.go`: `2025-01-14 JST (The commit hash: ccd2b4e8a719dabe8605b1e6b2e74bb5352696e1)` - `84.8%`
- `k8s.io/kubernetes/pkg/registry/core/pod/strategy.go`: `2025-01-14 JST (The commit hash: ccd2b4e8a719dabe8605b1e6b2e74bb5352696e1)` - `65%`

##### Integration tests

Expand Down Expand Up @@ -532,7 +561,9 @@ enhancement:

In the event of an upgrade, kube-apiserver will start to accept and store the field `MatchLabelKeys`.

In the event of a downgrade, kube-scheduler will ignore `MatchLabelKeys` even if it was set.
In the event of a downgrade, kube-apiserver will reject pod creation with `matchLabelKeys` in `TopologySpreadConstraint`.
But, regarding existing pods, we leave `matchLabelKeys` and generated `LabelSelector` even after downgraded.
kube-scheduler will ignore `MatchLabelKeys` even if it was set.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ditto

Suggested change
kube-scheduler will ignore `MatchLabelKeys` even if it was set.
kube-scheduler will ignore `MatchLabelKeys` if it was set in the cluster-level default constraints configuration.


### Version Skew Strategy

Expand Down Expand Up @@ -619,8 +650,11 @@ NOTE: Also set `disable-supported` to `true` or `false` in `kep.yaml`.
The feature can be disabled in Alpha and Beta versions by restarting
kube-apiserver and kube-scheduler with feature-gate off.
One caveat is that pods that used the feature will continue to have the
MatchLabelKeys field set even after disabling the feature gate,
however kube-scheduler will not take the field into account.
MatchLabelKeys field set and the corresponding LabelSelector even after
disabling the feature gate, however kube-scheduler will not take the MatchLabelKeys
field into account.
Comment on lines +654 to +655
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
disabling the feature gate, however kube-scheduler will not take the MatchLabelKeys
field into account.
disabling the feature gate.

In terms of Stable versions, users can choose to opt-out by not setting
the matchLabelKeys field.

###### What happens if we reenable the feature if it was previously rolled back?
Newly created pods need to follow this policy when scheduling. Old pods will
Expand Down Expand Up @@ -659,7 +693,8 @@ feature flags will be enabled on some API servers and not others during the
rollout. Similarly, consider large clusters and how enablement/disablement
will rollout across nodes.
-->
It won't impact already running workloads because it is an opt-in feature in scheduler.
It won't impact already running workloads because it is an opt-in feature in kube-apiserver
and kube-scheduler.
But during a rolling upgrade, if some apiservers have not enabled the feature, they will not
be able to accept and store the field "MatchLabelKeys" and the pods associated with these
apiservers will not be able to use this feature. As a result, pods belonging to the
Expand Down Expand Up @@ -896,8 +931,14 @@ Think about adding additional work or introducing new steps in between

[existing SLIs/SLOs]: https://git.k8s.io/community/sig-scalability/slos/slos.md#kubernetes-slisslos
-->
Yes. there is an additional work: the scheduler will use the keys in `matchLabelKeys` to look up label values from the pod and AND with `LabelSelector`.
Maybe result in a very samll impact in scheduling latency which directly contributes to pod-startup-latency SLO.
Yes. there is an additional work:
kube-apiserver uses the keys in `matchLabelKeys` to look up label values from the pod,
and change `LabelSelector` according to them.
kube-scheduler also looks up the label values from the pod and checks if those labels
are included in `LabelSelector`. If not, kube-scheduler will take those labels and AND
with `LabelSelector`.
Comment on lines +937 to +939
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
kube-scheduler also looks up the label values from the pod and checks if those labels
are included in `LabelSelector`. If not, kube-scheduler will take those labels and AND
with `LabelSelector`.
kube-scheduler also handles matchLabelKeys if the cluster-level default constraints has it.

The impact in the latency of pod creation request in kube-apiserver and kube-scheduler
should be negligible.

###### Will enabling / using this feature result in non-negligible increase of resource usage (CPU, RAM, disk, IO, ...) in any components?

Expand Down Expand Up @@ -937,7 +978,7 @@ details). For now, we leave it here.

###### How does this feature react if the API server and/or etcd is unavailable?
If the API server and/or etcd is not available, this feature will not be available.
This is because the scheduler needs to update the scheduling results to the pod via the API server/etcd.
This is because the kube-scheduler needs to update the scheduling results to the pod via the API server/etcd.

###### What are other known failure modes?

Expand All @@ -963,7 +1004,7 @@ N/A
- Check the metric `schedule_attempts_total{result="error|unschedulable"}` to determine if the number
of attempts increased. If increased, You need to determine the cause of the failure by the event of
the pod. If it's caused by plugin `PodTopologySpread`, You can further analyze this problem by looking
at the scheduler log.
at the kube-scheduler log.


## Implementation History
Expand Down Expand Up @@ -996,11 +1037,20 @@ not need to be as detailed as the proposal, but should include enough
information to express the idea and why it was not acceptable.
-->

### use pod generateName
Use `pod.generateName` to distinguish new/old pods that belong to the
revisions of the same workload in scheduler plugin. It's decided not to
support because of the following reason: scheduler needs to ensure universal
and scheduler plugin shouldn't have special treatment for any labels/fields.

### remove MatchLabelKeys implementation from the scheduler plugin
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
### remove MatchLabelKeys implementation from the scheduler plugin
### implement MatchLabelKeys in only either the scheduler plugin or kube-apiserver

Then, briefly mention why we have to implement it in kube-apiserver too.

Remove this implementation related to `MatchLabelKeys` from the scheduler plugin
and only kube-apiserver handles `MatchLabelKeys` and updates `LabelSelector`.

However, this idea is rejected because:
- This approach prevents the achievement of the Cluster-level default constraints by `matchLabelKeys: ["pod-template-hash"]`.([#129198](https://github.com/kubernetes/kubernetes/issues/129198)) because kube-apiserver can't be aware of the kube-scheduler configuration.
- The current implementation of the scheduler plugin is simple, and the risk of increased maintenance overhead is low.

## Infrastructure Needed (Optional)

<!--
Expand Down