-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathvalidation.go
More file actions
173 lines (131 loc) · 6.3 KB
/
validation.go
File metadata and controls
173 lines (131 loc) · 6.3 KB
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
// SPDX-FileCopyrightText: SAP SE or an SAP affiliate company and Gardener contributors
//
// SPDX-License-Identifier: Apache-2.0
package validation
import (
"net/url"
"path"
"slices"
"strings"
"k8s.io/apimachinery/pkg/util/sets"
"k8s.io/apimachinery/pkg/util/validation/field"
configv1alpha1 "github.com/gardener/gardener-landscape-kit/pkg/apis/config/v1alpha1"
)
// ValidateLandscapeKitConfiguration validates the given LandscapeKitConfiguration.
func ValidateLandscapeKitConfiguration(conf *configv1alpha1.LandscapeKitConfiguration) field.ErrorList {
allErrs := field.ErrorList{}
if conf.OCM != nil {
allErrs = append(allErrs, ValidateOCMConfig(conf.OCM, field.NewPath("ocm"))...)
}
if conf.Git != nil {
allErrs = append(allErrs, validateGitRepository(conf.Git, field.NewPath("git"))...)
}
if conf.Components != nil {
allErrs = append(allErrs, validateComponentsConfiguration(conf.Components, field.NewPath("components"))...)
}
if conf.VersionConfig != nil {
allErrs = append(allErrs, ValidateVersionConfig(conf.VersionConfig, field.NewPath("versionConfig"))...)
}
if conf.MergeMode != nil && !slices.Contains(configv1alpha1.AllowedMergeModes, string(*conf.MergeMode)) {
allErrs = append(allErrs, field.NotSupported(field.NewPath("mergeMode"), *conf.MergeMode, configv1alpha1.AllowedMergeModes))
}
return allErrs
}
func validateComponentsConfiguration(compConf *configv1alpha1.ComponentsConfiguration, fldPath *field.Path) field.ErrorList {
allErrs := field.ErrorList{}
if len(compConf.Exclude) > 0 && len(compConf.Include) > 0 {
allErrs = append(allErrs, field.Forbidden(fldPath, "only one of 'exclude' or 'include' can be specified"))
}
foundComponents := sets.New[string]()
for i, comp := range compConf.Exclude {
if foundComponents.Has(comp) {
allErrs = append(allErrs, field.Duplicate(fldPath.Child("exclude").Index(i), comp))
}
foundComponents.Insert(comp)
}
foundComponents = sets.New[string]()
for i, comp := range compConf.Include {
if foundComponents.Has(comp) {
allErrs = append(allErrs, field.Duplicate(fldPath.Child("include").Index(i), comp))
}
foundComponents.Insert(comp)
}
return allErrs
}
func validateGitRepository(repo *configv1alpha1.GitRepository, fldPath *field.Path) field.ErrorList {
allErrs := field.ErrorList{}
u, err := url.Parse(repo.URL)
if err != nil {
allErrs = append(allErrs, field.Invalid(fldPath.Child("url"), repo.URL, "must be a valid URL"))
}
if u.Scheme != "https" && u.Scheme != "http" && u.Scheme != "ssh" {
allErrs = append(allErrs, field.Invalid(fldPath.Child("url"), repo.URL, "must have http(s) or ssh scheme"))
}
allErrs = append(allErrs, validateGitRepositoryRef(&repo.Ref, fldPath.Child("ref"))...)
allErrs = append(allErrs, validatePathConfiguration(&repo.Paths, fldPath.Child("paths"))...)
return allErrs
}
func validateGitRepositoryRef(ref *configv1alpha1.GitRepositoryRef, fldPath *field.Path) field.ErrorList {
allErrs := field.ErrorList{}
if ref.Branch != nil && strings.TrimSpace(*ref.Branch) == "" {
allErrs = append(allErrs, field.Invalid(fldPath.Child("branch"), *ref.Branch, "branch must not be empty"))
}
if ref.Tag != nil && strings.TrimSpace(*ref.Tag) == "" {
allErrs = append(allErrs, field.Invalid(fldPath.Child("tag"), *ref.Tag, "tag must not be empty"))
}
if ref.Commit != nil && strings.TrimSpace(*ref.Commit) == "" {
allErrs = append(allErrs, field.Invalid(fldPath.Child("commit"), *ref.Commit, "commit SHA must not be empty"))
}
return allErrs
}
func validatePathConfiguration(paths *configv1alpha1.PathConfiguration, fldPath *field.Path) field.ErrorList {
allErrs := field.ErrorList{}
if strings.TrimSpace(paths.Base) == "" {
allErrs = append(allErrs, field.Required(fldPath.Child("base"), "base path must be specified"))
}
if path.IsAbs(paths.Base) {
allErrs = append(allErrs, field.Invalid(fldPath.Child("base"), paths.Base, "base path must be a relative path to the base dir within the git repository"))
}
if strings.TrimSpace(paths.Landscape) == "" {
allErrs = append(allErrs, field.Required(fldPath.Child("landscape"), "landscape path must be specified"))
}
if path.IsAbs(paths.Landscape) {
allErrs = append(allErrs, field.Invalid(fldPath.Child("landscape"), paths.Landscape, "landscape path must be a relative path to the landscape dir within the git repository"))
}
return allErrs
}
// ValidateOCMConfig validates the given OCMConfig.
func ValidateOCMConfig(conf *configv1alpha1.OCMConfig, fldPath *field.Path) field.ErrorList {
allErrs := field.ErrorList{}
allErrs = append(allErrs, validateOCMComponent(conf.RootComponent, fldPath.Child("rootComponent"))...)
if len(conf.Repositories) == 0 {
allErrs = append(allErrs, field.Required(fldPath.Child("repositories"), "at least one OCI repository must be specified in config file"))
}
for i, repo := range conf.Repositories {
repoURL, err := url.Parse(repo)
if err != nil || (repoURL != nil && len(repoURL.Host) == 0) {
allErrs = append(allErrs, field.Invalid(fldPath.Child("repositories").Index(i), repo, "must be a valid URL"))
}
}
return allErrs
}
func validateOCMComponent(conf configv1alpha1.OCMComponent, fldPath *field.Path) field.ErrorList {
allErrs := field.ErrorList{}
if strings.TrimSpace(conf.Name) == "" {
allErrs = append(allErrs, field.Required(fldPath.Child("name"), "component name is required in config file"))
} else if len(strings.Split(conf.Name, "/")) == 1 {
allErrs = append(allErrs, field.Invalid(fldPath.Child("name"), conf.Name, "component name must be qualified (format 'example.com/my-org/my-root-component:1.23.4')"))
}
if strings.TrimSpace(conf.Version) == "" {
allErrs = append(allErrs, field.Required(fldPath.Child("version"), "component version is required in config file"))
}
return allErrs
}
// ValidateVersionConfig validates the given VersionConfiguration.
func ValidateVersionConfig(conf *configv1alpha1.VersionConfiguration, fldPath *field.Path) field.ErrorList {
allErrs := field.ErrorList{}
if conf.DefaultVersionsUpdateStrategy != nil && !slices.Contains(configv1alpha1.AllowedDefaultVersionsUpdateStrategies, string(*conf.DefaultVersionsUpdateStrategy)) {
allErrs = append(allErrs, field.Invalid(fldPath.Child("defaultVersionsUpdateStrategy"), *conf.DefaultVersionsUpdateStrategy, "allowed values are: "+strings.Join(configv1alpha1.AllowedDefaultVersionsUpdateStrategies, ", ")))
}
return allErrs
}