Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Convert RestartPolicy to string for v1beta3. #5477

Merged
merged 1 commit into from
Mar 16, 2015
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 1 addition & 1 deletion cmd/integration/integration.go
Original file line number Diff line number Diff line change
Expand Up @@ -625,7 +625,7 @@ func runServiceTest(client *client.Client) {
ImagePullPolicy: "PullIfNotPresent",
},
},
RestartPolicy: api.RestartPolicy{Always: &api.RestartPolicyAlways{}},
RestartPolicy: api.RestartPolicyAlways,
DNSPolicy: api.DNSClusterFirst,
},
Status: api.PodStatus{
Expand Down
2 changes: 1 addition & 1 deletion pkg/api/serialization_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -162,7 +162,7 @@ func TestEncode_Ptr(t *testing.T) {
Labels: map[string]string{"name": "foo"},
},
Spec: api.PodSpec{
RestartPolicy: api.RestartPolicy{Always: &api.RestartPolicyAlways{}},
RestartPolicy: api.RestartPolicyAlways,
DNSPolicy: api.DNSClusterFirst,
},
}
Expand Down
4 changes: 2 additions & 2 deletions pkg/api/testing/fuzzer.go
Original file line number Diff line number Diff line change
Expand Up @@ -161,8 +161,8 @@ func FuzzerFor(t *testing.T, version string, src rand.Source) *fuzz.Fuzzer {
*p = policies[c.Rand.Intn(len(policies))]
},
func(rp *api.RestartPolicy, c fuzz.Continue) {
// Exactly one of the fields should be set.
fuzzOneOf(c, &rp.Always, &rp.OnFailure, &rp.Never)
policies := []api.RestartPolicy{api.RestartPolicyAlways, api.RestartPolicyNever, api.RestartPolicyOnFailure}
*rp = policies[c.Rand.Intn(len(policies))]
},
func(vs *api.VolumeSource, c fuzz.Continue) {
// Exactly one of the fields should be set.
Expand Down
20 changes: 7 additions & 13 deletions pkg/api/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -515,23 +515,17 @@ type PodContainerInfo struct {
ContainerInfo PodInfo `json:"containerInfo"`
}

type RestartPolicyAlways struct{}

// TODO(dchen1107): Define what kinds of failures should restart.
// TODO(dchen1107): Decide whether to support policy knobs, and, if so, which ones.
type RestartPolicyOnFailure struct{}

type RestartPolicyNever struct{}

// RestartPolicy describes how the container should be restarted.
// Only one of the following restart policies may be specified.
// If none of the following policies is specified, the default one
// is RestartPolicyAlways.
type RestartPolicy struct {
Always *RestartPolicyAlways `json:"always,omitempty"`
OnFailure *RestartPolicyOnFailure `json:"onFailure,omitempty"`
Never *RestartPolicyNever `json:"never,omitempty"`
}
type RestartPolicy string

const (
RestartPolicyAlways RestartPolicy = "Always"
RestartPolicyOnFailure RestartPolicy = "OnFailure"
RestartPolicyNever RestartPolicy = "Never"
)

// PodList is a list of Pods.
type PodList struct {
Expand Down
27 changes: 27 additions & 0 deletions pkg/api/v1beta1/conversion.go
Original file line number Diff line number Diff line change
Expand Up @@ -1139,6 +1139,33 @@ func init() {
return nil
},

func(in *newer.RestartPolicy, out *RestartPolicy, s conversion.Scope) error {
switch *in {
case newer.RestartPolicyAlways:
*out = RestartPolicy{Always: &RestartPolicyAlways{}}
case newer.RestartPolicyNever:
*out = RestartPolicy{Never: &RestartPolicyNever{}}
case newer.RestartPolicyOnFailure:
*out = RestartPolicy{OnFailure: &RestartPolicyOnFailure{}}
default:
*out = RestartPolicy{}
}
return nil
},
func(in *RestartPolicy, out *newer.RestartPolicy, s conversion.Scope) error {
switch {
case in.Always != nil:
*out = newer.RestartPolicyAlways
case in.Never != nil:
*out = newer.RestartPolicyNever
case in.OnFailure != nil:
*out = newer.RestartPolicyOnFailure
default:
*out = ""
}
return nil
},

func(in *newer.Probe, out *LivenessProbe, s conversion.Scope) error {
if err := s.Convert(&in.Exec, &out.Exec, 0); err != nil {
return err
Expand Down
27 changes: 27 additions & 0 deletions pkg/api/v1beta2/conversion.go
Original file line number Diff line number Diff line change
Expand Up @@ -1055,6 +1055,33 @@ func init() {
return nil
},

func(in *newer.RestartPolicy, out *RestartPolicy, s conversion.Scope) error {
switch *in {
case newer.RestartPolicyAlways:
*out = RestartPolicy{Always: &RestartPolicyAlways{}}
case newer.RestartPolicyNever:
*out = RestartPolicy{Never: &RestartPolicyNever{}}
case newer.RestartPolicyOnFailure:
*out = RestartPolicy{OnFailure: &RestartPolicyOnFailure{}}
default:
*out = RestartPolicy{}
}
return nil
},
func(in *RestartPolicy, out *newer.RestartPolicy, s conversion.Scope) error {
switch {
case in.Always != nil:
*out = newer.RestartPolicyAlways
case in.Never != nil:
*out = newer.RestartPolicyNever
case in.OnFailure != nil:
*out = newer.RestartPolicyOnFailure
default:
*out = ""
}
return nil
},

func(in *newer.Probe, out *LivenessProbe, s conversion.Scope) error {
if err := s.Convert(&in.Exec, &out.Exec, 0); err != nil {
return err
Expand Down
8 changes: 3 additions & 5 deletions pkg/api/v1beta3/defaults.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,11 +52,6 @@ func init() {
obj.TerminationMessagePath = TerminationMessagePathDefault
}
},
func(obj *RestartPolicy) {
if util.AllPtrFieldsNil(obj) {
obj.Always = &RestartPolicyAlways{}
}
},
func(obj *Service) {
if obj.Spec.Protocol == "" {
obj.Spec.Protocol = ProtocolTCP
Expand All @@ -69,6 +64,9 @@ func init() {
if obj.DNSPolicy == "" {
obj.DNSPolicy = DNSClusterFirst
}
if obj.RestartPolicy == "" {
obj.RestartPolicy = RestartPolicyAlways
}
},
func(obj *Probe) {
if obj.TimeoutSeconds == 0 {
Expand Down
2 changes: 1 addition & 1 deletion pkg/api/v1beta3/defaults_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ func TestSetDefaulPodSpec(t *testing.T) {
t.Errorf("Expected default dns policy :%s, got: %s", current.DNSClusterFirst, bp2.Spec.DNSPolicy)
}
policy := bp2.Spec.RestartPolicy
if policy.Never != nil || policy.OnFailure != nil || policy.Always == nil {
if policy != current.RestartPolicyAlways {
t.Errorf("Expected only policy.Always is set, got: %s", policy)
}
vsource := bp2.Spec.Volumes[0].VolumeSource
Expand Down
25 changes: 10 additions & 15 deletions pkg/api/v1beta3/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -522,22 +522,17 @@ type PodCondition struct {
// PodInfo contains one entry for every container with available info.
type PodInfo map[string]ContainerStatus

type RestartPolicyAlways struct{}
// RestartPolicy describes how the container should be restarted.
// Only one of the following restart policies may be specified.
// If none of the following policies is specified, the default one
// is RestartPolicyAlways.
type RestartPolicy string

// TODO(dchen1107): Define what kinds of failures should restart.
// TODO(dchen1107): Decide whether to support policy knobs, and, if so, which ones.
type RestartPolicyOnFailure struct{}

type RestartPolicyNever struct{}

type RestartPolicy struct {
// Only one of the following restart policies may be specified.
// If none of the following policies is specified, the default one
// is RestartPolicyAlways.
Always *RestartPolicyAlways `json:"always,omitempty" description:"always restart the container after termination"`
OnFailure *RestartPolicyOnFailure `json:"onFailure,omitempty" description:"restart the container if it fails for any reason, but not if it succeeds (exit 0)"`
Never *RestartPolicyNever `json:"never,omitempty" description:"never restart the container"`
}
const (
RestartPolicyAlways RestartPolicy = "Always"
RestartPolicyOnFailure RestartPolicy = "OnFailure"
RestartPolicyNever RestartPolicy = "Never"
)

// DNSPolicy defines how a pod's DNS will be configured.
type DNSPolicy string
Expand Down
26 changes: 10 additions & 16 deletions pkg/api/validation/validation.go
Original file line number Diff line number Diff line change
Expand Up @@ -600,20 +600,16 @@ func ValidateManifest(manifest *api.ContainerManifest) errs.ValidationErrorList
}

func validateRestartPolicy(restartPolicy *api.RestartPolicy) errs.ValidationErrorList {
numPolicies := 0
allErrors := errs.ValidationErrorList{}
if restartPolicy.Always != nil {
numPolicies++
}
if restartPolicy.OnFailure != nil {
numPolicies++
}
if restartPolicy.Never != nil {
numPolicies++
}
if numPolicies != 1 {
allErrors = append(allErrors, errs.NewFieldInvalid("", restartPolicy, "only 1 policy is allowed"))
switch *restartPolicy {
case api.RestartPolicyAlways, api.RestartPolicyOnFailure, api.RestartPolicyNever:
break
case "":
allErrors = append(allErrors, errs.NewFieldRequired("", *restartPolicy))
default:
allErrors = append(allErrors, errs.NewFieldNotSupported("", restartPolicy))
}

return allErrors
}

Expand Down Expand Up @@ -786,10 +782,8 @@ func ValidateReplicationControllerSpec(spec *api.ReplicationControllerSpec) errs
}
allErrs = append(allErrs, ValidatePodTemplateSpec(spec.Template, spec.Replicas).Prefix("template")...)
// RestartPolicy has already been first-order validated as per ValidatePodTemplateSpec().
if spec.Template.Spec.RestartPolicy.Always == nil {
// TODO: should probably be Unsupported
// TODO: api.RestartPolicy should have a String() method for nicer printing
allErrs = append(allErrs, errs.NewFieldInvalid("template.restartPolicy", spec.Template.Spec.RestartPolicy, "must be Always"))
if spec.Template.Spec.RestartPolicy != api.RestartPolicyAlways {
allErrs = append(allErrs, errs.NewFieldNotSupported("template.restartPolicy", spec.Template.Spec.RestartPolicy))
}
}
return allErrs
Expand Down