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

feat(webhook): add garcedelete webhook for PodOpsLifecycle #134

Merged
merged 4 commits into from
Dec 29, 2023
Merged

feat(webhook): add garcedelete webhook for PodOpsLifecycle #134

merged 4 commits into from
Dec 29, 2023

Conversation

cyh-ant
Copy link
Contributor

@cyh-ant cyh-ant commented Dec 18, 2023

1. Does this PR affect any open issues?(Y/N) and add issue references (e.g. "fix #123", "re #123".):

2. What is the scope of this PR (e.g. component or file name):

pkg/controllers/utils/podopslifecycle/adapter.go
pkg/webhook/server/generic/pod/gracedelete/lifecycle_adapter.go
pkg/webhook/server/generic/pod/gracedelete/validating.go
pkg/webhook/server/generic/pod/gracedelete/webhook.go
pkg/webhook/server/generic/pod/gracedelete/webhook_test.go
pkg/webhook/server/generic/pod/pod_validating_handler.go

3. Provide a description of the PR(e.g. more details, effects, motivations or doc link):

Add garcedelete validatingwebhookconfigurations of Pod. When , garceDeleteWebhook will trigger PodOpsLifecycle and reject the delete request. The delete request will be allowed until pod is labelled "PodOperateLabel".

4. Are there any breaking changes?(Y/N) and describe the breaking changes(e.g. more details, motivations or doc link):

  • Y

5. Are there test cases for these changes?(Y/N) select and add more details, references or doc links:

  • Unit test
    pkg/webhook/server/generic/pod/gracedelete/webhook_test.go

6. Release note

None

Copy link

github-actions bot commented Dec 18, 2023

CLA Assistant Lite bot All contributors have signed the CLA ✍️ ✅

@cyh-ant
Copy link
Contributor Author

cyh-ant commented Dec 18, 2023

I have read the CLA Document and I hereby sign the CLA

@cyh-ant
Copy link
Contributor Author

cyh-ant commented Dec 18, 2023

recheck


// GetID indicates ID of one PodOpsLifecycle
func (a *GraceDeleteOpsLifecycleAdapter) GetID() string {
return "grace-delete"
Copy link
Collaborator

Choose a reason for hiding this comment

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

Can we reuse the existing delete adapter?
Because the grace-delete here can share the same lifecycle progress with delete.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

The existing delete adapter is resued instead of GraceDeleteOpsLifecycleAdapter.

logger := h.Logger.WithValues(
"op", req.Operation,
"pod", commonutils.AdmissionRequestObjectKeyString(req),
)

if req.Operation == admissionv1.Delete {
if err := gracedelete.New().Validating(ctx, h.Client, req); err != nil {
Copy link
Collaborator

Choose a reason for hiding this comment

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

I think you can move gracedelete to webhooks list and run together later in line 77.

Of course, the for loop statement need to be changed to remove the short-circuit logic and aggregate all the returned errors, in order to execute every validating hook.

@wu8685
Copy link
Collaborator

wu8685 commented Dec 19, 2023

@cyh-ant I added some comments. PTAL.

Thanks for your contribution.


// if Pod is allow to operate, delete it
if _, allowed := podopslifecycle.AllowOps(OpsLifecycleAdapter, 0, pod); !allowed {
return fmt.Errorf("podOpsLifecycle denied, waiting for pod resource processing")
Copy link
Collaborator

Choose a reason for hiding this comment

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

Do we need to show this message more detailed and friendly to increase user experience?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

New error msg is show below podOpsLifecycle denied delete request, since related resources and finalizers have not been processed. Waiting for removing finalizers: %v.

return err
}

klog.Info("pod is deleted")
Copy link
Collaborator

Choose a reason for hiding this comment

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

Give it a high level?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

klog.V(2).Info is used in the new commit.

@@ -30,7 +30,7 @@ import (
)

func (lc *OpsLifecycle) Validating(ctx context.Context, c client.Client, oldPod, newPod *corev1.Pod, operation admissionv1.Operation) error {
if !utils.ControlledByKusionStack(newPod) {
if operation == admissionv1.Delete || !utils.ControlledByKusionStack(newPod) {
Copy link
Collaborator

Choose a reason for hiding this comment

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

Add a comment for later reading?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

The following annotations were added.

// GraceDeleteWebhook FeatureGate defaults to false
// Add '--feature-gates=GraceDeleteWebhook=true' to container args, to enable gracedelete webhook

Copy link

codecov bot commented Dec 28, 2023

Codecov Report

Attention: 16 lines in your changes are missing coverage. Please review.

Comparison is base (29ff994) 59.58% compared to head (84c09d7) 60.69%.
Report is 4 commits behind head on main.

Files Patch % Lines
.../webhook/server/generic/pod/gracedelete/webhook.go 57.14% 12 Missing and 3 partials ⚠️
...hook/server/generic/pod/opslifecycle/validating.go 0.00% 0 Missing and 1 partial ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##             main     #134      +/-   ##
==========================================
+ Coverage   59.58%   60.69%   +1.11%     
==========================================
  Files          46       47       +1     
  Lines        3489     3608     +119     
==========================================
+ Hits         2079     2190     +111     
- Misses       1192     1202      +10     
+ Partials      218      216       -2     
Flag Coverage Δ
unittests 60.69% <55.55%> (+1.11%) ⬆️

Flags with carried forward coverage won't be shown. Click here to find out more.

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

@shaofan-hs shaofan-hs merged commit af0a806 into KusionStack:main Dec 29, 2023
6 of 7 checks passed
@github-actions github-actions bot locked and limited conversation to collaborators Dec 29, 2023
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

提供灰度流量的能力 Enhancement: PodOpsLifecycle supports kubectl delete pod
3 participants