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 options to client.Delete() #94

Merged
merged 2 commits into from
Sep 10, 2018

Conversation

grantr
Copy link
Contributor

@grantr grantr commented Jul 26, 2018

Allows Delete requests to carry options like GracePeriodSeconds, Preconditions, and PropagationPolicy. I chose the functional options pattern here so the function signature remains the same for all existing callsites.

To use the default options, call Delete with the same signature as before:

client.Delete(context.TODO(), obj)

This Delete call sets a GracePeriodSeconds for the request:

client.Delete(context.TODO(), obj, client.GracePeriodSeconds(1))

Multiple options can be set:

client.Delete(context.TODO(), obj,
  client.GracePeriodSeconds(1),
  client.PropagationPolicy(metav1.DeletePropagationForeground))

I wrote tests for the DeleteOptions struct, but I wasn't sure how to test the actual request content. With the current testing strategy, we can only test behavior by inspecting what the apiserver did, and in this case that's fairly subtle since it involves timeouts and cascading deletes. It would be easier to use httptest or a similar mock to inspect the request that's sent by the client.

Fixes #76.

@k8s-ci-robot k8s-ci-robot added size/L Denotes a PR that changes 100-499 lines, ignoring generated files. cncf-cla: yes Indicates the PR's author has signed the CNCF CLA. labels Jul 26, 2018
@DirectXMan12
Copy link
Contributor

Our list options are chained instead of varargs. I'm not necessary objecting to the varargs approach (I like how it ends up looking, but I think there are merits to both), but I think we need to be consistent.

@grantr
Copy link
Contributor Author

grantr commented Jul 27, 2018

In general, the benefit of functional options is that they're invisible until you need them. This seems more useful for Delete than List. I expect Delete options will be rarely used, while almost all List calls will use options. The two operations have different ergonomic needs.

I can create another PR exploring functional ListOptions if that seems useful.

@DirectXMan12
Copy link
Contributor

In general, the benefit of functional options is that they're invisible until you need them.

Agreed, I do like that aspect.

I can create another PR

yeah, I think we should have a common experience. Please do create a separate PR for varargs list options.

Copy link
Contributor

@DirectXMan12 DirectXMan12 left a comment

Choose a reason for hiding this comment

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

generally looks good. I would rewrite the tests to capture desired behavior from a user perspective though, instead of being broken out into the smallest possible units.

For example, if I were describing this, I would say

  • it should allow setting GracePeriodSeconds
  • it should allow setting Precondition
  • it should allow setting PropogationPolicy
  • it should produce empty metav1.DeletionOptions if nil
  • it should be possible to merge multiple options together

For each of those first three, I'd start with the function that produces the desired flag, call apply on it, and then check that the output metav1.ListOptions was as expected. We still test the same set of functionality, but it's broken up into more logical sequences from the perspective of how we'd expect someone to use it.

@droot
Copy link
Contributor

droot commented Jul 30, 2018

@grantr will wait for your second PR to compare how it looks.

Also, another major factor to consider is that the changes should be backward compatible. Implementation in current PR meets that criterion.

@grantr
Copy link
Contributor Author

grantr commented Aug 8, 2018

@droot @DirectXMan12 #106 converts client.List to use functional list options also. It's not backward compatible, but it's close for the common case of a single package-scoped helper function:

// current
List(ctx, client.InNamespace(ns), list)
// proposed
List(ctx, list, client.InNamespace(ns))

More examples in #106.

@grantr
Copy link
Contributor Author

grantr commented Aug 15, 2018

I would rewrite the tests to capture desired behavior from a user perspective though, instead of being broken out into the smallest possible units.

The list you propose is all the existing tests less one! 😜I folded the metav1.DeleteOptions test into the others and refactored a bit. Take a look; let me know what you think.

@DirectXMan12
Copy link
Contributor

tests look good. will do one more pass tonight or tomorrow morning, then merge.

@DirectXMan12
Copy link
Contributor

/lgtm
/approve

@k8s-ci-robot k8s-ci-robot added lgtm "Looks good to me", indicates that a PR is ready to be merged. approved Indicates a PR has been approved by an approver from all required OWNERS files. labels Aug 24, 2018
@DirectXMan12
Copy link
Contributor

whoops, looks like you missed a fake implementation in one of the tests.

@k8s-ci-robot k8s-ci-robot removed the lgtm "Looks good to me", indicates that a PR is ready to be merged. label Aug 24, 2018
@grantr
Copy link
Contributor Author

grantr commented Aug 24, 2018

Oops, should have caught that earlier. I updated the fake client interface, but didn't actually implement any delete options. I added a TODO to implement delete propagation.

This brings up a subtlety of backward compatibility for this change: usage of the client interface (Delete() calls) is backward compatible, but implementation of the client interface is not. The only realistic scenario I can think of is that client mocks used in other projects might need to be updated.

@DirectXMan12
Copy link
Contributor

Still technically a incompatible change then, I think. We'll do it once everything's migrated to the pinned deps.

@grantr
Copy link
Contributor Author

grantr commented Sep 5, 2018

Can this change also go forward with #106 (comment)?

@DirectXMan12
Copy link
Contributor

Yep, this can go forward now.

@DirectXMan12
Copy link
Contributor

Fix golint issues (some missing godocs), and this is good to go.

Allows Delete requests to carry options like GracePeriodSeconds,
Preconditions, and PropagationPolicy.

To use the default options, call Delete with the same signature as
before:

client.Delete(context.TODO(), obj)

Delete options are passed to the request using the functional options
pattern. This Delete call sets a GracePeriodSeconds:

client.Delete(context.TODO(), obj, client.GracePeriodSeconds(1))

Multiple options can be set:

client.Delete(context.TODO(), obj,
  client.GracePeriodSeconds(1),
  client.PropagationPolicy(metav1.DeletePropagationForeground))

The fake client doesn't yet implement delete options like propagation,
so keep that in mind when writing tests.
@grantr
Copy link
Contributor Author

grantr commented Sep 6, 2018

Fixed the golint issues and rebased into a single commit.

@grantr
Copy link
Contributor Author

grantr commented Sep 6, 2018

Looks like the gosec check is failing, but it doesn't seem to be anything related to the changes in this PR.

@DirectXMan12
Copy link
Contributor

there's an open PR about gosec

@grantr
Copy link
Contributor Author

grantr commented Sep 7, 2018

All good now.

@DirectXMan12
Copy link
Contributor

/lgtm
/approve

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

[APPROVALNOTIFIER] This PR is APPROVED

This pull-request has been approved by: DirectXMan12, grantr

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 merged commit 3956e22 into kubernetes-sigs:master Sep 10, 2018
@grantr grantr deleted the delete-options branch September 10, 2018 17:22
justinsb pushed a commit to justinsb/controller-runtime that referenced this pull request Dec 7, 2018
DirectXMan12 pushed a commit that referenced this pull request Jan 31, 2020
Update thirdparty binaries to 1.10
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. lgtm "Looks good to me", indicates that a PR is ready to be merged. size/L Denotes a PR that changes 100-499 lines, ignoring generated files.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

4 participants