forked from gardener/gardener
-
Notifications
You must be signed in to change notification settings - Fork 0
/
imagevector_validation.go
96 lines (78 loc) · 3.49 KB
/
imagevector_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
// Copyright (c) 2021 SAP SE or an SAP affiliate company. All rights reserved. This file is licensed under the Apache Software License, v. 2 except as noted otherwise in the LICENSE file
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package imagevector
import (
"strings"
"github.com/Masterminds/semver"
"k8s.io/apimachinery/pkg/util/validation/field"
)
// ValidateImageVector validates the given ImageVector.
func ValidateImageVector(imageVector ImageVector, fldPath *field.Path) field.ErrorList {
allErrs := field.ErrorList{}
for i, imageSource := range imageVector {
allErrs = append(allErrs, validateImageSource(imageSource, fldPath.Index(i))...)
}
return allErrs
}
// ValidateComponentImageVectors validates the given ComponentImageVectors.
func ValidateComponentImageVectors(componentImageVectors ComponentImageVectors, fldPath *field.Path) field.ErrorList {
allErrs := field.ErrorList{}
for key, value := range componentImageVectors {
componentImageVector := &ComponentImageVector{
Name: key,
ImageVectorOverwrite: value,
}
allErrs = append(allErrs, validateComponentImageVector(componentImageVector, fldPath.Key(key))...)
}
return allErrs
}
func validateImageSource(imageSource *ImageSource, fldPath *field.Path) field.ErrorList {
allErrs := field.ErrorList{}
// Ensure name and repository are non-empty
if imageSource.Name == "" {
allErrs = append(allErrs, field.Required(fldPath.Child("name"), "image name is required"))
}
if imageSource.Repository == "" {
allErrs = append(allErrs, field.Required(fldPath.Child("repository"), "image repository is required"))
}
// Ensure tag is non-empty if specified
if imageSource.Tag != nil && *imageSource.Tag == "" {
allErrs = append(allErrs, field.Invalid(fldPath.Child("tag"), *imageSource.Tag, "image tag must not be empty if specified"))
}
// Ensure runtimeVersion and targetVersion are valid semver constraints if specified
if imageSource.RuntimeVersion != nil {
if _, err := semver.NewConstraint(*imageSource.RuntimeVersion); err != nil {
allErrs = append(allErrs, field.Invalid(fldPath.Child("runtimeVersion"), imageSource.RuntimeVersion, err.Error()))
}
}
if imageSource.TargetVersion != nil {
if _, err := semver.NewConstraint(*imageSource.TargetVersion); err != nil {
allErrs = append(allErrs, field.Invalid(fldPath.Child("targetVersion"), imageSource.TargetVersion, err.Error()))
}
}
return allErrs
}
func validateComponentImageVector(componentImageVector *ComponentImageVector, fldPath *field.Path) field.ErrorList {
allErrs := field.ErrorList{}
// Ensure name is non-empty
if componentImageVector.Name == "" {
allErrs = append(allErrs, field.Required(fldPath.Child("name"), "component name is required"))
}
// Read (and validate) imageVectorOverwrite as image vector
imageVector, err := Read(strings.NewReader(componentImageVector.ImageVectorOverwrite))
if err != nil {
allErrs = append(allErrs, field.Invalid(fldPath.Child("imageVectorOverwrite"), imageVector, err.Error()))
}
return allErrs
}