diff --git a/internal/webhooks/pod.go b/internal/webhooks/pod.go index c9446a9..af3c67f 100644 --- a/internal/webhooks/pod.go +++ b/internal/webhooks/pod.go @@ -134,6 +134,12 @@ func (p *PodImageWebhookAdmission) handlePodUpdate(ctx context.Context, ns, podN if !ok { return 0, nil } + + if !sp.Spec.AutoSaveOptions.AutoSaveOnTermination { + klog.Infof("no auto save for pod %s/%s", ns, podName) + return 0, nil + } + sp.Spec.TriggerRound += 1 err = p.Update(ctx, &sp) return sp.Spec.TriggerRound, err diff --git a/internal/webhooks/pod_test.go b/internal/webhooks/pod_test.go index 7039129..0f3a302 100644 --- a/internal/webhooks/pod_test.go +++ b/internal/webhooks/pod_test.go @@ -287,7 +287,11 @@ func TestPodImageWebhookAdmission_Handle(t *testing.T) { { name: "test handle delete event", fields: fields{ - Client: newFakeClient(newSnapshotPod()), + Client: newFakeClient(func() client.Object { + pod := newSnapshotPod() + pod.Spec.AutoSaveOptions.AutoSaveOnTermination = true + return pod + }()), }, args: args{ request: admission.Request{ @@ -315,6 +319,37 @@ func TestPodImageWebhookAdmission_Handle(t *testing.T) { }, }, }, + { + name: "test disable auto save", + fields: fields{ + Client: newFakeClient(newSnapshotPod()), + }, + args: args{ + request: admission.Request{ + AdmissionRequest: admissionv1.AdmissionRequest{ + Operation: admissionv1.Delete, + UID: "test-uid", + OldObject: runtime.RawExtension{ + Raw: mustMarshalObject(newPod1()), + }, + Resource: metav1.GroupVersionResource{ + Group: "", + Version: "v1", + Resource: "pods", + }, + }, + }, + }, + want: admission.Response{ + AdmissionResponse: admissionv1.AdmissionResponse{ + Allowed: true, + Result: &metav1.Status{ + Message: "new round: 0", + Code: 200, + }, + }, + }, + }, { name: "test event already handled", fields: fields{