Skip to content

Port aggregated apiserver discovery to EndpointSlices #129837

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

Open
wants to merge 3 commits into
base: master
Choose a base branch
from

Conversation

danwinship
Copy link
Contributor

@danwinship danwinship commented Jan 27, 2025

What this PR does / why we need it:

Belatedly ports the aggregated apiserver code to use EndpointSlices rather than Endpoints.

Does this PR introduce a user-facing change?

When proxying to an aggregated API server, kube-apiserver now uses the
EndpointSlices of the `service` indicated by the `APIServer`, rather than
using Endpoints.

If you are using the aggregated API server feature, and you are writing out
the endpoints for it by hand (rather than letting kube-controller-manager
generate Endpoints and EndpointSlices for it automatically based on the
Service definition), then you should write out an EndpointSlice object rather
than (or in addition to) an Endpoints object.

Additional documentation e.g., KEPs (Kubernetes Enhancement Proposals), usage docs, etc.:

- [KEP]: https://github.com/kubernetes/enhancements/tree/master/keps/sig-network/4974-deprecate-endpoints

(The KEP is not yet merged, but this PR is correct/desirable in terms of internal consistency even if the KEP is rejected.)

/kind cleanup
/kind feature
/sig network
/sig api-machinery

@k8s-ci-robot k8s-ci-robot added release-note-action-required Denotes a PR that introduces potentially breaking changes that require user action. size/L Denotes a PR that changes 100-499 lines, ignoring generated files. kind/cleanup Categorizes issue or PR as related to cleaning up code, process, or technical debt. kind/feature Categorizes issue or PR as related to a new feature. sig/network Categorizes an issue or PR as relevant to SIG Network. sig/api-machinery Categorizes an issue or PR as relevant to SIG API Machinery. cncf-cla: yes Indicates the PR's author has signed the CNCF CLA. needs-triage Indicates an issue or PR lacks a `triage/foo` label and requires one. needs-priority Indicates a PR lacks a `priority/foo` label and requires one. labels Jan 27, 2025
@aojea
Copy link
Member

aojea commented Jan 27, 2025

(rather than letting
kube-controller-manager generate Endpoints and EndpointSlices for it
automatically based on the Service definition), then you need to write out an
EndpointSlice object rather than (or in addition to) an Endpoints object.

will not be solved by the mirroring controller?

@aojea
Copy link
Member

aojea commented Jan 27, 2025

one suggestion, in the eventual situation something goes wrong with one of the changes, having all of them in the same commit will revert all, should we have one commit for aggregator, other for proxy and other for remote available controller so we avoid that possible problem?

@danwinship
Copy link
Contributor Author

will not be solved by the mirroring controller?

yes, but

  1. I want the mirroring controller to eventually be optional too.
  2. There's no good reason for writing out an Endpoints and then letting the mirroring controller rewrite it to an EndpointSlice for you, rather than just writing out the EndpointSlice yourself.

@danwinship
Copy link
Contributor Author

one commit for aggregator, other for proxy and other for remote available controller

They're all basically a single feature (proxy.ResolveEndpoints is only called by the aggregator code). I don't think we want to ship a state where part of the feature uses Endpoints and part uses EndpointSlices.

@danwinship danwinship force-pushed the aggregated-apiserver-endpointslices branch from 6e33fa6 to 3aed1e3 Compare January 27, 2025 20:03
@aojea
Copy link
Member

aojea commented Jan 27, 2025

/lgtm

/assign @liggitt @deads2k

@k8s-ci-robot k8s-ci-robot added the lgtm "Looks good to me", indicates that a PR is ready to be merged. label Jan 27, 2025
@k8s-ci-robot
Copy link
Contributor

LGTM label has been added.

Git tree hash: cbc6a1a992fedf76a537663543b4993d4eee61aa

