Commit b9247598 authored by k8s-merge-robot's avatar k8s-merge-robot Committed by GitHub

Merge pull request #27160 from janetkuo/illegal-deployment-strategy

Automatic merge from submit-queue Validation for illegal deployment strategy Fixes #23677 @kubernetes/deployment [![Analytics](https://kubernetes-site.appspot.com/UA-36037335-10/GitHub/.github/PULL_REQUEST_TEMPLATE.md?pixel)]()
parents adb06748 c160f751
......@@ -204,6 +204,30 @@ func TestSetDefaultDeployment(t *testing.T) {
{
original: &Deployment{
Spec: DeploymentSpec{
Replicas: newInt32(3),
Strategy: DeploymentStrategy{
Type: RollingUpdateDeploymentStrategyType,
RollingUpdate: nil,
},
},
},
expected: &Deployment{
Spec: DeploymentSpec{
Replicas: newInt32(3),
Strategy: DeploymentStrategy{
Type: RollingUpdateDeploymentStrategyType,
RollingUpdate: &RollingUpdateDeployment{
MaxSurge: &defaultIntOrString,
MaxUnavailable: &defaultIntOrString,
},
},
Template: defaultTemplate,
},
},
},
{
original: &Deployment{
Spec: DeploymentSpec{
Replicas: newInt32(5),
Strategy: DeploymentStrategy{
Type: RecreateDeploymentStrategyType,
......
......@@ -198,14 +198,21 @@ func ValidateRollingUpdateDeployment(rollingUpdate *extensions.RollingUpdateDepl
func ValidateDeploymentStrategy(strategy *extensions.DeploymentStrategy, fldPath *field.Path) field.ErrorList {
allErrs := field.ErrorList{}
if strategy.RollingUpdate == nil {
return allErrs
}
switch strategy.Type {
case extensions.RecreateDeploymentStrategyType:
allErrs = append(allErrs, field.Forbidden(fldPath.Child("rollingUpdate"), "may not be specified when strategy `type` is '"+string(extensions.RecreateDeploymentStrategyType+"'")))
if strategy.RollingUpdate != nil {
allErrs = append(allErrs, field.Forbidden(fldPath.Child("rollingUpdate"), "may not be specified when strategy `type` is '"+string(extensions.RecreateDeploymentStrategyType+"'")))
}
case extensions.RollingUpdateDeploymentStrategyType:
allErrs = append(allErrs, ValidateRollingUpdateDeployment(strategy.RollingUpdate, fldPath.Child("rollingUpdate"))...)
// This should never happen since it's set and checked in defaults.go
if strategy.RollingUpdate == nil {
allErrs = append(allErrs, field.Required(fldPath.Child("rollingUpdate"), "this should be defaulted and never be nil"))
} else {
allErrs = append(allErrs, ValidateRollingUpdateDeployment(strategy.RollingUpdate, fldPath.Child("rollingUpdate"))...)
}
default:
validValues := []string{string(extensions.RecreateDeploymentStrategyType), string(extensions.RollingUpdateDeploymentStrategyType)}
allErrs = append(allErrs, field.NotSupported(fldPath, strategy, validValues))
}
return allErrs
}
......
......@@ -547,6 +547,13 @@ func validDeployment() *extensions.Deployment {
"name": "abc",
},
},
Strategy: extensions.DeploymentStrategy{
Type: extensions.RollingUpdateDeploymentStrategyType,
RollingUpdate: &extensions.RollingUpdateDeployment{
MaxSurge: intstr.FromInt(1),
MaxUnavailable: intstr.FromInt(1),
},
},
Template: api.PodTemplateSpec{
ObjectMeta: api.ObjectMeta{
Name: "abc",
......@@ -604,6 +611,11 @@ func TestValidateDeployment(t *testing.T) {
invalidRestartPolicyDeployment.Spec.Template.Spec.RestartPolicy = api.RestartPolicyNever
errorCases["Unsupported value: \"Never\""] = invalidRestartPolicyDeployment
// must have valid strategy type
invalidStrategyDeployment := validDeployment()
invalidStrategyDeployment.Spec.Strategy.Type = extensions.DeploymentStrategyType("randomType")
errorCases["supported values: Recreate, RollingUpdate"] = invalidStrategyDeployment
// rollingUpdate should be nil for recreate.
invalidRecreateDeployment := validDeployment()
invalidRecreateDeployment.Spec.Strategy = extensions.DeploymentStrategy{
......
......@@ -34,6 +34,7 @@ import (
"k8s.io/kubernetes/pkg/storage/etcd/etcdtest"
etcdtesting "k8s.io/kubernetes/pkg/storage/etcd/testing"
"k8s.io/kubernetes/pkg/util/diff"
"k8s.io/kubernetes/pkg/util/intstr"
)
const defaultReplicas = 100
......@@ -56,6 +57,13 @@ func validNewDeployment() *extensions.Deployment {
},
Spec: extensions.DeploymentSpec{
Selector: &unversioned.LabelSelector{MatchLabels: map[string]string{"a": "b"}},
Strategy: extensions.DeploymentStrategy{
Type: extensions.RollingUpdateDeploymentStrategyType,
RollingUpdate: &extensions.RollingUpdateDeployment{
MaxSurge: intstr.FromInt(1),
MaxUnavailable: intstr.FromInt(1),
},
},
Template: api.PodTemplateSpec{
ObjectMeta: api.ObjectMeta{
Labels: map[string]string{"a": "b"},
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment