-
Notifications
You must be signed in to change notification settings - Fork 771
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
Add DeletionProtection mechanism reject Namespace deletion when PVCs are included under NS #1228
Add DeletionProtection mechanism reject Namespace deletion when PVCs are included under NS #1228
Conversation
Welcome @kevin1689-cloud! It looks like this is your first PR to openkruise/kruise 🎉 |
Codecov Report
📣 This organization is not using Codecov’s GitHub App Integration. We recommend you install it so Codecov can continue to function properly for your repositories. Learn more @@ Coverage Diff @@
## master #1228 +/- ##
==========================================
- Coverage 48.50% 48.46% -0.04%
==========================================
Files 149 149
Lines 20606 20606
==========================================
- Hits 9995 9987 -8
- Misses 9517 9524 +7
- Partials 1094 1095 +1
Flags with carried forward coverage won't be shown. Click here to find out more. see 1 file with indirect coverage changes 📣 We’re building smart automated test selection to slash your CI/CD build times. Learn more |
2cb9866
to
33b35c9
Compare
… are included under namespace (openkruise#1228) Signed-off-by: kevin1689 <kevinyang1689@163.com>
33b35c9
to
08d7100
Compare
@kevin1689-cloud good, can you can some E2E in code? |
Sure, l will working on that. |
@furykerry Is only PVC bound to PV worth protecting? |
@kevin1689-cloud I think PVC which unbound to pv or deleting are not worth protecting. |
@zmberg Got it, as the Deletion Protection mechanism is aim to enhance applications' availability, I agree with the PVC which not bound to PV is not worth protecting. I will modify my code and E2E to achieve that. I checked kubernetes code, PVC have 3 status: Pending, Bound, Lost. So the logic is when PVC's status is in "Bound", protect the NameSpace to be delete, and when PVC is in other status, the NameSpace can be delete, is that correct? |
Yes. |
Ok. |
StorageClassName: &storageClassName, | ||
}, | ||
} | ||
pvc, err = c.CoreV1().PersistentVolumeClaims(ns.Name).Create(context.TODO(), pvc, metav1.CreateOptions{}) |
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.
SA4006: this value of pvc is never used
ℹ️ Expand to see all @sonatype-lift commands
You can reply with the following commands. For example, reply with @sonatype-lift ignoreall to leave out all findings.
Command | Usage |
---|---|
@sonatype-lift ignore |
Leave out the above finding from this PR |
@sonatype-lift ignoreall |
Leave out all the existing findings from this PR |
@sonatype-lift exclude <file|issue|path|tool> |
Exclude specified file|issue|path|tool from Lift findings by updating your config.toml file |
Note: When talking to LiftBot, you need to refresh the page to see its response.
Click here to add LiftBot to another repo.
Help us improve LIFT! (Sonatype LiftBot external survey)
Was this a good recommendation for you? Answering this survey will not impact your Lift settings.
[ 🙁 Not relevant ] - [ 😕 Won't fix ] - [ 😑 Not critical, will fix ] - [ 🙂 Critical, will fix ] - [ 😊 Critical, fixing now ]
}, | ||
}, | ||
} | ||
pv, err = framework.CreatePV(c, pv) |
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.
SA4006: this value of pv is never used
ℹ️ Expand to see all @sonatype-lift commands
You can reply with the following commands. For example, reply with @sonatype-lift ignoreall to leave out all findings.
Command | Usage |
---|---|
@sonatype-lift ignore |
Leave out the above finding from this PR |
@sonatype-lift ignoreall |
Leave out all the existing findings from this PR |
@sonatype-lift exclude <file|issue|path|tool> |
Exclude specified file|issue|path|tool from Lift findings by updating your config.toml file |
Note: When talking to LiftBot, you need to refresh the page to see its response.
Click here to add LiftBot to another repo.
Help us improve LIFT! (Sonatype LiftBot external survey)
Was this a good recommendation for you? Answering this survey will not impact your Lift settings.
[ 🙁 Not relevant ] - [ 😕 Won't fix ] - [ 😑 Not critical, will fix ] - [ 🙂 Critical, will fix ] - [ 😊 Critical, fixing now ]
ginkgo.By("Delete the PV bounded to PVC") | ||
err = c.CoreV1().PersistentVolumes().Delete(context.TODO(), pvName, metav1.DeleteOptions{}) | ||
_, err = c.CoreV1().PersistentVolumes().Patch(context.TODO(), pvName, types.StrategicMergePatchType, | ||
[]byte(fmt.Sprintf(`{"metadata":{"finalizers":null}}`)), metav1.PatchOptions{}) |
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.
S1039: unnecessary use of fmt.Sprintf
ℹ️ Expand to see all @sonatype-lift commands
You can reply with the following commands. For example, reply with @sonatype-lift ignoreall to leave out all findings.
Command | Usage |
---|---|
@sonatype-lift ignore |
Leave out the above finding from this PR |
@sonatype-lift ignoreall |
Leave out all the existing findings from this PR |
@sonatype-lift exclude <file|issue|path|tool> |
Exclude specified file|issue|path|tool from Lift findings by updating your config.toml file |
Note: When talking to LiftBot, you need to refresh the page to see its response.
Click here to add LiftBot to another repo.
Help us improve LIFT! (Sonatype LiftBot external survey)
Was this a good recommendation for you? Answering this survey will not impact your Lift settings.
[ 🙁 Not relevant ] - [ 😕 Won't fix ] - [ 😑 Not critical, will fix ] - [ 🙂 Critical, will fix ] - [ 😊 Critical, fixing now ]
… are included under namespace (openkruise#1228) Signed-off-by: kevin1689 <kevinyang1689@163.com>
5fc170f
to
3ff577b
Compare
@zmberg Done. Now namespace will be protected only when PVCs in "Bound" status are included under the namespce. The relevant E2E tests have been added. Please take a look. |
var boundCount int | ||
for i := range pvcs.Items { | ||
pvc := &pvcs.Items[i] | ||
if pvc.Status.Phase == v1.ClaimBound { |
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.
if pvc.deletionTimestamp == nil && pvc.Status.Phase == v1.ClaimBound {
}
@@ -19,6 +19,7 @@ package policy | |||
import ( | |||
"context" | |||
"fmt" | |||
"k8s.io/apimachinery/pkg/api/resource" |
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.
go imports the 'import' section.
… are included under namespace (openkruise#1228) Signed-off-by: kevin1689 <kevinyang1689@163.com>
1f18afb
to
6fa1bab
Compare
6fa1bab
to
738b036
Compare
… in Bound status are included under NS (openkruise#1228) Signed-off-by: kevin1689 <kevinyang1689@163.com>
335a828
to
5e1004d
Compare
… in Bound status are included under NS (openkruise#1228) Signed-off-by: kevin1689 <kevinyang1689@163.com>
5e1004d
to
ba0c168
Compare
@zmberg Done. I have made a misoperation when push the code and submitted serveral recently merged issue's code. Now I have fixed this misoperation. Please check and if there are any problem, please tell me. Thanks! |
/lgtm |
@@ -71,6 +71,21 @@ func ValidateNamespaceDeletion(c client.Client, namespace *v1.Namespace) error { | |||
if activeCount > 0 { | |||
return fmt.Errorf("forbidden by ResourcesProtectionDeletion for %s=%s and active pods %d>0", policyv1alpha1.DeletionProtectionKey, val, activeCount) | |||
} | |||
|
|||
pvcs := v1.PersistentVolumeClaimList{} | |||
if err := c.List(context.TODO(), &pvcs, client.InNamespace(namespace.Name)); 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.
plz add DisableDeepCopy option 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.
and add DisableDeepCopy option in list pods function again.
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.
@furykerry @zmberg Done. Now the list pvc and list pods function is as shown below:
pvcs := v1.PersistentVolumeClaimList{}
if err := c.List(context.TODO(), &pvcs, client.InNamespace(namespace.Name), utilclient.DisableDeepCopy);
pods := v1.PodList{}
if err := c.List(context.TODO(), &pods, client.InNamespace(namespace.Name), utilclient.DisableDeepCopy);
Please take a look.
… in Bound status are included under NS (openkruise#1228) Signed-off-by: kevin1689 <kevinyang1689@163.com>
ba0c168
to
b41e773
Compare
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.
/lgtm
/approve |
[APPROVALNOTIFIER] This PR is APPROVED This pull-request has been approved by: zmberg 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 |
… in Bound status are included under NS (openkruise#1228) (openkruise#1228) Signed-off-by: kevin1689 <kevinyang1689@163.com>
… in Bound status are included under NS (openkruise#1228) (openkruise#1228) Signed-off-by: kevin1689 <kevinyang1689@163.com>
… in Bound status are included under NS (openkruise#1228) (openkruise#1228)
Ⅰ. Describe what this PR does
This PR complete a feature: The DeletionProtection mechanism reject Namespace deletion when PVCs are included under NameSpace
Ⅱ. Does this pull request fix one issue?
fixes #1223
Ⅲ. Describe how to verify it
1.Create a namespace and label it with "policy.kruise.io/delete-protection=Cascading"
2.Create a pvc in that namespace
3.Try to delete the namespce, the namespace can't be deleted and show an error which tell user the reason is there are existing pvc in the namespce
Ⅳ. Special notes for reviews
None