-
Notifications
You must be signed in to change notification settings - Fork 41.1k
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
base: master
Are you sure you want to change the base?
Conversation
Please note that we're already in Test Freeze for the Fast forwards are scheduled to happen every 6 hours, whereas the most recent run was: Fri Dec 6 16:44:50 UTC 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) |
There was a problem hiding this comment.
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)
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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
?
There was a problem hiding this comment.
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
.
89a610e
to
7af7a6a
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
// 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) |
There was a problem hiding this comment.
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.
/test pull-kubernetes-apidiff |
7af7a6a
to
71efc9a
Compare
638a635
to
edb084b
Compare
/triage accepted |
edb084b
to
3366e33
Compare
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.
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.
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.
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.
21e9af1
to
852983a
Compare
[APPROVALNOTIFIER] This PR is NOT APPROVED This pull-request has been approved by: pohly 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 |
/test pull-kubernetes-integration |
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
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
There was a problem hiding this 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:
- I believe we agreed to NOT do deprecation on methods w/o context.
- 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{ |
There was a problem hiding this comment.
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
kubernetes/staging/src/k8s.io/client-go/tools/cache/listwatch.go
Lines 88 to 99 in 39f1c90
func ToWatcherWithContext(w Watcher) WatcherWithContext { | |
if w, ok := w.(WatcherWithContext); ok { | |
return w | |
} | |
return watcherWrapper{ | |
parent: w, | |
} | |
} | |
type watcherWrapper struct { | |
parent Watcher | |
} |
// 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) |
There was a problem hiding this comment.
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{} |
There was a problem hiding this comment.
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{} |
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done.
There was a problem hiding this comment.
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{} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ditto about fmt.Stringer
There was a problem hiding this comment.
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. |
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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.
852983a
to
74102a6
Compare
@pohly: The following test failed, say
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. |
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.
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.
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.
The Kubernetes project currently lacks enough contributors to adequately respond to all PRs. This bot triages PRs according to the following rules:
You can:
Please send feedback to sig-contributor-experience at kubernetes/community. /lifecycle stale |
/remove-lifecycle stale |
LGTM from my side, but @soltysh do the changes look okay to you? |
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?
/cc @ardaguclu @sttts