forked from openshift/origin
-
Notifications
You must be signed in to change notification settings - Fork 0
/
validation.go
93 lines (70 loc) · 3.01 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
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.ErrorList {
result := validateDeploymentStrategy(&deployment.Strategy).Prefix("strategy")
controllerStateErrors := validation.ValidateReplicationControllerState(&deployment.ControllerTemplate)
result = append(result, controllerStateErrors.Prefix("controllerTemplate")...)
return result
}
func validateDeploymentStrategy(strategy *deployapi.DeploymentStrategy) errors.ErrorList {
result := errors.ErrorList{}
if len(strategy.Type) == 0 {
result = append(result, errors.NewFieldRequired("type", ""))
}
if strategy.Type == deployapi.DeploymentStrategyTypeCustomPod {
if strategy.CustomPod == nil {
result = append(result, errors.NewFieldRequired("customPod", nil))
} else {
result = append(result, validateCustomPodStrategy(strategy.CustomPod).Prefix("customPod")...)
}
}
return result
}
func validateCustomPodStrategy(customPod *deployapi.CustomPodDeploymentStrategy) errors.ErrorList {
result := errors.ErrorList{}
if len(customPod.Image) == 0 {
result = append(result, errors.NewFieldRequired("image", ""))
}
return result
}
func validateTrigger(trigger *deployapi.DeploymentTriggerPolicy) errors.ErrorList {
result := errors.ErrorList{}
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.ErrorList {
result := errors.ErrorList{}
if len(params.RepositoryName) == 0 {
result = append(result, errors.NewFieldRequired("repositoryName", ""))
}
if len(params.ContainerNames) == 0 {
result = append(result, errors.NewFieldRequired("containerNames", ""))
}
return result
}
func ValidateDeploymentConfig(config *deployapi.DeploymentConfig) errors.ErrorList {
result := errors.ErrorList{}
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.ValidateReplicationControllerState(&config.Template.ControllerTemplate)
result = append(result, controllerStateErrors.Prefix("template.controllerTemplate")...)
return result
}