/
parse.go
251 lines (215 loc) · 5.69 KB
/
parse.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
package registry
import (
"encoding/json"
"fmt"
"io/fs"
"strings"
operatorsv1alpha1 "github.com/operator-framework/api/pkg/operators/v1alpha1"
"github.com/sirupsen/logrus"
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
)
type bundleParser struct {
log *logrus.Entry
}
func newBundleParser(log *logrus.Entry) *bundleParser {
return &bundleParser{
log: log,
}
}
// Parse parses the given FS into a Bundle.
func (b *bundleParser) Parse(root fs.FS) (*Bundle, error) {
if root == nil {
return nil, fmt.Errorf("filesystem is nil")
}
bundle := &Bundle{}
manifests, err := fs.Sub(root, "manifests")
if err != nil {
return nil, fmt.Errorf("error opening manifests directory: %s", err)
}
if err := b.addManifests(manifests, bundle); err != nil {
return nil, err
}
metadata, err := fs.Sub(root, "metadata")
if err != nil {
return nil, fmt.Errorf("error opening metadata directory: %s", err)
}
if err := b.addMetadata(metadata, bundle); err != nil {
return nil, err
}
derived, err := b.derivedProperties(bundle)
if err != nil {
return nil, fmt.Errorf("failed to derive properties: %s", err)
}
bundle.Properties = propertySet(append(bundle.Properties, derived...))
return bundle, nil
}
// addManifests adds the result of parsing the manifests directory to a bundle.
func (b *bundleParser) addManifests(manifests fs.FS, bundle *Bundle) error {
files, err := fs.ReadDir(manifests, ".")
if err != nil {
return err
}
var csvFound bool
for _, f := range files {
if f.IsDir() {
continue
}
name := f.Name()
if strings.HasPrefix(name, ".") {
continue
}
obj := &unstructured.Unstructured{}
if err = decodeFileFS(manifests, name, obj, b.log); err != nil {
b.log.Warnf("failed to decode: %s", err)
continue
}
// Only include the first CSV we find in the
if obj.GetKind() == operatorsv1alpha1.ClusterServiceVersionKind {
if csvFound {
continue
}
csvFound = true
}
if obj.Object != nil {
bundle.Add(obj)
}
}
if bundle.Size() == 0 {
return fmt.Errorf("no bundle objects found")
}
csv, err := bundle.ClusterServiceVersion()
if err != nil {
return err
}
if csv == nil {
return fmt.Errorf("no csv in bundle")
}
bundle.Name = csv.GetName()
if err := bundle.AllProvidedAPIsInBundle(); err != nil {
return fmt.Errorf("error checking provided apis in bundle %s: %s", bundle.Name, err)
}
return nil
}
// addManifests adds the result of parsing the metadata directory to a bundle.
func (b *bundleParser) addMetadata(metadata fs.FS, bundle *Bundle) error {
files, err := fs.ReadDir(metadata, ".")
if err != nil {
return err
}
var (
af *AnnotationsFile
df *DependenciesFile
pf *PropertiesFile
)
for _, f := range files {
name := f.Name()
if af == nil {
decoded := AnnotationsFile{}
if err = decodeFileFS(metadata, name, &decoded, b.log); err == nil {
if decoded != (AnnotationsFile{}) {
af = &decoded
}
}
}
if df == nil {
decoded := DependenciesFile{}
if err = decodeFileFS(metadata, name, &decoded, b.log); err == nil {
if len(decoded.Dependencies) > 0 {
df = &decoded
}
}
}
if pf == nil {
decoded := PropertiesFile{}
if err = decodeFileFS(metadata, name, &decoded, b.log); err == nil {
if len(decoded.Properties) > 0 {
pf = &decoded
}
}
}
}
if af != nil {
bundle.Annotations = &af.Annotations
bundle.Package = af.Annotations.PackageName
bundle.Channels = af.GetChannels()
} else {
return fmt.Errorf("Could not find annotations file")
}
if df != nil {
bundle.Dependencies = append(bundle.Dependencies, df.GetDependencies()...)
} else {
b.log.Info("Could not find optional dependencies file")
}
if pf != nil {
bundle.Properties = append(bundle.Properties, pf.Properties...)
} else {
b.log.Info("Could not find optional properties file")
}
return nil
}
func (b *bundleParser) derivedProperties(bundle *Bundle) ([]Property, error) {
// Add properties from CSV annotations
csv, err := bundle.ClusterServiceVersion()
if err != nil {
return nil, fmt.Errorf("error getting csv: %s", err)
}
if csv == nil {
return nil, fmt.Errorf("bundle missing csv")
}
var derived []Property
if len(csv.GetAnnotations()) > 0 {
properties, ok := csv.GetAnnotations()[PropertyKey]
if ok {
if err := json.Unmarshal([]byte(properties), &derived); err != nil {
b.log.Warnf("failed to unmarshal csv annotation properties: %s", err)
}
}
}
if bundle.Annotations != nil && bundle.Annotations.PackageName != "" {
pkg := bundle.Annotations.PackageName
version, err := bundle.Version()
if err != nil {
return nil, err
}
value, err := json.Marshal(PackageProperty{
PackageName: pkg,
Version: version,
})
if err != nil {
return nil, fmt.Errorf("failed to marshal package property: %s", err)
}
// Annotations file takes precedent over CSV annotations
derived = append([]Property{{Type: PackageType, Value: value}}, derived...)
}
providedAPIs, err := bundle.ProvidedAPIs()
if err != nil {
return nil, fmt.Errorf("error getting provided apis: %s", err)
}
for api := range providedAPIs {
value, err := json.Marshal(GVKProperty{
Group: api.Group,
Kind: api.Kind,
Version: api.Version,
})
if err != nil {
return nil, fmt.Errorf("failed to marshal gvk property: %s", err)
}
derived = append(derived, Property{Type: GVKType, Value: value})
}
return propertySet(derived), nil
}
// propertySet returns the deduplicated set of a property list.
func propertySet(properties []Property) []Property {
var (
set []Property
visited = map[string]struct{}{}
)
for _, p := range properties {
if _, ok := visited[p.String()]; ok {
continue
}
visited[p.String()] = struct{}{}
set = append(set, p)
}
return set
}