99
1010 "github.com/sirupsen/logrus"
1111 "k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
12- utilerrors "k8s.io/apimachinery/pkg/util/errors"
1312 "k8s.io/apimachinery/pkg/util/yaml"
1413
1514 "github.com/operator-framework/operator-registry/pkg/image"
@@ -45,17 +44,11 @@ func (i *DirectoryPopulator) Populate() error {
4544 // manifests of the bundle should be loaded into the database.
4645 annotationsFile := & AnnotationsFile {}
4746 for _ , f := range files {
48- fileReader , err := os .Open (filepath .Join (metadata , f .Name ()))
49- if err != nil {
50- return fmt .Errorf ("unable to read file %s: %s" , f .Name (), err )
51- }
52- decoder := yaml .NewYAMLOrJSONDecoder (fileReader , 30 )
53- err = decoder .Decode (& annotationsFile )
47+ err = decodeFile (filepath .Join (metadata , f .Name ()), err )
5448 if err != nil || * annotationsFile == (AnnotationsFile {}) {
5549 continue
56- } else {
57- log .Info ("found annotations file searching for csv" )
5850 }
51+ log .Info ("found annotations file searching for csv" )
5952 }
6053
6154 if * annotationsFile == (AnnotationsFile {}) {
@@ -133,7 +126,6 @@ func loadBundle(csvName string, dir string) (*Bundle, error) {
133126 return nil , err
134127 }
135128
136- var errs []error
137129 bundle := & Bundle {}
138130 for _ , f := range files {
139131 log = log .WithField ("file" , f .Name ())
@@ -148,17 +140,12 @@ func loadBundle(csvName string, dir string) (*Bundle, error) {
148140 }
149141
150142 log .Info ("loading bundle file" )
151- path := filepath .Join (dir , f .Name ())
152- fileReader , err := os .Open (path )
153- if err != nil {
154- errs = append (errs , fmt .Errorf ("unable to load file %s: %s" , path , err ))
155- continue
156- }
157-
158- decoder := yaml .NewYAMLOrJSONDecoder (fileReader , 30 )
159- obj := & unstructured.Unstructured {}
160- if err = decoder .Decode (obj ); err != nil {
161- logrus .WithError (err ).Debugf ("could not decode file contents for %s" , path )
143+ var (
144+ obj = & unstructured.Unstructured {}
145+ path = filepath .Join (dir , f .Name ())
146+ )
147+ if err = decodeFile (path , obj ); err != nil {
148+ log .WithError (err ).Debugf ("could not decode file contents for %s" , path )
162149 continue
163150 }
164151
@@ -172,7 +159,7 @@ func loadBundle(csvName string, dir string) (*Bundle, error) {
172159 }
173160 }
174161
175- return bundle , utilerrors . NewAggregate ( errs )
162+ return bundle , nil
176163}
177164
178165// findCSV looks through the bundle directory to find a csv
@@ -184,7 +171,6 @@ func (i *DirectoryPopulator) findCSV(manifests string) (*unstructured.Unstructur
184171 return nil , fmt .Errorf ("unable to read directory %s: %s" , manifests , err )
185172 }
186173
187- var errs []error
188174 for _ , f := range files {
189175 log = log .WithField ("file" , f .Name ())
190176 if f .IsDir () {
@@ -197,29 +183,23 @@ func (i *DirectoryPopulator) findCSV(manifests string) (*unstructured.Unstructur
197183 continue
198184 }
199185
200- path := filepath .Join (manifests , f .Name ())
201- fileReader , err := os .Open (path )
202- if err != nil {
203- errs = append (errs , fmt .Errorf ("unable to read file %s: %s" , path , err ))
204- continue
205- }
206-
207- dec := yaml .NewYAMLOrJSONDecoder (fileReader , 30 )
208- unst := & unstructured.Unstructured {}
209- if err := dec .Decode (unst ); err != nil {
186+ var (
187+ obj = & unstructured.Unstructured {}
188+ path = filepath .Join (manifests , f .Name ())
189+ )
190+ if err = decodeFile (path , obj ); err != nil {
191+ log .WithError (err ).Debugf ("could not decode file contents for %s" , path )
210192 continue
211193 }
212194
213- if unst .GetKind () != clusterServiceVersionKind {
195+ if obj .GetKind () != clusterServiceVersionKind {
214196 continue
215197 }
216198
217- return unst , nil
218-
199+ return obj , nil
219200 }
220201
221- errs = append (errs , fmt .Errorf ("no csv found in bundle" ))
222- return nil , utilerrors .NewAggregate (errs )
202+ return nil , fmt .Errorf ("no csv found in bundle" )
223203}
224204
225205// loadOperatorBundle adds the package information to the loader's store
@@ -256,3 +236,16 @@ func translateAnnotationsIntoPackage(annotations *AnnotationsFile, csv *ClusterS
256236
257237 return manifest , nil
258238}
239+
240+ // decodeFile decodes the file at a path into the given interface.
241+ func decodeFile (path string , into interface {}) error {
242+ fileReader , err := os .Open (path )
243+ if err != nil {
244+ return fmt .Errorf ("unable to read file %s: %s" , path , err )
245+ }
246+ defer fileReader .Close ()
247+
248+ decoder := yaml .NewYAMLOrJSONDecoder (fileReader , 30 )
249+
250+ return decoder .Decode (into )
251+ }
0 commit comments