Skip to content

Commit

Permalink
fix, no blocking when delete po
Browse files Browse the repository at this point in the history
  • Loading branch information
shaofan-hs committed Apr 15, 2024
1 parent 4a97d1d commit 0ab85ee
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 12 deletions.
8 changes: 6 additions & 2 deletions pkg/controllers/podopslifecycle/podopslifecycle_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -101,9 +101,9 @@ type ReconcilePodOpsLifecycle struct {
// +kubebuilder:rbac:groups=core,resources=events,verbs=create;update;patch

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

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

if pod.DeletionTimestamp != nil {
return reconcile.Result{}, nil
}

idToLabelsMap, _, err := PodIDAndTypesMap(pod)
if err != nil {
return reconcile.Result{}, err
Expand Down
7 changes: 4 additions & 3 deletions pkg/webhook/server/generic/pod/opslifecycle/mutating.go
Original file line number Diff line number Diff line change
Expand Up @@ -77,13 +77,14 @@ func (lc *OpsLifecycle) Mutating(ctx context.Context, c client.Client, oldPod, n

if _, ok := labels[v1alpha1.PodPreCheckedLabelPrefix]; ok { // pre-checked
_, hasPreparing := labels[v1alpha1.PodPreparingLabelPrefix]
if !hasPreparing {
_, hasOperate := labels[v1alpha1.PodOperateLabelPrefix]

if !hasPreparing && !hasOperate {
delete(newPod.Labels, v1alpha1.PodServiceAvailableLabel)

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

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

Expand Down Expand Up @@ -112,7 +113,7 @@ func (lc *OpsLifecycle) Mutating(ctx context.Context, c client.Client, oldPod, n
completeCount++
}
}
klog.Infof("pod: %s/%s, numOfIDs: %d, operatingCount: %d, operateCount: %d, operatedCount: %d, completeCount: %d", newPod.Namespace, newPod.Name, numOfIDs, operatingCount, operateCount, operatedCount, completeCount)
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)

for t, num := range undoTypeToNumsMap {
if num == typeToNumsMap[t] { // Reset the permission with type t if all operating with type t are canceled
Expand Down
21 changes: 16 additions & 5 deletions pkg/webhook/server/generic/pod/opslifecycle/validating.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,18 +23,26 @@ import (

admissionv1 "k8s.io/api/admission/v1"
corev1 "k8s.io/api/core/v1"
"k8s.io/klog/v2"
"sigs.k8s.io/controller-runtime/pkg/client"

controllerutils "kusionstack.io/operating/pkg/controllers/utils"
"kusionstack.io/operating/pkg/utils"
)

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

if _, err := controllerutils.PodAvailableConditions(newPod); err != nil {
defer func() {
if err != nil {
klog.Errorf("opslifecycle failed to validate pod: %s/%s, err: %v", newPod.Namespace, newPod.Name, err)
}
}()

_, err = controllerutils.PodAvailableConditions(newPod)
if err != nil {
return err
}

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

s := strings.Split(label, "/")
if len(s) != 2 {
return fmt.Errorf("invalid label %s", label)
err = fmt.Errorf("invalid label %s", label)
return
}
id := s[1]

if id != "" {
pairLabel := fmt.Sprintf("%s/%s", pairLabelPrefixesMap[v], id)
_, ok := newPod.Labels[pairLabel]
if !ok {
return fmt.Errorf("not found label %s", pairLabel)
err = fmt.Errorf("not found label %s", pairLabel)
return
}
}
}
Expand All @@ -82,7 +92,8 @@ func (lc *OpsLifecycle) Validating(ctx context.Context, c client.Client, oldPod,
}

if len(expectedLabels) != len(foundLabels) {
return fmt.Errorf("not found the expected label prefixes: %v", expectedLabels)
err = fmt.Errorf("not found the expected label prefixes: %v", expectedLabels)
return
}
return nil
}
4 changes: 2 additions & 2 deletions pkg/webhook/server/generic/pod/pod_mutating_handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,11 +51,11 @@ func NewMutatingHandler() *MutatingHandler {

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

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

logger := h.Logger.WithValues(
Expand Down

0 comments on commit 0ab85ee

Please sign in to comment.