Skip to content

Commit 02bfec5

Browse files
committed
pkg/validation/interfaces: Validator's implement WithValidators to append non-default Validators to the Validator; ValidatorFunc implements Validator
1 parent 439c769 commit 02bfec5

File tree

9 files changed

+71
-65
lines changed

9 files changed

+71
-65
lines changed

pkg/manifests/directory.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,6 @@ func GetManifestsDir(dir string) (registry.PackageManifest, []*registry.Bundle,
2626
for _, obj := range bundles {
2727
objs = append(objs, obj)
2828
}
29-
results := validation.DefaultValidators().Apply(objs...)
29+
results := validation.AllValidators.Validate(objs...)
3030
return pkg, bundles, results
3131
}

pkg/validation/doc.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
// This package defines the valid Operator manifests directory format
2-
// by exposing a set of interfaces.Validator's to verify a directory and
2+
// by exposing a set of Validator's to verify a directory and
33
// its constituent manifests. A manifests directory consists of a
4-
// Package Manifest and a set of versioned Bundles. Each Bundle contains a
4+
// package manifest and a set of versioned Bundles. Each Bundle contains a
55
// ClusterServiceVersion and one or more CustomResourceDefinition's.
66
//
7-
// Errors and warnings, both represented by the errors.Error type, are
8-
// returned by exported functions for missing mandatory and optional fields,
7+
// Errors and warnings, both represented by the Error type, are returned
8+
// by exported functions for missing mandatory and optional fields,
99
// respectively. Each Error implements the error interface.
1010
//
1111
// Manifest and Bundle format: https://github.com/operator-framework/operator-registry/#manifest-format

pkg/validation/interfaces/validator.go

Lines changed: 26 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,16 +9,38 @@ type Validator interface {
99
// Validate takes a list of arbitrary objects and returns a slice of results,
1010
// one for each object validated.
1111
Validate(...interface{}) []errors.ManifestResult
12+
// WithValidators returns a Validator appended to a variable number of
13+
// Validator's.
14+
WithValidators(...Validator) Validators
1215
}
1316

14-
// Validators is a set of Validator's that can be run via Apply.
17+
// ValidatorFunc implements Validator. ValidatorFunc can be used as a wrapper
18+
// for functions that run object validators.
19+
type ValidatorFunc func(...interface{}) []errors.ManifestResult
20+
21+
// Validate runs the ValidatorFunc on objs.
22+
func (f ValidatorFunc) Validate(objs ...interface{}) (results []errors.ManifestResult) {
23+
return f(objs...)
24+
}
25+
26+
// WithValidators appends the ValidatorFunc to vals.
27+
func (f ValidatorFunc) WithValidators(vals ...Validator) Validators {
28+
return append(vals, f)
29+
}
30+
31+
// Validators is a set of Validator's that implements Validate.
1532
type Validators []Validator
1633

