-
Notifications
You must be signed in to change notification settings - Fork 38.7k
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
namespace: remove gc finalizers based on delete options #76051
Conversation
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 Once the patch is verified, the new status will be reflected by the 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. |
/assign |
/ok-to-test |
} | ||
if obj, _, err := storage.Delete(ctx, test.name, test.deleteOptions); err != nil { | ||
t.Errorf("unexpected error: %v", err) | ||
} else { |
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.
Use t.Fatal in the first case and remove the "else" here to reduce indention.
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.
Sounds good.
newFinalizers = append(newFinalizers, metav1.FinalizerDeleteDependents) | ||
} | ||
|
||
existingNamespace.ObjectMeta.Finalizers = newFinalizers |
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.
Check if finalizers have changed to avoid unnecessary update.
kubernetes/staging/src/k8s.io/apiserver/pkg/registry/generic/registry/store.go
Lines 740 to 744 in 20cece6
oldFinalizerSet := sets.NewString(accessor.GetFinalizers()...) | |
newFinalizersSet := sets.NewString(newFinalizers...) | |
if oldFinalizerSet.Equal(newFinalizersSet) { | |
return false, accessor.GetFinalizers() | |
} |
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.
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
kubernetes/staging/src/k8s.io/apiserver/pkg/registry/generic/registry/store.go
Lines 619 to 623 in 20cece6
// 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. |
kubernetes/staging/src/k8s.io/apiserver/pkg/registry/generic/registry/store.go
Lines 669 to 673 in 20cece6
// 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. |
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.
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.
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.
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.
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.
@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, | ||
}, |
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.
Also test a namespace that originally doesn't have finalizers?
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.
Yes, new tests added.
03278a2
to
51f7344
Compare
/test pull-kubernetes-bazel-test |
3ee2e45
to
d9af2c2
Compare
/retest |
/lgtm |
/assign @lavalamp |
if string(finalizer) != metav1.FinalizerOrphanDependents { | ||
newFinalizers = append(newFinalizers, finalizer) | ||
} | ||
// remove orphan and foregroundDeletion first, add them back if needed |
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 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.
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.
Ok, let me organize the code in the suggested way.
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.
Thanks, sorry for being a pain!
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.
Updated. Thanks! (That's a great suggestion!)
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.
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. :) )
Why is the code different between namespace and other resources? Thanks for the extensive tests. |
@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.
d9af2c2
to
a458e9b
Compare
Kindly ping @caesarxuchao and @lavalamp |
/lgtm |
[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 |
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. |
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