Skip to content

Commit

Permalink
move recreatMethod Anno from oj to pod
Browse files Browse the repository at this point in the history
  • Loading branch information
ColdsteelRail committed Jul 9, 2024
1 parent 003a8d8 commit 3346669
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 43 deletions.
7 changes: 1 addition & 6 deletions pkg/controllers/operationjob/operationjob_manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,12 +38,7 @@ func (r *ReconcileOperationJob) newOperator(ctx context.Context, instance *appsv

switch instance.Spec.Action {
case appsv1alpha1.OpsActionRecreate:
recreateMethodAnno := instance.ObjectMeta.Annotations[appsv1alpha1.AnnotationOperationJobRecreateMethod]
if recreateMethodAnno == "" || recreate.GetRecreateHandler(recreateMethodAnno) == nil {
// use Kruise ContainerRecreateRequest to recreate container by default
return &recreate.ContainerRecreateControl{OperateInfo: operateInfo, Handler: recreate.GetRecreateHandler(recreate.KruiseCcontainerRecreateRequest)}
}
return &recreate.ContainerRecreateControl{OperateInfo: operateInfo, Handler: recreate.GetRecreateHandler(recreateMethodAnno)}
return &recreate.ContainerRecreateControl{OperateInfo: operateInfo}
case appsv1alpha1.OpsActionReplace:
return &replace.PodReplaceControl{OperateInfo: operateInfo,
PodControl: podcontrol.NewRealPodControl(r.ReconcilerMixin.Client, r.ReconcilerMixin.Scheme)}
Expand Down
18 changes: 18 additions & 0 deletions pkg/controllers/operationjob/recreate/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ package recreate
import (
"context"

corev1 "k8s.io/api/core/v1"
"sigs.k8s.io/controller-runtime/pkg/client"

appsv1alpha1 "kusionstack.io/operating/apis/apps/v1alpha1"
Expand Down Expand Up @@ -46,3 +47,20 @@ func GetRecreateHandler(methodName string) RestartHandler {
}
return nil
}

func GetRecreateHandlerFromPod(pod *corev1.Pod) RestartHandler {
// use Kruise ContainerRecreateRequest to recreate container by default
defaultRecreateHandler := GetRecreateHandler(KruiseCcontainerRecreateRequest)
if pod == nil || pod.ObjectMeta.Annotations == nil {
return defaultRecreateHandler
}

recreateMethodAnno, exist := pod.ObjectMeta.Annotations[appsv1alpha1.AnnotationOperationJobRecreateMethod]
if !exist || recreateMethodAnno == KruiseCcontainerRecreateRequest {
return defaultRecreateHandler
} else if currRecreateHandler := GetRecreateHandler(recreateMethodAnno); currRecreateHandler != nil {
return currRecreateHandler
} else {
return defaultRecreateHandler
}
}
20 changes: 13 additions & 7 deletions pkg/controllers/operationjob/recreate/recreate.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,6 @@ import (

type ContainerRecreateControl struct {
*OperateInfo
Handler RestartHandler
}

func (p *ContainerRecreateControl) ListTargets() ([]*OpsCandidate, error) {
Expand Down Expand Up @@ -90,10 +89,11 @@ func (p *ContainerRecreateControl) OperateTarget(candidate *OpsCandidate) error
return nil
}

handler := GetRecreateHandlerFromPod(candidate.Pod)
// if Pod is not during RecreateOpsLifecycle, trigger it
isDuringRecreateOps := podopslifecycle.IsDuringOps(ojutils.RecreateOpsLifecycleAdapter, candidate.Pod)
if !isDuringRecreateOps {
p.Recorder.Eventf(candidate.Pod, corev1.EventTypeNormal, "ConainerRecreateLifecycle", "try to begin PodOpsLifecycle for recreating Container of Pod")
p.Recorder.Eventf(candidate.Pod, corev1.EventTypeNormal, "ContainerRecreateLifecycle", "try to begin PodOpsLifecycle for recreating Container of Pod")
if err := ojutils.BeginRecreateLifecycle(p.Client, ojutils.RecreateOpsLifecycleAdapter, candidate.Pod); err != nil {
return err
}
Expand All @@ -102,14 +102,14 @@ func (p *ContainerRecreateControl) OperateTarget(candidate *OpsCandidate) error
// if Pod is allowed to recreate, try to do restart
_, allowed := podopslifecycle.AllowOps(ojutils.RecreateOpsLifecycleAdapter, realValue(p.OperationJob.Spec.OperationDelaySeconds), candidate.Pod)
if allowed {
err := p.Handler.DoRestartContainers(p.Context, p.Client, p.OperationJob, candidate, candidate.Containers)
err := handler.DoRestartContainers(p.Context, p.Client, p.OperationJob, candidate, candidate.Containers)
if err != nil {
return err
}
}

// if CRR completed or during updating opsLifecycle, try to finish Recreate PodOpsLifeCycle
candidate.PodOpsStatus.Progress = p.Handler.GetRestartProgress(p.Context, p.Client, p.OperationJob, candidate)
candidate.PodOpsStatus.Progress = handler.GetRestartProgress(p.Context, p.Client, p.OperationJob, candidate)
if IsCandidateOpsFinished(candidate) && isDuringRecreateOps {
if err := ojutils.FinishRecreateLifecycle(p.Client, ojutils.RecreateOpsLifecycleAdapter, candidate.Pod); err != nil {
return err
Expand Down Expand Up @@ -138,18 +138,24 @@ func (p *ContainerRecreateControl) FulfilPodOpsStatus(candidate *OpsCandidate) e
}

// calculate restart progress of podOpsStatus
candidate.PodOpsStatus.Progress = p.Handler.GetRestartProgress(p.Context, p.Client, p.OperationJob, candidate)
handler := GetRecreateHandlerFromPod(candidate.Pod)
candidate.PodOpsStatus.Progress = handler.GetRestartProgress(p.Context, p.Client, p.OperationJob, candidate)
return nil
}

func (p *ContainerRecreateControl) ReleaseTarget(candidate *OpsCandidate) error {
if candidate.Pod == nil {
return nil
}

// 1. release target
if err := p.Handler.ReleasePod(p.Context, p.Client, p.OperationJob, candidate); err != nil {
handler := GetRecreateHandlerFromPod(candidate.Pod)
if err := handler.ReleasePod(p.Context, p.Client, p.OperationJob, candidate); err != nil {
return err
}

// 2. cancel lifecycle if pod is during recreate lifecycle
if candidate.Pod != nil && podopslifecycle.IsDuringOps(ojutils.RecreateOpsLifecycleAdapter, candidate.Pod) {
if podopslifecycle.IsDuringOps(ojutils.RecreateOpsLifecycleAdapter, candidate.Pod) {
return ojutils.CancelOpsLifecycle(p.Context, p.Client, ojutils.RecreateOpsLifecycleAdapter, candidate.Pod)
}
return nil
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,15 +18,10 @@ package operationjob

import (
"context"
"encoding/json"
"net/http"

admissionv1 "k8s.io/api/admission/v1"
"sigs.k8s.io/controller-runtime/pkg/runtime/inject"
"sigs.k8s.io/controller-runtime/pkg/webhook/admission"

appsv1alpha1 "kusionstack.io/operating/apis/apps/v1alpha1"
"kusionstack.io/operating/pkg/controllers/operationjob/recreate"
"kusionstack.io/operating/pkg/utils/mixin"
)

Expand All @@ -44,30 +39,5 @@ func NewMutatingHandler() *MutatingHandler {
}

func (h *MutatingHandler) Handle(ctx context.Context, req admission.Request) (resp admission.Response) {
if req.Operation == admissionv1.Delete {
return admission.Allowed("")
}

var instance appsv1alpha1.OperationJob
if err := h.Decoder.Decode(req, &instance); err != nil {
return admission.Errored(http.StatusBadRequest, err)
}

if instance.Spec.Action != appsv1alpha1.OpsActionRecreate {
return admission.Allowed("")
}

if instance.ObjectMeta.Annotations == nil {
instance.ObjectMeta.Annotations = make(map[string]string)
}
if _, exist := instance.ObjectMeta.Annotations[appsv1alpha1.AnnotationOperationJobRecreateMethod]; !exist {
instance.ObjectMeta.Annotations[appsv1alpha1.AnnotationOperationJobRecreateMethod] = recreate.KruiseCcontainerRecreateRequest
marshalled, err := json.Marshal(instance)
if err != nil {
return admission.Errored(http.StatusInternalServerError, err)
}
return admission.PatchResponseFromRaw(req.AdmissionRequest.Object.Raw, marshalled)
}

return admission.Allowed("")
}

0 comments on commit 3346669

Please sign in to comment.