Skip to content

Commit

Permalink
Avoid race condition during importer termination by returning 0 exitC…
Browse files Browse the repository at this point in the history
…ode when scratch space is required

The restart policy on failure along with manual pod deletion caused some issues after the importer exited with scratch space needed.

This commit sets the exit code to 0 when exiting for scratch space required so we manually delete the pod and avoid the described race condition.

Signed-off-by: Alvaro Romero <alromero@redhat.com>
  • Loading branch information
alromeros committed Feb 29, 2024
1 parent d3421bf commit 64641fb
Show file tree
Hide file tree
Showing 4 changed files with 10 additions and 14 deletions.
2 changes: 1 addition & 1 deletion cmd/cdi-importer/importer.go
Original file line number Diff line number Diff line change
Expand Up @@ -187,7 +187,7 @@ func handleImport(
if err := util.WriteTerminationMessage(common.ScratchSpaceRequired); err != nil {
klog.Errorf("%+v", err)
}
return common.ScratchSpaceNeededExitCode
return 0
}
err = util.WriteTerminationMessage(fmt.Sprintf("Unable to process data: %v", err.Error()))
if err != nil {
Expand Down
3 changes: 0 additions & 3 deletions pkg/common/common.go
Original file line number Diff line number Diff line change
Expand Up @@ -205,9 +205,6 @@ const (
// DefaultResyncPeriod sets a 10 minute resync period, used in the controller pkg and the controller cmd executable
DefaultResyncPeriod = 10 * time.Minute

// ScratchSpaceNeededExitCode is the exit code that indicates the importer pod requires scratch space to function properly.
ScratchSpaceNeededExitCode = 42

// ScratchNameSuffix (controller pkg only)
ScratchNameSuffix = "scratch"

Expand Down
11 changes: 5 additions & 6 deletions pkg/controller/import-controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -372,15 +372,14 @@ func (r *ImportReconciler) updatePvcFromPod(pvc *corev1.PersistentVolumeClaim, p

scratchExitCode := false
if pod.Status.ContainerStatuses != nil &&
pod.Status.ContainerStatuses[0].LastTerminationState.Terminated != nil &&
pod.Status.ContainerStatuses[0].LastTerminationState.Terminated.ExitCode > 0 {
log.Info("Pod termination code", "pod.Name", pod.Name, "ExitCode", pod.Status.ContainerStatuses[0].LastTerminationState.Terminated.ExitCode)
if pod.Status.ContainerStatuses[0].LastTerminationState.Terminated.ExitCode == common.ScratchSpaceNeededExitCode {
pod.Status.ContainerStatuses[0].State.Terminated != nil {
if message := pod.Status.ContainerStatuses[0].State.Terminated.Message; strings.Contains(message, common.ScratchSpaceRequired) {
log.V(1).Info("Pod requires scratch space, terminating pod, and restarting with scratch space", "pod.Name", pod.Name)
scratchExitCode = true
anno[cc.AnnRequiresScratch] = "true"
} else {
r.recorder.Event(pvc, corev1.EventTypeWarning, ErrImportFailedPVC, pod.Status.ContainerStatuses[0].LastTerminationState.Terminated.Message)
} else if pod.Status.ContainerStatuses[0].State.Terminated.ExitCode > 0 {
log.Info("Pod termination code", "pod.Name", pod.Name, "ExitCode", pod.Status.ContainerStatuses[0].State.Terminated.ExitCode)
r.recorder.Event(pvc, corev1.EventTypeWarning, ErrImportFailedPVC, pod.Status.ContainerStatuses[0].State.Terminated.Message)
}
}

Expand Down
8 changes: 4 additions & 4 deletions pkg/controller/import-controller_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -542,8 +542,8 @@ var _ = Describe("Update PVC from POD", func() {
{
LastTerminationState: corev1.ContainerState{
Terminated: &corev1.ContainerStateTerminated{
ExitCode: common.ScratchSpaceNeededExitCode,
Message: "scratch space needed",
ExitCode: 0,
Message: common.ScratchSpaceRequired,
},
},
State: v1.ContainerState{
Expand Down Expand Up @@ -683,8 +683,8 @@ var _ = Describe("Update PVC from POD", func() {
{
LastTerminationState: corev1.ContainerState{
Terminated: &corev1.ContainerStateTerminated{
ExitCode: common.ScratchSpaceNeededExitCode,
Message: "",
ExitCode: 0,
Message: common.ScratchSpaceRequired,
},
},
},
Expand Down

0 comments on commit 64641fb

Please sign in to comment.