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

Create TransformingInformer #104300

Merged
merged 2 commits into from
Sep 28, 2021
Merged

Conversation

wojtek-t
Copy link
Member

TransformingInfomer is like a regular Informer, but allows for applying custom transform functions on the objects received via list/watch API calls.

This pattern was first used in CoreDNS couple years ago:
https://github.com/coredns/coredns/blob/9d5b8cd13d541cc3d2b7598ab255643d6298ba9d/plugin/kubernetes/object/informer.go#L30
Over time, with increasing amount of controllers and developers being more conscious about memory usage, I'm seeing more and more places where this could be potentially used. So it makes sense to actually make it supported by the client-go libraries natively.

NONE

/sig api-machinery
/kind feature
/priority important-longterm

@k8s-ci-robot k8s-ci-robot added release-note-none Denotes a PR that doesn't merit a release note. do-not-merge/work-in-progress Indicates that a PR should not merge because it is a work in progress. sig/api-machinery Categorizes an issue or PR as relevant to SIG API Machinery. kind/feature Categorizes issue or PR as related to a new feature. size/M Denotes a PR that changes 30-99 lines, ignoring generated files. priority/important-longterm Important over the long term, but may not be staffed and/or may need multiple releases to complete. 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. labels Aug 11, 2021
@wojtek-t
Copy link
Member Author

/assign @liggitt

return clientState, newInformer(lw, objType, resyncPeriod, h, clientState, nil)
}

// TransformFunc allows for transforming an object before it will be processed
Copy link
Member Author

Choose a reason for hiding this comment

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

@liggitt - obviously tests are missing in this PR, but before I go ahead and add them I would appreciate some early feedback on this change.

Copy link
Member Author

Choose a reason for hiding this comment

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

@deads2k - would you mind providing some early feedback?

Copy link
Contributor

Choose a reason for hiding this comment

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

I'm ok with the idea, but I'd like to be sure that the layers above this one make it difficult to accidentally confuse your listers with different transformations.

Copy link
Member Author

Choose a reason for hiding this comment

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

make it difficult to accidentally confuse your listers with different transformations.

Can you clarify? Is the comment about naming mostly? If so, I'm happy to change if you have better suggestions.

Copy link
Contributor

Choose a reason for hiding this comment

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

make it difficult to accidentally confuse your listers with different transformations.

Can you clarify? Is the comment about naming mostly? If so, I'm happy to change if you have better suggestions.

Today we generate Informers and Listers for every type. I'm somewhat concerned that people will make a TransformingPodLister, but if the type is still PodLister, it could get improperly passed around. I think we can expect people making use of a transformed PodLister to accept a different type. Does that seem reasonable?

Copy link
Member Author

Choose a reason for hiding this comment

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

