|
@@ -1,6 +1,7 @@
|
|
package container // import "github.com/docker/docker/api/types/container"
|
|
package container // import "github.com/docker/docker/api/types/container"
|
|
|
|
|
|
import (
|
|
import (
|
|
|
|
+ "fmt"
|
|
"strings"
|
|
"strings"
|
|
|
|
|
|
"github.com/docker/docker/api/types/blkiodev"
|
|
"github.com/docker/docker/api/types/blkiodev"
|
|
@@ -271,33 +272,42 @@ type DeviceMapping struct {
|
|
|
|
|
|
// RestartPolicy represents the restart policies of the container.
|
|
// RestartPolicy represents the restart policies of the container.
|
|
type RestartPolicy struct {
|
|
type RestartPolicy struct {
|
|
- Name string
|
|
|
|
|
|
+ Name RestartPolicyMode
|
|
MaximumRetryCount int
|
|
MaximumRetryCount int
|
|
}
|
|
}
|
|
|
|
|
|
|
|
+type RestartPolicyMode string
|
|
|
|
+
|
|
|
|
+const (
|
|
|
|
+ RestartPolicyDisabled RestartPolicyMode = "no"
|
|
|
|
+ RestartPolicyAlways RestartPolicyMode = "always"
|
|
|
|
+ RestartPolicyOnFailure RestartPolicyMode = "on-failure"
|
|
|
|
+ RestartPolicyUnlessStopped RestartPolicyMode = "unless-stopped"
|
|
|
|
+)
|
|
|
|
+
|
|
// IsNone indicates whether the container has the "no" restart policy.
|
|
// IsNone indicates whether the container has the "no" restart policy.
|
|
// This means the container will not automatically restart when exiting.
|
|
// This means the container will not automatically restart when exiting.
|
|
func (rp *RestartPolicy) IsNone() bool {
|
|
func (rp *RestartPolicy) IsNone() bool {
|
|
- return rp.Name == "no" || rp.Name == ""
|
|
|
|
|
|
+ return rp.Name == RestartPolicyDisabled || rp.Name == ""
|
|
}
|
|
}
|
|
|
|
|
|
// IsAlways indicates whether the container has the "always" restart policy.
|
|
// IsAlways indicates whether the container has the "always" restart policy.
|
|
// This means the container will automatically restart regardless of the exit status.
|
|
// This means the container will automatically restart regardless of the exit status.
|
|
func (rp *RestartPolicy) IsAlways() bool {
|
|
func (rp *RestartPolicy) IsAlways() bool {
|
|
- return rp.Name == "always"
|
|
|
|
|
|
+ return rp.Name == RestartPolicyAlways
|
|
}
|
|
}
|
|
|
|
|
|
// IsOnFailure indicates whether the container has the "on-failure" restart policy.
|
|
// IsOnFailure indicates whether the container has the "on-failure" restart policy.
|
|
// This means the container will automatically restart of exiting with a non-zero exit status.
|
|
// This means the container will automatically restart of exiting with a non-zero exit status.
|
|
func (rp *RestartPolicy) IsOnFailure() bool {
|
|
func (rp *RestartPolicy) IsOnFailure() bool {
|
|
- return rp.Name == "on-failure"
|
|
|
|
|
|
+ return rp.Name == RestartPolicyOnFailure
|
|
}
|
|
}
|
|
|
|
|
|
// IsUnlessStopped indicates whether the container has the
|
|
// IsUnlessStopped indicates whether the container has the
|
|
// "unless-stopped" restart policy. This means the container will
|
|
// "unless-stopped" restart policy. This means the container will
|
|
// automatically restart unless user has put it to stopped state.
|
|
// automatically restart unless user has put it to stopped state.
|
|
func (rp *RestartPolicy) IsUnlessStopped() bool {
|
|
func (rp *RestartPolicy) IsUnlessStopped() bool {
|
|
- return rp.Name == "unless-stopped"
|
|
|
|
|
|
+ return rp.Name == RestartPolicyUnlessStopped
|
|
}
|
|
}
|
|
|
|
|
|
// IsSame compares two RestartPolicy to see if they are the same
|
|
// IsSame compares two RestartPolicy to see if they are the same
|
|
@@ -305,6 +315,29 @@ func (rp *RestartPolicy) IsSame(tp *RestartPolicy) bool {
|
|
return rp.Name == tp.Name && rp.MaximumRetryCount == tp.MaximumRetryCount
|
|
return rp.Name == tp.Name && rp.MaximumRetryCount == tp.MaximumRetryCount
|
|
}
|
|
}
|
|
|
|
|
|
|
|
+// ValidateRestartPolicy validates the given RestartPolicy.
|
|
|
|
+func ValidateRestartPolicy(policy RestartPolicy) error {
|
|
|
|
+ switch policy.Name {
|
|
|
|
+ case RestartPolicyAlways, RestartPolicyUnlessStopped, RestartPolicyDisabled:
|
|
|
|
+ if policy.MaximumRetryCount != 0 {
|
|
|
|
+ return &errInvalidParameter{fmt.Errorf("invalid restart policy: maximum retry count cannot be used with restart policy '%s'", policy.Name)}
|
|
|
|
+ }
|
|
|
|
+ return nil
|
|
|
|
+ case RestartPolicyOnFailure:
|
|
|
|
+ if policy.MaximumRetryCount < 0 {
|
|
|
|
+ return &errInvalidParameter{fmt.Errorf("invalid restart policy: maximum retry count cannot be negative")}
|
|
|
|
+ }
|
|
|
|
+ return nil
|
|
|
|
+ case "":
|
|
|
|
+ // Versions before v25.0.0 created an empty restart-policy "name" as
|
|
|
|
+ // default. Allow an empty name with "any" MaximumRetryCount for
|
|
|
|
+ // backward-compatibility.
|
|
|
|
+ return nil
|
|
|
|
+ default:
|
|
|
|
+ return &errInvalidParameter{fmt.Errorf("invalid restart policy: '%s'", policy.Name)}
|
|
|
|
+ }
|
|
|
|
+}
|
|
|
|
+
|
|
// LogMode is a type to define the available modes for logging
|
|
// LogMode is a type to define the available modes for logging
|
|
// These modes affect how logs are handled when log messages start piling up.
|
|
// These modes affect how logs are handled when log messages start piling up.
|
|
type LogMode string
|
|
type LogMode string
|