-
Notifications
You must be signed in to change notification settings - Fork 239
/
csv.go
469 lines (384 loc) · 12.4 KB
/
csv.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
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
package registry
import (
"encoding/json"
"errors"
"fmt"
"os"
"path"
prettyunmarshaler "github.com/operator-framework/operator-registry/pkg/prettyunmarshaler"
v1 "k8s.io/api/apps/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
"k8s.io/apimachinery/pkg/runtime"
"k8s.io/apimachinery/pkg/util/yaml"
"github.com/operator-framework/api/pkg/operators"
)
const (
// Name of the CSV's kind
clusterServiceVersionKind = "ClusterServiceVersion"
// Name of the section under which the list of owned and required list of
// CRD(s) is specified inside an operator manifest.
customResourceDefinitions = "customresourcedefinitions"
// Name of the section under which the list of owned and required list of
// apiservices is specified inside an operator manifest.
apiServiceDefinitions = "apiservicedefinitions"
// The yaml attribute that points to the name of an older
// ClusterServiceVersion object that the current ClusterServiceVersion
// replaces.
replaces = "replaces"
// The yaml attribute that points to the names of older
// ClusterServiceVersion objects that the current ClusterServiceVersion
// skips
skips = "skips"
// The yaml attribute that points to the icon for the ClusterServiceVersion
icon = "icon"
// The yaml attribute that points to the icon.base64data for the ClusterServiceVersion
base64data = "base64data"
// The yaml attribute that points to the icon.mediatype for the ClusterServiceVersion
mediatype = "mediatype"
// The yaml attribute that points to the description for the ClusterServiceVersion
description = "description"
// The yaml attribute that specifies the version of the ClusterServiceVersion
// expected to be semver and parseable by blang/semver/v4
version = "version"
// The yaml attribute that specifies the related images of the ClusterServiceVersion
relatedImages = "relatedImages"
// The yaml attribute that specifies the skipRange of the ClusterServiceVersion
skipRangeAnnotationKey = "olm.skipRange"
// The yaml attribute that specifies the optional substitutesfor of the ClusterServiceVersion
substitutesForAnnotationKey = "olm.substitutesFor"
)
// ClusterServiceVersion is a structured representation of cluster service
// version object(s) specified inside the 'clusterServiceVersions' section of
// an operator manifest.
type ClusterServiceVersion struct {
// Type metadata.
metav1.TypeMeta `json:",inline"`
// Object metadata.
metav1.ObjectMeta `json:"metadata"`
// Spec is the raw representation of the 'spec' element of
// ClusterServiceVersion object. Since we are
// not interested in the content of spec we are not parsing it.
Spec json.RawMessage `json:"spec"`
}
// ReadCSVFromBundleDirectory tries to parse every YAML file in the directory without inspecting sub-directories and
// returns a CSV. According to the strict one CSV per bundle rule, func returns an error if more than one CSV is found.
func ReadCSVFromBundleDirectory(bundleDir string) (*ClusterServiceVersion, error) {
dirContent, err := os.ReadDir(bundleDir)
if err != nil {
return nil, fmt.Errorf("error reading bundle directory %s, %v", bundleDir, err)
}
files := []string{}
for _, f := range dirContent {
if !f.IsDir() {
files = append(files, f.Name())
}
}
csv := ClusterServiceVersion{}
foundCSV := false
for _, file := range files {
yamlReader, err := os.Open(path.Join(bundleDir, file))
if err != nil {
continue
}
defer yamlReader.Close()
unstructuredCSV := unstructured.Unstructured{}
decoder := yaml.NewYAMLOrJSONDecoder(yamlReader, 30)
if err = decoder.Decode(&unstructuredCSV); err != nil {
continue
}
if unstructuredCSV.GetKind() != operators.ClusterServiceVersionKind {
continue
}
if foundCSV {
return nil, fmt.Errorf("more than one ClusterServiceVersion is found in bundle")
}
if err := runtime.DefaultUnstructuredConverter.FromUnstructured(unstructuredCSV.UnstructuredContent(),
&csv); err != nil {
return nil, err
}
foundCSV = true
}
if foundCSV {
return &csv, nil
}
return nil, fmt.Errorf("no ClusterServiceVersion object found in %s", bundleDir)
}
// GetReplaces returns the name of the older ClusterServiceVersion object that
// is replaced by this ClusterServiceVersion object.
//
// If not defined, the function returns an empty string.
func (csv *ClusterServiceVersion) GetReplaces() (string, error) {
var objmap map[string]*json.RawMessage
if err := json.Unmarshal(csv.Spec, &objmap); err != nil {
return "", err
}
rawValue, ok := objmap[replaces]
if !ok || rawValue == nil {
return "", nil
}
var replaces string
if err := json.Unmarshal(*rawValue, &replaces); err != nil {
return "", err
}
return replaces, nil
}
// GetVersion returns the version of the CSV
//
// If not defined, the function returns an empty string.
func (csv *ClusterServiceVersion) GetVersion() (string, error) {
var objmap map[string]*json.RawMessage
if err := json.Unmarshal(csv.Spec, &objmap); err != nil {
return "", err
}
rawValue, ok := objmap[version]
if !ok || rawValue == nil {
return "", nil
}
var v string
if err := json.Unmarshal(*rawValue, &v); err != nil {
return "", err
}
return v, nil
}
// GetSkipRange returns the skiprange of the CSV
//
// If not defined, the function returns an empty string.
func (csv *ClusterServiceVersion) GetSkipRange() string {
skipRange, ok := csv.Annotations[skipRangeAnnotationKey]
if !ok {
return ""
}
return skipRange
}
// GetSkips returns the name of the older ClusterServiceVersion objects that
// are skipped by this ClusterServiceVersion object.
//
// If not defined, the function returns an empty string.
func (csv *ClusterServiceVersion) GetSkips() ([]string, error) {
var objmap map[string]*json.RawMessage
if err := json.Unmarshal(csv.Spec, &objmap); err != nil {
return nil, err
}
rawValue, ok := objmap[skips]
if !ok || rawValue == nil {
return nil, nil
}
var skips []string
if err := json.Unmarshal(*rawValue, &skips); err != nil {
return nil, err
}
return skips, nil
}
// GetCustomResourceDefintions returns a list of owned and required
// CustomResourceDefinition object(s) specified inside the
// 'customresourcedefinitions' section of a ClusterServiceVersion 'spec'.
//
// owned represents the list of CRD(s) managed by this ClusterServiceVersion
// object.
// required represents the list of CRD(s) that this ClusterServiceVersion
// object depends on.
//
// If owned or required is not defined in the spec then an empty list is
// returned respectively.
func (csv *ClusterServiceVersion) GetCustomResourceDefintions() (owned []*DefinitionKey, required []*DefinitionKey, err error) {
var objmap map[string]*json.RawMessage
if err = json.Unmarshal(csv.Spec, &objmap); err != nil {
return
}
rawValue, ok := objmap[customResourceDefinitions]
if !ok || rawValue == nil {
return
}
var definitions struct {
Owned []*DefinitionKey `json:"owned"`
Required []*DefinitionKey `json:"required"`
}
if err = json.Unmarshal(*rawValue, &definitions); err != nil {
return
}
owned = definitions.Owned
required = definitions.Required
return
}
// GetApiServiceDefinitions returns a list of owned and required
// APISerivces specified inside the
// 'apiservicedefinitions' section of a ClusterServiceVersion 'spec'.
//
// owned represents the list of apiservices managed by this ClusterServiceVersion
// object.
// required represents the list of apiservices that this ClusterServiceVersion
// object depends on.
//
// If owned or required is not defined in the spec then an empty list is
// returned respectively.
func (csv *ClusterServiceVersion) GetApiServiceDefinitions() (owned []*DefinitionKey, required []*DefinitionKey, err error) {
var objmap map[string]*json.RawMessage
if err = json.Unmarshal(csv.Spec, &objmap); err != nil {
return nil, nil, fmt.Errorf("error unmarshaling into object map: %s", err)
}
rawValue, ok := objmap[apiServiceDefinitions]
if !ok || rawValue == nil {
return
}
var definitions struct {
Owned []*DefinitionKey `json:"owned"`
Required []*DefinitionKey `json:"required"`
}
if err = json.Unmarshal(*rawValue, &definitions); err != nil {
return
}
owned = definitions.Owned
required = definitions.Required
return
}
// GetRelatedImage returns the list of associated images for the operator
func (csv *ClusterServiceVersion) GetRelatedImages() (imageSet map[string]struct{}, err error) {
var objmap map[string]*json.RawMessage
imageSet = make(map[string]struct{})
if err = json.Unmarshal(csv.Spec, &objmap); err != nil {
return
}
rawValue, ok := objmap[relatedImages]
if !ok || rawValue == nil {
return
}
type relatedImage struct {
Name string `json:"name"`
Ref string `json:"image"`
}
var relatedImages []relatedImage
if err = json.Unmarshal(*rawValue, &relatedImages); err != nil {
return
}
for _, img := range relatedImages {
imageSet[img.Ref] = struct{}{}
}
return
}
// GetOperatorImages returns a list of any images used to run the operator.
// Currently this pulls any images in the pod specs of operator deployments.
func (csv *ClusterServiceVersion) GetOperatorImages() (map[string]struct{}, error) {
type dep struct {
Name string
Spec v1.DeploymentSpec
}
type strategySpec struct {
Deployments []dep
}
type strategy struct {
Name string `json:"strategy"`
Spec strategySpec `json:"spec"`
}
type csvSpec struct {
Install strategy
}
var spec csvSpec
if err := json.Unmarshal(csv.Spec, &spec); err != nil {
return nil, err
}
// this is the only install strategy we know about
if spec.Install.Name != "deployment" {
return nil, nil
}
images := map[string]struct{}{}
for _, d := range spec.Install.Spec.Deployments {
for _, c := range d.Spec.Template.Spec.Containers {
images[c.Image] = struct{}{}
}
for _, c := range d.Spec.Template.Spec.InitContainers {
images[c.Image] = struct{}{}
}
}
return images, nil
}
type Icon struct {
MediaType string `json:"mediatype"`
Base64data []byte `json:"base64data"`
}
// GetIcons returns the icons from the ClusterServiceVersion
func (csv *ClusterServiceVersion) GetIcons() ([]Icon, error) {
var objmap map[string]*json.RawMessage
if err := json.Unmarshal(csv.Spec, &objmap); err != nil {
return nil, err
}
rawValue, ok := objmap[icon]
if !ok || rawValue == nil {
return nil, nil
}
var icons []Icon
if err := json.Unmarshal(*rawValue, &icons); err != nil {
return nil, err
}
return icons, nil
}
// GetDescription returns the description from the ClusterServiceVersion
// If not defined, the function returns an empty string.
func (csv *ClusterServiceVersion) GetDescription() (string, error) {
var objmap map[string]*json.RawMessage
if err := json.Unmarshal(csv.Spec, &objmap); err != nil {
return "", err
}
rawValue, ok := objmap[description]
if !ok || rawValue == nil {
return "", nil
}
var desc string
if err := json.Unmarshal(*rawValue, &desc); err != nil {
return "", err
}
return desc, nil
}
// GetSubstitutesFor returns the name of the ClusterServiceVersion object that
// is substituted by this ClusterServiceVersion object.
//
// If not defined, the function returns an empty string.
func (csv *ClusterServiceVersion) GetSubstitutesFor() string {
substitutesFor, ok := csv.Annotations[substitutesForAnnotationKey]
if !ok {
return ""
}
return substitutesFor
}
func (csv *ClusterServiceVersion) UnmarshalJSON(data []byte) error {
if err := csv.UnmarshalSpec(data); err != nil {
return err
}
if err := csv.UnmarshalTypeMeta(data); err != nil {
return err
}
if err := csv.UnmarshalObjectMeta(data); err != nil {
return err
}
return nil
}
func (csv *ClusterServiceVersion) UnmarshalSpec(data []byte) error {
var m map[string]json.RawMessage
if err := json.Unmarshal(data, &m); err != nil {
return errors.New(prettyunmarshaler.NewJSONUnmarshalError(data, err).Pretty())
}
spec, ok := m["spec"]
if !ok {
return nil
}
csv.Spec = spec
return nil
}
func (csv *ClusterServiceVersion) UnmarshalTypeMeta(data []byte) error {
var t metav1.TypeMeta
if err := json.Unmarshal(data, &t); err != nil {
return errors.New(prettyunmarshaler.NewJSONUnmarshalError(data, err).Pretty())
}
csv.TypeMeta = t
return nil
}
func (csv *ClusterServiceVersion) UnmarshalObjectMeta(data []byte) error {
var o struct {
Metadata metav1.ObjectMeta `json:"metadata"`
}
if err := json.Unmarshal(data, &o); err != nil {
return errors.New(prettyunmarshaler.NewJSONUnmarshalError(data, err).Pretty())
}
csv.ObjectMeta = o.Metadata
return nil
}