Skip to content

restmapper + discovery: add context-aware APIs #129109

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 2 commits into
base: master
Choose a base branch
from

Conversation

pohly
Copy link
Contributor

@pohly pohly commented Dec 6, 2024

What type of PR is this?

/kind feature

What this PR does / why we need it:

The main purpose is to replace context.TODO with a context provided by the caller. A secondary purpose is to enable contextual logging.

Which issue(s) this PR fixes:

Fixes #110810

Special notes for your reviewer:

Modifying the existing interfaces and APIs would have a big impact on the ecosystem. This is a no-go. Instead, the following approach was taken:

  • All interfaces get duplicated in a *WithContext variant where the methods also have a *WithContext suffix and the ctx parameter. All methods are treated this way except for obvious local get methods (like RESTClient) because it cannot be ruled out entirely that some implementation may need a context.

  • Implementations of these interfaces implement both method variants, which is possible because the method names are different. The old methods are implemented as thin wrappers around the updated code which is now the body of the new methods or shared helpers. In some cases there is additional overhead (type checks, potentially additional allocations) when using the old methods.

  • *2Context helpers bridge from the old to the new interfaces. They try a type cast first. Because the in-tree implementations implement both, they can be used directly. For other implementations wrappers are used.

  • All old APIs and interfaces are marked as deprecated. There is no intent to ever remove them, but consumers should be made aware that there are now better alternatives. Implementations also get marked this way even if nothing ever calls them directly because it shows which code, at least theoretically, could get removed.

  • Existing unit tests do not get updated to the new APIs. This gives us unit test coverage of the old and new API because the old APIs call the new ones.

  • In-tree consumers will be updated in follow-up PRs. This is likely to be a longer process. Because of the deprecation comment, hack/golangci-lint.sh -n can be used to find code which needs to be updated.

Does this PR introduce a user-facing change?

client-go now has context-aware APIs for REST mapping and discovery. This addresses a long-standing problem that the implementations under the hood made blocking API calls with `context.TODO`. Consumers of client-go are encouraged to switch to the new APIs and therefore they get marked as `Deprecated`. However, there is no plan to ever remove the old APIs.

/cc @ardaguclu @sttts

@k8s-ci-robot k8s-ci-robot added the release-note Denotes a PR that will be considered when it comes time to generate release notes. label Dec 6, 2024
@k8s-ci-robot
Copy link
Contributor

Please note that we're already in Test Freeze for the release-1.32 branch. This means every merged PR will be automatically fast-forwarded via the periodic ci-fast-forward job to the release branch of the upcoming v1.32.0 release.

Fast forwards are scheduled to happen every 6 hours, whereas the most recent run was: Fri Dec 6 16:44:50 UTC 2024.

