Enhance hooks tracker by adding a returned error to record function

Signed-off-by: allenxu404 <qix2@vmware.com>
This commit is contained in:
allenxu404
2023-11-28 18:17:29 +08:00
parent 85482aefaf
commit 6051b3cbe0
6 changed files with 97 additions and 24 deletions

View File

@@ -233,14 +233,19 @@ func (h *DefaultItemHookHandler) HandleHooks(
},
)
hookTracker.Record(namespace, name, hookFromAnnotations.Container, HookSourceAnnotation, "", phase, false)
if err := h.PodCommandExecutor.ExecutePodCommand(hookLog, obj.UnstructuredContent(), namespace, name, "<from-annotation>", hookFromAnnotations); err != nil {
hookLog.WithError(err).Error("Error executing hook")
hookTracker.Record(namespace, name, hookFromAnnotations.Container, HookSourceAnnotation, "", phase, true)
hookFailed := false
var errExec error
if errExec = h.PodCommandExecutor.ExecutePodCommand(hookLog, obj.UnstructuredContent(), namespace, name, "<from-annotation>", hookFromAnnotations); errExec != nil {
hookLog.WithError(errExec).Error("Error executing hook")
hookFailed = true
}
errTracker := hookTracker.Record(namespace, name, hookFromAnnotations.Container, HookSourceAnnotation, "", phase, hookFailed)
if errTracker != nil {
hookLog.WithError(errTracker).Warn("Error recording the hook in hook tracker")
}
if hookFromAnnotations.OnError == velerov1api.HookErrorModeFail {
return err
}
if errExec != nil && hookFromAnnotations.OnError == velerov1api.HookErrorModeFail {
return errExec
}
return nil
@@ -277,15 +282,19 @@ func (h *DefaultItemHookHandler) HandleHooks(
},
)
hookTracker.Record(namespace, name, hook.Exec.Container, HookSourceSpec, resourceHook.Name, phase, false)
hookFailed := false
err := h.PodCommandExecutor.ExecutePodCommand(hookLog, obj.UnstructuredContent(), namespace, name, resourceHook.Name, hook.Exec)
if err != nil {
hookLog.WithError(err).Error("Error executing hook")
hookTracker.Record(namespace, name, hook.Exec.Container, HookSourceSpec, resourceHook.Name, phase, true)
hookFailed = true
if hook.Exec.OnError == velerov1api.HookErrorModeFail {
modeFailError = err
}
}
errTracker := hookTracker.Record(namespace, name, hook.Exec.Container, HookSourceSpec, resourceHook.Name, phase, hookFailed)
if errTracker != nil {
hookLog.WithError(errTracker).Warn("Error recording the hook in hook tracker")
}
}
}
}