From 4c68049faf520041b44096f8f0f3bf5c96d41d50 Mon Sep 17 00:00:00 2001 From: Nicolas De Loof Date: Mon, 28 Aug 2023 09:01:34 +0200 Subject: [PATCH] add support for start_interval Signed-off-by: Nicolas De Loof --- pkg/compose/compose.go | 19 +++++++++++++++++++ pkg/compose/convert.go | 30 ++++++++++++++++++++++-------- pkg/compose/create.go | 12 +++++++----- 3 files changed, 48 insertions(+), 13 deletions(-) diff --git a/pkg/compose/compose.go b/pkg/compose/compose.go index 33f93397a03..3e0c3bac1ff 100644 --- a/pkg/compose/compose.go +++ b/pkg/compose/compose.go @@ -300,3 +300,22 @@ func (s *composeService) isSWarmEnabled(ctx context.Context) (bool, error) { return swarmEnabled.val, swarmEnabled.err } + +var runtimeVersion = struct { + once sync.Once + val string + err error +}{} +} + +func (s *composeService) RuntimeVersion(ctx context.Context) (string, error) { + runtimeVersion.once.Do(func() { + version, err := s.dockerCli.Client().ServerVersion(ctx) + if err != nil { + runtimeVersion.err = err + } + runtimeVersion.val = version.APIVersion + }) + return runtimeVersion.val, runtimeVersion.err + +} diff --git a/pkg/compose/convert.go b/pkg/compose/convert.go index 6f092ee2d51..0ef88bc2ffb 100644 --- a/pkg/compose/convert.go +++ b/pkg/compose/convert.go @@ -17,11 +17,13 @@ package compose import ( + "context" "fmt" "time" compose "github.com/compose-spec/compose-go/types" "github.com/docker/docker/api/types/container" + "github.com/docker/docker/api/types/versions" ) // ToMobyEnv convert into []string @@ -38,9 +40,9 @@ func ToMobyEnv(environment compose.MappingWithEquals) []string { } // ToMobyHealthCheck convert into container.HealthConfig -func ToMobyHealthCheck(check *compose.HealthCheckConfig) *container.HealthConfig { +func (s *composeService) ToMobyHealthCheck(ctx context.Context, check *compose.HealthCheckConfig) (*container.HealthConfig, error) { if check == nil { - return nil + return nil, nil } var ( interval time.Duration @@ -64,13 +66,25 @@ func ToMobyHealthCheck(check *compose.HealthCheckConfig) *container.HealthConfig if check.Disable { test = []string{"NONE"} } - return &container.HealthConfig{ - Test: test, - Interval: interval, - Timeout: timeout, - StartPeriod: period, - Retries: retries, + var startInterval time.Duration + if check.StartInterval != nil { + version, err := s.RuntimeVersion(ctx) + if err != nil { + return nil, err + } + if versions.LessThan(version, "1.25") { + return nil, fmt.Errorf("Can't set healthcheck.start_interval as feature require Moby 1.25 or later - ignored") + } + startInterval = time.Duration(*check.StartInterval) } + return &container.HealthConfig{ + Test: test, + Interval: interval, + Timeout: timeout, + StartPeriod: period, + StartInterval: startInterval, + Retries: retries, + }, nil } // ToSeconds convert into seconds diff --git a/pkg/compose/create.go b/pkg/compose/create.go index 4d35bb70487..9e17e80b422 100644 --- a/pkg/compose/create.go +++ b/pkg/compose/create.go @@ -212,7 +212,11 @@ func (s *composeService) getCreateConfigs(ctx context.Context, proxyConfig := types.MappingWithEquals(s.configFile().ParseProxyConfig(s.apiClient().DaemonHost(), nil)) env := proxyConfig.OverrideBy(service.Environment) - containerConfig := container.Config{ + healthcheck, err := s.ToMobyHealthCheck(ctx, service.HealthCheck) + if err != nil { + return createConfigs{}, err + } + var containerConfig = container.Config{ Hostname: service.Hostname, Domainname: service.DomainName, User: service.User, @@ -232,11 +236,9 @@ func (s *composeService) getCreateConfigs(ctx context.Context, Labels: labels, StopSignal: service.StopSignal, Env: ToMobyEnv(env), - Healthcheck: ToMobyHealthCheck(service.HealthCheck), + Healthcheck: healthcheck, StopTimeout: ToSeconds(service.StopGracePeriod), - } - - // VOLUMES/MOUNTS/FILESYSTEMS + } // VOLUMES/MOUNTS/FILESYSTEMS tmpfs := map[string]string{} for _, t := range service.Tmpfs { if arr := strings.SplitN(t, ":", 2); len(arr) > 1 {