17-
// ApplyParallel invokes each Validator in vals, and collects and returns
34+
// Validate invokes each Validator in Validators, collecting and returning
1835
// the results.
19-
func (vals Validators) Apply(objs ...interface{}) (results []errors.ManifestResult) {
20-
for _, validator := range vals {
36+
func (validators Validators) Validate(objs ...interface{}) (results []errors.ManifestResult) {
37+
for _, validator := range validators {
2138
results = append(results, validator.Validate(objs...)...)
2239
}
2340
return results
2441
}
42+
43+
// WithValidators appends vals to Validators.
44+
func (validators Validators) WithValidators(vals ...Validator) Validators {
45+
return append(vals, validators...)
46+
}

pkg/validation/internal/bundle.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,14 +5,15 @@ import (
55
"fmt"
66

77
"github.com/operator-framework/api/pkg/validation/errors"
8+
interfaces "github.com/operator-framework/api/pkg/validation/interfaces"
89

910
operatorsv1alpha1 "github.com/operator-framework/operator-lifecycle-manager/pkg/api/apis/operators/v1alpha1"
1011
"github.com/operator-framework/operator-registry/pkg/registry"
1112
)
1213

13-
type BundleValidator struct{}
14+
var BundleValidator interfaces.Validator = interfaces.ValidatorFunc(validateBundles)
1415

15-
func (f BundleValidator) Validate(objs ...interface{}) (results []errors.ManifestResult) {
16+
func validateBundles(objs ...interface{}) (results []errors.ManifestResult) {
1617
for _, obj := range objs {
1718
switch v := obj.(type) {
1819
case *registry.Bundle:

pkg/validation/internal/crd.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import (
44
"strings"
55

66
"github.com/operator-framework/api/pkg/validation/errors"
7+
interfaces "github.com/operator-framework/api/pkg/validation/interfaces"
78

89
"k8s.io/apiextensions-apiserver/pkg/apis/apiextensions"
910
"k8s.io/apiextensions-apiserver/pkg/apis/apiextensions/install"
@@ -19,9 +20,9 @@ func init() {
1920
install.Install(Scheme)
2021
}
2122

22-
type CRDValidator struct{}
23+
var CRDValidator interfaces.Validator = interfaces.ValidatorFunc(validateCRDs)
2324

24-
func (f CRDValidator) Validate(objs ...interface{}) (results []errors.ManifestResult) {
25+
func validateCRDs(objs ...interface{}) (results []errors.ManifestResult) {
2526
for _, obj := range objs {
2627
switch v := obj.(type) {
2728
case *v1beta1.CustomResourceDefinition:

pkg/validation/internal/csv.go

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,19 +7,20 @@ import (
77
"strings"
88

99
"github.com/operator-framework/api/pkg/validation/errors"
10-
"github.com/operator-framework/operator-registry/pkg/registry"
10+
interfaces "github.com/operator-framework/api/pkg/validation/interfaces"
1111

1212
"github.com/blang/semver"
1313
"github.com/operator-framework/operator-lifecycle-manager/pkg/api/apis/operators/v1alpha1"
14+
"github.com/operator-framework/operator-registry/pkg/registry"
1415
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
1516
"k8s.io/apimachinery/pkg/runtime/schema"
1617
k8svalidation "k8s.io/apimachinery/pkg/util/validation"
1718
"k8s.io/apimachinery/pkg/util/yaml"
1819
)
1920

20-
type CSVValidator struct{}
21+
var CSVValidator interfaces.Validator = interfaces.ValidatorFunc(validateCSVs)
2122

22-
func (f CSVValidator) Validate(objs ...interface{}) (results []errors.ManifestResult) {
23+
func validateCSVs(objs ...interface{}) (results []errors.ManifestResult) {
2324
for _, obj := range objs {
2425
switch v := obj.(type) {
2526
case *v1alpha1.ClusterServiceVersion:

pkg/validation/internal/manifests.go

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,16 +4,17 @@ import (
44
"fmt"
55

66
"github.com/operator-framework/api/pkg/validation/errors"
7+
interfaces "github.com/operator-framework/api/pkg/validation/interfaces"
78

89
"github.com/blang/semver"
910
"github.com/operator-framework/operator-registry/pkg/registry"
1011
)
1112

1213
const skipPackageAnnotationKey = "olm.skipRange"
1314

14-
type ManifestsValidator struct{}
15+
var PackageUpdateGraphValidator interfaces.Validator = interfaces.ValidatorFunc(validatePackageUpdateGraphs)
1516

16-
func (f ManifestsValidator) Validate(objs ...interface{}) (results []errors.ManifestResult) {
17+
func validatePackageUpdateGraphs(objs ...interface{}) (results []errors.ManifestResult) {
1718
var pkg *registry.PackageManifest
1819
bundles := []*registry.Bundle{}
1920
for _, obj := range objs {
@@ -27,12 +28,12 @@ func (f ManifestsValidator) Validate(objs ...interface{}) (results []errors.Mani
2728
}
2829
}
2930
if pkg != nil && len(bundles) > 0 {
30-
results = append(results, validateManifests(pkg, bundles))
31+
results = append(results, validatePackageUpdateGraph(pkg, bundles))
3132
}
3233
return results
3334
}
3435

35-
func validateManifests(pkg *registry.PackageManifest, bundles []*registry.Bundle) (result errors.ManifestResult) {
36+
func validatePackageUpdateGraph(pkg *registry.PackageManifest, bundles []*registry.Bundle) (result errors.ManifestResult) {
3637
// Collect all CSV names and ensure no duplicates. We will use these names
3738
// to check whether a spec.replaces references an existing CSV in bundles.
3839
csvNameMap := map[string]struct{}{}

pkg/validation/internal/package_manifest.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,14 @@ import (
44
"fmt"
55

66
"github.com/operator-framework/api/pkg/validation/errors"
7+
interfaces "github.com/operator-framework/api/pkg/validation/interfaces"
78

89
"github.com/operator-framework/operator-registry/pkg/registry"
910
)
1011

11-
type PackageManifestValidator struct{}
12+
var PackageManifestValidator interfaces.Validator = interfaces.ValidatorFunc(validatePackageManifests)
1213

13-
func (f PackageManifestValidator) Validate(objs ...interface{}) (results []errors.ManifestResult) {
14+
func validatePackageManifests(objs ...interface{}) (results []errors.ManifestResult) {
1415
for _, obj := range objs {
1516
switch v := obj.(type) {
1617
case *registry.PackageManifest:

pkg/validation/validation.go

Lines changed: 22 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,8 @@
33
// implemented by this validation library.
44
//
55
// Each default Validator runs an independent set of validation functions on
6-
// a set of objects. To run all implemented Validator's, use DefaultValidators().
7-
// The Validator will not be run on objects not of the appropriate type.
6+
// a set of objects. To run all implemented Validator's, use AllValidators.
7+
// The Validator will not be run on objects of an inappropriate type.
88

99
package validation
1010

@@ -13,50 +13,29 @@ import (
1313
"github.com/operator-framework/api/pkg/validation/internal"
1414
)
1515

16-
// DefaultPackageManifestValidators returns a package manifest Validator that
17-
// can be run directly with Apply(objs...). Optionally, any additional
18-
// Validator's can be added to the returned Validators set.
19-
func DefaultPackageManifestValidators(vals ...interfaces.Validator) interfaces.Validators {
20-
return append(vals, internal.PackageManifestValidator{})
21-
}
16+
// PackageManifestValidator implements Validator to validate package manifests.
17+
var PackageManifestValidator = internal.PackageManifestValidator
2218

23-
// DefaultClusterServiceVersionValidators returns a ClusterServiceVersion
24-
// Validator that can be run directly with Apply(objs...). Optionally, any
25-
// additional Validator's can be added to the returned Validators set.
26-
func DefaultClusterServiceVersionValidators(vals ...interfaces.Validator) interfaces.Validators {
27-
return append(vals, internal.CSVValidator{})
28-
}
19+
// ClusterServiceVersionValidator implements Validator to validate
20+
// ClusterServiceVersions.
21+
var ClusterServiceVersionValidator = internal.CSVValidator
2922

30-
// DefaultCustomResourceDefinitionValidators returns a CustomResourceDefinition
31-
// Validator that can be run directly with Apply(objs...). Optionally, any
32-
// additional Validator's can be added to the returned Validators set.
33-
func DefaultCustomResourceDefinitionValidators(vals ...interfaces.Validator) interfaces.Validators {
34-
return append(vals, internal.CRDValidator{})
35-
}
23+
// CustomResourceDefinitionValidator implements Validator to validate
24+
// CustomResourceDefinitions.
25+
var CustomResourceDefinitionValidator = internal.CRDValidator
3626

37-
// DefaultBundleValidators returns a bundle Validator that can be run directly
38-
// with Apply(objs...). Optionally, any additional Validator's can be added to
39-
// the returned Validators set.
40-
func DefaultBundleValidators(vals ...interfaces.Validator) interfaces.Validators {
41-
return append(vals, internal.BundleValidator{})
42-
}
27+
// BundleValidator implements Validator to validate Bundles.
28+
var BundleValidator = internal.BundleValidator
4329

44-
// DefaultManifestsValidators returns a manifests Validator that can be run
45-
// directly with Apply(objs...). Optionally, any additional Validator's can be
46-
// added to the returned Validators set.
47-
func DefaultManifestsValidators(vals ...interfaces.Validator) interfaces.Validators {
48-
return append(vals, internal.ManifestsValidator{})
49-
}
30+
// PackageUpdateGraphValidator implements Validator to validate the
31+
// package update graph between a package manifest and Bundles.
32+
var PackageUpdateGraphValidator = internal.PackageUpdateGraphValidator
5033

51-
// DefaultValidators returns all default Validator's, which can be run directly
52-
// with Apply(objs...). Optionally, any additional Validator's can be added to
53-
// the returned Validators set.
54-
func DefaultValidators(vals ...interfaces.Validator) interfaces.Validators {
55-
return append(vals,
56-
internal.PackageManifestValidator{},
57-
internal.CSVValidator{},
58-
internal.CRDValidator{},
59-
internal.BundleValidator{},
60-
internal.ManifestsValidator{},
61-
)
34+
// AllValidators implements Validator to validate all Operator manifest types.
35+
var AllValidators = interfaces.Validators{
36+
PackageManifestValidator,
37+
ClusterServiceVersionValidator,
38+
CustomResourceDefinitionValidator,
39+
BundleValidator,
40+
PackageUpdateGraphValidator,
6241
}

0 commit comments

Comments
 (0)