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

namespace: remove gc finalizers based on delete options #76051

Merged
merged 1 commit into from
Apr 30, 2019

Conversation

zhouhaibing089
Copy link
Contributor

@zhouhaibing089 zhouhaibing089 commented Apr 2, 2019

This makes the behavior being consistent with generic store, The
orphan finalizer should be removed if the delete options does not
specify propagarionPolicy as orphan.

/kind bug
/sig api-machinery

Fixes #76042

NONE

@k8s-ci-robot k8s-ci-robot added release-note-none Denotes a PR that doesn't merit a release note. 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. needs-kind Indicates a PR lacks a `kind/foo` label and requires one. needs-sig Indicates an issue or PR lacks a `sig/foo` label and requires one. needs-priority Indicates a PR lacks a `priority/foo` label and requires one. needs-ok-to-test Indicates a PR that requires an org member to verify it is safe to test. labels Apr 2, 2019
@k8s-ci-robot
Copy link
Contributor

Hi @zhouhaibing089. Thanks for your PR.

I'm waiting for a kubernetes 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/test-infra repository.

@k8s-ci-robot k8s-ci-robot added kind/bug Categorizes issue or PR as related to a bug. and removed needs-kind Indicates a PR lacks a `kind/foo` label and requires one. labels Apr 2, 2019
@k8s-ci-robot k8s-ci-robot added sig/api-machinery Categorizes an issue or PR as relevant to SIG API Machinery. and removed needs-sig Indicates an issue or PR lacks a `sig/foo` label and requires one. labels Apr 2, 2019
@caesarxuchao
Copy link
Member

/assign

@caesarxuchao
Copy link
Member

/ok-to-test

@k8s-ci-robot k8s-ci-robot added ok-to-test Indicates a non-member PR verified by an org member that is safe to test. and removed needs-ok-to-test Indicates a PR that requires an org member to verify it is safe to test. labels Apr 5, 2019
}
if obj, _, err := storage.Delete(ctx, test.name, test.deleteOptions); err != nil {
t.Errorf("unexpected error: %v", err)
} else {
Copy link
Member

Choose a reason for hiding this comment

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

Use t.Fatal in the first case and remove the "else" here to reduce indention.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Sounds good.

newFinalizers = append(newFinalizers, metav1.FinalizerDeleteDependents)
}

existingNamespace.ObjectMeta.Finalizers = newFinalizers
Copy link
Member

Choose a reason for hiding this comment

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

Check if finalizers have changed to avoid unnecessary update.

oldFinalizerSet := sets.NewString(accessor.GetFinalizers()...)
newFinalizersSet := sets.NewString(newFinalizers...)
if oldFinalizerSet.Equal(newFinalizersSet) {
return false, accessor.GetFinalizers()
}

Copy link
Member

Choose a reason for hiding this comment

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

Also this is not the logic of the generic store, which will preserve the existing finalizers in the object is the deleteOptions doesn't explicitly set the mode of the deletion. See

// shouldOrphanDependents returns true if the finalizer for orphaning should be set
// updated for FinalizerOrphanDependents. In the order of highest to lowest
// priority, there are three factors affect whether to add/remove the
// FinalizerOrphanDependents: options, existing finalizers of the object,
// and e.DeleteStrategy.DefaultGarbageCollectionPolicy.
and
// shouldDeleteDependents returns true if the finalizer for foreground deletion should be set
// updated for FinalizerDeleteDependents. In the order of highest to lowest
// priority, there are three factors affect whether to add/remove the
// FinalizerDeleteDependents: options, existing finalizers of the object, and
// e.DeleteStrategy.DefaultGarbageCollectionPolicy.

Copy link
Member

Choose a reason for hiding this comment

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

Also, could you double check how the namespace controller will react if the namespace deletion is blocked by others?

The old code doesn't honor the foregroundDeletion, which could cause the namespace deletion to be blocked.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Ok, thanks for catching this. Now the behavior is changed to preserve finalizers if the delete options does not say explicitly.

could you double check how the namespace controller will react if the namespace deletion is blocked by others?

Checking now.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

@caesarxuchao: Namespace controller does not care about finalizers in object meta too much, it only cares about the finalizer kubernetes in the spec.