Got it [I though that this comment is related to Transform itself, whereas it's more related to the returned values of "NewTransformingInformer"], i.e. the fact that it returns (Store, Controller).

The Controller itself doesn't seem controversial to me.

The potential problem is Store, as (IIUC what you're saying) it may lead to people thinking it contains different things (i.e. whole objects) than it really contains.

[The Lister itself isn't (at least in the shape of this PR a problem, because it requires indexer which I don't create here, but the change would be too simple to add to ignore it.]

So IIUC, what you're saying is that instead of returning Store, we should consider returning TransformingStore (or sth like that). Do I understand correctly?

If so, this effectively would be the same interface, so it would be possible once to another, so I'm not sure it would really bring value. WDYT?

Copy link
Contributor

Choose a reason for hiding this comment

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

If so, this effectively would be the same interface, so it would be possible once to another, so I'm not sure it would really bring value. WDYT?

Duck typing never fails to disappoint. I think you're right.

@k8s-ci-robot
Copy link
Contributor

[APPROVALNOTIFIER] This PR is APPROVED

This pull-request has been approved by: wojtek-t

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

The pull request process is described 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

@k8s-ci-robot k8s-ci-robot added the approved Indicates a PR has been approved by an approver from all required OWNERS files. label Aug 11, 2021
@caesarxuchao
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 Aug 12, 2021
@wojtek-t wojtek-t assigned deads2k and unassigned liggitt Aug 23, 2021
@@ -393,24 +425,33 @@ func newInformer(
Process: func(obj interface{}) error {
// from oldest to newest
for _, d := range obj.(Deltas) {
obj := d.Object
if transformer != nil {
Copy link
Contributor

Choose a reason for hiding this comment

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

Have I correctly skimmed the code below and it computes the keys for any indexes after the transform, so the data to compute is always present in the returned instances?

Copy link
Member Author

Choose a reason for hiding this comment

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

and it computes the keys for any indexes after the transform,

Yes.

so the data to compute is always present in the returned instances?

I guess it depends on the indexes that are passed. But in the correct usage the index should only use data from transformed objects.

@deads2k
Copy link
Contributor

deads2k commented Aug 24, 2021

TransformingInfomer is like a regular Informer, but allows for applying custom transform functions on the objects received via list/watch API calls.

I don't object in principle, but are we sure that we'd rather invest this way as opposed to making it easy to use the metadata only list/watch? I remember garbage collection tried, but I don't recall how successful it was.

@wojtek-t
Copy link
Member Author

I don't object in principle, but are we sure that we'd rather invest this way as opposed to making it easy to use the metadata only list/watch? I remember garbage collection tried, but I don't recall how successful it was.

Thanks @deads2k .
The metadata-only listwatch is useful (and I though we're already doing it, but I'm not 100% sure - I will double check). But it doesn't solve many of the usecases, including the one by CoreDNS where they really want some non-metadata fields too, just not all of them, and they really want to be memory-efficient.

Given the above, I will go ahead and try to add tests in the upcoming weeks and will ask you for more detailed review then.

@k8s-ci-robot k8s-ci-robot added size/L Denotes a PR that changes 100-499 lines, ignoring generated files. and removed size/M Denotes a PR that changes 30-99 lines, ignoring generated files. labels Sep 1, 2021
Copy link
Member Author

@wojtek-t wojtek-t left a comment

Choose a reason for hiding this comment

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

I have added a test - this is now ready for a more formal review.
@deads2k - PTAL when you will have a chance.

@@ -393,24 +425,33 @@ func newInformer(
Process: func(obj interface{}) error {
// from oldest to newest
for _, d := range obj.(Deltas) {
obj := d.Object
if transformer != nil {
Copy link
Member Author

Choose a reason for hiding this comment

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

and it computes the keys for any indexes after the transform,

Yes.

so the data to compute is always present in the returned instances?

I guess it depends on the indexes that are passed. But in the correct usage the index should only use data from transformed objects.

@wojtek-t wojtek-t changed the title [WIP] Create TransformingInformer Create TransformingInformer Sep 1, 2021
@k8s-ci-robot k8s-ci-robot removed the do-not-merge/work-in-progress Indicates that a PR should not merge because it is a work in progress. label Sep 1, 2021
// The given transform function will be called on all objects before they will
// put put into the Store and corresponding Add/Modify/Delete handlers will
// be invokved for them.
func NewTransformingInformer(
Copy link
Contributor

Choose a reason for hiding this comment

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

Will there be an indexer option? Why or why not?

Copy link
Member Author

Choose a reason for hiding this comment

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

I didn't initially add indexer option as neither of the examples of this pattern I saw was using indexes.

But in the end - adding it is just a boilerplate, so we should probably do that in one shot.
I added that in a second commit.

TransformingInfomer is like a regular Informer, but allows for applying
custom transform functions on the objects received via list/watch API calls.
Copy link
Member Author

@wojtek-t wojtek-t left a comment

Choose a reason for hiding this comment

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

@deads2k - PTAL

// The given transform function will be called on all objects before they will
// put put into the Store and corresponding Add/Modify/Delete handlers will
// be invokved for them.
func NewTransformingInformer(
Copy link
Member Author

Choose a reason for hiding this comment

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

I didn't initially add indexer option as neither of the examples of this pattern I saw was using indexes.

But in the end - adding it is just a boilerplate, so we should probably do that in one shot.
I added that in a second commit.

@deads2k
Copy link
Contributor

deads2k commented Sep 28, 2021

/lgtm

@k8s-ci-robot k8s-ci-robot added the lgtm "Looks good to me", indicates that a PR is ready to be merged. label Sep 28, 2021
@wojtek-t
Copy link
Member Author

/retest

2 similar comments
@wojtek-t
Copy link
Member Author

/retest

@wojtek-t
Copy link
Member Author

/retest

@k8s-ci-robot k8s-ci-robot merged commit 66e1d27 into kubernetes:master Sep 28, 2021
@k8s-ci-robot k8s-ci-robot added this to the v1.23 milestone Sep 28, 2021
@jberkus
Copy link

jberkus commented Oct 5, 2021

Given that this is a feature, shouldn't it have a release note?

@wojtek-t
Copy link
Member Author

wojtek-t commented Oct 5, 2021

Given that this is a feature, shouldn't it have a release note?

I don't agree. This isn't a feature per-se. This isn't anything that impact users and/or cluster admins.
It's something that developers may use - but it's not what release notes are used for. It's just a library.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
approved Indicates a PR has been approved by an approver from all required OWNERS files. 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. lgtm "Looks good to me", indicates that a PR is ready to be merged. priority/important-longterm Important over the long term, but may not be staffed and/or may need multiple releases to complete. release-note-none Denotes a PR that doesn't merit a release note. sig/api-machinery Categorizes an issue or PR as relevant to SIG API Machinery. size/L Denotes a PR that changes 100-499 lines, ignoring generated files. triage/accepted Indicates an issue or PR is ready to be actively worked on.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

6 participants