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

✨ Add client.WithWatch wrapper #2929

Closed
wants to merge 1 commit into from

Conversation

2uasimojo
Copy link
Contributor

@2uasimojo 2uasimojo commented Aug 18, 2024

In some cases, one can wrap already-extant client.Clients to confer extra functionality. For example:

cWithFieldOwner := client.WithFieldOwner(cWithoutFieldOwner, "owner")

However, it has not heretofore been possible to confer WithWatch on an existing client: one could only obtain a client.WithWatch via a constructor:

cWithWatch, err := client.NewWithWatch(cfg, opts)

The resulting client is "frozen" -- i.e. can't be subjected to decorators like WithFieldOwner -- because those wrappers return a client.Client, which can't Watch()!

This commit adds a decorator function to allow an existing client to grow WithWatch functionality:

c, err := client.New(cfg, opts)
c = client.WithFieldOwner(c, "owner")
// ... chain other decorators ...
cWithAllTheThings, err := client.AsWithWatch(c)

Notes:

  • We would have preferred to call the wrapper WithWatch, but that name is already taken by the interface. Sad face.
  • This client is "the end of the line". It can't be further wrapped, unless the wrapper is WithWatch-specific. Sad face.
  • As currently conceived, you can't just do this to any client.Client because the Watch() implementation relies on internals of the specific client (lowercase) implementation in the library. Sad face.

Closes #2967

In some cases, one can wrap already-extant `client.Client`s to confer
extra functionality. For example:

```go
cWithFieldOwner := client.WithFieldOwner(cWithoutFieldOwner, "owner")
```

However, it has not heretofore been possible to confer `WithWatch` on an
existing client: one could only obtain a `client.WithWatch` via a
constructor:

```go
cWithWatch, err := client.NewWithWatch(cfg, opts)
```

The resulting client is "frozen" -- i.e. can't be subjected to
decorators like `WithFieldOwner` -- because those wrappers return a
`client.Client`, which can't `Watch()`!

This commit adds a decorator function to allow an existing client to
grow `WithWatch` functionality:

```go
c, err := client.New(cfg, opts)
c = client.WithFieldOwner(c, "owner")
// ... chain other decorators ...
cWithAllTheThings, err := client.AsWithWatch(c)
```

Notes:
- We would have preferred to call the wrapper `WithWatch`, but that name
  is already taken by the interface. Sad face.
- This client is "the end of the line". It can't be further wrapped,
  unless the wrapper is `WithWatch`-specific. Sad face.
- As currently conceived, you can't just do this to any `client.Client`
  because the `Watch()` implementation relies on internals of the
  specific `client` (lowercase) implementation in the library. Sad face.
@k8s-ci-robot k8s-ci-robot added do-not-merge/work-in-progress Indicates that a PR should not merge because it is a work in progress. cncf-cla: yes Indicates the PR's author has signed the CNCF CLA. labels Aug 18, 2024
@k8s-ci-robot
Copy link
Contributor

Hi @2uasimojo. Thanks for your PR.

I'm waiting for a kubernetes-sigs member to verify that this patch is reasonable to test. If it is, they should reply with /ok-to-test on its own line. Until that is done, I will not automatically test new commits in this PR, but the usual testing commands by org members will still work. Regular contributors should join the org to skip this step.

Once the patch is verified, the new status will be reflected by the ok-to-test label.

I understand the commands that are listed here.

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.

@k8s-ci-robot k8s-ci-robot added the needs-ok-to-test Indicates a PR that requires an org member to verify it is safe to test. label Aug 18, 2024
@k8s-ci-robot
Copy link
Contributor

[APPROVALNOTIFIER] This PR is NOT APPROVED

This pull-request has been approved by: 2uasimojo
Once this PR has been reviewed and has the lgtm label, please assign vincepri for approval. For more information see the Kubernetes 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

@k8s-ci-robot k8s-ci-robot added the size/S Denotes a PR that changes 10-29 lines, ignoring generated files. label Aug 18, 2024
@2uasimojo
Copy link
Contributor Author

2uasimojo commented Aug 18, 2024

Reviewers: I don't think this PR is acceptable without addressing at least the second and third Notes in the description.

By the way, here's the concrete use case that led to this: https://github.com/openshift/hive/pull/2416/files#diff-dd2b5dc061a42ad471ddce4733423593aa40a07fc2e877b76abccee42233a11cR14-R23

@2uasimojo
Copy link
Contributor Author

This prototype is pretty far from a good solution. I'm hoping whatever comes out of #2888 will be clean and pretty.

@2uasimojo 2uasimojo closed this Oct 4, 2024
@2uasimojo 2uasimojo deleted the client.AsWithWatch branch October 4, 2024 20:08
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
cncf-cla: yes Indicates the PR's author has signed the CNCF CLA. do-not-merge/work-in-progress Indicates that a PR should not merge because it is a work in progress. needs-ok-to-test Indicates a PR that requires an org member to verify it is safe to test. size/S Denotes a PR that changes 10-29 lines, ignoring generated files.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Can't wrap existing client with WithWatch()
2 participants