瀏覽代碼

Merge pull request #28294 from vdemeester/compose-swarm-healthcheck

Add support for healthcheck in compose to swarm
Vincent Demeester 8 年之前
父節點
當前提交
3739c3bed3

+ 54 - 1
cli/command/stack/deploy.go

@@ -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"
@@ -492,6 +494,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,
@@ -504,6 +511,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,
@@ -536,6 +544,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 {
@@ -571,8 +620,12 @@ func convertUpdateConfig(source *composetypes.UpdateConfig) *swarm.UpdateConfig
 	if source == nil {
 		return nil
 	}
+	parallel := uint64(1)
+	if source.Parallelism != nil {
+		parallel = *source.Parallelism
+	}
 	return &swarm.UpdateConfig{
-		Parallelism:     source.Parallelism,
+		Parallelism:     parallel,
 		Delay:           source.Delay,
 		FailureAction:   source.FailureAction,
 		Monitor:         source.Monitor,

+ 1 - 1
vendor.conf

@@ -132,7 +132,7 @@ github.com/flynn-archive/go-shlex 3f9db97f856818214da2e1057f8ad84803971cff
 github.com/docker/go-metrics 86138d05f285fd9737a99bee2d9be30866b59d72
 
 # composefile
-github.com/aanand/compose-file 8cff34df885ef07824138236bc4d27d359888b17
+github.com/aanand/compose-file a70cb6ea5c403c383b5117dcd1dea78880a47824
 github.com/mitchellh/mapstructure f3009df150dadf309fdee4a54ed65c124afad715
 github.com/xeipuuv/gojsonpointer e0fe6f68307607d540ed8eac07a342c33fa1b54a
 github.com/xeipuuv/gojsonreference e02fc20de94c78484cd5ffb007f8af96be030a45

+ 10 - 1
vendor/github.com/aanand/compose-file/loader/loader.go

@@ -134,7 +134,7 @@ func GetUnsupportedProperties(configDetails types.ConfigDetails) []string {
 
 func sortedKeys(set map[string]bool) []string {
 	var keys []string
-	for key, _ := range set {
+	for key := range set {
 		keys = append(keys, key)
 	}
 	sort.Strings(keys)
@@ -467,6 +467,8 @@ func convertField(
 	switch fieldTag {
 	case "":
 		return data, nil
+	case "healthcheck":
+		return loadHealthcheck(data)
 	case "list_or_dict_equals":
 		return loadMappingOrList(data, "="), nil
 	case "list_or_dict_colon":
@@ -571,6 +573,13 @@ func loadShellCommand(value interface{}) (interface{}, error) {
 	return value, nil
 }
 
+func loadHealthcheck(value interface{}) (interface{}, error) {
+	if str, ok := value.(string); ok {
+		return append([]string{"CMD-SHELL"}, str), nil
+	}
+	return value, nil
+}
+
 func loadSize(value interface{}) (int64, error) {
 	switch value := value.(type) {
 	case int:

File diff suppressed because it is too large
+ 1 - 1
vendor/github.com/aanand/compose-file/schema/bindata.go


+ 1 - 1
vendor/github.com/aanand/compose-file/schema/schema.go

@@ -32,7 +32,7 @@ func init() {
 
 // Validate uses the jsonschema to validate the configuration
 func Validate(config map[string]interface{}) error {
-	schemaData, err := Asset("data/config_schema_v3.json")
+	schemaData, err := Asset("data/config_schema_v3.0.json")
 	if err != nil {
 		return err
 	}

+ 3 - 2
vendor/github.com/aanand/compose-file/types/types.go

@@ -124,14 +124,15 @@ type DeployConfig struct {
 }
 
 type HealthCheckConfig struct {
-	Command  []string `compose:"shell_command"`
+	Test     []string `compose:"healthcheck"`
 	Timeout  string
 	Interval string
 	Retries  *uint64
+	Disable  bool
 }
 
 type UpdateConfig struct {
-	Parallelism     uint64
+	Parallelism     *uint64
 	Delay           time.Duration
 	FailureAction   string `mapstructure:"failure_action"`
 	Monitor         time.Duration

Some files were not shown because too many files changed in this diff