forked from openshift/origin
-
Notifications
You must be signed in to change notification settings - Fork 0
/
validation.go
245 lines (195 loc) · 8.75 KB
/
validation.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
package validation
import (
"fmt"
"strings"
kapi "github.com/GoogleCloudPlatform/kubernetes/pkg/api"
"github.com/GoogleCloudPlatform/kubernetes/pkg/api/validation"
"github.com/GoogleCloudPlatform/kubernetes/pkg/util"
"github.com/GoogleCloudPlatform/kubernetes/pkg/util/fielderrors"
deployapi "github.com/openshift/origin/pkg/deploy/api"
)
// TODO: These tests validate the ReplicationControllerState in a Deployment or DeploymentConfig.
// The upstream validation API isn't factored currently to allow this; we'll make a PR to
// upstream and fix when it goes in.
func ValidateDeploymentConfig(config *deployapi.DeploymentConfig) fielderrors.ValidationErrorList {
allErrs := fielderrors.ValidationErrorList{}
allErrs = append(allErrs, validation.ValidateObjectMeta(&config.ObjectMeta, true, validation.NameIsDNSSubdomain).Prefix("metadata")...)
for i := range config.Triggers {
allErrs = append(allErrs, validateTrigger(&config.Triggers[i]).PrefixIndex(i).Prefix("triggers")...)
}
allErrs = append(allErrs, validateDeploymentStrategy(&config.Template.Strategy).Prefix("template.strategy")...)
allErrs = append(allErrs, validation.ValidateReplicationControllerSpec(&config.Template.ControllerTemplate).Prefix("template.controllerTemplate")...)
if config.LatestVersion < 0 {
allErrs = append(allErrs, fielderrors.NewFieldInvalid("latestVersion", config.LatestVersion, "latestVersion cannot be negative"))
}
return allErrs
}
func ValidateDeploymentConfigUpdate(newConfig *deployapi.DeploymentConfig, oldConfig *deployapi.DeploymentConfig) fielderrors.ValidationErrorList {
allErrs := fielderrors.ValidationErrorList{}
allErrs = append(allErrs, validation.ValidateObjectMetaUpdate(&newConfig.ObjectMeta, &oldConfig.ObjectMeta).Prefix("metadata")...)
allErrs = append(allErrs, ValidateDeploymentConfig(newConfig)...)
if newConfig.LatestVersion < oldConfig.LatestVersion {
allErrs = append(allErrs, fielderrors.NewFieldInvalid("latestVersion", newConfig.LatestVersion, "latestVersion cannot be decremented"))
} else if newConfig.LatestVersion > (oldConfig.LatestVersion + 1) {
allErrs = append(allErrs, fielderrors.NewFieldInvalid("latestVersion", newConfig.LatestVersion, "latestVersion can only be incremented by 1"))
}
return allErrs
}
func ValidateDeploymentConfigRollback(rollback *deployapi.DeploymentConfigRollback) fielderrors.ValidationErrorList {
result := fielderrors.ValidationErrorList{}
if len(rollback.Spec.From.Name) == 0 {
result = append(result, fielderrors.NewFieldRequired("spec.from.name"))
}
if len(rollback.Spec.From.Kind) == 0 {
rollback.Spec.From.Kind = "ReplicationController"
}
if rollback.Spec.From.Kind != "ReplicationController" {
result = append(result, fielderrors.NewFieldInvalid("spec.from.kind", rollback.Spec.From.Kind, "the kind of the rollback target must be 'ReplicationController'"))
}
return result
}
func validateDeploymentStrategy(strategy *deployapi.DeploymentStrategy) fielderrors.ValidationErrorList {
errs := fielderrors.ValidationErrorList{}
if len(strategy.Type) == 0 {
errs = append(errs, fielderrors.NewFieldRequired("type"))
}
switch strategy.Type {
case deployapi.DeploymentStrategyTypeRecreate:
if strategy.RecreateParams != nil {
errs = append(errs, validateRecreateParams(strategy.RecreateParams).Prefix("recreateParams")...)
}
case deployapi.DeploymentStrategyTypeRolling:
if strategy.RollingParams == nil {
errs = append(errs, fielderrors.NewFieldRequired("rollingParams"))
} else {
errs = append(errs, validateRollingParams(strategy.RollingParams).Prefix("rollingParams")...)
}
case deployapi.DeploymentStrategyTypeCustom:
if strategy.CustomParams == nil {
errs = append(errs, fielderrors.NewFieldRequired("customParams"))
} else {
errs = append(errs, validateCustomParams(strategy.CustomParams).Prefix("customParams")...)
}
}
// TODO: validate resource requirements (prereq: https://github.com/GoogleCloudPlatform/kubernetes/pull/7059)
return errs
}
func validateCustomParams(params *deployapi.CustomDeploymentStrategyParams) fielderrors.ValidationErrorList {
errs := fielderrors.ValidationErrorList{}
if len(params.Image) == 0 {
errs = append(errs, fielderrors.NewFieldRequired("image"))
}
return errs
}
func validateRecreateParams(params *deployapi.RecreateDeploymentStrategyParams) fielderrors.ValidationErrorList {
errs := fielderrors.ValidationErrorList{}
if params.Pre != nil {
errs = append(errs, validateLifecycleHook(params.Pre).Prefix("pre")...)
}
if params.Post != nil {
errs = append(errs, validateLifecycleHook(params.Post).Prefix("post")...)
}
return errs
}
func validateLifecycleHook(hook *deployapi.LifecycleHook) fielderrors.ValidationErrorList {
errs := fielderrors.ValidationErrorList{}
if len(hook.FailurePolicy) == 0 {
errs = append(errs, fielderrors.NewFieldRequired("failurePolicy"))
}
if hook.ExecNewPod == nil {
errs = append(errs, fielderrors.NewFieldRequired("execNewPod"))
} else {
errs = append(errs, validateExecNewPod(hook.ExecNewPod).Prefix("execNewPod")...)
}
return errs
}
func validateExecNewPod(hook *deployapi.ExecNewPodHook) fielderrors.ValidationErrorList {
errs := fielderrors.ValidationErrorList{}
if len(hook.Command) == 0 {
errs = append(errs, fielderrors.NewFieldRequired("command"))
}
if len(hook.ContainerName) == 0 {
errs = append(errs, fielderrors.NewFieldRequired("containerName"))
}
if len(hook.Env) > 0 {
errs = append(errs, validateEnv(hook.Env).Prefix("env")...)
}
return errs
}
func validateEnv(vars []kapi.EnvVar) fielderrors.ValidationErrorList {
allErrs := fielderrors.ValidationErrorList{}
for i, ev := range vars {
vErrs := fielderrors.ValidationErrorList{}
if len(ev.Name) == 0 {
vErrs = append(vErrs, fielderrors.NewFieldRequired("name"))
}
if !util.IsCIdentifier(ev.Name) {
vErrs = append(vErrs, fielderrors.NewFieldInvalid("name", ev.Name, "must match regex "+util.CIdentifierFmt))
}
allErrs = append(allErrs, vErrs.PrefixIndex(i)...)
}
return allErrs
}
func validateRollingParams(params *deployapi.RollingDeploymentStrategyParams) fielderrors.ValidationErrorList {
errs := fielderrors.ValidationErrorList{}
if params.IntervalSeconds != nil && *params.IntervalSeconds < 1 {
errs = append(errs, fielderrors.NewFieldInvalid("intervalSeconds", *params.IntervalSeconds, "must be >0"))
}
if params.UpdatePeriodSeconds != nil && *params.UpdatePeriodSeconds < 1 {
errs = append(errs, fielderrors.NewFieldInvalid("updatePeriodSeconds", *params.UpdatePeriodSeconds, "must be >0"))
}
if params.TimeoutSeconds != nil && *params.TimeoutSeconds < 1 {
errs = append(errs, fielderrors.NewFieldInvalid("timeoutSeconds", *params.TimeoutSeconds, "must be >0"))
}
if params.Pre != nil {
errs = append(errs, validateLifecycleHook(params.Pre).Prefix("pre")...)
}
if params.Post != nil {
errs = append(errs, validateLifecycleHook(params.Post).Prefix("post")...)
}
return errs
}
func validateTrigger(trigger *deployapi.DeploymentTriggerPolicy) fielderrors.ValidationErrorList {
errs := fielderrors.ValidationErrorList{}
if len(trigger.Type) == 0 {
errs = append(errs, fielderrors.NewFieldRequired("type"))
}
if trigger.Type == deployapi.DeploymentTriggerOnImageChange {
if trigger.ImageChangeParams == nil {
errs = append(errs, fielderrors.NewFieldRequired("imageChangeParams"))
} else {
errs = append(errs, validateImageChangeParams(trigger.ImageChangeParams).Prefix("imageChangeParams")...)
}
}
return errs
}
func validateImageChangeParams(params *deployapi.DeploymentTriggerImageChangeParams) fielderrors.ValidationErrorList {
errs := fielderrors.ValidationErrorList{}
if len(params.From.Name) != 0 {
if len(params.From.Kind) == 0 {
params.From.Kind = "ImageStream"
}
kinds := util.NewStringSet("ImageRepository", "ImageStream", "ImageStreamTag")
if !kinds.Has(params.From.Kind) {
msg := fmt.Sprintf("kind must be one of: %s", strings.Join(kinds.List(), ", "))
errs = append(errs, fielderrors.NewFieldInvalid("from.kind", params.From.Kind, msg))
}
if !util.IsDNS1123Subdomain(params.From.Name) {
errs = append(errs, fielderrors.NewFieldInvalid("from.name", params.From.Name, "name must be a valid subdomain"))
}
if len(params.From.Namespace) != 0 && !util.IsDNS1123Subdomain(params.From.Namespace) {
errs = append(errs, fielderrors.NewFieldInvalid("from.namespace", params.From.Namespace, "namespace must be a valid subdomain"))
}
if len(params.RepositoryName) != 0 {
errs = append(errs, fielderrors.NewFieldInvalid("repositoryName", params.RepositoryName, "only one of 'from', 'repository' name may be specified"))
}
} else {
if len(params.RepositoryName) == 0 {
errs = append(errs, fielderrors.NewFieldRequired("from"))
}
}
if len(params.ContainerNames) == 0 {
errs = append(errs, fielderrors.NewFieldRequired("containerNames"))
}
return errs
}