Skip to content

Commit

Permalink
fix: Podman flag for healthcheck start interval
Browse files Browse the repository at this point in the history
  • Loading branch information
aksiksi committed Sep 28, 2024
1 parent fed9565 commit 82045f5
Show file tree
Hide file tree
Showing 17 changed files with 59 additions and 40 deletions.
75 changes: 43 additions & 32 deletions compose.go
Original file line number Diff line number Diff line change
Expand Up @@ -252,6 +252,47 @@ func healthCheckCommandToString(cmd []string) (string, error) {
panic("unreachable")
}

// Health check.
// https://docs.docker.com/compose/compose-file/05-services/#healthcheck
func parseHealthCheck(c *NixContainer, service types.ServiceConfig, runtime ContainerRuntime) error {
if healthCheck := service.HealthCheck; healthCheck != nil {
// Figure out if the Dockerfile health check is disabled.
disable := healthCheck.Disable || (len(healthCheck.Test) > 0 && healthCheck.Test[0] == "NONE")
if disable {
c.ExtraOptions = append(c.ExtraOptions, "--no-healthcheck")
} else {
if len(healthCheck.Test) > 0 {
cmd, err := healthCheckCommandToString(healthCheck.Test)
if err != nil {
return fmt.Errorf("failed to convert healthcheck command: %w", err)
}
c.ExtraOptions = append(c.ExtraOptions, fmt.Sprintf("--health-cmd=%s", cmd))
}
if timeout := healthCheck.Timeout; timeout != nil {
c.ExtraOptions = append(c.ExtraOptions, fmt.Sprintf("--health-timeout=%v", *timeout))
}
if interval := healthCheck.Interval; interval != nil {
c.ExtraOptions = append(c.ExtraOptions, fmt.Sprintf("--health-interval=%v", *interval))
}
if retries := healthCheck.Retries; retries != nil {
c.ExtraOptions = append(c.ExtraOptions, fmt.Sprintf("--health-retries=%d", *retries))
}
if startPeriod := healthCheck.StartPeriod; startPeriod != nil {
c.ExtraOptions = append(c.ExtraOptions, fmt.Sprintf("--health-start-period=%v", *startPeriod))
}
if startInterval := healthCheck.StartInterval; startInterval != nil {
flagName := "health-start-interval"
if runtime == ContainerRuntimePodman {
// https://docs.podman.io/en/latest/markdown/podman-run.1.html#health-startup-interval-interval
flagName = "health-startup-interval"
}
c.ExtraOptions = append(c.ExtraOptions, fmt.Sprintf("--%s=%v", flagName, *startInterval))
}
}
}
return nil
}

