Skip to content

Commit

Permalink
replace opj deny newPod (#289)
Browse files Browse the repository at this point in the history
* replace-opj-handle-newpod

* fix go lint

* refactor extrainfo value

* refactor ExtraInfoNotAllowedToReplaceNewPod
  • Loading branch information
ColdsteelRail authored Oct 10, 2024
1 parent 4fa8b0e commit d67d3e1
Show file tree
Hide file tree
Showing 2 changed files with 76 additions and 1 deletion.
63 changes: 63 additions & 0 deletions pkg/controllers/operationjob/operationjob_controller_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ import (
appsv1alpha1 "kusionstack.io/kube-api/apps/v1alpha1"

"kusionstack.io/kuperator/pkg/controllers/collaset"
"kusionstack.io/kuperator/pkg/controllers/operationjob/replace"
"kusionstack.io/kuperator/pkg/controllers/poddeletion"
"kusionstack.io/kuperator/pkg/utils/inject"
)
Expand Down Expand Up @@ -285,6 +286,68 @@ var _ = Describe("operationjob controller", func() {

})

It("[replace] new pod", func() {
testcase := "test-replace-new-pod"
Expect(createNamespace(c, testcase)).Should(BeNil())
cs := createCollaSetWithReplicas("foo", testcase, 1)
podNames := getPodNamesFromCollaSet(cs)

oj := &appsv1alpha1.OperationJob{
ObjectMeta: metav1.ObjectMeta{
Namespace: testcase,
Name: "foo",
},
Spec: appsv1alpha1.OperationJobSpec{
Action: appsv1alpha1.OpsActionReplace,
Targets: []appsv1alpha1.PodOpsTarget{
{
Name: podNames[0],
},
},
},
}

Expect(c.Create(ctx, oj)).Should(BeNil())

// wait for new pod created
podList := &corev1.PodList{}
Eventually(func() bool {
Expect(c.List(ctx, podList, client.InNamespace(cs.Namespace))).Should(BeNil())
return len(podList.Items) == 2
}, time.Second*10, time.Second).Should(BeTrue())

// replace new pod
newPod := corev1.Pod{}
Expect(c.List(ctx, podList, client.InNamespace(cs.Namespace))).Should(BeNil())
for i := range podList.Items {
if _, exist := podList.Items[i].Labels[appsv1alpha1.PodReplacePairOriginName]; exist {
newPod = podList.Items[i]
}
}

oj2 := &appsv1alpha1.OperationJob{
ObjectMeta: metav1.ObjectMeta{
Namespace: testcase,
Name: "foo1",
},
Spec: appsv1alpha1.OperationJobSpec{
Action: appsv1alpha1.OpsActionReplace,
Targets: []appsv1alpha1.PodOpsTarget{
{
Name: newPod.Name,
},
},
},
}

Expect(c.Create(ctx, oj2)).Should(BeNil())
assertJobProgressProcessing(oj2, time.Second*10)

// the oj which replaces new pod should be blocked
_, exist := oj2.Status.TargetDetails[0].ExtraInfo[replace.ExtraInfoNotAllowedToReplaceNewPod]
Expect(exist).Should(BeTrue())
})

It("[replace] non-exist pod", func() {
testcase := "test-replace-non-exist-pod"
Expect(createNamespace(c, testcase)).Should(BeNil())
Expand Down
14 changes: 13 additions & 1 deletion pkg/controllers/operationjob/replace/replace.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,10 @@ const (
OperationJobReplacePodFinalizer = "finalizer.operationjob.kusionstack.io/replace-protected"
)

const (
ExtraInfoNotAllowedToReplaceNewPod = "NotAllowedToReplaceNewPod"
)

var _ ActionHandler = &PodReplaceHandler{}

type PodReplaceHandler struct {
Expand All @@ -61,10 +65,18 @@ func (p *PodReplaceHandler) Setup(controller controller.Controller, reconcileMix
func (p *PodReplaceHandler) OperateTargets(ctx context.Context, candidates []*OpsCandidate, operationJob *appsv1alpha1.OperationJob) error {
_, err := controllerutils.SlowStartBatch(len(candidates), controllerutils.SlowStartInitialBatchSize, false, func(i int, _ error) error {
candidate := candidates[i]
if candidate.Pod == nil {
if candidate.Pod == nil || candidate.Pod.Labels == nil {
return nil
}

// parse replace information from new pod
if originPodName, replaceOriginPodExists := candidate.Pod.Labels[appsv1alpha1.PodReplacePairOriginName]; replaceOriginPodExists {
candidate.OpsStatus.ExtraInfo[ExtraInfoNotAllowedToReplaceNewPod] = fmt.Sprintf("Not allowed to replace pod when it is a new pod for origin pod [%s]", originPodName)
return nil
} else {
delete(candidate.OpsStatus.ExtraInfo, ExtraInfoNotAllowedToReplaceNewPod)
}

// parse replace information from origin pod
_, replaceIndicated := candidate.Pod.Labels[appsv1alpha1.PodReplaceIndicationLabelKey]
_, replaceByReplaceUpdate := candidate.Pod.Labels[appsv1alpha1.PodReplaceByReplaceUpdateLabelKey]
Expand Down

0 comments on commit d67d3e1

Please sign in to comment.