|
| 1 | +package internal |
| 2 | + |
| 3 | +import ( |
| 4 | + "fmt" |
| 5 | + "net/mail" |
| 6 | + "net/url" |
| 7 | + "strings" |
| 8 | + |
| 9 | + "github.com/operator-framework/api/pkg/manifests" |
| 10 | + "github.com/operator-framework/api/pkg/operators/v1alpha1" |
| 11 | + "github.com/operator-framework/api/pkg/validation/errors" |
| 12 | + interfaces "github.com/operator-framework/api/pkg/validation/interfaces" |
| 13 | +) |
| 14 | + |
| 15 | +var OperatorHubValidator interfaces.Validator = interfaces.ValidatorFunc(validateOperatorHub) |
| 16 | + |
| 17 | +var validCapabilities = map[string]struct{}{ |
| 18 | + "Basic Install": struct{}{}, |
| 19 | + "Seamless Upgrades": struct{}{}, |
| 20 | + "Full Lifecycle": struct{}{}, |
| 21 | + "Deep Insights": struct{}{}, |
| 22 | + "Auto Pilot": struct{}{}, |
| 23 | +} |
| 24 | + |
| 25 | +var validMediatypes = map[string]struct{}{ |
| 26 | + "image/gif": struct{}{}, |
| 27 | + "image/jpeg": struct{}{}, |
| 28 | + "image/png": struct{}{}, |
| 29 | + "image/svg+xml": struct{}{}, |
| 30 | +} |
| 31 | + |
| 32 | +var validCategories = map[string]struct{}{ |
| 33 | + "AI/Machine Learning": struct{}{}, |
| 34 | + "Application Runtime": struct{}{}, |
| 35 | + "Big Data": struct{}{}, |
| 36 | + "Cloud Provider": struct{}{}, |
| 37 | + "Developer Tools": struct{}{}, |
| 38 | + "Database": struct{}{}, |
| 39 | + "Integration & Delivery": struct{}{}, |
| 40 | + "Logging & Tracing": struct{}{}, |
| 41 | + "Monitoring": struct{}{}, |
| 42 | + "Networking": struct{}{}, |
| 43 | + "OpenShift Optional": struct{}{}, |
| 44 | + "Security": struct{}{}, |
| 45 | + "Storage": struct{}{}, |
| 46 | + "Streaming & Messaging": struct{}{}, |
| 47 | +} |
| 48 | + |
| 49 | +func validateOperatorHub(objs ...interface{}) (results []errors.ManifestResult) { |
| 50 | + for _, obj := range objs { |
| 51 | + switch v := obj.(type) { |
| 52 | + case *manifests.Bundle: |
| 53 | + results = append(results, validateBundleOperatorHub(v)) |
| 54 | + } |
| 55 | + } |
| 56 | + return results |
| 57 | +} |
| 58 | + |
| 59 | +func validateBundleOperatorHub(bundle *manifests.Bundle) errors.ManifestResult { |
| 60 | + result := errors.ManifestResult{Name: bundle.Name} |
| 61 | + |
| 62 | + if bundle == nil { |
| 63 | + result.Add(errors.ErrInvalidBundle("Bundle is nil", nil)) |
| 64 | + return result |
| 65 | + } |
| 66 | + |
| 67 | + if bundle.CSV == nil { |
| 68 | + result.Add(errors.ErrInvalidBundle("Bundle csv is nil", bundle.Name)) |
| 69 | + return result |
| 70 | + } |
| 71 | + |
| 72 | + errs := validateHubCSVSpec(*bundle.CSV) |
| 73 | + for _, err := range errs { |
| 74 | + result.Add(errors.ErrInvalidCSV(err.Error(), bundle.CSV.GetName())) |
| 75 | + } |
| 76 | + |
| 77 | + return result |
| 78 | +} |
| 79 | + |
| 80 | +func validateHubCSVSpec(csv v1alpha1.ClusterServiceVersion) []error { |
| 81 | + var errs []error |
| 82 | + |
| 83 | + if csv.Spec.Provider.Name == "" { |
| 84 | + errs = append(errs, fmt.Errorf("csv.Spec.Provider.Name not specified")) |
| 85 | + } |
| 86 | + |
| 87 | + for _, maintainer := range csv.Spec.Maintainers { |
| 88 | + if maintainer.Name == "" || maintainer.Email == "" { |
| 89 | + errs = append(errs, fmt.Errorf("csv.Spec.Maintainers elements should contain both name and email")) |
| 90 | + } |
| 91 | + if maintainer.Email != "" { |
| 92 | + _, err := mail.ParseAddress(maintainer.Email) |
| 93 | + if err != nil { |
| 94 | + errs = append(errs, fmt.Errorf("csv.Spec.Maintainers email %s is invalid: %v", maintainer.Email, err)) |
| 95 | + } |
| 96 | + } |
| 97 | + } |
| 98 | + |
| 99 | + for _, link := range csv.Spec.Links { |
| 100 | + if link.Name == "" || link.URL == "" { |
| 101 | + errs = append(errs, fmt.Errorf("csv.Spec.Links elements should contain both name and url")) |
| 102 | + } |
| 103 | + if link.URL != "" { |
| 104 | + _, err := url.ParseRequestURI(link.URL) |
| 105 | + if err != nil { |
| 106 | + errs = append(errs, fmt.Errorf("csv.Spec.Links url %s is invalid: %v", link.URL, err)) |
| 107 | + } |
| 108 | + } |
| 109 | + } |
| 110 | + |
| 111 | + if csv.GetAnnotations() == nil { |
| 112 | + csv.SetAnnotations(make(map[string]string)) |
| 113 | + } |
| 114 | + |
| 115 | + if capability, ok := csv.ObjectMeta.Annotations["capabilities"]; ok { |
| 116 | + if _, ok := validCapabilities[capability]; !ok { |
| 117 | + errs = append(errs, fmt.Errorf("csv.Metadata.Annotations.Capabilities %s is not a valid capabilities level", capability)) |
| 118 | + } |
| 119 | + } |
| 120 | + |
| 121 | + if csv.Spec.Icon != nil { |
| 122 | + // only one icon is allowed |
| 123 | + if len(csv.Spec.Icon) != 1 { |
| 124 | + errs = append(errs, fmt.Errorf("csv.Spec.Icon should only have one element")) |
| 125 | + } |
| 126 | + |
| 127 | + icon := csv.Spec.Icon[0] |
| 128 | + if icon.MediaType == "" || icon.Data == "" { |
| 129 | + errs = append(errs, fmt.Errorf("csv.Spec.Icon elements should contain both data and mediatype")) |
| 130 | + } |
| 131 | + |
| 132 | + if icon.MediaType != "" { |
| 133 | + if _, ok := validMediatypes[icon.MediaType]; !ok { |
| 134 | + errs = append(errs, fmt.Errorf("csv.Spec.Icon %s does not have a valid mediatype", icon.MediaType)) |
| 135 | + } |
| 136 | + } |
| 137 | + } else { |
| 138 | + errs = append(errs, fmt.Errorf("csv.Spec.Icon not specified")) |
| 139 | + } |
| 140 | + |
| 141 | + if categories, ok := csv.ObjectMeta.Annotations["categories"]; ok { |
| 142 | + categorySlice := strings.Split(categories, ",") |
| 143 | + |
| 144 | + for _, category := range categorySlice { |
| 145 | + if _, ok := validCategories[category]; !ok { |
| 146 | + errs = append(errs, fmt.Errorf("csv.Metadata.Annotations.Categories %s is not a valid category", category)) |
| 147 | + } |
| 148 | + } |
| 149 | + } |
| 150 | + |
| 151 | + return errs |
| 152 | +} |
0 commit comments