diff --git a/pkg/api/types.go b/pkg/api/types.go index e654ccb29f6..673e35ab3fa 100644 --- a/pkg/api/types.go +++ b/pkg/api/types.go @@ -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"` @@ -225,7 +240,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. diff --git a/pkg/api/v1beta1/types.go b/pkg/api/v1beta1/types.go index 3b9b73bfe90..aa2615f0225 100644 --- a/pkg/api/v1beta1/types.go +++ b/pkg/api/v1beta1/types.go @@ -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. diff --git a/pkg/api/validation.go b/pkg/api/validation.go index 2946444795b..2c0991f72d5 100644 --- a/pkg/api/validation.go +++ b/pkg/api/validation.go @@ -281,13 +281,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) } diff --git a/pkg/api/validation_test.go b/pkg/api/validation_test.go index ec5f432d1d4..7b83e56a20d 100644 --- a/pkg/api/validation_test.go +++ b/pkg/api/validation_test.go @@ -262,6 +262,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"},