ep := &slice.Endpoints[(epi+offset)%len(slice.Endpoints)]
if ep.Conditions.Ready == nil || *ep.Conditions.Ready {
ip := ep.Addresses[0]
port := int(*slice.Ports[i].Port)
Copy link
Member

Choose a reason for hiding this comment

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

godoc on the Port field sounds like Port can be nil... should we check / require a non-nil port value above when deciding if the Port is valid for our use?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Huh. This is another unused future-extensibility point. Port is allowed to be unset (and thus not validated as required) even though it's required to be set for EndpointSlices corresponding to Services. Grumble.

Copy link
Member

Choose a reason for hiding this comment

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

Yeah... I just don't know the EndpointSlice API surface well enough to know which of these "allowed by the spec" things we have to handle, so my default is to check everything we could possibly encounter

Copy link
Member

Choose a reason for hiding this comment

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

maybe I'm missing it, but it still looks like we can hit a nil port and panic here? should we add || slice.Ports[i].Port == nil to the conditions above where we just continue?

@BenTheElder
Copy link
Member

/triage accepted
/cc @seans3

@k8s-ci-robot k8s-ci-robot requested a review from seans3 January 28, 2025 21:17
@k8s-ci-robot k8s-ci-robot added triage/accepted Indicates an issue or PR is ready to be actively worked on. and removed needs-triage Indicates an issue or PR lacks a `triage/foo` label and requires one. labels Jan 28, 2025
@danwinship danwinship force-pushed the aggregated-apiserver-endpointslices branch from 3aed1e3 to 139a933 Compare January 29, 2025 18:45
@aojea
Copy link
Member

aojea commented Mar 17, 2025

/lgtm

@k8s-ci-robot k8s-ci-robot added the lgtm "Looks good to me", indicates that a PR is ready to be merged. label Mar 17, 2025
@k8s-ci-robot
Copy link
Contributor

LGTM label has been added.

Git tree hash: 626403b39cc1e94c2349cfd9ae9b5f4d95fbc5a9

// If there are multiple slices, we want to look at them in a random
// order, but we need to look at all of the slices of the primary IP
// family first.
preferredAddressType := discoveryv1.AddressType(svc.Spec.IPFamilies[0])
Copy link
Member

Choose a reason for hiding this comment

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

ensure svc.Spec.IPFamilies is non-zero length before indexing into [0], since there are circumstances where IPFamilies is blank

Copy link
Contributor Author

@danwinship danwinship Mar 18, 2025

Choose a reason for hiding this comment

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

IPFamilies can only be blank if the Service is ExternalName or headless, but it appears that ResolveEndpoint does allow you to use a headless Service. OK.

EDIT: Actually, from what I can tell, IPFamilies seems to get defaulted (via registry, not apis) even for headless services. However, I definitely couldn't prove that that's the case so I'll add the check anyway...

ep := &slice.Endpoints[(epi+offset)%len(slice.Endpoints)]
if ep.Conditions.Ready == nil || *ep.Conditions.Ready {
ip := ep.Addresses[0]
port := int(*slice.Ports[i].Port)
Copy link
Member

Choose a reason for hiding this comment

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

maybe I'm missing it, but it still looks like we can hit a nil port and panic here? should we add || slice.Ports[i].Port == nil to the conditions above where we just continue?

@@ -70,32 +76,50 @@ func ResolveEndpoint(services listersv1.ServiceLister, endpoints listersv1.Endpo
return nil, err
}

eps, err := endpoints.Endpoints(namespace).Get(svc.Name)
endpointSliceSelector := labels.SelectorFromSet(labels.Set{discoveryv1.LabelServiceName: svc.Name})
slices, err := endpointSlices.EndpointSlices(namespace).List(endpointSliceSelector)
Copy link
Member

@liggitt liggitt Mar 18, 2025

Choose a reason for hiding this comment

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

This changes an O(1) lookup by name into a O(n) selection.

I don't expect this to be an issue under normal circumstances, but when there are bananas numbers of services in a single namespace, this could be observably more expensive. We sort of won't know until this change gets into the wild what clusters with unexpectedly large numbers of endpointslices in a namespace this lookup degrades.

Should we add a namespaceName+svcName indexer onto the endpointslice informer/lister, and make use of that to keep from having to traverse / do selection on all endpointslices in a namespace on every webhook / aggregated server dial?

The top commit of https://github.com/liggitt/kubernetes/commits/aggregated-apiserver-endpointslices/ has a sketch of what that could look like. I'm on the fence...

Copy link
Contributor Author

Choose a reason for hiding this comment

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

do selection on all endpointslices in a namespace on every webhook

oh... huh, I was assuming that because this function is only called from within staging/src/k8s.io/kube-aggregator, that that meant it was only used for aggregated apiserver stuff (and thus "bananas numbers of services in the same namespace" was unlikely to be a problem). Looks like the API outgrew its original use case.

I'm on the fence...

Yeah, I understand the general concern, but I don't have any sense of if it matters in this specific case...

(Though if we add EndpointSliceGetter it should eventually migrate somewhere more public/generic, possibly as part of #124777.)

Copy link
Member

Choose a reason for hiding this comment

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

We are limited by IPs in Services so it will unlikely to have 1M of Services (famous last words :) ) but with 100k we already take a 7 ms hits that is really high for http requests #130967

Copy link
Contributor

Choose a reason for hiding this comment

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

1M Services isn't unreasonable - folks have reached a third of that using Knative and ended up re-architecting it to remove the use of services

See: https://render.com/blog/knative

Copy link
Member

@aojea aojea Mar 21, 2025

Choose a reason for hiding this comment

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

folks have reached a third of that using Knative

glad to know, and I also heard good feedback from that community with the ServiceCIDR feature since they seems to consume those IPs very quickly, but still feel is a long shot to get there, those are a lot of ips , 2^20 = 1,048,576 , means they have to use a /12 mask ... I'll be the most happy person in the world if we get to the 1M of Services

return err
} else if err != nil {
endpointSliceSelector := labels.SelectorFromSet(labels.Set{discoveryv1.LabelServiceName: apiService.Spec.Service.Name})
endpointSlices, err := c.endpointSliceLister.EndpointSlices(apiService.Spec.Service.Namespace).List(endpointSliceSelector)
Copy link
Member

Choose a reason for hiding this comment

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

same comment about wondering if this should make use of an indexer, the POC commit I linked to also fixes up this call site

@danwinship danwinship force-pushed the aggregated-apiserver-endpointslices branch from 139a933 to f79dcf9 Compare March 18, 2025 18:04
@k8s-ci-robot k8s-ci-robot removed the lgtm "Looks good to me", indicates that a PR is ready to be merged. label Mar 18, 2025
@k8s-ci-robot k8s-ci-robot requested review from aojea and liggitt March 18, 2025 18:04
@k8s-ci-robot
Copy link
Contributor

New changes are detected. LGTM label has been removed.

@danwinship danwinship force-pushed the aggregated-apiserver-endpointslices branch from f79dcf9 to c141385 Compare May 26, 2025 17:21
@k8s-ci-robot k8s-ci-robot added size/XXL Denotes a PR that changes 1000+ lines, ignoring generated files. area/test sig/testing Categorizes an issue or PR as relevant to SIG Testing. and removed size/XL Denotes a PR that changes 500-999 lines, ignoring generated files. labels May 26, 2025
@k8s-ci-robot
Copy link
Contributor

[APPROVALNOTIFIER] This PR is NOT APPROVED

This pull-request has been approved by: danwinship
Once this PR has been reviewed and has the lgtm label, please ask for approval from liggitt. For more information see the Code Review Process.

The full list of commands accepted by this bot can be found here.

Needs approval from an approver in each of these files:

Approvers can indicate their approval by writing /approve in a comment
Approvers can cancel approval by writing /approve cancel in a comment

@danwinship
Copy link
Contributor Author

Now includes an updated version of Jordan's "Sketch of indexing on endpointslice namespace/service-name". As compared to that:

  • The code is moved to k8s.io/endpointslice/util.
  • Fixed a race condition if two different goroutines tried to create an EndpointSliceIndexerGetter off the same indexer at the same time.
  • The new indexer now skips EndpointSlices that don't have the service-name label.
  • Has unit tests and slightly-improved documentation.

Plus the new push also includes the e2e test update that got bumped from #131732 because it depends specifically on whether the proxying code uses Endpoints or EndpointSlices.

@k8s-ci-robot
Copy link
Contributor

@danwinship: The following tests failed, say /retest to rerun all failed tests or /retest-required to rerun all mandatory failed tests:

Test name Commit Details Required Rerun command
pull-kubernetes-dependencies c141385 link true /test pull-kubernetes-dependencies
pull-kubernetes-verify c141385 link true /test pull-kubernetes-verify

Full PR test history. Your PR dashboard. Please help us cut down on flakes by linking to an open issue when you hit one in your PR.

Instructions for interacting with me using PR comments are available here. If you have questions or suggestions related to my behavior, please file an issue against the kubernetes-sigs/prow repository. I understand the commands that are listed here.

@danwinship
Copy link
Contributor Author

The code is moved to k8s.io/endpointslice/util.

which makes verify-imports.sh fail... not sure if I should update import-restrictions.yaml or move the code somewhere else. It's only used by apiserver stuff as of this commit, but it's theoretically more generically useful. (Though, FWIW, it would not ever be used by kube-proxy, because it would be inefficient there to re-construct the []*discoveryv1.EndpointSlice every time we needed the slices for a Service; we really need a modified Informer there, not a modified Lister.)

@danwinship
Copy link
Contributor Author

@liggitt this is ready for review (though we need to decide where to put the code / how to deal with the verify-imports failure)

@BenTheElder
Copy link
Member

Jordan is OOO through 6/6 (From slack status, github just says on vacation)

@Prajyot-Parab
Copy link

Hello @danwinship @liggitt
This PR has not been updated for a month, so I'd like to check what's the status. If there's anything we can do, please let us know. The code freeze is starting 02:00 UTC Friday 25th July 2025 (about 4 weeks from now). Please make sure the PR has both lgtm and approved labels before the code freeze.
Thanks!

@Prajyot-Parab Prajyot-Parab moved this from Pending inclusion to Tracked in [sig-release] Bug Triage Jun 30, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area/apiserver area/test cncf-cla: yes Indicates the PR's author has signed the CNCF CLA. kind/cleanup Categorizes issue or PR as related to cleaning up code, process, or technical debt. kind/feature Categorizes issue or PR as related to a new feature. needs-priority Indicates a PR lacks a `priority/foo` label and requires one. release-note Denotes a PR that will be considered when it comes time to generate release notes. sig/api-machinery Categorizes an issue or PR as relevant to SIG API Machinery. sig/network Categorizes an issue or PR as relevant to SIG Network. sig/testing Categorizes an issue or PR as relevant to SIG Testing. size/XXL Denotes a PR that changes 1000+ lines, ignoring generated files. triage/accepted Indicates an issue or PR is ready to be actively worked on.
Projects
Status: Tracked
Development

Successfully merging this pull request may close these issues.

9 participants