fix(lifecycle): cleanup lifecycle

- removes unwieldy SkipUpdate return value in favor of errors.Is
- generalizes the code for all four phases
- allows timeout to be defined for all phases
- enables explicit unit in timeout label values (in addition to implicit minutes)
This commit is contained in:
nils måsén 2024-01-05 19:12:11 +01:00
parent 76f9cea516
commit 023c1a7d93
10 changed files with 160 additions and 179 deletions

View file

@ -5,6 +5,7 @@ import (
"fmt"
"time"
c "github.com/containrrr/watchtower/pkg/container"
t "github.com/containrrr/watchtower/pkg/types"
)
@ -72,16 +73,16 @@ func (client MockClient) GetContainer(_ t.ContainerID) (t.Container, error) {
}
// ExecuteCommand is a mock method
func (client MockClient) ExecuteCommand(_ t.ContainerID, command string, _ int) (SkipUpdate bool, err error) {
func (client MockClient) ExecuteCommand(_ t.ContainerID, command string, _ time.Duration) error {
switch command {
case "/PreUpdateReturn0.sh":
return false, nil
return nil
case "/PreUpdateReturn1.sh":
return false, fmt.Errorf("command exited with code 1")
return fmt.Errorf("command exited with code 1")
case "/PreUpdateReturn75.sh":
return true, nil
return c.ErrorLifecycleSkip
default:
return false, nil
return nil
}
}

15
internal/util/time.go Normal file
View file

@ -0,0 +1,15 @@
package util
import (
"strconv"
"time"
)
// ParseDuration parses the input string as a duration, treating a plain number as implicitly using the specified unit
func ParseDuration(input string, unitlessUnit time.Duration) (time.Duration, error) {
if unitless, err := strconv.Atoi(input); err == nil {
return unitlessUnit * time.Duration(unitless), nil
}
return time.ParseDuration(input)
}