Skip to content

Commit 39941b5

Browse files
authored
Merge pull request #12 from yyzxw/fix/mutil-snapshottask-created
fix: multiple snapshot task triggered
2 parents 27a6ff1 + ac23068 commit 39941b5

File tree

3 files changed

+84
-2
lines changed

3 files changed

+84
-2
lines changed

Dockerfile

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ RUN go mod download
1515
COPY cmd/main.go cmd/main.go
1616
COPY api/ api/
1717
COPY internal/ internal/
18+
COPY pkg/ pkg/
1819

1920
# Build
2021
# the GOARCH has not a default value to allow the binary be built according to the host where the command

internal/webhooks/pod.go

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -50,9 +50,21 @@ func (p *PodImageWebhookAdmission) Handle(ctx context.Context, request admission
5050
}
5151
switch request.Operation {
5252
case admissionv1.Delete:
53-
round, _ := p.handlePodUpdate(ctx, request.Namespace, request.Name)
53+
pod := &corev1.Pod{}
54+
err := json.Unmarshal(request.OldObject.Raw, pod)
55+
if err != nil {
56+
return admission.Errored(500, err)
57+
}
58+
59+
// avoid handling multiple pod deletion events. when pod deleted, webhook will send 3 times events, first event no delete timestamp.
60+
// see https://www.qinglite.cn/doc/527564767afc0aac0.
61+
if !pod.DeletionTimestamp.IsZero() {
62+
return admission.Allowed("already handled")
63+
}
64+
65+
round, _ := p.handlePodUpdate(ctx, pod.Namespace, pod.Name)
5466
if round != 0 {
55-
klog.Infof("trigger new round %d for pod deleting %s/%s", round, request.Namespace, request.Name)
67+
klog.Infof("trigger new round %d for pod deleting %s/%s", round, pod.Namespace, pod.Name)
5668
}
5769
return admission.Allowed(fmt.Sprintf("new round: %d", round))
5870
case admissionv1.Create:

internal/webhooks/pod_test.go

Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ import (
1919
"sigs.k8s.io/controller-runtime/pkg/webhook/admission"
2020

2121
"github.com/google/go-cmp/cmp"
22+
"github.com/samber/lo"
2223

2324
snapshotpodv1alpha1 "github.com/baizeai/kube-snapshot/api/v1alpha1"
2425
)
@@ -149,6 +150,12 @@ func mustMarshalObject(obj runtime.Object) []byte {
149150
return data
150151
}
151152

153+
func newPodWithDeleteTime() *corev1.Pod {
154+
pod := newPod()
155+
pod.DeletionTimestamp = lo.ToPtr(metav1.NewTime(time.Now()))
156+
return pod
157+
}
158+
152159
func TestPodImageWebhookAdmission_Handle(t *testing.T) {
153160
type fields struct {
154161
Client client.Client
@@ -277,6 +284,68 @@ func TestPodImageWebhookAdmission_Handle(t *testing.T) {
277284
},
278285
},
279286
},
287+
{
288+
name: "test handle delete event",
289+
fields: fields{
290+
Client: newFakeClient(newSnapshotPod()),
291+
},
292+
args: args{
293+
request: admission.Request{
294+
AdmissionRequest: admissionv1.AdmissionRequest{
295+
Operation: admissionv1.Delete,
296+
UID: "test-uid",
297+
OldObject: runtime.RawExtension{
298+
Raw: mustMarshalObject(newPod1()),
299+
},
300+
Resource: metav1.GroupVersionResource{
301+
Group: "",
302+
Version: "v1",
303+
Resource: "pods",
304+
},
305+
},
306+
},
307+
},
308+
want: admission.Response{
309+
AdmissionResponse: admissionv1.AdmissionResponse{
310+
Allowed: true,
311+
Result: &metav1.Status{
312+
Message: "new round: 3",
313+
Code: 200,
314+
},
315+
},
316+
},
317+
},
318+
{
319+
name: "test event already handled",
320+
fields: fields{
321+
Client: newFakeClient(newSnapshotPod()),
322+
},
323+
args: args{
324+
request: admission.Request{
325+
AdmissionRequest: admissionv1.AdmissionRequest{
326+
Operation: admissionv1.Delete,
327+
UID: "test-uid",
328+
OldObject: runtime.RawExtension{
329+
Raw: mustMarshalObject(newPodWithDeleteTime()),
330+
},
331+
Resource: metav1.GroupVersionResource{
332+
Group: "",
333+
Version: "v1",
334+
Resource: "pods",
335+
},
336+
},
337+
},
338+
},
339+
want: admission.Response{
340+
AdmissionResponse: admissionv1.AdmissionResponse{
341+
Allowed: true,
342+
Result: &metav1.Status{
343+
Message: "already handled",
344+
Code: 200,
345+
},
346+
},
347+
},
348+
},
280349
}
281350

282351
for _, tt := range tests {

0 commit comments

Comments
 (0)