Skip to content
This repository has been archived by the owner on Apr 25, 2023. It is now read-only.

Feature: support DeleteOptions when deleting resources in member clusters #1355

Merged

Conversation

RolandMa1986
Copy link
Contributor

What this PR does / why we need it:
This PR implements PR #1348 to support DeleteOptions when deleting resources in member clusters. When deleting a federated resource with kubefed.io/deleteoptions annotation, it will be deserialized as a DeleteOptons object and apply to the delete operation.

Which issue(s) this PR fixes (optional, in fixes #<issue number>(, fixes #<issue_number>, ...) format, will close the issue(s) when PR gets merged):
Fixes #1348

@k8s-ci-robot
Copy link
Contributor

Thanks for your pull request. Before we can look at your pull request, you'll need to sign a Contributor License Agreement (CLA).

📝 Please follow instructions at https://git.k8s.io/community/CLA.md#the-contributor-license-agreement to sign the CLA.

It may take a couple minutes for the CLA signature to be fully registered; after that, please reply here with a new comment and we'll verify. Thanks.


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. I understand the commands that are listed here.

@k8s-ci-robot k8s-ci-robot added the cncf-cla: no Indicates the PR's author has not signed the CNCF CLA. label Feb 2, 2021
@k8s-ci-robot
Copy link
Contributor

Welcome @RolandMa1986!

It looks like this is your first PR to kubernetes-sigs/kubefed 🎉. Please refer to our pull request process documentation to help your PR have a smooth ride to approval.

You will be prompted by a bot to use commands during the review process. Do not be afraid to follow the prompts! It is okay to experiment. Here is the bot commands documentation.

You can also check if kubernetes-sigs/kubefed has its own contribution guidelines.

You may want to refer to our testing guide if you run into trouble with your tests not passing.

If you are having difficulty getting your pull request seen, please follow the recommended escalation practices. Also, for tips and tricks in the contribution process you may want to read the Kubernetes contributor cheat sheet. We want to make sure your contribution gets all the attention it needs!

Thank you, and welcome to Kubernetes. 😃

@k8s-ci-robot k8s-ci-robot added the size/L Denotes a PR that changes 100-499 lines, ignoring generated files. label Feb 2, 2021
@RolandMa1986 RolandMa1986 force-pushed the feat/kubefed-deleteoption branch 3 times, most recently from 99c3f4e to 992cbfc Compare February 2, 2021 03:52
@k8s-ci-robot k8s-ci-robot added cncf-cla: yes Indicates the PR's author has signed the CNCF CLA. and removed cncf-cla: no Indicates the PR's author has not signed the CNCF CLA. labels Feb 2, 2021
@k8s-ci-robot
Copy link
Contributor

@RolandMa1986: Cannot trigger testing until a trusted user reviews the PR and leaves an /ok-to-test message.

In response to this:

/retest

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.

@RolandMa1986 RolandMa1986 force-pushed the feat/kubefed-deleteoption branch 2 times, most recently from 967ce5b to fe0376a Compare February 5, 2021 07:49
@hectorj2f
Copy link
Contributor

I will review this PR during this week.

Copy link
Contributor

@makkes makkes left a comment

Choose a reason for hiding this comment

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

This might come in as a handy feature in general. Just some minor change requests. I will also comment on the associated issue with a question that came up while reviewing.

@@ -0,0 +1,67 @@
/*
Copyright 2019 The Kubernetes Authors.
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
Copyright 2019 The Kubernetes Authors.
Copyright 2021 The Kubernetes Authors.

)

// GetDeleteOptions return delete options from the annotation
func GetDeleteOptions(obj *unstructured.Unstructured) []client.DeleteOption {
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
func GetDeleteOptions(obj *unstructured.Unstructured) []client.DeleteOption {
func GetDeleteOptions(obj *unstructured.Unstructured) ([]client.DeleteOption, error) {

Comment on lines 43 to 46
if err := json.Unmarshal([]byte(optStr), opt); err == nil {
clientOpt := &client.DeleteOptions{}
clientOpt.GracePeriodSeconds = opt.GracePeriodSeconds
clientOpt.PropagationPolicy = opt.PropagationPolicy
clientOpt.Preconditions = opt.Preconditions
options = append(options, clientOpt)
}
Copy link
Contributor

Choose a reason for hiding this comment

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

As this is a user-supplied value, we should expect the unmarshalling to fail, e.g. because the user failed to supply valid JSON in the annotation:

Suggested change
if err := json.Unmarshal([]byte(optStr), opt); err == nil {
clientOpt := &client.DeleteOptions{}
clientOpt.GracePeriodSeconds = opt.GracePeriodSeconds
clientOpt.PropagationPolicy = opt.PropagationPolicy
clientOpt.Preconditions = opt.Preconditions
options = append(options, clientOpt)
}
if err := json.Unmarshal([]byte(optStr), opt); err != nil {
return fmt.Errorf("could not deserialize delete options from annotation value '%s': %w", optStr, err)
}
clientOpt := &client.DeleteOptions{}
clientOpt.GracePeriodSeconds = opt.GracePeriodSeconds
clientOpt.PropagationPolicy = opt.PropagationPolicy
clientOpt.Preconditions = opt.Preconditions
options = append(options, clientOpt)

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I agree. Changes have been committed as you suggested.

options := make([]client.DeleteOption, 0)
annotations := obj.GetAnnotations()
if annotations == nil {
return options
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
return options
return options, nil

pkg/controller/util/deletionannotation.go Outdated Show resolved Hide resolved

fixture := typeConfigFixtures[typeConfigName]

It(`Deployment should be created and deleted successfully, but ReplicaSet that created by Deployment won't be deleted`, func() {
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
It(`Deployment should be created and deleted successfully, but ReplicaSet that created by Deployment won't be deleted`, func() {
It("Deployment should be created and deleted successfully, but ReplicaSet that created by Deployment won't be deleted", func() {

just a nit

@makkes
Copy link
Contributor

makkes commented Feb 10, 2021

/ok-to-test

@k8s-ci-robot k8s-ci-robot added the ok-to-test Indicates a non-member PR verified by an org member that is safe to test. label Feb 10, 2021
if optStr, ok := annotations[DeleteOptionAnnotation]; ok {
opt := &metav1.DeleteOptions{}
if err := json.Unmarshal([]byte(optStr), opt); err != nil {
return nil, fmt.Errorf("could not deserialize delete options from annotation value '%s': %w", optStr, err)
Copy link
Contributor

Choose a reason for hiding this comment

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

Sorry, my suggestion actually made CI complain:

Suggested change
return nil, fmt.Errorf("could not deserialize delete options from annotation value '%s': %w", optStr, err)
return nil, errors.Wrapf(err, "could not deserialize delete options from annotation value '%s'", optStr)


kubeClient := kubeclientset.NewForConfigOrDie(clusterConfig)
WaitForNamespaceOrDie(c.tl, kubeClient, clusterName, fedObject.GetNamespace(),
c.waitInterval, 30*time.Second)
Copy link
Contributor

Choose a reason for hiding this comment

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

please use framework.PollInterval, framework.TestContext.SingleCallTimeout instead of c.waitInterval, 30*time.Second.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I guess the framework package depends on common, so it doesn't allow me to import framework in crudtester.go.
Any idea to resolve this? I'm pretty new to Golang.

Copy link
Contributor

Choose a reason for hiding this comment

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

I believe you faced some cycle go deps here. It is alright, I'll move those common attributes to a different package. Not a blocker.

Copy link
Contributor

@hectorj2f hectorj2f left a comment

Choose a reason for hiding this comment

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

@RolandMa1986 Great work! Overall looks good to me, I just left some comments in addition to those from @makkes.

@hectorj2f
Copy link
Contributor

The PR build failed due to:

+echo 'Checking that correct Error Package is used.'
Checking that correct Error Package is used.
+./hack/verify-errpkg.sh
Please Switch from fmt.errorf to github/pkg/errors to fix the following files:
./pkg/controller/util/deletionannotation.go

Signed-off-by: RolandMa1986 <rolandma@outlook.com>
@makkes
Copy link
Contributor

makkes commented Feb 12, 2021

/retest
pull request limit hitting us again (see #1359)

Copy link
Contributor

@makkes makkes left a comment

Choose a reason for hiding this comment

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

lgtm now, but somehow I missed an ignored possible error. If that's addressed, I'm all ✅

pkg/controller/util/deletionannotation.go Outdated Show resolved Hide resolved
pkg/controller/util/deletionannotation.go Outdated Show resolved Hide resolved
@hectorj2f
Copy link
Contributor

@RolandMa1986 lgtm too, let's just address these minor comments from @makkes

Signed-off-by: RolandMa1986 <rolandma@outlook.com>
@makkes
Copy link
Contributor

makkes commented Feb 18, 2021

/lgtm

@k8s-ci-robot k8s-ci-robot added the lgtm Indicates that a PR is ready to be merged. label Feb 18, 2021
Copy link
Contributor

@hectorj2f hectorj2f left a comment

Choose a reason for hiding this comment

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

/approve

@k8s-ci-robot
Copy link
Contributor

[APPROVALNOTIFIER] This PR is APPROVED

This pull-request has been approved by: hectorj2f, makkes, RolandMa1986

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 Feb 18, 2021
@hectorj2f
Copy link
Contributor

Good job @RolandMa1986! Thanks for your contribution 👍🏻 !

@hectorj2f
Copy link
Contributor

/lgtm

@k8s-ci-robot k8s-ci-robot merged commit 485bcf0 into kubernetes-retired:master Feb 18, 2021
@RolandMa1986
Copy link
Contributor Author

Good job @RolandMa1986! Thanks for your contribution 👍🏻 !

Thanks for your help @hectorj2f and @makkes.

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
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 Indicates that a PR is ready to be merged. ok-to-test Indicates a non-member PR verified by an org member that is safe to test. 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.

Support DeleteOptions when deleting resources in member clusters
4 participants