forked from operator-framework/operator-controller
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsource.go
More file actions
178 lines (155 loc) · 5.66 KB
/
source.go
File metadata and controls
178 lines (155 loc) · 5.66 KB
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
package source
import (
"encoding/json"
"errors"
"fmt"
"io/fs"
"path/filepath"
apiextensionsv1 "k8s.io/apiextensions-apiserver/pkg/apis/apiextensions/v1"
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
"k8s.io/apimachinery/pkg/runtime"
"k8s.io/cli-runtime/pkg/resource"
"sigs.k8s.io/yaml"
"github.com/operator-framework/api/pkg/operators/v1alpha1"
"github.com/operator-framework/operator-registry/alpha/property"
"github.com/operator-framework/operator-controller/internal/operator-controller/rukpak/bundle"
registry "github.com/operator-framework/operator-controller/internal/operator-controller/rukpak/operator-registry"
)
type BundleSource interface {
GetBundle() (bundle.RegistryV1, error)
}
type RegistryV1Properties struct {
Properties []property.Property `json:"properties"`
}
// identitySource is a bundle source that returns itself
type identitySource bundle.RegistryV1
func (r identitySource) GetBundle() (bundle.RegistryV1, error) {
return bundle.RegistryV1(r), nil
}
func FromBundle(rv1 bundle.RegistryV1) BundleSource {
return identitySource(rv1)
}
// FromFS returns a BundleSource that loads a registry+v1 bundle from a filesystem.
// The filesystem is expected to conform to the registry+v1 format:
// metadata/annotations.yaml
// metadata/properties.yaml
// manifests/
// - csv.yaml
// - ...
//
// manifests directory should not contain subdirectories
func FromFS(fs fs.FS) BundleSource {
return fsBundleSource{
FS: fs,
}
}
type fsBundleSource struct {
FS fs.FS
}
func (f fsBundleSource) GetBundle() (bundle.RegistryV1, error) {
reg := bundle.RegistryV1{}
annotationsFileData, err := fs.ReadFile(f.FS, filepath.Join("metadata", "annotations.yaml"))
if err != nil {
return reg, err
}
annotationsFile := registry.AnnotationsFile{}
if err := yaml.Unmarshal(annotationsFileData, &annotationsFile); err != nil {
return reg, err
}
reg.PackageName = annotationsFile.Annotations.PackageName
const manifestsDir = "manifests"
foundCSV := false
if err := fs.WalkDir(f.FS, manifestsDir, func(path string, e fs.DirEntry, err error) error {
if err != nil {
return err
}
if e.IsDir() {
if path == manifestsDir {
return nil
}
return fmt.Errorf("subdirectories are not allowed within the %q directory of the bundle image filesystem: found %q", manifestsDir, path)
}
manifestFile, err := f.FS.Open(path)
if err != nil {
return err
}
defer manifestFile.Close()
result := resource.NewLocalBuilder().Unstructured().Flatten().Stream(manifestFile, path).Do()
if err := result.Err(); err != nil {
return err
}
if err := result.Visit(func(info *resource.Info, err error) error {
if err != nil {
return err
}
switch info.Object.GetObjectKind().GroupVersionKind().Kind {
case "ClusterServiceVersion":
csv := v1alpha1.ClusterServiceVersion{}
if err := runtime.DefaultUnstructuredConverter.FromUnstructured(info.Object.(*unstructured.Unstructured).Object, &csv); err != nil {
return err
}
reg.CSV = csv
foundCSV = true
case "CustomResourceDefinition":
crd := apiextensionsv1.CustomResourceDefinition{}
if err := runtime.DefaultUnstructuredConverter.FromUnstructured(info.Object.(*unstructured.Unstructured).Object, &crd); err != nil {
return err
}
reg.CRDs = append(reg.CRDs, crd)
default:
reg.Others = append(reg.Others, *info.Object.(*unstructured.Unstructured))
}
return nil
}); err != nil {
return fmt.Errorf("error parsing objects in %q: %v", path, err)
}
return nil
}); err != nil {
return reg, err
}
if !foundCSV {
return reg, fmt.Errorf("no ClusterServiceVersion found in %q", manifestsDir)
}
if err := copyMetadataPropertiesToCSV(®.CSV, f.FS); err != nil {
return reg, err
}
return reg, nil
}
// copyMetadataPropertiesToCSV copies properties from `metadata/propeties.yaml` (in the filesystem fsys) into
// the CSV's `.metadata.annotations['olm.properties']` value, preserving any properties that are already
// present in the annotations.
func copyMetadataPropertiesToCSV(csv *v1alpha1.ClusterServiceVersion, fsys fs.FS) error {
var allProperties []property.Property
// First load existing properties from the CSV. We want to preserve these.
if csvPropertiesJSON, ok := csv.Annotations["olm.properties"]; ok {
var csvProperties []property.Property
if err := json.Unmarshal([]byte(csvPropertiesJSON), &csvProperties); err != nil {
return fmt.Errorf("failed to unmarshal csv.metadata.annotations['olm.properties']: %w", err)
}
allProperties = append(allProperties, csvProperties...)
}
// Next, load properties from the metadata/properties.yaml file, if it exists.
metadataPropertiesJSON, err := fs.ReadFile(fsys, filepath.Join("metadata", "properties.yaml"))
if err != nil && !errors.Is(err, fs.ErrNotExist) {
return fmt.Errorf("failed to read properties.yaml file: %w", err)
}
// If there are no properties, we can stick with whatever
// was already present in the CSV annotations.
if len(metadataPropertiesJSON) == 0 {
return nil
}
// Otherwise, we need to parse the properties.yaml file and
// append its properties into the CSV annotation.
var metadataProperties RegistryV1Properties
if err := yaml.Unmarshal(metadataPropertiesJSON, &metadataProperties); err != nil {
return fmt.Errorf("failed to unmarshal metadata/properties.yaml: %w", err)
}
allProperties = append(allProperties, metadataProperties.Properties...)
// Lastly re-marshal all the properties back into a JSON array and update the CSV annotation
allPropertiesJSON, err := json.Marshal(allProperties)
if err != nil {
return fmt.Errorf("failed to marshal registry+v1 properties to json: %w", err)
}
csv.Annotations["olm.properties"] = string(allPropertiesJSON)
return nil
}