-
Notifications
You must be signed in to change notification settings - Fork 78
OperatorHub io validator #42
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,152 @@ | ||
package internal | ||
|
||
import ( | ||
"fmt" | ||
"net/mail" | ||
"net/url" | ||
"strings" | ||
|
||
"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" | ||
) | ||
|
||
var OperatorHubValidator interfaces.Validator = interfaces.ValidatorFunc(validateOperatorHub) | ||
|
||
var validCapabilities = map[string]struct{}{ | ||
"Basic Install": struct{}{}, | ||
"Seamless Upgrades": struct{}{}, | ||
"Full Lifecycle": struct{}{}, | ||
"Deep Insights": struct{}{}, | ||
"Auto Pilot": struct{}{}, | ||
} | ||
|
||
var validMediatypes = map[string]struct{}{ | ||
"image/gif": struct{}{}, | ||
"image/jpeg": struct{}{}, | ||
"image/png": struct{}{}, | ||
"image/svg+xml": struct{}{}, | ||
} | ||
|
||
var validCategories = map[string]struct{}{ | ||
"AI/Machine Learning": struct{}{}, | ||
"Application Runtime": struct{}{}, | ||
"Big Data": struct{}{}, | ||
"Cloud Provider": struct{}{}, | ||
"Developer Tools": struct{}{}, | ||
"Database": struct{}{}, | ||
"Integration & Delivery": struct{}{}, | ||
"Logging & Tracing": struct{}{}, | ||
"Monitoring": struct{}{}, | ||
"Networking": struct{}{}, | ||
"OpenShift Optional": struct{}{}, | ||
"Security": struct{}{}, | ||
"Storage": struct{}{}, | ||
"Streaming & Messaging": struct{}{}, | ||
} | ||
|
||
func validateOperatorHub(objs ...interface{}) (results []errors.ManifestResult) { | ||
for _, obj := range objs { | ||
switch v := obj.(type) { | ||
case *manifests.Bundle: | ||
results = append(results, validateBundleOperatorHub(v)) | ||
} | ||
} | ||
return results | ||
} | ||
|
||
func validateBundleOperatorHub(bundle *manifests.Bundle) 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)) | ||
dinhxuanvu marked this conversation as resolved.
Show resolved
Hide resolved
|
||
return result | ||
} | ||
|
||
errs := validateHubCSVSpec(*bundle.CSV) | ||
for _, err := range errs { | ||
result.Add(errors.ErrInvalidCSV(err.Error(), bundle.CSV.GetName())) | ||
} | ||
|
||
return result | ||
} | ||
|
||
func validateHubCSVSpec(csv v1alpha1.ClusterServiceVersion) []error { | ||
var errs []error | ||
|
||
if csv.Spec.Provider.Name == "" { | ||
errs = append(errs, fmt.Errorf("csv.Spec.Provider.Name not specified")) | ||
} | ||
|
||
for _, maintainer := range csv.Spec.Maintainers { | ||
if maintainer.Name == "" || maintainer.Email == "" { | ||
errs = append(errs, fmt.Errorf("csv.Spec.Maintainers elements should contain both name and email")) | ||
} | ||
if maintainer.Email != "" { | ||
_, err := mail.ParseAddress(maintainer.Email) | ||
if err != nil { | ||
errs = append(errs, fmt.Errorf("csv.Spec.Maintainers email %s is invalid: %v", maintainer.Email, err)) | ||
} | ||
} | ||
} | ||
|
||
for _, link := range csv.Spec.Links { | ||
if link.Name == "" || link.URL == "" { | ||
errs = append(errs, fmt.Errorf("csv.Spec.Links elements should contain both name and url")) | ||
} | ||
if link.URL != "" { | ||
_, err := url.ParseRequestURI(link.URL) | ||
if err != nil { | ||
errs = append(errs, fmt.Errorf("csv.Spec.Links url %s is invalid: %v", link.URL, err)) | ||
} | ||
} | ||
} | ||
|
||
dinhxuanvu marked this conversation as resolved.
Show resolved
Hide resolved
|
||
if csv.GetAnnotations() == nil { | ||
csv.SetAnnotations(make(map[string]string)) | ||
} | ||
|
||
if capability, ok := csv.ObjectMeta.Annotations["capabilities"]; ok { | ||
if _, ok := validCapabilities[capability]; !ok { | ||
errs = append(errs, fmt.Errorf("csv.Metadata.Annotations.Capabilities %s is not a valid capabilities level", capability)) | ||
} | ||
} | ||
|
||
if csv.Spec.Icon != nil { | ||
// only one icon is allowed | ||
if len(csv.Spec.Icon) != 1 { | ||
errs = append(errs, fmt.Errorf("csv.Spec.Icon should only have one element")) | ||
} | ||
|
||
icon := csv.Spec.Icon[0] | ||
if icon.MediaType == "" || icon.Data == "" { | ||
errs = append(errs, fmt.Errorf("csv.Spec.Icon elements should contain both data and mediatype")) | ||
} | ||
|
||
if icon.MediaType != "" { | ||
if _, ok := validMediatypes[icon.MediaType]; !ok { | ||
errs = append(errs, fmt.Errorf("csv.Spec.Icon %s does not have a valid mediatype", icon.MediaType)) | ||
} | ||
} | ||
} else { | ||
errs = append(errs, fmt.Errorf("csv.Spec.Icon not specified")) | ||
} | ||
|
||
if categories, ok := csv.ObjectMeta.Annotations["categories"]; ok { | ||
categorySlice := strings.Split(categories, ",") | ||
|
||
for _, category := range categorySlice { | ||
if _, ok := validCategories[category]; !ok { | ||
errs = append(errs, fmt.Errorf("csv.Metadata.Annotations.Categories %s is not a valid category", category)) | ||
} | ||
} | ||
} | ||
|
||
return errs | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
package internal | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/operator-framework/api/pkg/manifests" | ||
|
||
"github.com/stretchr/testify/require" | ||
) | ||
|
||
func TestValidateBundleOperatorHub(t *testing.T) { | ||
var table = []struct { | ||
description string | ||
directory string | ||
hasError bool | ||
errStrings []string | ||
}{ | ||
{ | ||
description: "registryv1 bundle/valid bundle", | ||
directory: "./testdata/valid_bundle", | ||
hasError: false, | ||
}, | ||
{ | ||
description: "registryv1 bundle/invald bundle operatorhubio", | ||
directory: "./testdata/invalid_bundle_operatorhub", | ||
hasError: true, | ||
errStrings: []string{ | ||
`Error: Value : (etcdoperator.v0.9.4) csv.Spec.Provider.Name not specified`, | ||
`Error: Value : (etcdoperator.v0.9.4) csv.Spec.Maintainers elements should contain both name and email`, | ||
`Error: Value : (etcdoperator.v0.9.4) csv.Spec.Maintainers email invalidemail is invalid: mail: missing '@' or angle-addr`, | ||
`Error: Value : (etcdoperator.v0.9.4) csv.Spec.Links elements should contain both name and url`, | ||
`Error: Value : (etcdoperator.v0.9.4) csv.Spec.Links url https//coreos.com/operators/etcd/docs/latest/ is invalid: parse https//coreos.com/operators/etcd/docs/latest/: invalid URI for request`, | ||
`Error: Value : (etcdoperator.v0.9.4) csv.Metadata.Annotations.Capabilities Installs and stuff is not a valid capabilities level`, | ||
`Error: Value : (etcdoperator.v0.9.4) csv.Spec.Icon should only have one element`, | ||
`Error: Value : (etcdoperator.v0.9.4) csv.Metadata.Annotations.Categories Magic is not a valid category`, | ||
}, | ||
}, | ||
} | ||
|
||
for _, tt := range table { | ||
// Validate the bundle object | ||
bundle, err := manifests.GetBundleFromDir(tt.directory) | ||
require.NoError(t, err) | ||
|
||
results := OperatorHubValidator.Validate(bundle) | ||
|
||
if len(results) > 0 { | ||
require.Equal(t, results[0].HasError(), tt.hasError) | ||
if results[0].HasError() { | ||
require.Equal(t, len(tt.errStrings), len(results[0].Errors)) | ||
|
||
for _, err := range results[0].Errors { | ||
errString := err.Error() | ||
require.Contains(t, tt.errStrings, errString) | ||
} | ||
} | ||
} | ||
} | ||
} |
13 changes: 13 additions & 0 deletions
13
...nternal/testdata/invalid_bundle_operatorhub/etcdbackups.etcd.database.coreos.com.crd.yaml
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
apiVersion: apiextensions.k8s.io/v1beta1 | ||
kind: CustomResourceDefinition | ||
metadata: | ||
name: etcdbackups.etcd.database.coreos.com | ||
spec: | ||
group: etcd.database.coreos.com | ||
names: | ||
kind: EtcdBackup | ||
listKind: EtcdBackupList | ||
plural: etcdbackups | ||
singular: etcdbackup | ||
scope: Namespaced | ||
version: v1beta2 |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.