Skip to content

Commit 0ab85ee

Browse files
committed
fix, no blocking when delete po
1 parent 4a97d1d commit 0ab85ee

File tree

4 files changed

+28
-12
lines changed

4 files changed

+28
-12
lines changed

pkg/controllers/podopslifecycle/podopslifecycle_controller.go

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -101,9 +101,9 @@ type ReconcilePodOpsLifecycle struct {
101101
// +kubebuilder:rbac:groups=core,resources=events,verbs=create;update;patch
102102

103103
func (r *ReconcilePodOpsLifecycle) Reconcile(ctx context.Context, request reconcile.Request) (reconcile.Result, error) {
104-
key := fmt.Sprintf("%s/%s", request.Namespace, request.Name)
104+
key := request.String()
105105
logger := r.Logger.WithValues("pod", key)
106-
logger.Info("reconciling pod lifecycle")
106+
defer logger.Info("reconcile finished")
107107

108108
pod := &corev1.Pod{}
109109
err := r.Client.Get(ctx, request.NamespacedName, pod)
@@ -121,6 +121,10 @@ func (r *ReconcilePodOpsLifecycle) Reconcile(ctx context.Context, request reconc
121121
return reconcile.Result{}, nil
122122
}
123123

124+
if pod.DeletionTimestamp != nil {
125+
return reconcile.Result{}, nil
126+
}
127+
124128
idToLabelsMap, _, err := PodIDAndTypesMap(pod)
125129
if err != nil {
126130
return reconcile.Result{}, err

pkg/webhook/server/generic/pod/opslifecycle/mutating.go

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -77,13 +77,14 @@ func (lc *OpsLifecycle) Mutating(ctx context.Context, c client.Client, oldPod, n
7777

7878
if _, ok := labels[v1alpha1.PodPreCheckedLabelPrefix]; ok { // pre-checked
7979
_, hasPreparing := labels[v1alpha1.PodPreparingLabelPrefix]
80-
if !hasPreparing {
80+
_, hasOperate := labels[v1alpha1.PodOperateLabelPrefix]
81+
82+
if !hasPreparing && !hasOperate {
8183
delete(newPod.Labels, v1alpha1.PodServiceAvailableLabel)
8284

8385
lc.addLabelWithTime(newPod, fmt.Sprintf("%s/%s", v1alpha1.PodPreparingLabelPrefix, id)) // preparing
8486
}
8587

86-
_, hasOperate := labels[v1alpha1.PodOperateLabelPrefix]
8788
if !hasOperate && lc.readyToOperate(newPod) {
8889
delete(newPod.Labels, fmt.Sprintf("%s/%s", v1alpha1.PodPreparingLabelPrefix, id))
8990

@@ -112,7 +113,7 @@ func (lc *OpsLifecycle) Mutating(ctx context.Context, c client.Client, oldPod, n
112113
completeCount++
113114
}
114115
}
115-
klog.Infof("pod: %s/%s, numOfIDs: %d, operatingCount: %d, operateCount: %d, operatedCount: %d, completeCount: %d", newPod.Namespace, newPod.Name, numOfIDs, operatingCount, operateCount, operatedCount, completeCount)
116+
klog.V(5).Infof("pod: %s/%s, numOfIDs: %d, operatingCount: %d, operateCount: %d, operatedCount: %d, completeCount: %d", newPod.Namespace, newPod.Name, numOfIDs, operatingCount, operateCount, operatedCount, completeCount)
116117

117118
for t, num := range undoTypeToNumsMap {
118119
if num == typeToNumsMap[t] { // Reset the permission with type t if all operating with type t are canceled

pkg/webhook/server/generic/pod/opslifecycle/validating.go

Lines changed: 16 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -23,18 +23,26 @@ import (
2323

2424
admissionv1 "k8s.io/api/admission/v1"
2525
corev1 "k8s.io/api/core/v1"
26+
"k8s.io/klog/v2"
2627
"sigs.k8s.io/controller-runtime/pkg/client"
2728

2829
controllerutils "kusionstack.io/operating/pkg/controllers/utils"
2930
"kusionstack.io/operating/pkg/utils"
3031
)
3132

32-
func (lc *OpsLifecycle) Validating(ctx context.Context, c client.Client, oldPod, newPod *corev1.Pod, operation admissionv1.Operation) error {
33+
func (lc *OpsLifecycle) Validating(ctx context.Context, c client.Client, oldPod, newPod *corev1.Pod, operation admissionv1.Operation) (err error) {
3334
if operation == admissionv1.Delete || !utils.ControlledByKusionStack(newPod) {
3435
return nil
3536
}
3637

37-
if _, err := controllerutils.PodAvailableConditions(newPod); err != nil {
38+
defer func() {
39+
if err != nil {
40+
klog.Errorf("opslifecycle failed to validate pod: %s/%s, err: %v", newPod.Namespace, newPod.Name, err)
41+
}
42+
}()
43+
44+
_, err = controllerutils.PodAvailableConditions(newPod)
45+
if err != nil {
3846
return err
3947
}
4048

@@ -48,15 +56,17 @@ func (lc *OpsLifecycle) Validating(ctx context.Context, c client.Client, oldPod,
4856

4957
s := strings.Split(label, "/")
5058
if len(s) != 2 {
51-
return fmt.Errorf("invalid label %s", label)
59+
err = fmt.Errorf("invalid label %s", label)
60+
return
5261
}
5362
id := s[1]
5463

5564
if id != "" {
5665
pairLabel := fmt.Sprintf("%s/%s", pairLabelPrefixesMap[v], id)
5766
_, ok := newPod.Labels[pairLabel]
5867
if !ok {
59-
return fmt.Errorf("not found label %s", pairLabel)
68+
err = fmt.Errorf("not found label %s", pairLabel)
69+
return
6070
}
6171
}
6272
}
@@ -82,7 +92,8 @@ func (lc *OpsLifecycle) Validating(ctx context.Context, c client.Client, oldPod,
8292
}
8393

8494
if len(expectedLabels) != len(foundLabels) {
85-
return fmt.Errorf("not found the expected label prefixes: %v", expectedLabels)
95+
err = fmt.Errorf("not found the expected label prefixes: %v", expectedLabels)
96+
return
8697
}
8798
return nil
8899
}

pkg/webhook/server/generic/pod/pod_mutating_handler.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -51,11 +51,11 @@ func NewMutatingHandler() *MutatingHandler {
5151

5252
func (h *MutatingHandler) Handle(ctx context.Context, req admission.Request) (resp admission.Response) {
5353
if req.Kind.Kind != "Pod" {
54-
return admission.Patched("Invalid kind")
54+
return admission.Patched("invalid kind")
5555
}
5656

5757
if req.Operation != admissionv1.Create && req.Operation != admissionv1.Update {
58-
return admission.Patched("Not Create or Update, but " + string(req.Operation))
58+
return admission.Patched("not Create or Update, but " + string(req.Operation))
5959
}
6060

6161
logger := h.Logger.WithValues(

0 commit comments

Comments
 (0)