If kubernetes finalizer is removed, and it gets an event, it just sends a delete call again.

Finalizers: []string{
metav1.FinalizerOrphanDependents,
metav1.FinalizerDeleteDependents,
},
Copy link
Member

Choose a reason for hiding this comment

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

Also test a namespace that originally doesn't have finalizers?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yes, new tests added.

@zhouhaibing089
Copy link
Contributor Author

/test pull-kubernetes-bazel-test

@zhouhaibing089 zhouhaibing089 force-pushed the rm-orphan-by-default branch 2 times, most recently from 3ee2e45 to d9af2c2 Compare April 12, 2019 01:04
@zhouhaibing089
Copy link
Contributor Author

/retest

@caesarxuchao
Copy link
Member

/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 Apr 12, 2019
@zhouhaibing089
Copy link
Contributor Author

/assign @lavalamp

if string(finalizer) != metav1.FinalizerOrphanDependents {
newFinalizers = append(newFinalizers, finalizer)
}
// remove orphan and foregroundDeletion first, add them back if needed
Copy link
Member

Choose a reason for hiding this comment

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

Why is the logic structured this way? I am finding it very hard to follow. I would expect:

// 1. collect data
currentFinalizers := map[string]bool{}
for range existingNamespace.ObjectMeta.Finalizers { ... }

// 2. compute which finalizers it should have
shouldHaveFinalizers := map[string]bool{
  metav1.FinalizerOrphanDependents: shouldHaveOrphanFinalizer(options, ...),
  metav1.FinalizerDeleteDependents: shouldHaveForgroundFinalizer(options, ...),
}

// 3. do we need to make changes?
for k, v := range shouldHaveFinalizers {
  if v2, ok := currentFinalizers[k]; !ok || v != v2 {
    changeNeeded = true; break
  }
}

// 4. Make changes
if changeNeeded {
  ...
}

Putting the "shouldHaveXXXFinalizer" logic in a separate function will make it easier to unit test.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Ok, let me organize the code in the suggested way.

Copy link
Member

Choose a reason for hiding this comment

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

Thanks, sorry for being a pain!

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Updated. Thanks! (That's a great suggestion!)

Copy link
Member

Choose a reason for hiding this comment

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

Thanks, much easier to follow now. (Next time we change this I'll recommend not mutating the "current" map, but this is fine for now. :) )

@lavalamp
Copy link
Member

Why is the code different between namespace and other resources?

Thanks for the extensive tests.

@zhouhaibing089
Copy link
Contributor Author

Why is the code different between namespace and other resources?

@lavalamp: It should not be different. And that's exactly the thing this change would like to fix.

This makes the behavior being consistent with generic store, The
orphan finalizer should be removed if the delete options does not
specify propagarionPolicy as orphan.
@k8s-ci-robot k8s-ci-robot removed the lgtm "Looks good to me", indicates that a PR is ready to be merged. label Apr 26, 2019
@zhouhaibing089
Copy link
Contributor Author

Kindly ping @caesarxuchao and @lavalamp

@lavalamp
Copy link
Member

/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 Apr 30, 2019
@k8s-ci-robot
Copy link
Contributor

[APPROVALNOTIFIER] This PR is APPROVED

This pull-request has been approved by: caesarxuchao, lavalamp, zhouhaibing089

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 Apr 30, 2019
@k8s-ci-robot k8s-ci-robot merged commit b4cc2a5 into kubernetes:master Apr 30, 2019
@caesarxuchao
Copy link
Member

Note that the behavior of the namespace regarding finalizers is still slightly different from other built-in resources. When an UPDATE request removes the last metadata.finalizers, even if the spec.finalizers is empty, the namespace is not deleted by the apiserver. Clients need to send yet another DELETE request to delete the namespace.

CRD has the similar behavior.

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. kind/bug Categorizes issue or PR as related to a bug. lgtm "Looks good to me", indicates that a PR is ready to be merged. needs-priority Indicates a PR lacks a `priority/foo` label and requires one. ok-to-test Indicates a non-member PR verified by an org member that is safe to test. release-note-none Denotes a PR that doesn't merit a release note. sig/api-machinery Categorizes an issue or PR as relevant to SIG API Machinery. 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.

Namespace storage does not remove OrphanFinalizer during cascade deletion
4 participants