@k8s-ci-robot k8s-ci-robot added kind/feature Categorizes issue or PR as related to a new feature. size/XXL Denotes a PR that changes 1000+ lines, ignoring generated files. cncf-cla: yes Indicates the PR's author has signed the CNCF CLA. do-not-merge/needs-sig Indicates an issue or PR lacks a `sig/foo` label and requires one. 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. sig/api-machinery Categorizes an issue or PR as relevant to SIG API Machinery. and removed do-not-merge/needs-sig Indicates an issue or PR lacks a `sig/foo` label and requires one. labels Dec 6, 2024
// unique across groups.
type RESTMapperWithContext interface {
// KindFor takes a partial resource and returns the single match. Returns an error if there are multiple matches
KindForWithContext(ctx context.Context, resource schema.GroupVersionResource) (schema.GroupVersionKind, error)
Copy link
Contributor Author

Choose a reason for hiding this comment

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

I am using the *WithContext pattern consistently because that's what we settled on in Kubernetes. However, it leads to long names which in this case are also a bit odd.

In Go, *Context is used more often than *WithContext:

# e.g. database/sql/sql.go:func (db *DB) PingContext(ctx context.Context) error {
$ grep -r 'func.*Context(ctx' /nvme/gopath/go/src/ | grep -v WithContext | wc -l
55
# e.g. net/http/request.go:func NewRequestWithContext(ctx context.Context, method, url string, body io.Reader) (*Request, error)
$ grep -r 'func.*WithContext(ctx' /nvme/gopath/go/src/ | wc -l
5

Should we follow that practice and use the shorter *Context pattern?

Then this becomes:

type RESTMapperContext interface {
    KindForContext(ctx context.Context, resource schema.GroupVersionResource) (schema.GroupVersionKind, error)

Copy link
Member

Choose a reason for hiding this comment

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

Although *Context is used far more and makes sense, RESTMapperContext may mislead people about it behaves as a Context rather than having a Context.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

That's a good point.

So let's stick with *WithContext?

Copy link
Contributor

Choose a reason for hiding this comment

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

I will admit I'm not super thrilled by the lengthiness of this, but I'm always for consistency. So 👍 for *WithContext.

@pohly pohly force-pushed the log-client-go-restmapper-discovery branch from 89a610e to 7af7a6a Compare December 9, 2024 07:11
Copy link
Member

@ardaguclu ardaguclu left a comment

Choose a reason for hiding this comment

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

Without any in-depth code review, overall method seems to be the most plausible one for least disruption and makes sense to me.
We may need to ask additional opinions from;
openapi/discovery
@seans3 @Jefftree
restmapper
@deads2k @liggitt

// unique across groups.
type RESTMapperWithContext interface {
// KindFor takes a partial resource and returns the single match. Returns an error if there are multiple matches
KindForWithContext(ctx context.Context, resource schema.GroupVersionResource) (schema.GroupVersionKind, error)
Copy link
Member

Choose a reason for hiding this comment

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

Although *Context is used far more and makes sense, RESTMapperContext may mislead people about it behaves as a Context rather than having a Context.

@pohly
Copy link
Contributor Author

pohly commented Dec 9, 2024

/test pull-kubernetes-apidiff

@pohly pohly force-pushed the log-client-go-restmapper-discovery branch from 7af7a6a to 71efc9a Compare December 9, 2024 17:05
@pohly pohly force-pushed the log-client-go-restmapper-discovery branch 2 times, most recently from 638a635 to edb084b Compare December 10, 2024 10:27
@Jefftree
Copy link
Member

/triage accepted

@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 Dec 10, 2024
@pohly pohly force-pushed the log-client-go-restmapper-discovery branch from edb084b to 3366e33 Compare December 12, 2024 14:17
pohly added a commit to pohly/kubernetes that referenced this pull request Dec 12, 2024
@k8s-ci-robot k8s-ci-robot added do-not-merge/contains-merge-commits Indicates a PR which contains merge commits. area/code-generation area/dependency Issues or PRs related to dependency changes area/test labels Dec 12, 2024
pohly added a commit to pohly/kubernetes that referenced this pull request Jan 20, 2025
The Lister and Watcher interfaces only supported methods without context, but
were typically implemented with client-go API calls which need a context. New
interfaces get added using the same approach as in
kubernetes#129109.
pohly added a commit to pohly/kubernetes that referenced this pull request Jan 20, 2025
pohly added a commit to pohly/kubernetes that referenced this pull request Jan 22, 2025
The Lister and Watcher interfaces only supported methods without context, but
were typically implemented with client-go API calls which need a context. New
interfaces get added using the same approach as in
kubernetes#129109.
pohly added a commit to pohly/kubernetes that referenced this pull request Jan 22, 2025
pohly added a commit to pohly/kubernetes that referenced this pull request Jan 23, 2025
The Lister and Watcher interfaces only supported methods without context, but
were typically implemented with client-go API calls which need a context. New
interfaces get added using the same approach as in
kubernetes#129109.
pohly added a commit to pohly/kubernetes that referenced this pull request Jan 28, 2025
pohly added a commit to pohly/kubernetes that referenced this pull request Jan 30, 2025
The Lister and Watcher interfaces only supported methods without context, but
were typically implemented with client-go API calls which need a context. New
interfaces get added using the same approach as in
kubernetes#129109.
@pohly pohly force-pushed the log-client-go-restmapper-discovery branch from 21e9af1 to 852983a Compare January 30, 2025 11:01
@k8s-ci-robot
Copy link
Contributor

[APPROVALNOTIFIER] This PR is NOT APPROVED

This pull-request has been approved by: pohly
Once this PR has been reviewed and has the lgtm label, please assign deads2k for approval. 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

@pohly
Copy link
Contributor Author

pohly commented Jan 30, 2025

/test pull-kubernetes-integration

#129908

pohly added a commit to pohly/kubernetes that referenced this pull request Feb 7, 2025
k8s-publishing-bot pushed a commit to kubernetes/apimachinery that referenced this pull request Feb 7, 2025
The Lister and Watcher interfaces only supported methods without context, but
were typically implemented with client-go API calls which need a context. New
interfaces get added using the same approach as in
kubernetes/kubernetes#129109.

Kubernetes-commit: 6688adae142e37114d9dfa8d94cd1d8a91fbcc13
k8s-publishing-bot pushed a commit to kubernetes/client-go that referenced this pull request Feb 7, 2025
The Lister and Watcher interfaces only supported methods without context, but
were typically implemented with client-go API calls which need a context. New
interfaces get added using the same approach as in
kubernetes/kubernetes#129109.

Kubernetes-commit: 6688adae142e37114d9dfa8d94cd1d8a91fbcc13
Copy link
Contributor

@soltysh soltysh left a comment

Choose a reason for hiding this comment

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

I left a few comments, but the major ones are:

  1. I believe we agreed to NOT do deprecation on methods w/o context.
  2. What should we do with tests in staging/src/k8s.io/apimachinery/pkg/api/meta/ directory, currently they only work with non-context variants. Should we duplicate them for context variants?

The rest seems reasonable, but I will want to do another pass.

if m, ok := m.(RESTMapperWithContext); ok {
return m
}
return &restMapperWrapper{
Copy link
Contributor

Choose a reason for hiding this comment

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

In other place (#129341) you used value receiver. So for consistency, and based on what you're writing above we should also update

func ToWatcherWithContext(w Watcher) WatcherWithContext {
if w, ok := w.(WatcherWithContext); ok {
return w
}
return watcherWrapper{
parent: w,
}
}
type watcherWrapper struct {
parent Watcher
}
to use pointer as well.

// unique across groups.
type RESTMapperWithContext interface {
// KindFor takes a partial resource and returns the single match. Returns an error if there are multiple matches
KindForWithContext(ctx context.Context, resource schema.GroupVersionResource) (schema.GroupVersionKind, error)
Copy link
Contributor

Choose a reason for hiding this comment

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

I will admit I'm not super thrilled by the lengthiness of this, but I'm always for consistency. So 👍 for *WithContext.

_ ResettableRESTMapper = MultiRESTMapper{}
_ fmt.Stringer = MultiRESTMapper{}
_ ResettableRESTMapperWithContext = MultiRESTMapperWithContext{}
_ fmt.Stringer = MultiRESTMapperWithContext{}
Copy link
Contributor

Choose a reason for hiding this comment

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

👍

@@ -30,11 +31,14 @@ const (
)

var (
_ ResettableRESTMapper = PriorityRESTMapper{}
_ ResettableRESTMapper = PriorityRESTMapper{}
_ ResettableRESTMapperWithContext = PriorityRESTMapperWithContext{}
Copy link
Contributor

Choose a reason for hiding this comment

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

Probably also worth adding fmt.Stringer like you did with MultiRESTMapper above.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Done.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Note that I decided to keep using PriorityRESTMapperWithContext as value: that's because ToPriorityRESTMapperWithContext returns the actual struct, not an interface, and the struct is small enough that passing it by value should be better, in particular because all callers immediately discard it again.

@@ -72,6 +73,7 @@ func (m *DefaultRESTMapper) String() string {
}

var _ RESTMapper = &DefaultRESTMapper{}
var _ RESTMapperWithContext = &DefaultRESTMapper{}
Copy link
Contributor

Choose a reason for hiding this comment

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

Ditto about fmt.Stringer

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Done.

@@ -109,7 +111,7 @@ type RESTMapping struct {
// to API groups. In other words, kinds and resources should not be assumed to be
// unique across groups.
//
// TODO: split into sub-interfaces
// Deprecated: use RESTMapperWithContext instead.
Copy link
Contributor

Choose a reason for hiding this comment

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

This applies to other files as well. I remember we agreed NOT to deprecate these, but rather put a message like:

// Contextual logging: RESTMapperWithContext should be used instead of RESTMapper in code which supports contextual logging.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

That was for functions where the original version is functionally sufficient for its intended purpose.

Here we have a case of an interface that looks like it works locally (no context) but under the hood often must call blocking functions which really should be called with a context if the user of this interface has one. I think this is a case where we should nudge the ecosystem towards updating their code by declaring the old functions as deprecated.

I wasn't entirely sure whether this should return a value or a pointer to
satisfy the interface. Both works, so I benchmarked it elsewhere (REST
mapper). Mem allocs are the same (one alloc/call), but returning a value is 10%
slower when calling one method.

What I then benchmarked is whether pointer vs value receiver in the wrapper
makes a difference. Converting from value receiver (what I had before) to
pointer receiver reduced call overhead by 6%. That's because with a value
receiver, Go has to auto-generate a variant with pointer receiver and calls the
value receiver through that.

That can be seen in a debugger (call stack) and when setting breakpoints:

    (dlv) b restMapperWrapper.KindForWithContext
    Command failed: Location "restMapperWrapper.KindForWithContext" ambiguous: k8s.io/apimachinery/pkg/api/meta.restMapperWrapper.KindForWithContext, k8s.io/apimachinery/pkg/api/meta.(*restMapperWrapper).KindForWithContext…

Conventional wisdom is to define types with value receiver because those can be
called also on unmutable instances, making them more flexible.

But for types which will only ever be used via a pointer, I think pointer
receiver is better for the reasons above (small performance difference, easier
to debug).
The main purpose is to replace context.TODO with a context provided by the
caller. A secondary purpose is to enable contextual logging.

Modifying the existing interfaces and APIs would have a big impact on the
ecosystem. This is a no-go. Instead, the following approach was taken:

- All interfaces get duplicated in a *WithContext variant where the methods
  also have a *WithContext suffix and the ctx parameter. All methods are
  treated this way except for obvious local get methods (like RESTClient)
  because it cannot be ruled out entirely that some implementation may
  need a context.

- Implementations of these interfaces implement both method variants
  which is possible because the method names are different.
  The old methods are implemented as thin wrappers around the updated
  code which is now the body of the new methods or shared helpers.
  In some cases there is additional overhead (type checks, potentially
  additional allocations) when using the old methods.

- To*WithContext helpers bridge from the old to the new interfaces. They
  try a type cast first. Because the in-tree implementations implement
  both, they can be used directly. For other implementations wrappers
  are used.

- All old APIs and interfaces are marked as deprecated. There is no
  intent to ever remove them, but consumers should be made aware
  that there are now better alternatives. Implementations also
  get marked this way even if nothing ever calls them directly
  because it shows which code, at least theoretically, could get
  removed.

- Existing unit tests do not get updated to the new APIs. This gives
  us unit test coverage of the old and new API because the old
  APIs call the new ones.

- In-tree consumers will be updated in follow-up PRs. This is likely
  to be a longer process. Because of the deprecation comment,
  `hack/golangci-lint.sh -n` can be used to find code which needs
  to be updated.
@pohly pohly force-pushed the log-client-go-restmapper-discovery branch from 852983a to 74102a6 Compare February 13, 2025 11:09
@k8s-ci-robot
Copy link
Contributor

@pohly: The following test 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-linter-hints 74102a6 link false /test pull-kubernetes-linter-hints

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.

richabanker pushed a commit to richabanker/kubernetes that referenced this pull request Feb 28, 2025
The Lister and Watcher interfaces only supported methods without context, but
were typically implemented with client-go API calls which need a context. New
interfaces get added using the same approach as in
kubernetes#129109.
richabanker pushed a commit to richabanker/kubernetes that referenced this pull request Feb 28, 2025
The Lister and Watcher interfaces only supported methods without context, but
were typically implemented with client-go API calls which need a context. New
interfaces get added using the same approach as in
kubernetes#129109.
zylxjtu pushed a commit to zylxjtu/kubernetes that referenced this pull request Mar 20, 2025
The Lister and Watcher interfaces only supported methods without context, but
were typically implemented with client-go API calls which need a context. New
interfaces get added using the same approach as in
kubernetes#129109.
pohly added a commit to pohly/kubernetes that referenced this pull request Apr 16, 2025
@k8s-triage-robot
Copy link

The Kubernetes project currently lacks enough contributors to adequately respond to all PRs.

This bot triages PRs according to the following rules:

  • After 90d of inactivity, lifecycle/stale is applied
  • After 30d of inactivity since lifecycle/stale was applied, lifecycle/rotten is applied
  • After 30d of inactivity since lifecycle/rotten was applied, the PR is closed

You can:

  • Mark this PR as fresh with /remove-lifecycle stale
  • Close this PR with /close
  • Offer to help out with Issue Triage

Please send feedback to sig-contributor-experience at kubernetes/community.

/lifecycle stale

@k8s-ci-robot k8s-ci-robot added the lifecycle/stale Denotes an issue or PR has remained open with no activity and has become stale. label May 14, 2025
@pohly
Copy link
Contributor Author

pohly commented May 15, 2025

/remove-lifecycle stale

@k8s-ci-robot k8s-ci-robot removed the lifecycle/stale Denotes an issue or PR has remained open with no activity and has become stale. label May 15, 2025
@harche
Copy link
Contributor

harche commented May 30, 2025

LGTM from my side, but @soltysh do the changes look okay to you?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area/code-generation area/dependency Issues or PRs related to dependency changes area/test cncf-cla: yes Indicates the PR's author has signed the CNCF CLA. 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/apps Categorizes an issue or PR as relevant to SIG Apps. sig/auth Categorizes an issue or PR as relevant to SIG Auth. sig/node Categorizes an issue or PR as relevant to SIG Node. 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. wg/device-management Categorizes an issue or PR as relevant to WG Device Management.
Projects
Archived in project
Status: Needs Triage
Status: !SIG Auth
Archived in project
Development

Successfully merging this pull request may close these issues.

Add a context to DiscoveryClient functions and interfaces
8 participants