-
Notifications
You must be signed in to change notification settings - Fork 18
/
validation.go
200 lines (171 loc) · 6.82 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
// SPDX-FileCopyrightText: 2022 SAP SE or an SAP affiliate company and Open Component Model contributors.
//
// SPDX-License-Identifier: Apache-2.0
package v3alpha1
import (
"k8s.io/apimachinery/pkg/util/validation/field"
"github.com/open-component-model/ocm/v2/pkg/contexts/ocm/compdesc"
metav1 "github.com/open-component-model/ocm/v2/pkg/contexts/ocm/compdesc/meta/v1"
)
// Validate validates a parsed v2 component descriptor.
func (cd *ComponentDescriptor) Validate() error {
if err := Validate(nil, cd); err != nil {
return err.ToAggregate()
}
return nil
}
func Validate(fldPath *field.Path, component *ComponentDescriptor) field.ErrorList {
if component == nil {
return nil
}
allErrs := field.ErrorList{}
if len(component.APIVersion) == 0 {
allErrs = append(allErrs, field.Required(fldPath.Child("apiVersion"), "must specify a version"))
}
if len(component.Kind) == 0 {
allErrs = append(allErrs, field.Required(fldPath.Child("kind"), "must specify kind "+Kind))
}
if component.Kind != Kind {
allErrs = append(allErrs, field.Invalid(fldPath.Child("kind"), component.Kind, "must be "+Kind))
}
metaPath := fldPath.Child("metadata")
if err := validateProvider(metaPath.Child("provider"), component.Provider); err != nil {
allErrs = append(allErrs, err)
}
allErrs = append(allErrs, ValidateObjectMeta(metaPath, component)...)
specPath := fldPath.Child("spec")
srcPath := specPath.Child("sources")
allErrs = append(allErrs, ValidateSources(srcPath, component.Spec.Sources)...)
refPath := specPath.Child("references")
allErrs = append(allErrs, ValidateComponentReferences(refPath, component.Spec.References)...)
resourcePath := specPath.Child("resources")
allErrs = append(allErrs, ValidateResources(resourcePath, component.Spec.Resources, component.GetVersion())...)
return allErrs
}
// ValidateObjectMeta Validate the metadata of an object.
func ValidateObjectMeta(fldPath *field.Path, om compdesc.ObjectMetaAccessor) field.ErrorList {
allErrs := field.ErrorList{}
if len(om.GetName()) == 0 {
allErrs = append(allErrs, field.Required(fldPath.Child("name"), "must specify a name"))
}
if len(om.GetVersion()) == 0 {
allErrs = append(allErrs, field.Required(fldPath.Child("version"), "must specify a version"))
}
if len(om.GetLabels()) != 0 {
allErrs = append(allErrs, metav1.ValidateLabels(fldPath.Child("labels"), om.GetLabels())...)
}
return allErrs
}
// ValidateSources validates a list of sources.
// It makes sure that no duplicate sources are present.
func ValidateSources(fldPath *field.Path, sources Sources) field.ErrorList {
allErrs := field.ErrorList{}
sourceIDs := make(map[string]struct{})
for i, src := range sources {
srcPath := fldPath.Index(i)
allErrs = append(allErrs, ValidateSource(srcPath, src, false)...)
id := string(src.GetIdentityDigest(sources))
if _, ok := sourceIDs[id]; ok {
allErrs = append(allErrs, field.Duplicate(srcPath, "duplicate source"))
continue
}
sourceIDs[id] = struct{}{}
}
return allErrs
}
// ValidateSource validates the a component's source object.
func ValidateSource(fldPath *field.Path, src Source, access bool) field.ErrorList {
allErrs := field.ErrorList{}
if len(src.GetName()) == 0 {
allErrs = append(allErrs, field.Required(fldPath.Child("name"), "must specify a name"))
}
if len(src.GetType()) == 0 {
allErrs = append(allErrs, field.Required(fldPath.Child("type"), "must specify a type"))
}
if src.Access == nil && access {
allErrs = append(allErrs, field.Required(fldPath.Child("access"), "must specify a access"))
}
allErrs = append(allErrs, metav1.ValidateIdentity(fldPath.Child("extraIdentity"), src.ExtraIdentity)...)
return allErrs
}
// ValidateResource validates a components resource.
func ValidateResource(fldPath *field.Path, res Resource, access bool) field.ErrorList {
allErrs := field.ErrorList{}
allErrs = append(allErrs, ValidateObjectMeta(fldPath, &res)...)
if err := metav1.ValidateRelation(fldPath.Child("relation"), res.Relation); err != nil {
allErrs = append(allErrs, err)
}
if !metav1.IsIdentity(res.Name) {
allErrs = append(allErrs, field.Invalid(fldPath.Child("name"), res.Name, metav1.IdentityKeyValidationErrMsg))
}
if len(res.GetType()) == 0 {
allErrs = append(allErrs, field.Required(fldPath.Child("type"), "must specify a type"))
}
if res.Access == nil && access {
allErrs = append(allErrs, field.Required(fldPath.Child("access"), "must specify a access"))
}
allErrs = append(allErrs, metav1.ValidateIdentity(fldPath.Child("extraIdentity"), res.ExtraIdentity)...)
return allErrs
}
func validateProvider(fldPath *field.Path, provider metav1.Provider) *field.Error {
if len(provider.Name) == 0 {
return field.Required(fldPath.Child("name"), "provider name must be set")
}
return nil
}
// ValidateComponentReference validates a component version reference.
func ValidateComponentReference(fldPath *field.Path, cr Reference) field.ErrorList {
allErrs := field.ErrorList{}
if len(cr.ComponentName) == 0 {
allErrs = append(allErrs, field.Required(fldPath.Child("componentName"), "must specify a component name"))
}
allErrs = append(allErrs, ValidateObjectMeta(fldPath, &cr)...)
return allErrs
}
// ValidateComponentReferences validates a list of component version references.
// It makes sure that no duplicate sources are present.
func ValidateComponentReferences(fldPath *field.Path, refs References) field.ErrorList {
allErrs := field.ErrorList{}
refIDs := make(map[string]struct{})
for i, ref := range refs {
refPath := fldPath.Index(i)
allErrs = append(allErrs, ValidateComponentReference(refPath, ref)...)
id := string(ref.GetIdentityDigest(refs))
if _, ok := refIDs[id]; ok {
allErrs = append(allErrs, field.Duplicate(refPath, "duplicate component reference name"))
continue
}
refIDs[id] = struct{}{}
}
return allErrs
}
// ValidateResources validates a list of resources.
// It makes sure that no duplicate sources are present.
func ValidateResources(fldPath *field.Path, resources Resources, componentVersion string) field.ErrorList {
allErrs := field.ErrorList{}
resourceIDs := make(map[string]struct{})
for i, res := range resources {
localPath := fldPath.Index(i)
allErrs = append(allErrs, ValidateResource(localPath, res, true)...)
if err := ValidateSourceRefs(localPath.Child("sourceRef"), res.SourceRef); err != nil {
allErrs = append(allErrs, err...)
}
id := string(res.GetIdentityDigest(resources))
if _, ok := resourceIDs[id]; ok {
allErrs = append(allErrs, field.Duplicate(localPath, "duplicated resource"))
continue
}
resourceIDs[id] = struct{}{}
}
return allErrs
}
func ValidateSourceRefs(fldPath *field.Path, srcs []SourceRef) field.ErrorList {
allErrs := field.ErrorList{}
for i, src := range srcs {
localPath := fldPath.Index(i)
if err := metav1.ValidateLabels(localPath.Child("labels"), src.Labels); err != nil {
allErrs = append(allErrs, err...)
}
}
return allErrs
}