forked from openshift/origin
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathvalidation.go
55 lines (49 loc) · 2.47 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
package validation
import (
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/util/sets"
"k8s.io/apimachinery/pkg/util/validation/field"
"github.com/openshift/origin/pkg/image/admission/apis/imagepolicy"
)
func Validate(config *imagepolicy.ImagePolicyConfig) field.ErrorList {
allErrs := field.ErrorList{}
if config == nil {
return allErrs
}
names := sets.NewString()
for i, rule := range config.ExecutionRules {
if names.Has(rule.Name) {
allErrs = append(allErrs, field.Duplicate(field.NewPath(imagepolicy.PluginName, "executionRules").Index(i).Child("name"), rule.Name))
}
names.Insert(rule.Name)
for j, selector := range rule.MatchImageLabels {
_, err := metav1.LabelSelectorAsSelector(&selector)
if err != nil {
allErrs = append(allErrs, field.Invalid(field.NewPath(imagepolicy.PluginName, "executionRules").Index(i).Child("matchImageLabels").Index(j), nil, err.Error()))
}
}
}
for i, rule := range config.ResolutionRules {
if len(rule.Policy) == 0 {
allErrs = append(allErrs, field.Required(field.NewPath(imagepolicy.PluginName, "resolutionRules").Index(i).Child("policy"), "a policy must be specified for this resource"))
}
if len(rule.TargetResource.Resource) == 0 {
allErrs = append(allErrs, field.Required(field.NewPath(imagepolicy.PluginName, "resolutionRules").Index(i).Child("targetResource", "resource"), "a target resource name or '*' must be provided"))
}
}
// if you don't attempt resolution, you'll never be able to pass any rule that logically requires it
if config.ResolveImages == imagepolicy.DoNotAttempt {
for i, rule := range config.ExecutionRules {
if len(rule.MatchDockerImageLabels) > 0 {
allErrs = append(allErrs, field.Invalid(field.NewPath(imagepolicy.PluginName, "executionRules").Index(i).Child("matchDockerImageLabels"), rule.MatchDockerImageLabels, "images are not being resolved, this condition will always fail"))
}
if len(rule.MatchImageLabels) > 0 {
allErrs = append(allErrs, field.Invalid(field.NewPath(imagepolicy.PluginName, "executionRules").Index(i).Child("matchImageLabels"), rule.MatchImageLabels, "images are not being resolved, this condition will always fail"))
}
if len(rule.MatchImageAnnotations) > 0 {
allErrs = append(allErrs, field.Invalid(field.NewPath(imagepolicy.PluginName, "executionRules").Index(i).Child("matchImageAnnotations"), rule.MatchImageAnnotations, "images are not being resolved, this condition will always fail"))
}
}
}
return allErrs
}