forked from openshift/origin
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathvalidation.go
112 lines (84 loc) · 3.71 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
package validation
import (
"github.com/GoogleCloudPlatform/kubernetes/pkg/api/errors"
"github.com/GoogleCloudPlatform/kubernetes/pkg/api/validation"
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 ValidateDeployment(deployment *deployapi.Deployment) errors.ValidationErrorList {
result := validateDeploymentStrategy(&deployment.Strategy).Prefix("strategy")
controllerStateErrors := validation.ValidateReplicationControllerSpec(&deployment.ControllerTemplate)
result = append(result, controllerStateErrors.Prefix("controllerTemplate")...)
return result
}
func ValidateDeploymentConfig(config *deployapi.DeploymentConfig) errors.ValidationErrorList {
result := errors.ValidationErrorList{}
for i := range config.Triggers {
result = append(result, validateTrigger(&config.Triggers[i]).PrefixIndex(i).Prefix("triggers")...)
}
result = append(result, validateDeploymentStrategy(&config.Template.Strategy).Prefix("template.strategy")...)
controllerStateErrors := validation.ValidateReplicationControllerSpec(&config.Template.ControllerTemplate)
result = append(result, controllerStateErrors.Prefix("template.controllerTemplate")...)
return result
}
func ValidateDeploymentConfigRollback(rollback *deployapi.DeploymentConfigRollback) errors.ValidationErrorList {
result := errors.ValidationErrorList{}
if len(rollback.Spec.From.Name) == 0 {
result = append(result, errors.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, errors.NewFieldInvalid("spec.from.kind", rollback.Spec.From.Kind, "the kind of the rollback target must be 'ReplicationController'"))
}
return result
}
func validateDeploymentStrategy(strategy *deployapi.DeploymentStrategy) errors.ValidationErrorList {
result := errors.ValidationErrorList{}
if len(strategy.Type) == 0 {
result = append(result, errors.NewFieldRequired("type", ""))
}
switch strategy.Type {
case deployapi.DeploymentStrategyTypeCustom:
if strategy.CustomParams == nil {
result = append(result, errors.NewFieldRequired("customParams", ""))
} else {
result = append(result, validateCustomParams(strategy.CustomParams).Prefix("customParams")...)
}
}
return result
}
func validateCustomParams(params *deployapi.CustomDeploymentStrategyParams) errors.ValidationErrorList {
result := errors.ValidationErrorList{}
if len(params.Image) == 0 {
result = append(result, errors.NewFieldRequired("image", ""))
}
return result
}
func validateTrigger(trigger *deployapi.DeploymentTriggerPolicy) errors.ValidationErrorList {
result := errors.ValidationErrorList{}
if len(trigger.Type) == 0 {
result = append(result, errors.NewFieldRequired("type", ""))
}
if trigger.Type == deployapi.DeploymentTriggerOnImageChange {
if trigger.ImageChangeParams == nil {
result = append(result, errors.NewFieldRequired("imageChangeParams", nil))
} else {
result = append(result, validateImageChangeParams(trigger.ImageChangeParams).Prefix("imageChangeParams")...)
}
}
return result
}
func validateImageChangeParams(params *deployapi.DeploymentTriggerImageChangeParams) errors.ValidationErrorList {
result := errors.ValidationErrorList{}
if len(params.RepositoryName) == 0 {
result = append(result, errors.NewFieldRequired("repositoryName", ""))
}
if len(params.ContainerNames) == 0 {
result = append(result, errors.NewFieldRequired("containerNames", ""))
}
return result
}