Merge pull request #691 from dchen1107/restart

Add RestartPolicy to Pod and PodTemplate
This commit is contained in:
Dawn Chen 2014-07-30 15:32:20 -07:00
commit 21513b1e08
4 changed files with 90 additions and 3 deletions

View File

@ -208,6 +208,21 @@ const (
// PodInfo contains one entry for every container with available info.
type PodInfo map[string]docker.Container
// RestartPolicyType represents a restart policy for a pod.
type RestartPolicyType string
// Valid restart policies defined for a PodState.RestartPolicy.
const (
RestartAlways RestartPolicyType = "RestartAlways"
RestartOnFailure RestartPolicyType = "RestartOnFailure"
RestartNever RestartPolicyType = "RestartNever"
)
type RestartPolicy struct {
// Optional: Defaults to "RestartAlways".
Type RestartPolicyType `yaml:"type,omitempty" json:"type,omitempty"`
}
// PodState is the state of a pod, used as either input (desired state) or output (current state)
type PodState struct {
Manifest ContainerManifest `json:"manifest,omitempty" yaml:"manifest,omitempty"`
@ -222,7 +237,8 @@ type PodState struct {
// upon.
// TODO: Make real decisions about what our info should look like. Re-enable fuzz test
// when we have done this.
Info PodInfo `json:"info,omitempty" yaml:"info,omitempty"`
Info PodInfo `json:"info,omitempty" yaml:"info,omitempty"`
RestartPolicy RestartPolicy `json:"restartpolicy,omitempty" yaml:"restartpolicy,omitempty"`
}
// PodList is a list of Pods.

View File

@ -211,6 +211,21 @@ const (
// PodInfo contains one entry for every container with available info.
type PodInfo map[string]docker.Container
// RestartPolicyType represents a restart policy for a pod.
type RestartPolicyType string
// Valid restart policies defined for a PodState.RestartPolicy.
const (
RestartAlways RestartPolicyType = "RestartAlways"
RestartOnFailure RestartPolicyType = "RestartOnFailure"
RestartNever RestartPolicyType = "RestartNever"
)
type RestartPolicy struct {
// Optional: Defaults to "RestartAlways".
Type RestartPolicyType `yaml:"type,omitempty" json:"type,omitempty"`
}
// PodState is the state of a pod, used as either input (desired state) or output (current state)
type PodState struct {
Manifest ContainerManifest `json:"manifest,omitempty" yaml:"manifest,omitempty"`
@ -224,7 +239,8 @@ type PodState struct {
// of `docker inspect`. This output format is *not* final and should not be relied
// upon.
// TODO: Make real decisions about what our info should look like.
Info PodInfo `json:"info,omitempty" yaml:"info,omitempty"`
Info PodInfo `json:"info,omitempty" yaml:"info,omitempty"`
RestartPolicy RestartPolicy `json:"restartpolicy,omitempty" yaml:"restartpolicy,omitempty"`
}
// PodList is a list of Pods.

View File

@ -274,13 +274,26 @@ func ValidateManifest(manifest *ContainerManifest) []error {
return []error(allErrs)
}
func ValidatePodState(podState *PodState) []error {
allErrs := errorList(ValidateManifest(&podState.Manifest))
if podState.RestartPolicy.Type == "" {
podState.RestartPolicy.Type = RestartAlways
} else if podState.RestartPolicy.Type != RestartAlways &&
podState.RestartPolicy.Type != RestartOnFailure &&
podState.RestartPolicy.Type != RestartNever {
allErrs.Append(makeNotSupportedError("PodState.RestartPolicy.Type", podState.RestartPolicy.Type))
}
return []error(allErrs)
}
// Pod tests if required fields in the pod are set.
func ValidatePod(pod *Pod) []error {
allErrs := errorList{}
if pod.ID == "" {
allErrs.Append(makeInvalidError("Pod.ID", pod.ID))
}
allErrs.Append(ValidateManifest(&pod.DesiredState.Manifest)...)
allErrs.Append(ValidatePodState(&pod.DesiredState)...)
return []error(allErrs)
}

View File

@ -290,6 +290,48 @@ func TestValidateManifest(t *testing.T) {
}
}
func TestValidatePod(t *testing.T) {
errs := ValidatePod(&Pod{
JSONBase: JSONBase{ID: "foo"},
Labels: map[string]string{
"foo": "bar",
},
DesiredState: PodState{
Manifest: ContainerManifest{Version: "v1beta1", ID: "abc"},
RestartPolicy: RestartPolicy{Type: "RestartAlways"},
},
})
if len(errs) != 0 {
t.Errorf("Unexpected non-zero error list: %#v", errs)
}
errs = ValidatePod(&Pod{
JSONBase: JSONBase{ID: "foo"},
Labels: map[string]string{
"foo": "bar",
},
DesiredState: PodState{
Manifest: ContainerManifest{Version: "v1beta1", ID: "abc"},
},
})
if len(errs) != 0 {
t.Errorf("Unexpected non-zero error list: %#v", errs)
}
errs = ValidatePod(&Pod{
JSONBase: JSONBase{ID: "foo"},
Labels: map[string]string{
"foo": "bar",
},
DesiredState: PodState{
Manifest: ContainerManifest{Version: "v1beta1", ID: "abc"},
RestartPolicy: RestartPolicy{Type: "WhatEver"},
},
})
if len(errs) != 1 {
t.Errorf("Unexpected error list: %#v", errs)
}
}
func TestValidateService(t *testing.T) {
errs := ValidateService(&Service{
JSONBase: JSONBase{ID: "foo"},