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
kubectl prunev2: Refactor the applyset to be more reusable #116580
Conversation
e381732
to
70a02c0
Compare
} | ||
return nil | ||
} | ||
|
||
func (p *applySetPruner) delete(ctx context.Context, namespace string, name string, mapping *meta.RESTMapping) error { | ||
return runDelete(ctx, namespace, name, mapping, p.dynamicClient, p.cascadingStrategy, p.gracePeriod, p.dryRunStrategy == cmdutil.DryRunServer) | ||
func (a *ApplySet) delete(ctx context.Context, dynamicClient dynamic.Interface, namespace string, name string, mapping *meta.RESTMapping, o *ApplySetDeleteOptions) 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.
nit: would this make more sense as a method on ApplySetDeleteOptions
? It uses three of its values, and doesn't interact with ApplySet
at all. Or maybe just call runDelete
directly from deleteObjects
unless I'm missing some value in the indirection here.
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.
My 2c is that Options types should be POGO (like POJOs, but for Go!), but I do agree that we didn't need the indirection so I inlined delete 👍
|
||
if opt.DryRunStrategy != cmdutil.DryRunClient { | ||
if err := a.delete(ctx, dynamicClient, namespace, name, mapping, opt); err != nil { | ||
return fmt.Errorf("pruning %v %s/%s: %w", pruneObject.Mapping.GroupVersionKind.String(), namespace, name, err) |
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.
nit: maybe make a pruneObject.String()
than could prevent the slash for non-namespaced objects? (e.g. Kind=Namespace /bar
in the test)
return err | ||
} | ||
if err := pruner.pruneAll(ctx, o.ApplySet); err != nil { | ||
if err := o.ApplySet.Prune(ctx, o); err != nil { |
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.
Should o.ApplySet.UpdateParent
be merged into o.ApplySet.Prune
as well (and then UpdateParent made private)? IIRC I originally had it that way but moved it out because the parent update went in before pruning was implemented at all. I don't think there's a conceivable case where pruning was completed but parent update should not be attempted?
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.
Good call - done!
|
||
// FindObjectsToPrune returns the list of objects that will be pruned. | ||
// Calling this instead of Prune can be useful for dry-run / diff behaviour. | ||
func (a *ApplySet) FindObjectsToPrune(ctx context.Context, dynamicClient dynamic.Interface, visitedUids sets.Set[types.UID]) ([]PruneObject, 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.
Why have FindObjectsToPrune
that is public wrapping the very similarly named findAllObjectsToPrune
that is private?
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.
Good point, removed the extra layer.
@@ -457,3 +459,58 @@ func ParseApplySetParentRef(parentRefStr string, mapper meta.RESTMapper) (*Apply | |||
} | |||
return &ApplySetParentRef{Name: name, RESTMapping: mapping}, nil | |||
} | |||
|
|||
// Prune deletes any objects from the apiserver that are no longer in the applyset. | |||
func (a *ApplySet) Prune(ctx context.Context, o *ApplyOptions) 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.
Same question as below about having a Prune
and a pruneAll
on the same object (though this one does more than just wrap). Also, now that applySetPruner isn't a separate object, should that file's content move here? Or if we're collecting the pruning related functionality in that file, shouldn't this and the method below be there?
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 one I think is more defensible... it does a lot more (particularly now we've merged in updateParent). Also we're mapping from ApplyOptions which is a CLI concern (and specific to kubectl apply
, not just kubectl
) to ApplySetDeleteOptions which is more of an applyset library concern.
The overall motivation for the PR here is that we need to create a reusable "applyset" library, if we are to support diff as well without too much code duplication.
I think in terms of code organization, we should try to create a new package (probably applyset
) which just has the applyset logic in it, and does not have kubectl apply
specific types (like ApplyOptions
). I just didn't want to do that here because moving code around at the same time as doing some separable refactors would make it harder to review..
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.
Separate package in a later refactor SGTM!
/test pull-kubernetes-unit |
[APPROVALNOTIFIER] This PR is APPROVED This pull-request has been approved by: justinsb, KnVerey 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 |
/hold |
LGTM label has been added. Git tree hash: 7b6746856106a425b7e42d152dc567be0c11ff81
|
70a02c0
to
6620e5c
Compare
Thanks for review - addressed the nits (which I agree with, though I think we should do the refactor in a separate PR once we have this code shared with diff) |
/lgtm |
LGTM label has been added. Git tree hash: dc30d37deb76dfd1d59c50cfd1a60f72cbe30b31
|
6620e5c
to
f68a546
Compare
/lgtm |
LGTM label has been added. Git tree hash: e18fd4e02ca5a55ddedf037f2707afca7fb1ed5f
|
PR needs rebase. 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/test-infra repository. |
2 similar comments
PR needs rebase. 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/test-infra repository. |
PR needs rebase. 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/test-infra repository. |
This enables sharing with diff.
f68a546
to
d016fdc
Compare
/lgtm |
LGTM label has been added. Git tree hash: f03f89c5f7659f8147fb432624706a67eed34b49
|
This enables sharing with diff.
/kind cleanup