-
Notifications
You must be signed in to change notification settings - Fork 78
Separate Category and Capability Validation #304
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
openshift-ci
merged 1 commit into
operator-framework:master
from
kevinrizza:separate-category-validator
Nov 2, 2023
Merged
Changes from all commits
Commits
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,80 @@ | ||
package internal | ||
|
||
import ( | ||
"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 OperatorHubV2Validator interfaces.Validator = interfaces.ValidatorFunc(validateOperatorHubV2) | ||
|
||
func validateOperatorHubV2(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, validateBundleOperatorHubV2(v, k8sVersion)) | ||
} | ||
} | ||
|
||
return results | ||
} | ||
|
||
func validateBundleOperatorHubV2(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 := validateHubCSVSpecV2(*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 validateHubCSVSpecV2(csv v1alpha1.ClusterServiceVersion) CSVChecks { | ||
checks := CSVChecks{csv: csv, errs: []error{}, warns: []error{}} | ||
|
||
checks = checkSpecProviderName(checks) | ||
checks = checkSpecMaintainers(checks) | ||
checks = checkSpecLinks(checks) | ||
checks = checkSpecVersion(checks) | ||
checks = checkSpecIcon(checks) | ||
checks = checkSpecMinKubeVersion(checks) | ||
|
||
return checks | ||
} |
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,58 @@ | ||
package internal | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/operator-framework/api/pkg/manifests" | ||
|
||
"github.com/stretchr/testify/require" | ||
) | ||
|
||
func TestValidateBundleOperatorHubV2(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.Spec.Icon should only have one element`, | ||
`Error: Value : (etcdoperator.v0.9.4) csv.Spec.Version must be set`, | ||
}, | ||
}, | ||
} | ||
|
||
for _, tt := range table { | ||
// Validate the bundle object | ||
bundle, err := manifests.GetBundleFromDir(tt.directory) | ||
require.NoError(t, err) | ||
|
||
results := OperatorHubV2Validator.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) | ||
} | ||
} | ||
} | ||
} | ||
} |
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 ( | ||
"fmt" | ||
|
||
"github.com/operator-framework/api/pkg/manifests" | ||
"github.com/operator-framework/api/pkg/validation/errors" | ||
interfaces "github.com/operator-framework/api/pkg/validation/interfaces" | ||
) | ||
|
||
var StandardCapabilitiesValidator interfaces.Validator = interfaces.ValidatorFunc(validateCapabilities) | ||
|
||
var validCapabilities = map[string]struct{}{ | ||
"Basic Install": {}, | ||
"Seamless Upgrades": {}, | ||
"Full Lifecycle": {}, | ||
"Deep Insights": {}, | ||
"Auto Pilot": {}, | ||
} | ||
|
||
func validateCapabilities(objs ...interface{}) (results []errors.ManifestResult) { | ||
for _, obj := range objs { | ||
switch v := obj.(type) { | ||
case *manifests.Bundle: | ||
results = append(results, validateCapabilitiesBundle(v)) | ||
} | ||
} | ||
|
||
return results | ||
} | ||
|
||
func validateCapabilitiesBundle(bundle *manifests.Bundle) errors.ManifestResult { | ||
result := errors.ManifestResult{Name: bundle.Name} | ||
csvCategoryCheck := CSVChecks{csv: *bundle.CSV, errs: []error{}, warns: []error{}} | ||
|
||
csvChecksResult := checkCapabilities(csvCategoryCheck) | ||
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())) | ||
} | ||
|
||
return result | ||
} | ||
|
||
// checkAnnotations will validate the values informed via annotations such as; capabilities and categories | ||
func checkCapabilities(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 %q is not a valid capabilities level", capability)) | ||
} | ||
} | ||
return checks | ||
} |
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.