forked from AliyunContainerService/pouch
/
restart_policy.go
61 lines (52 loc) · 1.44 KB
/
restart_policy.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
package opts
import (
"fmt"
"strconv"
"strings"
"github.com/alibaba/pouch/apis/types"
)
// ParseRestartPolicy parses restart policy param of container.
func ParseRestartPolicy(restartPolicy string) (*types.RestartPolicy, error) {
policy := &types.RestartPolicy{}
if restartPolicy == "" {
policy.Name = "no"
return policy, nil
}
fields := strings.Split(restartPolicy, ":")
policy.Name = fields[0]
switch policy.Name {
case "always", "unless-stopped", "no":
case "on-failure":
if len(fields) > 2 {
return nil, fmt.Errorf("invalid restart policy: %s", restartPolicy)
}
if len(fields) == 2 {
n, err := strconv.Atoi(fields[1])
if err != nil {
return nil, fmt.Errorf("invalid restart policy: %v", err)
}
policy.MaximumRetryCount = int64(n)
}
default:
return nil, fmt.Errorf("invalid restart policy: %s", restartPolicy)
}
return policy, nil
}
// ValidateRestartPolicy verifies the correctness of restart policy of container.
func ValidateRestartPolicy(policy *types.RestartPolicy) error {
switch policy.Name {
case "always", "unless-stopped", "no":
if policy.MaximumRetryCount != 0 {
return fmt.Errorf("maximum retry count can not be used with restart policy: %s", policy.Name)
}
case "on-failure":
if policy.MaximumRetryCount < 0 {
return fmt.Errorf("maximum retry count can not be negative")
}
case "":
// ignore
default:
return fmt.Errorf("invalid restart policy '%s'", policy.Name)
}
return nil
}