Skip to content

Commit

Permalink
fix: multiple snapshot task triggred
Browse files Browse the repository at this point in the history
  • Loading branch information
authored and committed Aug 16, 2024
1 parent d031b0a commit ac23068
Show file tree
Hide file tree
Showing 3 changed files with 84 additions and 2 deletions.
1 change: 1 addition & 0 deletions Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ RUN go mod download
COPY cmd/main.go cmd/main.go
COPY api/ api/
COPY internal/ internal/
COPY pkg/ pkg/

# Build
# the GOARCH has not a default value to allow the binary be built according to the host where the command
Expand Down
16 changes: 14 additions & 2 deletions internal/webhooks/pod.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,9 +50,21 @@ func (p *PodImageWebhookAdmission) Handle(ctx context.Context, request admission
}
switch request.Operation {
case admissionv1.Delete:
round, _ := p.handlePodUpdate(ctx, request.Namespace, request.Name)
pod := &corev1.Pod{}
err := json.Unmarshal(request.OldObject.Raw, pod)
if err != nil {
return admission.Errored(500, err)

Check warning on line 56 in internal/webhooks/pod.go

View check run for this annotation

Codecov / codecov/patch

internal/webhooks/pod.go#L56

Added line #L56 was not covered by tests
}

// avoid handling multiple pod deletion events. when pod deleted, webhook will send 3 times events, first event no delete timestamp.
// see https://www.qinglite.cn/doc/527564767afc0aac0.
if !pod.DeletionTimestamp.IsZero() {
return admission.Allowed("already handled")
}

round, _ := p.handlePodUpdate(ctx, pod.Namespace, pod.Name)
if round != 0 {
klog.Infof("trigger new round %d for pod deleting %s/%s", round, request.Namespace, request.Name)
klog.Infof("trigger new round %d for pod deleting %s/%s", round, pod.Namespace, pod.Name)
}
return admission.Allowed(fmt.Sprintf("new round: %d", round))
case admissionv1.Create:
Expand Down
69 changes: 69 additions & 0 deletions internal/webhooks/pod_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import (
"sigs.k8s.io/controller-runtime/pkg/webhook/admission"

"github.com/google/go-cmp/cmp"
"github.com/samber/lo"

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

func newPodWithDeleteTime() *corev1.Pod {
pod := newPod()
pod.DeletionTimestamp = lo.ToPtr(metav1.NewTime(time.Now()))
return pod
}

func TestPodImageWebhookAdmission_Handle(t *testing.T) {
type fields struct {
Client client.Client
Expand Down Expand Up @@ -277,6 +284,68 @@ func TestPodImageWebhookAdmission_Handle(t *testing.T) {
},
},
},
{
name: "test handle delete event",
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: 3",
Code: 200,
},
},
},
},
{
name: "test event already handled",
fields: fields{
Client: newFakeClient(newSnapshotPod()),
},
args: args{
request: admission.Request{
AdmissionRequest: admissionv1.AdmissionRequest{
Operation: admissionv1.Delete,
UID: "test-uid",
OldObject: runtime.RawExtension{
Raw: mustMarshalObject(newPodWithDeleteTime()),
},
Resource: metav1.GroupVersionResource{
Group: "",
Version: "v1",
Resource: "pods",
},
},
},
},
want: admission.Response{
AdmissionResponse: admissionv1.AdmissionResponse{
Allowed: true,
Result: &metav1.Status{
Message: "already handled",
Code: 200,
},
},
},
},
}

for _, tt := range tests {
Expand Down

0 comments on commit ac23068

Please sign in to comment.