Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Log when a hook timeout duration can't be parsed #2610

Merged
merged 1 commit into from
Jun 5, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
1 change: 1 addition & 0 deletions changelogs/unreleased/2610-nrb
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
When a timeout string can't be parsed, log the error as a warning instead of silently consuming the error.
9 changes: 5 additions & 4 deletions pkg/backup/item_hook_handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -83,10 +83,10 @@ func (h *defaultItemHookHandler) handleHooks(
name := metadata.GetName()

// If the pod has the hook specified via annotations, that takes priority.
hookFromAnnotations := getPodExecHookFromAnnotations(metadata.GetAnnotations(), phase)
hookFromAnnotations := getPodExecHookFromAnnotations(metadata.GetAnnotations(), phase, log)
if phase == hookPhasePre && hookFromAnnotations == nil {
// See if the pod has the legacy hook annotation keys (i.e. without a phase specified)
hookFromAnnotations = getPodExecHookFromAnnotations(metadata.GetAnnotations(), "")
hookFromAnnotations = getPodExecHookFromAnnotations(metadata.GetAnnotations(), "", log)
}
if hookFromAnnotations != nil {
hookLog := log.WithFields(
Expand Down Expand Up @@ -164,7 +164,8 @@ func getHookAnnotation(annotations map[string]string, key string, phase hookPhas

// getPodExecHookFromAnnotations returns an ExecHook based on the annotations, as long as the
// 'command' annotation is present. If it is absent, this returns nil.
func getPodExecHookFromAnnotations(annotations map[string]string, phase hookPhase) *api.ExecHook {
// If there is an error in parsing a supplied timeout, it is logged.
func getPodExecHookFromAnnotations(annotations map[string]string, phase hookPhase, log logrus.FieldLogger) *api.ExecHook {
commandValue := getHookAnnotation(annotations, podBackupHookCommandAnnotationKey, phase)
if commandValue == "" {
return nil
Expand Down Expand Up @@ -192,7 +193,7 @@ func getPodExecHookFromAnnotations(annotations map[string]string, phase hookPhas
if temp, err := time.ParseDuration(timeoutString); err == nil {
timeout = temp
} else {
// TODO: log error that we couldn't parse duration
log.Warn(errors.Wrapf(err, "Unable to parse provided timeout %s, using default", timeoutString))
}
}

Expand Down
5 changes: 3 additions & 2 deletions pkg/backup/item_hook_handler_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -575,7 +575,7 @@ func TestGetPodExecHookFromAnnotations(t *testing.T) {
},
},
{
name: "invalid timeout is ignored",
name: "invalid timeout is logged",
annotations: map[string]string{
phasedKey(phase, podBackupHookCommandAnnotationKey): "/usr/bin/foo",
phasedKey(phase, podBackupHookTimeoutAnnotationKey): "invalid",
Expand All @@ -599,7 +599,8 @@ func TestGetPodExecHookFromAnnotations(t *testing.T) {

for _, test := range tests {
t.Run(fmt.Sprintf("%s (phase=%q)", test.name, phase), func(t *testing.T) {
hook := getPodExecHookFromAnnotations(test.annotations, phase)
l := velerotest.NewLogger()
hook := getPodExecHookFromAnnotations(test.annotations, phase, l)
assert.Equal(t, test.expectedHook, hook)
})
}
Expand Down