Skip to content

Commit 129c3f6

Browse files
author
Shawn Hurley
committedJul 17, 2020
Updating based on feedback
* Cleaning up imports * Updating names to be more clear * code optimizations.
1 parent a21d9e9 commit 129c3f6

File tree

5 files changed

+28
-32
lines changed

5 files changed

+28
-32
lines changed
 

‎cmd/opm/main.go

+11-9
Original file line numberDiff line numberDiff line change
@@ -5,13 +5,13 @@ import (
55

66
"github.com/sirupsen/logrus"
77
"github.com/spf13/cobra"
8+
utilerrors "k8s.io/apimachinery/pkg/util/errors"
89

910
"github.com/operator-framework/operator-registry/cmd/opm/alpha"
1011
"github.com/operator-framework/operator-registry/cmd/opm/index"
1112
"github.com/operator-framework/operator-registry/cmd/opm/registry"
1213
"github.com/operator-framework/operator-registry/cmd/opm/version"
1314
registrylib "github.com/operator-framework/operator-registry/pkg/registry"
14-
utilerrors "k8s.io/apimachinery/pkg/util/errors"
1515
)
1616

1717
func main() {
@@ -37,14 +37,16 @@ func main() {
3737
}
3838

3939
if err := rootCmd.Execute(); err != nil {
40-
if agg, ok := err.(utilerrors.Aggregate); ok {
41-
for _, e := range agg.Errors() {
42-
if _, ok := e.(registrylib.BundleAlreadyAddedError); ok {
43-
os.Exit(2)
44-
}
45-
if _, ok := e.(registrylib.BundleAlreadyInDatabaseError); ok {
46-
os.Exit(3)
47-
}
40+
agg, ok := err.(utilerrors.Aggregate)
41+
if !ok {
42+
os.Exit(1)
43+
}
44+
for _, e := range agg.Errors() {
45+
if _, ok := e.(registrylib.BundleImageAlreadyAddedErr); ok {
46+
os.Exit(2)
47+
}
48+
if _, ok := e.(registrylib.PackageVersionAlreadyAddedErr); ok {
49+
os.Exit(3)
4850
}
4951
}
5052
os.Exit(1)

‎pkg/lib/registry/registry.go

+2-3
Original file line numberDiff line numberDiff line change
@@ -8,14 +8,13 @@ import (
88
"io/ioutil"
99
"os"
1010

11-
"github.com/operator-framework/operator-registry/pkg/containertools"
12-
"github.com/operator-framework/operator-registry/pkg/image/execregistry"
13-
1411
"github.com/sirupsen/logrus"
1512
utilerrors "k8s.io/apimachinery/pkg/util/errors"
1613

14+
"github.com/operator-framework/operator-registry/pkg/containertools"
1715
"github.com/operator-framework/operator-registry/pkg/image"
1816
"github.com/operator-framework/operator-registry/pkg/image/containerdregistry"
17+
"github.com/operator-framework/operator-registry/pkg/image/execregistry"
1918
"github.com/operator-framework/operator-registry/pkg/registry"
2019
"github.com/operator-framework/operator-registry/pkg/sqlite"
2120
)

‎pkg/registry/populator.go

+7-12
Original file line numberDiff line numberDiff line change
@@ -65,9 +65,9 @@ func (i *DirectoryPopulator) Populate(mode Mode) error {
6565

6666
func (i *DirectoryPopulator) globalSanityCheck(imagesToAdd []*ImageInput) error {
6767
var errs []error
68-
images := make(map[string]bool)
68+
images := make(map[string]struct{})
6969
for _, image := range imagesToAdd {
70-
images[image.bundle.BundleImage] = true
70+
images[image.bundle.BundleImage] = struct{}{}
7171
}
7272

7373
for _, image := range imagesToAdd {
@@ -77,10 +77,10 @@ func (i *DirectoryPopulator) globalSanityCheck(imagesToAdd []*ImageInput) error
7777
// Or that this is the first time the package is loaded.
7878
return nil
7979
}
80-
isExactImage := false
8180
for _, bundlePath := range bundlePaths {
8281
if _, ok := images[bundlePath]; ok {
83-
isExactImage = true
82+
errs = append(errs, BundleImageAlreadyAddedErr{ErrorString: fmt.Sprintf("Bundle %s already exists", image.bundle.BundleImage)})
83+
continue
8484
}
8585
}
8686
for _, channel := range image.bundle.Channels {
@@ -89,15 +89,10 @@ func (i *DirectoryPopulator) globalSanityCheck(imagesToAdd []*ImageInput) error
8989
// Assume that if we can not find a bundle for the package, channel and or CSV Name that this is safe to add
9090
continue
9191
}
92-
if bundle != nil && !isExactImage {
92+
if bundle != nil {
9393
// raise error that this package + channel + csv combo is already in the db
94-
errs = append(errs, BundleAlreadyInDatabaseError{ErrorString: "Bundle already added that provides package and csv"})
95-
continue
96-
}
97-
if bundle != nil && isExactImage {
98-
// raise error that this bundle and channel is already in the db
99-
errs = append(errs, BundleAlreadyAddedError{ErrorString: fmt.Sprintf("Bundle %s already exists", image.bundle.BundleImage)})
100-
continue
94+
errs = append(errs, PackageVersionAlreadyAddedErr{ErrorString: "Bundle already added that provides package and csv"})
95+
break
10196
}
10297
}
10398
}

‎pkg/registry/populator_test.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -423,7 +423,7 @@ func TestImageLoading(t *testing.T) {
423423
},
424424
},
425425
wantErr: true,
426-
err: registry.BundleAlreadyInDatabaseError{},
426+
err: registry.PackageVersionAlreadyAddedErr{},
427427
},
428428
{
429429
name: "AddExactBundleAlreadyExists",
@@ -458,7 +458,7 @@ func TestImageLoading(t *testing.T) {
458458
},
459459
},
460460
wantErr: true,
461-
err: registry.BundleAlreadyAddedError{},
461+
err: registry.BundleImageAlreadyAddedErr{},
462462
},
463463
}
464464
for _, tt := range tests {

‎pkg/registry/types.go

+6-6
Original file line numberDiff line numberDiff line change
@@ -14,21 +14,21 @@ var (
1414
ErrPackageNotInDatabase = errors.New("Package not in database")
1515
)
1616

17-
// BundleAlreadyAddedError is an error that describes a bundle is already added
18-
type BundleAlreadyAddedError struct {
17+
// BundleImageAlreadyAddedErr is an error that describes a bundle is already added
18+
type BundleImageAlreadyAddedErr struct {
1919
ErrorString string
2020
}
2121

22-
func (e BundleAlreadyAddedError) Error() string {
22+
func (e BundleImageAlreadyAddedErr) Error() string {
2323
return e.ErrorString
2424
}
2525

26-
// BundleAlreadyInDatabseError is an error that describes that a bundle that is already in the databse that provides this CSV
27-
type BundleAlreadyInDatabaseError struct {
26+
// PackageVersionAlreadyAddedErr is an error that describes that a bundle that is already in the databse that provides this package and version
27+
type PackageVersionAlreadyAddedErr struct {
2828
ErrorString string
2929
}
3030

31-
func (e BundleAlreadyInDatabaseError) Error() string {
31+
func (e PackageVersionAlreadyAddedErr) Error() string {
3232
return e.ErrorString
3333
}
3434

0 commit comments

Comments
 (0)
Please sign in to comment.