Skip to content

Commit

Permalink
chore: add availablity check for rebuildInstance ops (#6900)
Browse files Browse the repository at this point in the history
  • Loading branch information
wangyelei authored Mar 27, 2024
1 parent b3fcb4d commit 48494b8
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 1 deletion.
18 changes: 18 additions & 0 deletions controllers/apps/operations/rebuild_instance.go
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,24 @@ func (r rebuildInstanceOpsHandler) Action(reqCtx intctrlutil.RequestCtx, cli cli
appsv1alpha1.AbnormalClusterCompPhase, appsv1alpha1.UpdatingClusterCompPhase}, compStatus.Phase) {
return intctrlutil.NewFatalError(fmt.Sprintf(`the phase of component "%s" can not be %s`, v.ComponentName, compStatus.Phase))
}
comp := opsRes.Cluster.Spec.GetComponentByName(v.ComponentName)
synthesizedComp, err := component.BuildSynthesizedComponentWrapper(reqCtx, cli, opsRes.Cluster, comp)
if err != nil {
return err
}
for _, podName := range v.InstanceNames {
targetPod := &corev1.Pod{}
if err = cli.Get(reqCtx.Ctx, client.ObjectKey{Name: podName, Namespace: opsRes.Cluster.Namespace}, targetPod); err != nil {
return err
}
isAvailable, err := r.instanceIsAvailable(synthesizedComp, targetPod)
if err != nil {
return err
}
if isAvailable {
return intctrlutil.NewFatalError(fmt.Sprintf(`instance "%s" is availabled, can not rebuild it`, podName))
}
}
}
return nil
}
Expand Down
12 changes: 11 additions & 1 deletion controllers/apps/operations/rebuild_instance_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -164,7 +164,17 @@ var _ = Describe("OpsUtil functions", func() {
opsRes.Cluster.Status.Components[consensusComp] = compStatus
})).Should(Succeed())

By("expect for opsRequest phase is Running")
By("expect for opsRequest phase is Failed due to the pod is Available")
_, _ = GetOpsManager().Do(reqCtx, k8sClient, opsRes)
Expect(opsRes.OpsRequest.Status.Phase).Should(Equal(appsv1alpha1.OpsFailedPhase))

By("fake pod is unavailable")
opsRes.OpsRequest.Status.Phase = appsv1alpha1.OpsCreatingPhase
for _, podName := range opsRes.OpsRequest.Spec.RebuildFrom[0].InstanceNames {
Expect(testapps.GetAndChangeObjStatus(&testCtx, client.ObjectKey{Name: podName, Namespace: opsRes.OpsRequest.Namespace}, func(pod *corev1.Pod) {
pod.Status.Conditions = nil
})()).Should(Succeed())
}
_, _ = GetOpsManager().Do(reqCtx, k8sClient, opsRes)
Expect(opsRes.OpsRequest.Status.Phase).Should(Equal(appsv1alpha1.OpsCreatingPhase))
})
Expand Down

0 comments on commit 48494b8

Please sign in to comment.