func (g *Generator) buildNixContainer(service types.ServiceConfig, networkMap map[string]*NixNetwork, volumeMap map[string]*NixVolume) (*NixContainer, error) {
name := g.serviceToContainerName[service.Name]

Expand Down Expand Up @@ -572,38 +613,8 @@ func (g *Generator) buildNixContainer(service types.ServiceConfig, networkMap ma
c.ExtraOptions = append(c.ExtraOptions, mapToRepeatedKeyValFlag("--log-opt", logging.Options)...)
}

// Health check.
// https://docs.docker.com/compose/compose-file/05-services/#healthcheck
if healthCheck := service.HealthCheck; healthCheck != nil {
// Figure out if the Dockerfile health check is disabled.
disable := healthCheck.Disable || (len(healthCheck.Test) > 0 && healthCheck.Test[0] == "NONE")
if disable {
c.ExtraOptions = append(c.ExtraOptions, "--no-healthcheck")
} else {
if len(healthCheck.Test) > 0 {
cmd, err := healthCheckCommandToString(healthCheck.Test)
if err != nil {
return nil, fmt.Errorf("failed to convert healthcheck command: %w", err)
}
c.ExtraOptions = append(c.ExtraOptions, fmt.Sprintf("--health-cmd=%s", cmd))
}
if timeout := healthCheck.Timeout; timeout != nil {
c.ExtraOptions = append(c.ExtraOptions, fmt.Sprintf("--health-timeout=%v", *timeout))
}
if interval := healthCheck.Interval; interval != nil {
c.ExtraOptions = append(c.ExtraOptions, fmt.Sprintf("--health-interval=%v", *interval))
}
if retries := healthCheck.Retries; retries != nil {
c.ExtraOptions = append(c.ExtraOptions, fmt.Sprintf("--health-retries=%d", *retries))
}
if startPeriod := healthCheck.StartPeriod; startPeriod != nil {
c.ExtraOptions = append(c.ExtraOptions, fmt.Sprintf("--health-start-period=%v", *startPeriod))
}
// Not supported by Docker.
if startInterval := healthCheck.StartInterval; startInterval != nil && g.Runtime == ContainerRuntimePodman {
c.ExtraOptions = append(c.ExtraOptions, fmt.Sprintf("--health-start-interval=%v", *startInterval))
}
}
if err := parseHealthCheck(c, service, g.Runtime); err != nil {
return nil, err
}

// Deploy resources configuration.
Expand Down
1 change: 1 addition & 0 deletions testdata/TestBasic.docker.nix
Original file line number Diff line number Diff line change
Expand Up @@ -148,6 +148,7 @@
"--health-cmd=[\"curl\", \"-f\", \"http://localhost\"]"
"--health-interval=1m30s"
"--health-retries=3"
"--health-start-interval=5s"
"--health-start-period=40s"
"--health-timeout=10s"
"--log-opt=compress=true"
Expand Down
2 changes: 1 addition & 1 deletion testdata/TestBasic.podman.nix
Original file line number Diff line number Diff line change
Expand Up @@ -148,8 +148,8 @@
"--health-cmd=[\"curl\", \"-f\", \"http://localhost\"]"
"--health-interval=1m30s"
"--health-retries=3"
"--health-start-interval=5s"
"--health-start-period=40s"
"--health-startup-interval=5s"
"--health-timeout=10s"
"--log-opt=compress=true"
"--log-opt=max-file=3"
Expand Down
1 change: 1 addition & 0 deletions testdata/TestBasicAutoFormat.docker.nix
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,7 @@
"--health-cmd=[\"curl\", \"-f\", \"http://localhost\"]"
"--health-interval=1m30s"
"--health-retries=3"
"--health-start-interval=5s"
"--health-start-period=40s"
"--health-timeout=10s"
"--log-opt=compress=true"
Expand Down
2 changes: 1 addition & 1 deletion testdata/TestBasicAutoFormat.podman.nix
Original file line number Diff line number Diff line change
Expand Up @@ -138,8 +138,8 @@
"--health-cmd=[\"curl\", \"-f\", \"http://localhost\"]"
"--health-interval=1m30s"
"--health-retries=3"
"--health-start-interval=5s"
"--health-start-period=40s"
"--health-startup-interval=5s"
"--health-timeout=10s"
"--log-opt=compress=true"
"--log-opt=max-file=3"
Expand Down
1 change: 1 addition & 0 deletions testdata/TestNoWriteNixSetup.docker.nix
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,7 @@
"--health-cmd=[\"curl\", \"-f\", \"http://localhost\"]"
"--health-interval=1m30s"
"--health-retries=3"
"--health-start-interval=5s"
"--health-start-period=40s"
"--health-timeout=10s"
"--log-opt=compress=true"
Expand Down
2 changes: 1 addition & 1 deletion testdata/TestNoWriteNixSetup.podman.nix
Original file line number Diff line number Diff line change
Expand Up @@ -128,8 +128,8 @@
"--health-cmd=[\"curl\", \"-f\", \"http://localhost\"]"
"--health-interval=1m30s"
"--health-retries=3"
"--health-start-interval=5s"
"--health-start-period=40s"
"--health-startup-interval=5s"
"--health-timeout=10s"
"--log-opt=compress=true"
"--log-opt=max-file=3"
Expand Down
1 change: 1 addition & 0 deletions testdata/TestOverrideSystemdStopTimeout.docker.nix
Original file line number Diff line number Diff line change
Expand Up @@ -141,6 +141,7 @@
"--health-cmd=[\"curl\", \"-f\", \"http://localhost\"]"
"--health-interval=1m30s"
"--health-retries=3"
"--health-start-interval=5s"
"--health-start-period=40s"
"--health-timeout=10s"
"--log-opt=compress=true"
Expand Down
2 changes: 1 addition & 1 deletion testdata/TestOverrideSystemdStopTimeout.podman.nix
Original file line number Diff line number Diff line change
Expand Up @@ -141,8 +141,8 @@
"--health-cmd=[\"curl\", \"-f\", \"http://localhost\"]"
"--health-interval=1m30s"
"--health-retries=3"
"--health-start-interval=5s"
"--health-start-period=40s"
"--health-startup-interval=5s"
"--health-timeout=10s"
"--log-opt=compress=true"
"--log-opt=max-file=3"
Expand Down
1 change: 1 addition & 0 deletions testdata/TestProject.docker.nix
Original file line number Diff line number Diff line change
Expand Up @@ -139,6 +139,7 @@
"--health-cmd=[\"curl\", \"-f\", \"http://localhost\"]"
"--health-interval=1m30s"
"--health-retries=3"
"--health-start-interval=5s"
"--health-start-period=40s"
"--health-timeout=10s"
"--log-opt=compress=true"
Expand Down
2 changes: 1 addition & 1 deletion testdata/TestProject.podman.nix
Original file line number Diff line number Diff line change
Expand Up @@ -139,8 +139,8 @@
"--health-cmd=[\"curl\", \"-f\", \"http://localhost\"]"
"--health-interval=1m30s"
"--health-retries=3"
"--health-start-interval=5s"
"--health-start-period=40s"
"--health-startup-interval=5s"
"--health-timeout=10s"
"--log-opt=compress=true"
"--log-opt=max-file=3"
Expand Down
1 change: 1 addition & 0 deletions testdata/TestRemoveVolumes.docker.nix
Original file line number Diff line number Diff line change
Expand Up @@ -139,6 +139,7 @@
"--health-cmd=[\"curl\", \"-f\", \"http://localhost\"]"
"--health-interval=1m30s"
"--health-retries=3"
"--health-start-interval=5s"
"--health-start-period=40s"
"--health-timeout=10s"
"--log-opt=compress=true"
Expand Down
2 changes: 1 addition & 1 deletion testdata/TestRemoveVolumes.podman.nix
Original file line number Diff line number Diff line change
Expand Up @@ -139,8 +139,8 @@
"--health-cmd=[\"curl\", \"-f\", \"http://localhost\"]"
"--health-interval=1m30s"
"--health-retries=3"
"--health-start-interval=5s"
"--health-start-period=40s"
"--health-startup-interval=5s"
"--health-timeout=10s"
"--log-opt=compress=true"
"--log-opt=max-file=3"
Expand Down
1 change: 1 addition & 0 deletions testdata/TestSystemdMount.docker.nix
Original file line number Diff line number Diff line change
Expand Up @@ -145,6 +145,7 @@
"--health-cmd=[\"curl\", \"-f\", \"http://localhost\"]"
"--health-interval=1m30s"
"--health-retries=3"
"--health-start-interval=5s"
"--health-start-period=40s"
"--health-timeout=10s"
"--log-opt=compress=true"
Expand Down
2 changes: 1 addition & 1 deletion testdata/TestSystemdMount.podman.nix
Original file line number Diff line number Diff line change
Expand Up @@ -145,8 +145,8 @@
"--health-cmd=[\"curl\", \"-f\", \"http://localhost\"]"
"--health-interval=1m30s"
"--health-retries=3"
"--health-start-interval=5s"
"--health-start-period=40s"
"--health-startup-interval=5s"
"--health-timeout=10s"
"--log-opt=compress=true"
"--log-opt=max-file=3"
Expand Down
1 change: 1 addition & 0 deletions testdata/TestUpheldBy.docker.nix
Original file line number Diff line number Diff line change
Expand Up @@ -142,6 +142,7 @@
"--health-cmd=[\"curl\", \"-f\", \"http://localhost\"]"
"--health-interval=1m30s"
"--health-retries=3"
"--health-start-interval=5s"
"--health-start-period=40s"
"--health-timeout=10s"
"--log-opt=compress=true"
Expand Down
2 changes: 1 addition & 1 deletion testdata/TestUpheldBy.podman.nix
Original file line number Diff line number Diff line change
Expand Up @@ -142,8 +142,8 @@
"--health-cmd=[\"curl\", \"-f\", \"http://localhost\"]"
"--health-interval=1m30s"
"--health-retries=3"
"--health-start-interval=5s"
"--health-start-period=40s"
"--health-startup-interval=5s"
"--health-timeout=10s"
"--log-opt=compress=true"
"--log-opt=max-file=3"
Expand Down

0 comments on commit 82045f5

Please sign in to comment.