Skip to content

Commit cd5681d

Browse files
authored
redefine restart and keep backward compatible (#345)
1 parent 86cafe8 commit cd5681d

File tree

7 files changed

+23
-22
lines changed

7 files changed

+23
-22
lines changed

engine/docker/container.go

+7-9
Original file line numberDiff line numberDiff line change
@@ -30,10 +30,9 @@ import (
3030
)
3131

3232
const (
33-
minMemory = units.MiB * 4
34-
maxMemory = math.MaxInt64
35-
restartAlways = "always"
36-
root = "root"
33+
minMemory = units.MiB * 4
34+
maxMemory = math.MaxInt64
35+
root = "root"
3736
)
3837

3938
type rawArgs struct {
@@ -57,11 +56,10 @@ func (e *Engine) VirtualizationCreate(ctx context.Context, opts *enginetypes.Vir
5756

5857
restartPolicy := ""
5958
restartRetry := 0
60-
if opts.Restart == -1 || opts.Restart > 0 {
61-
restartPolicy = restartAlways
62-
}
63-
if opts.Restart > 0 {
64-
restartRetry = opts.Restart
59+
restartStr := strings.Split(opts.Restart, ":")
60+
restartPolicy = restartStr[0]
61+
if r, err := strconv.Atoi(restartStr[len(restartStr)-1]); err == nil {
62+
restartRetry = r
6563
}
6664
// no longer use opts.Network as networkmode
6765
// always get network name from networks

engine/systemd/unit.go

+8-5
Original file line numberDiff line numberDiff line change
@@ -216,11 +216,14 @@ func (b *unitBuilder) buffer() (*bytes.Buffer, error) {
216216
return bytes.NewBufferString(unit), b.err
217217
}
218218

219-
func (b *unitBuilder) convertToSystemdRestartPolicy(restart int) (policy string) {
220-
if restart == 0 {
221-
return "no"
222-
}
223-
return "always"
219+
func (b *unitBuilder) convertToSystemdRestartPolicy(restart string) (policy string) {
220+
switch {
221+
case strings.HasPrefix(restart, "always"):
222+
return "always"
223+
case strings.HasPrefix(restart, "on-failure"):
224+
return "on-failure"
225+
}
226+
return "no"
224227
}
225228

226229
func (b *unitBuilder) convertToSystemdStdio(logType string) (stdioType string, err error) {

engine/types/virtualization.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ type VirtualizationCreateOptions struct {
3030
Labels map[string]string
3131

3232
Debug bool
33-
Restart int
33+
Restart string
3434

3535
Networks map[string]string
3636

rpc/gen/core.pb.go

+4-4
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

rpc/gen/core.proto

+1-1
Original file line numberDiff line numberDiff line change
@@ -382,7 +382,7 @@ message EntrypointOptions {
382382
repeated string publish = 6;
383383
HealthCheckOptions healthcheck = 7;
384384
HookOptions hook = 8;
385-
int32 restart = 9;
385+
string restart = 9;
386386
map<string, string> sysctls = 10;
387387
}
388388

rpc/transform.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -237,7 +237,7 @@ func toCoreDeployOptions(d *pb.DeployOptions) (*types.DeployOptions, error) {
237237
Privileged: entrypoint.Privileged,
238238
Dir: entrypoint.Dir,
239239
Publish: entrypoint.Publish,
240-
Restart: int(entrypoint.Restart),
240+
Restart: entrypoint.Restart,
241241
Sysctls: entrypoint.Sysctls,
242242
}
243243

types/specs.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ type Entrypoint struct {
2929
Publish []string `yaml:"publish,omitempty,flow"`
3030
HealthCheck *HealthCheck `yaml:"healthcheck,omitempty,flow"`
3131
Hook *Hook `yaml:"hook,omitempty,flow"`
32-
Restart int `yaml:"restart,omitempty"`
32+
Restart string `yaml:"restart,omitempty"`
3333
Sysctls map[string]string `yaml:"sysctls,omitempty,flow"`
3434
}
3535

0 commit comments

Comments
 (0)