@@ -8,57 +8,134 @@ import (
88
99 "sigs.k8s.io/controller-runtime/pkg/client"
1010 "sigs.k8s.io/yaml"
11+
12+ "github.com/operator-framework/api/pkg/operators/v1alpha1"
13+ "github.com/operator-framework/operator-registry/alpha/property"
14+
15+ "github.com/operator-framework/operator-controller/internal/operator-controller/rukpak/bundle/source"
16+ registry "github.com/operator-framework/operator-controller/internal/operator-controller/rukpak/operator-registry"
1117)
1218
1319const (
1420 BundlePathAnnotations = "metadata/annotations.yaml"
1521 BundlePathProperties = "metadata/properties.yaml"
1622 BundlePathManifests = "manifests"
17- BundlePathCSV = BundlePathManifests + "/csv.yaml"
1823)
1924
20- func NewBundleFS () fstest.MapFS {
21- annotationsYml := `
22- annotations:
23- operators.operatorframework.io.bundle.mediatype.v1: registry+v1
24- operators.operatorframework.io.bundle.package.v1: test
25- `
26-
27- propertiesYml := `
28- properties:
29- - type: "from-file-key"
30- value: "from-file-value"
31- `
32-
33- csvYml := `
34- apiVersion: operators.operatorframework.io/v1alpha1
35- kind: ClusterServiceVersion
36- metadata:
37- name: test.v1.0.0
38- annotations:
39- olm.properties: '[{"type":"from-csv-annotations-key", "value":"from-csv-annotations-value"}]'
40- spec:
41- installModes:
42- - type: AllNamespaces
43- supported: true
44- `
45-
46- return fstest.MapFS {
47- BundlePathAnnotations : & fstest.MapFile {Data : []byte (strings .Trim (annotationsYml , "\n " ))},
48- BundlePathProperties : & fstest.MapFile {Data : []byte (strings .Trim (propertiesYml , "\n " ))},
49- BundlePathCSV : & fstest.MapFile {Data : []byte (strings .Trim (csvYml , "\n " ))},
25+ type bundleData struct {
26+ annotations * registry.Annotations
27+ properties []property.Property
28+ resources map [string ]client.Object
29+ }
30+
31+ type BundleFSOption func (* bundleData )
32+
33+ // WithPackageName is an option for NewBundleFS used to set the package name annotation in the
34+ // bundle filesystem metadata/annotations.yaml file
35+ func WithPackageName (packageName string ) BundleFSOption {
36+ return func (data * bundleData ) {
37+ if data .annotations == nil {
38+ data .annotations = & registry.Annotations {}
39+ }
40+ data .annotations .PackageName = packageName
41+ }
42+ }
43+
44+ // WithChannels is an option for NewBundleFS used to set the channels annotation in the
45+ // bundle filesystem metadata/annotations.yaml file
46+ func WithChannels (channels ... string ) BundleFSOption {
47+ return func (data * bundleData ) {
48+ if data .annotations == nil {
49+ data .annotations = & registry.Annotations {}
50+ }
51+ data .annotations .Channels = strings .Join (channels , "," )
52+ }
53+ }
54+
55+ // WithDefaultChannel is an option for NewBundleFS used to set the channel annotation in the
56+ // bundle filesystem metadata/annotations.yaml file
57+ func WithDefaultChannel (channel string ) BundleFSOption {
58+ return func (data * bundleData ) {
59+ if data .annotations == nil {
60+ data .annotations = & registry.Annotations {}
61+ }
62+ data .annotations .DefaultChannelName = channel
63+ }
64+ }
65+
66+ // WithBundleProperty is an options for NewBundleFS used to add a property to the list of properties
67+ // in the bundle filesystem metadata/properties.yaml file
68+ func WithBundleProperty (propertyType string , value string ) BundleFSOption {
69+ return func (data * bundleData ) {
70+ data .properties = append (data .properties , property.Property {
71+ Type : propertyType ,
72+ Value : []byte (`"` + value + `"` ),
73+ })
5074 }
5175}
5276
53- func AddManifest (bundleFS fstest.MapFS , obj client.Object ) error {
54- gvk := obj .GetObjectKind ().GroupVersionKind ()
55- manifestName := fmt .Sprintf ("%s%s_%s_%s%s.yaml" , gvk .Group , gvk .Version , gvk .Kind , obj .GetNamespace (), obj .GetName ())
56- bytes , err := yaml .Marshal (obj )
57- if err != nil {
58- return err
77+ // WithBundleResource is an option for NewBundleFS use to add the yaml representation of resource to the
78+ // path manifests/<resourceName>.yaml on the bundles filesystem
79+ func WithBundleResource (resourceName string , resource client.Object ) BundleFSOption {
80+ return func (data * bundleData ) {
81+ if data .resources == nil {
82+ data .resources = make (map [string ]client.Object )
83+ }
84+ data .resources [resourceName ] = resource
5985 }
60- bundleFS [filepath .Join (BundlePathManifests , manifestName )] = & fstest.MapFile {
61- Data : bytes ,
86+ }
87+
88+ // WithCSV is an optiona for NewBundleFS used to add the yaml representation of csv to the
89+ // path manifests/csv.yaml on the bundle filesystem
90+ func WithCSV (csv v1alpha1.ClusterServiceVersion ) BundleFSOption {
91+ return func (data * bundleData ) {
92+ if data .resources == nil {
93+ data .resources = make (map [string ]client.Object )
94+ }
95+ data .resources ["csv.yaml" ] = & csv
6296 }
63- return nil
97+ }
98+
99+ // NewBundleFS creates a registry+v1 bundle filesystem with the applied options
100+ // By default, an empty registry+v1 bundle filesystem will be returned
101+ func NewBundleFS (opts ... BundleFSOption ) fstest.MapFS {
102+ bundleData := & bundleData {}
103+ for _ , applyOpt := range opts {
104+ applyOpt (bundleData )
105+ }
106+ bundleFS := fstest.MapFS {}
107+
108+ // Add annotations metadata
109+ if bundleData .annotations != nil {
110+ annotationsYml , err := yaml .Marshal (registry.AnnotationsFile {
111+ Annotations : * bundleData .annotations ,
112+ })
113+ if err != nil {
114+ panic (fmt .Errorf ("error building bundle fs: %w" , err ))
115+ }
116+ bundleFS [BundlePathAnnotations ] = & fstest.MapFile {Data : annotationsYml }
117+ }
118+
119+ // Add property metadata
120+ if len (bundleData .properties ) > 0 {
121+ propertiesYml , err := yaml .Marshal (source.RegistryV1Properties {
122+ Properties : bundleData .properties ,
123+ })
124+ if err != nil {
125+ panic (fmt .Errorf ("error building bundle fs: %w" , err ))
126+ }
127+ bundleFS [BundlePathProperties ] = & fstest.MapFile {Data : propertiesYml }
128+ }
129+
130+ // Add resources
131+ for name , obj := range bundleData .resources {
132+ resourcePath := filepath .Join (BundlePathManifests , name )
133+ resourceYml , err := yaml .Marshal (obj )
134+ if err != nil {
135+ panic (fmt .Errorf ("error building bundle fs: %w" , err ))
136+ }
137+ bundleFS [resourcePath ] = & fstest.MapFile {Data : resourceYml }
138+ }
139+
140+ return bundleFS
64141}
0 commit comments