Skip to content

Commit

Permalink
fix: gracedelete webhook compatible in the absence of ServiceReadyRea…
Browse files Browse the repository at this point in the history
…dinessGate (#152)

* fix(gracedelete webhook): compatible in the absence of ServiceReadyReadinessGates

* test(gracedelete webhook): fix test cases

* test(gracedelete webhook): complete test cases
  • Loading branch information
cyh-ant committed Feb 5, 2024
1 parent f2fabbb commit 4e14416
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 0 deletions.
14 changes: 14 additions & 0 deletions pkg/webhook/server/generic/pod/gracedelete/webhook.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,20 @@ func (gd *GraceDelete) Validating(ctx context.Context, c client.Client, oldPod,
return nil
}

// if has no service-ready ReadinessGate, skip gracedelete
hasReadinessGate := false
if oldPod.Spec.ReadinessGates != nil {
for _, readinessGate := range oldPod.Spec.ReadinessGates {
if readinessGate.ConditionType == v1alpha1.ReadinessGatePodServiceReady {
hasReadinessGate = true
break
}
}
}
if !hasReadinessGate {
return nil
}

// if pod is allowed to delete
if _, allowed := podopslifecycle.AllowOps(poddeletion.OpsLifecycleAdapter, 0, oldPod); allowed {
return nil
Expand Down
31 changes: 31 additions & 0 deletions pkg/webhook/server/generic/pod/gracedelete/webhook_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,24 @@ func TestGraceDelete(t *testing.T) {
{
reqOperation: admissionv1.Delete,
},
{
fakePod: corev1.Pod{
ObjectMeta: metav1.ObjectMeta{
Namespace: "default",
Name: "test",
},
},
oldPod: corev1.Pod{
ObjectMeta: metav1.ObjectMeta{
Namespace: "default",
Name: "test",
Labels: map[string]string{
fmt.Sprintf(v1alpha1.ControlledByKusionStackLabelKey): "true",
},
},
},
reqOperation: admissionv1.Delete,
},
{
fakePod: corev1.Pod{
ObjectMeta: metav1.ObjectMeta{
Expand All @@ -68,6 +86,9 @@ func TestGraceDelete(t *testing.T) {
fmt.Sprintf(v1alpha1.ControlledByKusionStackLabelKey): "true",
},
},
Spec: corev1.PodSpec{
ReadinessGates: []corev1.PodReadinessGate{{ConditionType: v1alpha1.ReadinessGatePodServiceReady}},
},
},
keyWords: "not found",
reqOperation: admissionv1.Delete,
Expand All @@ -91,6 +112,9 @@ func TestGraceDelete(t *testing.T) {
fmt.Sprintf(v1alpha1.ControlledByKusionStackLabelKey): "true",
},
},
Spec: corev1.PodSpec{
ReadinessGates: []corev1.PodReadinessGate{{ConditionType: v1alpha1.ReadinessGatePodServiceReady}},
},
},
expectedLabels: map[string]string{
appsv1alpha1.PodDeletionIndicationLabelKey: "true",
Expand All @@ -117,6 +141,9 @@ func TestGraceDelete(t *testing.T) {
},
Finalizers: []string{"prot.podopslifecycle.kusionstack.io/finalizer1,prot.podopslifecycle.kusionstack.io/finalizer2"},
},
Spec: corev1.PodSpec{
ReadinessGates: []corev1.PodReadinessGate{{ConditionType: v1alpha1.ReadinessGatePodServiceReady}},
},
},
expectedLabels: map[string]string{
appsv1alpha1.PodDeletionIndicationLabelKey: "true",
Expand All @@ -137,7 +164,11 @@ func TestGraceDelete(t *testing.T) {
"operate.podopslifecycle.kusionstack.io/pod-delete": "1704865212856080006",
},
},
Spec: corev1.PodSpec{
ReadinessGates: []corev1.PodReadinessGate{{ConditionType: v1alpha1.ReadinessGatePodServiceReady}},
},
},

reqOperation: admissionv1.Delete,
},
}
Expand Down

0 comments on commit 4e14416

Please sign in to comment.