Skip to content

Commit

Permalink
Fix nil healthcheck error
Browse files Browse the repository at this point in the history
  • Loading branch information
turtletowerz committed Sep 13, 2024
1 parent c8ea044 commit 807f3c4
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 6 deletions.
6 changes: 0 additions & 6 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ import (
"log"
"os"
"strings"
"time"

"github.com/docker/cli/opts"
"github.com/docker/docker/api/types"
Expand Down Expand Up @@ -72,11 +71,6 @@ func parseFromJSON(cli *client.Client, ct *types.ContainerJSON) ([]string, error
opt[string]{ct.HostConfig.ContainerIDFile, "", "--cidfile "},

optFunc[*container.HealthConfig]{ct.Config.Healthcheck, handleHealthcheck},
opt[time.Duration]{ct.Config.Healthcheck.Interval, 0, "--health-interval="},
opt[int]{ct.Config.Healthcheck.Retries, 0, "--health-retries="},
opt[time.Duration]{ct.Config.Healthcheck.Timeout, 0, "--health-timeout="},
opt[time.Duration]{ct.Config.Healthcheck.StartInterval, 0, "--health-start-interval="},
opt[time.Duration]{ct.Config.Healthcheck.StartPeriod, 0, "--health-start-period="},

// Less common options that can go at the end

Expand Down
23 changes: 23 additions & 0 deletions parse.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"slices"
"strconv"
"strings"
"time"

"github.com/docker/docker/api/types"
"github.com/docker/docker/api/types/container"
Expand Down Expand Up @@ -184,13 +185,32 @@ func handleLabels(l *labels) (ret []string) {
}

func handleHealthcheck(health *container.HealthConfig) (ret []string) {
if health == nil {
return
}

if len(health.Test) > 0 {
if health.Test[0] == "NONE" {
ret = append(ret, "--no-healthcheck")
return
}
ret = append(ret, "--health-cmd="+strconv.Quote(strings.Join(health.Test[1:], " "))) // TODO: This is probably not right
}

var opts = map[string]time.Duration{
"--health-interval=": health.Interval,
"--health-retries=": time.Duration(health.Retries),
"--health-timeout=": health.Timeout,
"--health-start-interval=": health.StartInterval,
"--health-start-period=": health.StartPeriod,
}

for name, val := range opts {
if val != 0 {
ret = append(ret, name+val.String())
}
}

return
}

Expand All @@ -201,6 +221,9 @@ func handlePorts(ctdata *types.ContainerJSON) (ret []string) {
}

ports := ctdata.HostConfig.PortBindings
if ports == nil {
return
}

for ctport, bindings := range ports {
protocol := ""
Expand Down

0 comments on commit 807f3c4

Please sign in to comment.