From 9c672194c481ac6d4fe26935a5312e1f3173ab19 Mon Sep 17 00:00:00 2001 From: CMGS Date: Fri, 19 Feb 2021 14:27:08 +0800 Subject: [PATCH] redefine restart and keep backward compatible --- engine/docker/container.go | 16 +++++++--------- engine/systemd/unit.go | 13 ++++++++----- engine/types/virtualization.go | 2 +- rpc/gen/core.pb.go | 8 ++++---- rpc/gen/core.proto | 2 +- rpc/transform.go | 2 +- types/specs.go | 2 +- 7 files changed, 23 insertions(+), 22 deletions(-) diff --git a/engine/docker/container.go b/engine/docker/container.go index c5db23287..5acd24e58 100644 --- a/engine/docker/container.go +++ b/engine/docker/container.go @@ -30,10 +30,9 @@ import ( ) const ( - minMemory = units.MiB * 4 - maxMemory = math.MaxInt64 - restartAlways = "always" - root = "root" + minMemory = units.MiB * 4 + maxMemory = math.MaxInt64 + root = "root" ) type rawArgs struct { @@ -57,11 +56,10 @@ func (e *Engine) VirtualizationCreate(ctx context.Context, opts *enginetypes.Vir restartPolicy := "" restartRetry := 0 - if opts.Restart == -1 || opts.Restart > 0 { - restartPolicy = restartAlways - } - if opts.Restart > 0 { - restartRetry = opts.Restart + restartStr := strings.Split(opts.Restart, ":") + restartPolicy = restartStr[0] + if r, err := strconv.Atoi(restartStr[len(restartStr)-1]); err != nil { + restartRetry = r } // no longer use opts.Network as networkmode // always get network name from networks diff --git a/engine/systemd/unit.go b/engine/systemd/unit.go index 33507a81d..c26fa4bec 100644 --- a/engine/systemd/unit.go +++ b/engine/systemd/unit.go @@ -216,11 +216,14 @@ func (b *unitBuilder) buffer() (*bytes.Buffer, error) { return bytes.NewBufferString(unit), b.err } -func (b *unitBuilder) convertToSystemdRestartPolicy(restart int) (policy string) { - if restart == 0 { - return "no" - } - return "always" +func (b *unitBuilder) convertToSystemdRestartPolicy(restart string) (policy string) { + switch { + case restart == "always": + return "always" + case strings.HasPrefix(restart, "on-failure"): + return "on-failure" + } + return "no" } func (b *unitBuilder) convertToSystemdStdio(logType string) (stdioType string, err error) { diff --git a/engine/types/virtualization.go b/engine/types/virtualization.go index b54366c5e..865bf10b6 100644 --- a/engine/types/virtualization.go +++ b/engine/types/virtualization.go @@ -30,7 +30,7 @@ type VirtualizationCreateOptions struct { Labels map[string]string Debug bool - Restart int + Restart string Networks map[string]string diff --git a/rpc/gen/core.pb.go b/rpc/gen/core.pb.go index e2ef95905..8b75e32bd 100644 --- a/rpc/gen/core.pb.go +++ b/rpc/gen/core.pb.go @@ -3356,7 +3356,7 @@ type EntrypointOptions struct { Publish []string `protobuf:"bytes,6,rep,name=publish,proto3" json:"publish,omitempty"` Healthcheck *HealthCheckOptions `protobuf:"bytes,7,opt,name=healthcheck,proto3" json:"healthcheck,omitempty"` Hook *HookOptions `protobuf:"bytes,8,opt,name=hook,proto3" json:"hook,omitempty"` - Restart int32 `protobuf:"varint,9,opt,name=restart,proto3" json:"restart,omitempty"` + Restart string `protobuf:"bytes,9,opt,name=restart,proto3" json:"restart,omitempty"` Sysctls map[string]string `protobuf:"bytes,10,rep,name=sysctls,proto3" json:"sysctls,omitempty" protobuf_key:"bytes,1,opt,name=key,proto3" protobuf_val:"bytes,2,opt,name=value,proto3"` } @@ -3448,11 +3448,11 @@ func (x *EntrypointOptions) GetHook() *HookOptions { return nil } -func (x *EntrypointOptions) GetRestart() int32 { +func (x *EntrypointOptions) GetRestart() string { if x != nil { return x.Restart } - return 0 + return "" } func (x *EntrypointOptions) GetSysctls() map[string]string { @@ -6189,7 +6189,7 @@ var file_core_proto_rawDesc = []byte{ 0x63, 0x68, 0x65, 0x63, 0x6b, 0x12, 0x23, 0x0a, 0x04, 0x68, 0x6f, 0x6f, 0x6b, 0x18, 0x08, 0x20, 0x01, 0x28, 0x0b, 0x32, 0x0f, 0x2e, 0x70, 0x62, 0x2e, 0x48, 0x6f, 0x6f, 0x6b, 0x4f, 0x70, 0x74, 0x69, 0x6f, 0x6e, 0x73, 0x52, 0x04, 0x68, 0x6f, 0x6f, 0x6b, 0x12, 0x18, 0x0a, 0x07, 0x72, 0x65, - 0x73, 0x74, 0x61, 0x72, 0x74, 0x18, 0x09, 0x20, 0x01, 0x28, 0x05, 0x52, 0x07, 0x72, 0x65, 0x73, + 0x73, 0x74, 0x61, 0x72, 0x74, 0x18, 0x09, 0x20, 0x01, 0x28, 0x09, 0x52, 0x07, 0x72, 0x65, 0x73, 0x74, 0x61, 0x72, 0x74, 0x12, 0x3c, 0x0a, 0x07, 0x73, 0x79, 0x73, 0x63, 0x74, 0x6c, 0x73, 0x18, 0x0a, 0x20, 0x03, 0x28, 0x0b, 0x32, 0x22, 0x2e, 0x70, 0x62, 0x2e, 0x45, 0x6e, 0x74, 0x72, 0x79, 0x70, 0x6f, 0x69, 0x6e, 0x74, 0x4f, 0x70, 0x74, 0x69, 0x6f, 0x6e, 0x73, 0x2e, 0x53, 0x79, 0x73, diff --git a/rpc/gen/core.proto b/rpc/gen/core.proto index 2a4a8f585..4673b399c 100644 --- a/rpc/gen/core.proto +++ b/rpc/gen/core.proto @@ -382,7 +382,7 @@ message EntrypointOptions { repeated string publish = 6; HealthCheckOptions healthcheck = 7; HookOptions hook = 8; - int32 restart = 9; + string restart = 9; map sysctls = 10; } diff --git a/rpc/transform.go b/rpc/transform.go index 3cc22cb3d..7f4fc87b3 100644 --- a/rpc/transform.go +++ b/rpc/transform.go @@ -237,7 +237,7 @@ func toCoreDeployOptions(d *pb.DeployOptions) (*types.DeployOptions, error) { Privileged: entrypoint.Privileged, Dir: entrypoint.Dir, Publish: entrypoint.Publish, - Restart: int(entrypoint.Restart), + Restart: entrypoint.Restart, Sysctls: entrypoint.Sysctls, } diff --git a/types/specs.go b/types/specs.go index 4ab34ad6f..25133cbd7 100644 --- a/types/specs.go +++ b/types/specs.go @@ -29,7 +29,7 @@ type Entrypoint struct { Publish []string `yaml:"publish,omitempty,flow"` HealthCheck *HealthCheck `yaml:"healthcheck,omitempty,flow"` Hook *Hook `yaml:"hook,omitempty,flow"` - Restart int `yaml:"restart,omitempty"` + Restart string `yaml:"restart,omitempty"` Sysctls map[string]string `yaml:"sysctls,omitempty,flow"` }