-
Notifications
You must be signed in to change notification settings - Fork 77
/
Copy pathoperatorhub.go
424 lines (381 loc) · 13.6 KB
/
operatorhub.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
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
package internal
import (
"encoding/json"
"fmt"
"io/ioutil"
"net/mail"
"net/url"
"os"
"path/filepath"
"regexp"
"strings"
"github.com/blang/semver/v4"
"github.com/operator-framework/api/pkg/manifests"
"github.com/operator-framework/api/pkg/operators/v1alpha1"
"github.com/operator-framework/api/pkg/validation/errors"
interfaces "github.com/operator-framework/api/pkg/validation/interfaces"
)
// OperatorHubValidator validates the bundle manifests against the required criteria to publish
// the projects on OperatorHub.io.
//
// This validator will ensure that:
//
// - The annotations capabilities into the CSV has a valid option, which are:
//
// * Basic Install
//
// * Seamless Upgrades
//
// * Full Lifecycle
//
// * Deep Insights
//
// * Auto Pilot
//
// - The annotations categories into the CSV has a valid option, which are:
//
// * AI/Machine Learning
//
// * Application Runtime
//
// * Big Data
//
// * Cloud Provider
//
// * Developer Tools
//
// * Database
//
// * Integration & Delivery
//
// * Logging & Tracing
//
// * Modernization & Migration
//
// * Monitoring
//
// * Networking
//
// * OpenShift Optional
//
// * Security
//
// * Storage
//
// * Streaming & Messaging
//
// NOTE: The OperatorHub validator can verify against custom bundle categories by setting the OPERATOR_BUNDLE_CATEGORIES
// environment variable. Setting the OPERATOR_BUNDLE_CATEGORIES environment variable to the path to a json file
// containing a list of categories will enable those categories to be used when comparing CSV categories for
// OperatorHub validation. The json file should be in the following format:
//
// ```json
// {
// "categories":[
// "Cloud Pak",
// "Registry",
// "MyCoolThing",
// ]
// }
// ```
//
// - The `csv.Spec.Provider.Name` was provided
//
// - The `csv.Spec.Maintainers` elements contains both name and email
//
// - The `csv.Spec.Links` elements contains both name and url
//
// - The `csv.Spec.Links.Url` is a valid value
//
// - The `csv.Spec.Version` is provided
//
// - The `csv.Spec.Icon` was provided and has not more than one element
//
// - The `csv.Spec.Icon` elements should contain both data and `mediatype`
//
// - The `csv.Spec.Icon` elements should contain both data and `mediatype`
//
// - The `csv.Spec.Icon` has a valid `mediatype`, which are
//
// * image/gif
//
// * image/jpeg
//
// * image/png
//
// * image/svg+xml
//
// - If informed ONLY, check if the value csv.Spec.MinKubeVersion is parsable according to semver (https://semver.org/)
// Also, this validator will raise warnings when:
//
// - The bundle name (CSV.metadata.name) does not follow the naming convention: <operator-name>.v<semver> e.g. memcached-operator.v0.0.1
//
// NOTE: The bundle name must be 63 characters or less because it will be used as k8s ownerref label which only allows max of 63 characters.
//
// - The channel names seems are not following the convention https://olm.operatorframework.io/docs/best-practices/channel-naming/
//
// - The usage of the removed APIs on Kubernetes 1.22 is found. More info: https://kubernetes.io/docs/reference/using-api/deprecation-guide/#v1-22
//
// Note that this validator allows to receive a List of optional values as key=values. Currently, only the
// `k8s-version` key is allowed. If informed, it will perform the checks against this specific Kubernetes version where the
// operator bundle is intend to be used and will raise errors instead of warnings.
// Currently, this check is capable of verifying the removed APIs only for Kubernetes 1.22 version.
var OperatorHubValidator interfaces.Validator = interfaces.ValidatorFunc(validateOperatorHub)
var validCapabilities = map[string]struct{}{
"Basic Install": {},
"Seamless Upgrades": {},
"Full Lifecycle": {},
"Deep Insights": {},
"Auto Pilot": {},
}
var validMediatypes = map[string]struct{}{
"image/gif": {},
"image/jpeg": {},
"image/png": {},
"image/svg+xml": {},
}
var validCategories = map[string]struct{}{
"AI/Machine Learning": {},
"Application Runtime": {},
"Big Data": {},
"Cloud Provider": {},
"Developer Tools": {},
"Database": {},
"Integration & Delivery": {},
"Logging & Tracing": {},
"Monitoring": {},
"Modernization & Migration": {},
"Networking": {},
"OpenShift Optional": {},
"Security": {},
"Storage": {},
"Streaming & Messaging": {},
}
const minKubeVersionWarnMessage = "csv.Spec.minKubeVersion is not informed. It is recommended you provide this information. " +
"Otherwise, it would mean that your operator project can be distributed and installed in any cluster version " +
"available, which is not necessarily the case for all projects."
func validateOperatorHub(objs ...interface{}) (results []errors.ManifestResult) {
// Obtain the k8s version if informed via the objects an optional
k8sVersion := ""
for _, obj := range objs {
switch obj.(type) {
case map[string]string:
k8sVersion = obj.(map[string]string)[k8sVersionKey]
if len(k8sVersion) > 0 {
break
}
}
}
for _, obj := range objs {
switch v := obj.(type) {
case *manifests.Bundle:
results = append(results, validateBundleOperatorHub(v, k8sVersion))
}
}
return results
}
func validateBundleOperatorHub(bundle *manifests.Bundle, k8sVersion string) errors.ManifestResult {
result := errors.ManifestResult{Name: bundle.Name}
if bundle == nil {
result.Add(errors.ErrInvalidBundle("Bundle is nil", nil))
return result
}
if bundle.CSV == nil {
result.Add(errors.ErrInvalidBundle("Bundle csv is nil", bundle.Name))
return result
}
csvChecksResult := validateHubCSVSpec(*bundle.CSV)
for _, err := range csvChecksResult.errs {
result.Add(errors.ErrInvalidCSV(err.Error(), bundle.CSV.GetName()))
}
for _, warn := range csvChecksResult.warns {
result.Add(errors.WarnInvalidCSV(warn.Error(), bundle.CSV.GetName()))
}
errs, warns := validateDeprecatedAPIS(bundle, k8sVersion)
for _, err := range errs {
result.Add(errors.ErrFailedValidation(err.Error(), bundle.CSV.GetName()))
}
for _, warn := range warns {
result.Add(errors.WarnFailedValidation(warn.Error(), bundle.CSV.GetName()))
}
return result
}
// validateHubCSVSpec will check the CSV against the criteria to publish an
// operator bundle in the OperatorHub.io
func validateHubCSVSpec(csv v1alpha1.ClusterServiceVersion) CSVChecks {
checks := CSVChecks{csv: csv, errs: []error{}, warns: []error{}}
checks = checkSpecProviderName(checks)
checks = checkSpecMaintainers(checks)
checks = checkSpecLinks(checks)
checks = checkAnnotations(checks)
checks = checkSpecVersion(checks)
checks = checkSpecIcon(checks)
checks = checkSpecMinKubeVersion(checks)
checks = checkBundleName(checks)
return checks
}
type CSVChecks struct {
csv v1alpha1.ClusterServiceVersion
errs []error
warns []error
}
// checkBundleName will validate the operator bundle name informed via CSV.metadata.name.
// The motivation for the following check is to ensure that operators authors knows that operator bundles names should
// follow a name and versioning convention
func checkBundleName(checks CSVChecks) CSVChecks {
// Check if is following the semver
re := regexp.MustCompile("([0-9]+)\\.([0-9]+)\\.([0-9]+)(?:-([0-9A-Za-z-]+(?:\\.[0-9A-Za-z-]+)*))?(?:\\+[0-9A-Za-z-]+)?$")
match := re.FindStringSubmatch(checks.csv.Name)
if len(match) > 0 {
if _, err := semver.Parse(match[0]); err != nil {
checks.warns = append(checks.warns, fmt.Errorf("csv.metadata.Name %v is not following the versioning "+
"convention (MAJOR.MINOR.PATCH e.g 0.0.1): https://semver.org/", checks.csv.Name))
}
} else {
checks.warns = append(checks.warns, fmt.Errorf("csv.metadata.Name %v is not following the versioning "+
"convention (MAJOR.MINOR.PATCH e.g 0.0.1): https://semver.org/", checks.csv.Name))
}
// Check if its following the name convention
if len(strings.Split(checks.csv.Name, ".v")) < 2 {
checks.warns = append(checks.errs, fmt.Errorf("csv.metadata.Name %v is not following the recommended "+
"naming convention: <operator-name>.v<semver> e.g. memcached-operator.v0.0.1", checks.csv.Name))
}
return checks
}
// checkSpecMinKubeVersion will validate the spec minKubeVersion informed via CSV.spec.minKubeVersion
func checkSpecMinKubeVersion(checks CSVChecks) CSVChecks {
if len(strings.TrimSpace(checks.csv.Spec.MinKubeVersion)) == 0 {
checks.warns = append(checks.warns, fmt.Errorf(minKubeVersionWarnMessage))
} else {
if _, err := semver.ParseTolerant(checks.csv.Spec.MinKubeVersion); err != nil {
checks.errs = append(checks.errs, fmt.Errorf("csv.Spec.MinKubeVersion has an invalid value: %s", checks.csv.Spec.MinKubeVersion))
}
}
return checks
}
// checkSpecVersion will validate the spec Version informed via CSV.spec.Version
func checkSpecVersion(checks CSVChecks) CSVChecks {
// spec.Version needs to be set
if checks.csv.Spec.Version.Equals(semver.Version{}) {
checks.errs = append(checks.errs, fmt.Errorf("csv.Spec.Version must be set"))
}
return checks
}
// checkAnnotations will validate the values informed via annotations such as; capabilities and categories
func checkAnnotations(checks CSVChecks) CSVChecks {
if checks.csv.GetAnnotations() == nil {
checks.csv.SetAnnotations(make(map[string]string))
}
if capability, ok := checks.csv.ObjectMeta.Annotations["capabilities"]; ok {
if _, ok := validCapabilities[capability]; !ok {
checks.errs = append(checks.errs, fmt.Errorf("csv.Metadata.Annotations.Capabilities %s is not a valid capabilities level", capability))
}
}
if categories, ok := checks.csv.ObjectMeta.Annotations["categories"]; ok {
categorySlice := strings.Split(categories, ",")
// use custom categories for validation if provided
customCategoriesPath := os.Getenv("OPERATOR_BUNDLE_CATEGORIES")
if customCategoriesPath != "" {
customCategories, err := extractCategories(customCategoriesPath)
if err != nil {
checks.errs = append(checks.errs, fmt.Errorf("could not extract custom categories from categories %#v: %s", customCategories, err))
} else {
for _, category := range categorySlice {
if _, ok := customCategories[strings.TrimSpace(category)]; !ok {
checks.errs = append(checks.errs, fmt.Errorf("csv.Metadata.Annotations[\"categories\"] value %s is not in the set of custom categories", category))
}
}
}
} else {
// use default categories
for _, category := range categorySlice {
if _, ok := validCategories[strings.TrimSpace(category)]; !ok {
checks.errs = append(checks.errs, fmt.Errorf("csv.Metadata.Annotations[\"categories\"] value %s is not in the set of default categories", category))
}
}
}
}
return checks
}
// checkSpecIcon will validate if the CSV.spec.Icon was informed and is correct
func checkSpecIcon(checks CSVChecks) CSVChecks {
if checks.csv.Spec.Icon != nil {
// only one icon is allowed
if len(checks.csv.Spec.Icon) != 1 {
checks.errs = append(checks.errs, fmt.Errorf("csv.Spec.Icon should only have one element"))
}
icon := checks.csv.Spec.Icon[0]
if icon.MediaType == "" || icon.Data == "" {
checks.errs = append(checks.errs, fmt.Errorf("csv.Spec.Icon elements should contain both data and mediatype"))
}
if icon.MediaType != "" {
if _, ok := validMediatypes[icon.MediaType]; !ok {
checks.errs = append(checks.errs, fmt.Errorf("csv.Spec.Icon %s does not have a valid mediatype", icon.MediaType))
}
}
} else {
checks.warns = append(checks.warns, fmt.Errorf("csv.Spec.Icon not specified"))
}
return checks
}
// checkSpecLinks will validate the value informed via csv.Spec.Links
func checkSpecLinks(checks CSVChecks) CSVChecks {
for _, link := range checks.csv.Spec.Links {
if link.Name == "" || link.URL == "" {
checks.errs = append(checks.errs, fmt.Errorf("csv.Spec.Links elements should contain both name and url"))
}
if link.URL != "" {
_, err := url.ParseRequestURI(link.URL)
if err != nil {
checks.errs = append(checks.errs, fmt.Errorf("csv.Spec.Links url %s is invalid: %v", link.URL, err))
}
}
}
return checks
}
// checkSpecMaintainers will validate the values informed via csv.Spec.Maintainers
func checkSpecMaintainers(checks CSVChecks) CSVChecks {
for _, maintainer := range checks.csv.Spec.Maintainers {
if maintainer.Name == "" || maintainer.Email == "" {
checks.errs = append(checks.errs, fmt.Errorf("csv.Spec.Maintainers elements should contain both name and email"))
}
if maintainer.Email != "" {
_, err := mail.ParseAddress(maintainer.Email)
if err != nil {
checks.errs = append(checks.errs, fmt.Errorf("csv.Spec.Maintainers email %s is invalid: %v", maintainer.Email, err))
}
}
}
return checks
}
// checkSpecProviderName will validate the values informed via csv.Spec.Provider.Name
func checkSpecProviderName(checks CSVChecks) CSVChecks {
if checks.csv.Spec.Provider.Name == "" {
checks.errs = append(checks.errs, fmt.Errorf("csv.Spec.Provider.Name not specified"))
}
return checks
}
type categories struct {
Contents []string `json:"categories"`
}
// extractCategories reads a custom categories file and returns the contents in a map[string]struct{}
func extractCategories(path string) (map[string]struct{}, error) {
path, err := filepath.Abs(path)
if err != nil {
return nil, fmt.Errorf("finding category file: %w", err)
}
data, err := ioutil.ReadFile(path)
if err != nil {
return nil, fmt.Errorf("reading category file: %w", err)
}
cat := categories{}
err = json.Unmarshal(data, &cat)
if err != nil {
return nil, fmt.Errorf("unmarshaling category file: %w", err)
}
customCategories := make(map[string]struct{})
for _, c := range cat.Contents {
customCategories[c] = struct{}{}
}
return customCategories, nil
}