Skip to content

Commit 9abde61

Browse files
Merge pull request #671 from njhale/feat/ex-props
Explicit bundle properties
2 parents f1629cd + d79f6da commit 9abde61

File tree

11 files changed

+951
-224
lines changed

11 files changed

+951
-224
lines changed

internal/property/property.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,10 @@ func (p Property) Validate() error {
3030
return nil
3131
}
3232

33+
func (p Property) String() string {
34+
return fmt.Sprintf("type: %q, value: %q", p.Type, p.Value)
35+
}
36+
3337
type Package struct {
3438
PackageName string `json:"packageName"`
3539
Version string `json:"version"`

pkg/registry/bundle.go

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ type Bundle struct {
4747
v1beta1crds []*apiextensionsv1beta1.CustomResourceDefinition
4848
v1crds []*apiextensionsv1.CustomResourceDefinition
4949
Dependencies []*Dependency
50-
Properties []*Property
50+
Properties []Property
5151
Annotations *Annotations
5252
cacheStale bool
5353
}
@@ -182,7 +182,7 @@ func (b *Bundle) ProvidedAPIs() (map[APIKey]struct{}, error) {
182182
provided := map[APIKey]struct{}{}
183183
crds, err := b.CustomResourceDefinitions()
184184
if err != nil {
185-
return nil, err
185+
return nil, fmt.Errorf("error getting crds: %s", err)
186186
}
187187

188188
for _, c := range crds {
@@ -210,7 +210,7 @@ func (b *Bundle) ProvidedAPIs() (map[APIKey]struct{}, error) {
210210

211211
ownedAPIs, _, err := csv.GetApiServiceDefinitions()
212212
if err != nil {
213-
return nil, err
213+
return nil, fmt.Errorf("error getting apiservice definitions: %s", err)
214214
}
215215
for _, api := range ownedAPIs {
216216
provided[APIKey{Group: api.Group, Version: api.Version, Kind: api.Kind, Plural: api.Name}] = struct{}{}
@@ -256,6 +256,7 @@ func (b *Bundle) AllProvidedAPIsInBundle() error {
256256
if err != nil {
257257
return err
258258
}
259+
259260
ownedCRDs, _, err := csv.GetCustomResourceDefintions()
260261
if err != nil {
261262
return err

pkg/registry/csv.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -263,7 +263,7 @@ func (csv *ClusterServiceVersion) GetApiServiceDefinitions() (owned []*Definitio
263263
var objmap map[string]*json.RawMessage
264264

265265
if err = json.Unmarshal(csv.Spec, &objmap); err != nil {
266-
return
266+
return nil, nil, fmt.Errorf("error unmarshaling into object map: %s", err)
267267
}
268268

269269
rawValue, ok := objmap[apiServiceDefinitions]

pkg/registry/decode.go

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import (
44
"errors"
55
"fmt"
66
"io"
7+
"io/fs"
78

89
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
910
"k8s.io/apimachinery/pkg/util/yaml"
@@ -42,3 +43,15 @@ func DecodePackageManifest(reader io.Reader) (manifest *PackageManifest, err err
4243
manifest = obj
4344
return
4445
}
46+
47+
func decodeFileFS(root fs.FS, path string, into interface{}) error {
48+
fileReader, err := root.Open(path)
49+
if err != nil {
50+
return fmt.Errorf("unable to read file %s: %s", path, err)
51+
}
52+
defer fileReader.Close()
53+
54+
decoder := yaml.NewYAMLOrJSONDecoder(fileReader, 30)
55+
56+
return decoder.Decode(into)
57+
}

pkg/registry/decode_test.go

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import (
44
"io"
55
"os"
66
"testing"
7+
"testing/fstest"
78

89
"github.com/stretchr/testify/assert"
910
"github.com/stretchr/testify/require"
@@ -90,6 +91,25 @@ func TestDecodePackageManifest(t *testing.T) {
9091
}
9192
}
9293

94+
func TestDecodeFileFS(t *testing.T) {
95+
type foo struct {
96+
Bar string
97+
}
98+
99+
root := fstest.MapFS{
100+
"foo.yaml": &fstest.MapFile{Data: []byte("bar: baz")},
101+
}
102+
103+
var nilPtr *foo
104+
require.NoError(t, decodeFileFS(root, "foo.yaml", nilPtr))
105+
require.Nil(t, nilPtr)
106+
107+
ptr := &foo{}
108+
require.NoError(t, decodeFileFS(root, "foo.yaml", ptr))
109+
require.NotNil(t, ptr)
110+
require.Equal(t, "baz", ptr.Bar)
111+
}
112+
93113
func loadFile(t *testing.T, path string) io.Reader {
94114
reader, err := os.Open(path)
95115
require.NoError(t, err, "unable to load from file %s", path)

pkg/registry/imageinput.go

Lines changed: 13 additions & 112 deletions
Original file line numberDiff line numberDiff line change
@@ -1,129 +1,30 @@
11
package registry
22

33
import (
4-
"fmt"
5-
"io/ioutil"
4+
"os"
65
"path/filepath"
7-
"strings"
8-
9-
"github.com/sirupsen/logrus"
106

117
"github.com/operator-framework/operator-registry/pkg/image"
8+
"github.com/sirupsen/logrus"
129
)
1310

1411
type ImageInput struct {
15-
manifestsDir string
16-
metadataDir string
17-
to image.Reference
18-
from string
19-
AnnotationsFile *AnnotationsFile
20-
dependenciesFile *DependenciesFile
21-
Bundle *Bundle
12+
to image.Reference
13+
from string
14+
Bundle *Bundle
2215
}
2316

2417
func NewImageInput(to image.Reference, from string) (*ImageInput, error) {
25-
var annotationsFound, dependenciesFound bool
26-
path := from
27-
manifests := filepath.Join(path, "manifests")
28-
metadata := filepath.Join(path, "metadata")
29-
// Get annotations file
30-
log := logrus.WithFields(logrus.Fields{"dir": from, "file": metadata, "load": "annotations"})
31-
files, err := ioutil.ReadDir(metadata)
32-
if err != nil {
33-
return nil, fmt.Errorf("unable to read directory %s: %s", metadata, err)
34-
}
35-
36-
// Look for the metadata and manifests sub-directories to find the annotations.yaml
37-
// file that will inform how the manifests of the bundle should be loaded into the database.
38-
// If dependencies.yaml which contains operator dependencies in metadata directory
39-
// exists, parse and load it into the DB
40-
annotationsFile := &AnnotationsFile{}
41-
dependenciesFile := &DependenciesFile{}
42-
for _, f := range files {
43-
if !annotationsFound {
44-
err = DecodeFile(filepath.Join(metadata, f.Name()), annotationsFile)
45-
if err == nil && *annotationsFile != (AnnotationsFile{}) {
46-
annotationsFound = true
47-
continue
48-
}
49-
}
50-
51-
if !dependenciesFound {
52-
err = DecodeFile(filepath.Join(metadata, f.Name()), &dependenciesFile)
53-
if err != nil {
54-
return nil, err
55-
}
56-
if len(dependenciesFile.Dependencies) > 0 {
57-
dependenciesFound = true
58-
}
59-
}
60-
}
61-
62-
if !annotationsFound {
63-
return nil, fmt.Errorf("Could not find annotations file")
64-
}
65-
66-
if !dependenciesFound {
67-
log.Info("Could not find optional dependencies file")
68-
}
69-
70-
imageInput := &ImageInput{
71-
manifestsDir: manifests,
72-
metadataDir: metadata,
73-
to: to,
74-
from: from,
75-
AnnotationsFile: annotationsFile,
76-
dependenciesFile: dependenciesFile,
77-
}
78-
79-
err = imageInput.getBundleFromManifests()
18+
parser := newBundleParser(logrus.WithFields(logrus.Fields{"with": from, "file": filepath.Join(from, "metadata"), "load": "annotations"}))
19+
bundle, err := parser.Parse(os.DirFS(from))
8020
if err != nil {
8121
return nil, err
8222
}
23+
bundle.BundleImage = to.String()
8324

84-
return imageInput, nil
85-
}
86-
87-
func (i *ImageInput) getBundleFromManifests() error {
88-
log := logrus.WithFields(logrus.Fields{"dir": i.from, "file": i.manifestsDir, "load": "bundle"})
89-
90-
csv, err := i.findCSV(i.manifestsDir)
91-
if err != nil {
92-
return err
93-
}
94-
95-
if csv.Object == nil {
96-
return fmt.Errorf("csv is empty: %s", err)
97-
}
98-
99-
log.Info("found csv, loading bundle")
100-
101-
csvName := csv.GetName()
102-
103-
bundle, err := loadBundle(csvName, i.manifestsDir)
104-
if err != nil {
105-
return fmt.Errorf("error loading objs in directory: %s", err)
106-
}
107-
108-
if bundle == nil || bundle.Size() == 0 {
109-
return fmt.Errorf("no bundle objects found")
110-
}
111-
112-
// set the bundleimage on the bundle
113-
bundle.BundleImage = i.to.String()
114-
// set the dependencies on the bundle
115-
bundle.Dependencies = i.dependenciesFile.GetDependencies()
116-
117-
bundle.Name = csvName
118-
bundle.Annotations = &i.AnnotationsFile.Annotations
119-
bundle.Package = i.AnnotationsFile.Annotations.PackageName
120-
bundle.Channels = strings.Split(i.AnnotationsFile.Annotations.Channels, ",")
121-
122-
if err := bundle.AllProvidedAPIsInBundle(); err != nil {
123-
return fmt.Errorf("error checking provided apis in bundle %s: %s", bundle.Name, err)
124-
}
125-
126-
i.Bundle = bundle
127-
128-
return nil
25+
return &ImageInput{
26+
to: to,
27+
from: from,
28+
Bundle: bundle,
29+
}, nil
12930
}

0 commit comments

Comments
 (0)