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

kubectl prunev2: Refactor the applyset to be more reusable #116580

Merged
merged 1 commit into from Mar 14, 2023

Conversation

justinsb
Copy link
Member

@justinsb justinsb commented Mar 14, 2023

This enables sharing with diff.

/kind cleanup

NONE

@k8s-ci-robot k8s-ci-robot added size/L Denotes a PR that changes 100-499 lines, ignoring generated files. do-not-merge/release-note-label-needed Indicates that a PR should not merge because it's missing one of the release note labels. cncf-cla: yes Indicates the PR's author has signed the CNCF CLA. do-not-merge/needs-kind Indicates a PR lacks a `kind/foo` label and requires one. do-not-merge/needs-sig Indicates an issue or PR lacks a `sig/foo` label and requires one. needs-triage Indicates an issue or PR lacks a `triage/foo` label and requires one. needs-priority Indicates a PR lacks a `priority/foo` label and requires one. labels Mar 14, 2023
@k8s-ci-robot k8s-ci-robot added area/kubectl sig/cli Categorizes an issue or PR as relevant to SIG CLI. release-note-none Denotes a PR that doesn't merit a release note. and removed do-not-merge/needs-sig Indicates an issue or PR lacks a `sig/foo` label and requires one. do-not-merge/release-note-label-needed Indicates that a PR should not merge because it's missing one of the release note labels. labels Mar 14, 2023
@justinsb justinsb force-pushed the applyset_refactor branch 2 times, most recently from e381732 to 70a02c0 Compare March 14, 2023 13:02
}
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 {
Copy link
Contributor

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.

Copy link
Member Author

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)
Copy link
Contributor

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 {
Copy link
Contributor

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?

Copy link
Member Author

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) {
Copy link
Contributor

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?

Copy link
Member Author

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 {
Copy link
Contributor

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?

Copy link
Member Author

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..

Copy link
Contributor

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!

@KnVerey
Copy link
Contributor

KnVerey commented Mar 14, 2023

/test pull-kubernetes-unit
/kind cleanup
/approve
/triage accepted
/priority important-longterm

@k8s-ci-robot k8s-ci-robot added kind/cleanup Categorizes issue or PR as related to cleaning up code, process, or technical debt. triage/accepted Indicates an issue or PR is ready to be actively worked on. priority/important-longterm Important over the long term, but may not be staffed and/or may need multiple releases to complete. and removed do-not-merge/needs-kind Indicates a PR lacks a `kind/foo` label and requires one. needs-triage Indicates an issue or PR lacks a `triage/foo` label and requires one. needs-priority Indicates a PR lacks a `priority/foo` label and requires one. labels Mar 14, 2023
@k8s-ci-robot
Copy link
Contributor

[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 /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 Mar 14, 2023
@KnVerey
Copy link
Contributor

KnVerey commented Mar 14, 2023

/hold
in case you want to address any of the nits on this PR (but feel free to do it in a follow up)
/lgtm

@k8s-ci-robot k8s-ci-robot added the do-not-merge/hold Indicates that a PR should not merge because someone has issued a /hold command. label Mar 14, 2023
@k8s-ci-robot k8s-ci-robot added the lgtm "Looks good to me", indicates that a PR is ready to be merged. label Mar 14, 2023
@k8s-ci-robot
Copy link
Contributor

LGTM label has been added.

Git tree hash: 7b6746856106a425b7e42d152dc567be0c11ff81

@k8s-ci-robot k8s-ci-robot removed the lgtm "Looks good to me", indicates that a PR is ready to be merged. label Mar 14, 2023
@justinsb
Copy link
Member Author

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)

@KnVerey
Copy link
Contributor

KnVerey commented Mar 14, 2023

/lgtm
/unhold

@k8s-ci-robot k8s-ci-robot added lgtm "Looks good to me", indicates that a PR is ready to be merged. and removed do-not-merge/hold Indicates that a PR should not merge because someone has issued a /hold command. labels Mar 14, 2023
@k8s-ci-robot
Copy link
Contributor

LGTM label has been added.

Git tree hash: dc30d37deb76dfd1d59c50cfd1a60f72cbe30b31

@k8s-ci-robot k8s-ci-robot removed the lgtm "Looks good to me", indicates that a PR is ready to be merged. label Mar 14, 2023
@KnVerey
Copy link
Contributor

KnVerey commented Mar 14, 2023

/lgtm

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

LGTM label has been added.

Git tree hash: e18fd4e02ca5a55ddedf037f2707afca7fb1ed5f

@k8s-ci-robot k8s-ci-robot added the needs-rebase Indicates a PR cannot be merged because it has merge conflicts with HEAD. label Mar 14, 2023
@k8s-ci-robot
Copy link
Contributor

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
@k8s-ci-robot
Copy link
Contributor

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.

@k8s-ci-robot
Copy link
Contributor

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.

@k8s-ci-robot k8s-ci-robot removed lgtm "Looks good to me", indicates that a PR is ready to be merged. needs-rebase Indicates a PR cannot be merged because it has merge conflicts with HEAD. labels Mar 14, 2023
@KnVerey
Copy link
Contributor

KnVerey commented Mar 14, 2023

/lgtm

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

LGTM label has been added.

Git tree hash: f03f89c5f7659f8147fb432624706a67eed34b49

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. area/kubectl cncf-cla: yes Indicates the PR's author has signed the CNCF CLA. kind/cleanup Categorizes issue or PR as related to cleaning up code, process, or technical debt. 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/cli Categorizes an issue or PR as relevant to SIG CLI. 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

3 participants