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

Make container startup timeout configurable #65

Merged
merged 10 commits into from
Aug 2, 2024
2 changes: 1 addition & 1 deletion cmd/validate.go
Original file line number Diff line number Diff line change
Expand Up @@ -86,5 +86,5 @@ func init() {
rootCmd.AddCommand(validateCmd)
validateCmd.PersistentFlags().String("file", "", "Path or URL of a manifest to validate against.")
validateCmd.PersistentFlags().Bool("debug", false, "Keep container running on failure for debugging.")

validateCmd.PersistentFlags().Int("startup-timeout", 10, "Maximum time (in seconds) to wait for the container to start up.")
}
2 changes: 1 addition & 1 deletion internal/container/container.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ type ContainerInfo struct {
}

type ContainerInterface interface {
Start() error
Start(timeoutSeconds int) error
Remove() error
Status() (*ContainerInfo, error)
Exec(command ...string) (string, error)
Expand Down
7 changes: 4 additions & 3 deletions internal/container/docker.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,11 @@ type DockerContainer struct {
Volumes []canaryv1.Volume
RunOptions []string
runCommand string
StartupTimeout int
}

// Start a container
func (c *DockerContainer) Start() error {
func (c *DockerContainer) Start(timeoutSeconds int) error {

commandArgs := []string{"run", "-d"}

Expand Down Expand Up @@ -78,12 +79,12 @@ func (c *DockerContainer) Start() error {
if info.State.Running {
break
}
if time.Since(startTime) > (time.Second * 10) {
if time.Since(startTime) > (time.Second * time.Duration(timeoutSeconds)) {
err := c.Remove()
if err != nil {
return err
}
return errors.New("container failed to start after 10 seconds")
return fmt.Errorf("container failed to start after %d seconds", timeoutSeconds)
}
time.Sleep(time.Second)
}
Expand Down
4 changes: 2 additions & 2 deletions internal/container/docker_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ func TestDockerContainer(t *testing.T) {
}
c := New("nginx", env, ports, volumes, nil, nil)

err := c.Start()
err := c.Start(10)

defer func() {
err := c.Remove()
Expand Down Expand Up @@ -71,7 +71,7 @@ func TestDockerContainer(t *testing.T) {
func TestDockerContainerRemoves(t *testing.T) {
c := New("nginx", nil, nil, nil, nil, nil)

err := c.Start()
err := c.Start(10)
if err != nil {
t.Errorf("Failed to start container: %s", err.Error())
return
Expand Down
29 changes: 15 additions & 14 deletions internal/validator/tui.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,19 +28,20 @@ import (
)

type model struct {
sub chan checkResult
container container.ContainerInterface
containerStarted bool
results []checkResult
allChecksPassed bool
spinner spinner.Model
progress progress.Model
debug bool
image string
validator *canaryv1.Validator
configPath string
err error
tty bool
sub chan checkResult
container container.ContainerInterface
containerStarted bool
containerStartupTimeout int
results []checkResult
allChecksPassed bool
spinner spinner.Model
progress progress.Model
debug bool
image string
validator *canaryv1.Validator
configPath string
err error
tty bool
}

func (m model) Init() tea.Cmd {
Expand Down Expand Up @@ -133,7 +134,7 @@ func handleConfigLoaded(m model, msg configLoaded) (model, tea.Cmd) {
if !m.tty {
commands = append(commands, tea.Printf("Starting container"))
}
commands = append(commands, startContainer(m.image, m.validator))
commands = append(commands, startContainer(m.image, m.validator, m.containerStartupTimeout))
return m, tea.Batch(commands...)
}

Expand Down
27 changes: 16 additions & 11 deletions internal/validator/validator.go
Original file line number Diff line number Diff line change
Expand Up @@ -69,16 +69,21 @@ func Validate(image string, configPath string, cmd *cobra.Command, debug bool) (
tty = bufio.NewReader(os.Stdin)
isTty = false
}
startupTimeout, err := cmd.Flags().GetInt("startup-timeout")
if err != nil {
return false, err
}
m := model{
sub: make(chan checkResult),
configPath: configPath,
containerStarted: false,
spinner: spinner.New(),
progress: progress.New(progress.WithSolidFill("#f2e63a")),
allChecksPassed: true,
debug: debug,
image: image,
tty: isTty,
sub: make(chan checkResult),
configPath: configPath,
containerStarted: false,
containerStartupTimeout: startupTimeout,
spinner: spinner.New(),
progress: progress.New(progress.WithSolidFill("#f2e63a")),
allChecksPassed: true,
debug: debug,
image: image,
tty: isTty,
}
p := tea.NewProgram(m, tea.WithInput(tty), tea.WithOutput(cmd.OutOrStderr()))
out, err := p.Run()
Expand Down Expand Up @@ -123,10 +128,10 @@ func loadConfig(filePath string) tea.Cmd {
}
}

func startContainer(image string, validator *canaryv1.Validator) tea.Cmd {
func startContainer(image string, validator *canaryv1.Validator, startupTimeout int) tea.Cmd {
return func() tea.Msg {
container := container.New(image, validator.Env, validator.Ports, validator.Volumes, validator.Command, validator.DockerRunOptions)
err := container.Start()
err := container.Start(startupTimeout)
if err != nil {
return containerFailed{Error: err}
}
Expand Down
Loading