|
1 | 1 | package registry |
2 | 2 |
|
3 | 3 | import ( |
4 | | - "fmt" |
5 | | - "io/ioutil" |
| 4 | + "os" |
6 | 5 | "path/filepath" |
7 | | - "strings" |
8 | | - |
9 | | - "github.com/sirupsen/logrus" |
10 | 6 |
|
11 | 7 | "github.com/operator-framework/operator-registry/pkg/image" |
| 8 | + "github.com/sirupsen/logrus" |
12 | 9 | ) |
13 | 10 |
|
14 | 11 | 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 |
22 | 15 | } |
23 | 16 |
|
24 | 17 | 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)) |
80 | 20 | if err != nil { |
81 | 21 | return nil, err |
82 | 22 | } |
| 23 | + bundle.BundleImage = to.String() |
83 | 24 |
|
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 |
129 | 30 | } |
0 commit comments