Skip to content

Commit cbd897a

Browse files
authored
fix: only increase trigger round when enable autosave (#15)
Co-authored-by: 朱 <[email protected]>
1 parent 39941b5 commit cbd897a

File tree

2 files changed

+42
-1
lines changed

2 files changed

+42
-1
lines changed

internal/webhooks/pod.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -134,6 +134,12 @@ func (p *PodImageWebhookAdmission) handlePodUpdate(ctx context.Context, ns, podN
134134
if !ok {
135135
return 0, nil
136136
}
137+
138+
if !sp.Spec.AutoSaveOptions.AutoSaveOnTermination {
139+
klog.Infof("no auto save for pod %s/%s", ns, podName)
140+
return 0, nil
141+
}
142+
137143
sp.Spec.TriggerRound += 1
138144
err = p.Update(ctx, &sp)
139145
return sp.Spec.TriggerRound, err

internal/webhooks/pod_test.go

Lines changed: 36 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -287,7 +287,11 @@ func TestPodImageWebhookAdmission_Handle(t *testing.T) {
287287
{
288288
name: "test handle delete event",
289289
fields: fields{
290-
Client: newFakeClient(newSnapshotPod()),
290+
Client: newFakeClient(func() client.Object {
291+
pod := newSnapshotPod()
292+
pod.Spec.AutoSaveOptions.AutoSaveOnTermination = true
293+
return pod
294+
}()),
291295
},
292296
args: args{
293297
request: admission.Request{
@@ -315,6 +319,37 @@ func TestPodImageWebhookAdmission_Handle(t *testing.T) {
315319
},
316320
},
317321
},
322+
{
323+
name: "test disable auto save",
324+
fields: fields{
325+
Client: newFakeClient(newSnapshotPod()),
326+
},
327+
args: args{
328+
request: admission.Request{
329+
AdmissionRequest: admissionv1.AdmissionRequest{
330+
Operation: admissionv1.Delete,
331+
UID: "test-uid",
332+
OldObject: runtime.RawExtension{
333+
Raw: mustMarshalObject(newPod1()),
334+
},
335+
Resource: metav1.GroupVersionResource{
336+
Group: "",
337+
Version: "v1",
338+
Resource: "pods",
339+
},
340+
},
341+
},
342+
},
343+
want: admission.Response{
344+
AdmissionResponse: admissionv1.AdmissionResponse{
345+
Allowed: true,
346+
Result: &metav1.Status{
347+
Message: "new round: 0",
348+
Code: 200,
349+
},
350+
},
351+
},
352+
},
318353
{
319354
name: "test event already handled",
320355
fields: fields{

0 commit comments

Comments
 (0)