Add support for healthcheck in composefile v3
`docker stack deploy` now supports a composefile v3 format that have a healthcheck. Signed-off-by: Vincent Demeester <vincent@sbr.pm>
This commit is contained in:
parent
b0903c9db0
commit
3bd64de7a9
2 changed files with 50 additions and 0 deletions
|
@ -6,6 +6,7 @@ import (
|
|||
"os"
|
||||
"sort"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"github.com/spf13/cobra"
|
||||
"golang.org/x/net/context"
|
||||
|
@ -13,6 +14,7 @@ import (
|
|||
"github.com/aanand/compose-file/loader"
|
||||
composetypes "github.com/aanand/compose-file/types"
|
||||
"github.com/docker/docker/api/types"
|
||||
"github.com/docker/docker/api/types/container"
|
||||
"github.com/docker/docker/api/types/mount"
|
||||
networktypes "github.com/docker/docker/api/types/network"
|
||||
"github.com/docker/docker/api/types/swarm"
|
||||
|
@ -487,6 +489,11 @@ func convertService(
|
|||
return swarm.ServiceSpec{}, err
|
||||
}
|
||||
|
||||
healthcheck, err := convertHealthcheck(service.HealthCheck)
|
||||
if err != nil {
|
||||
return swarm.ServiceSpec{}, err
|
||||
}
|
||||
|
||||
serviceSpec := swarm.ServiceSpec{
|
||||
Annotations: swarm.Annotations{
|
||||
Name: name,
|
||||
|
@ -499,6 +506,7 @@ func convertService(
|
|||
Args: service.Command,
|
||||
Hostname: service.Hostname,
|
||||
Hosts: convertExtraHosts(service.ExtraHosts),
|
||||
Healthcheck: healthcheck,
|
||||
Env: convertEnvironment(service.Environment),
|
||||
Labels: getStackLabels(namespace.name, service.Labels),
|
||||
Dir: service.WorkingDir,
|
||||
|
@ -531,6 +539,47 @@ func convertExtraHosts(extraHosts map[string]string) []string {
|
|||
return hosts
|
||||
}
|
||||
|
||||
func convertHealthcheck(healthcheck *composetypes.HealthCheckConfig) (*container.HealthConfig, error) {
|
||||
if healthcheck == nil {
|
||||
return nil, nil
|
||||
}
|
||||
var (
|
||||
err error
|
||||
timeout, interval time.Duration
|
||||
retries int
|
||||
)
|
||||
if healthcheck.Disable {
|
||||
if len(healthcheck.Test) != 0 {
|
||||
return nil, fmt.Errorf("command and disable key can't be set at the same time")
|
||||
}
|
||||
return &container.HealthConfig{
|
||||
Test: []string{"NONE"},
|
||||
}, nil
|
||||
|
||||
}
|
||||
if healthcheck.Timeout != "" {
|
||||
timeout, err = time.ParseDuration(healthcheck.Timeout)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
}
|
||||
if healthcheck.Interval != "" {
|
||||
interval, err = time.ParseDuration(healthcheck.Interval)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
}
|
||||
if healthcheck.Retries != nil {
|
||||
retries = int(*healthcheck.Retries)
|
||||
}
|
||||
return &container.HealthConfig{
|
||||
Test: healthcheck.Test,
|
||||
Timeout: timeout,
|
||||
Interval: interval,
|
||||
Retries: retries,
|
||||
}, nil
|
||||
}
|
||||
|
||||
func convertRestartPolicy(restart string, source *composetypes.RestartPolicy) (*swarm.RestartPolicy, error) {
|
||||
// TODO: log if restart is being ignored
|
||||
if source == nil {
|
||||
|
|
1
vendor/github.com/aanand/compose-file/types/types.go
generated
vendored
1
vendor/github.com/aanand/compose-file/types/types.go
generated
vendored
|
@ -128,6 +128,7 @@ type HealthCheckConfig struct {
|
|||
Timeout string
|
||||
Interval string
|
||||
Retries *uint64
|
||||
Disable bool
|
||||
}
|
||||
|
||||
type UpdateConfig struct {
|
||||
|
|
Loading…
Reference in a new issue