11package registry
22
33import (
4+ "encoding/json"
45 "fmt"
56 "strings"
67
@@ -41,33 +42,59 @@ type Bundle struct {
4142 Package string
4243 Channels []string
4344 BundleImage string
45+ version string
4446 csv * ClusterServiceVersion
4547 v1beta1crds []* apiextensionsv1beta1.CustomResourceDefinition
4648 v1crds []* apiextensionsv1.CustomResourceDefinition
4749 Dependencies []* Dependency
4850 Properties []* Property
51+ Annotations * Annotations
4952 cacheStale bool
5053}
5154
52- func NewBundle (name , pkgName string , channels []string , objs ... * unstructured.Unstructured ) * Bundle {
53- bundle := & Bundle {Name : name , Package : pkgName , Channels : channels , cacheStale : false }
55+ func NewBundle (name string , annotations * Annotations , objs ... * unstructured.Unstructured ) * Bundle {
56+ bundle := & Bundle {
57+ Name : name ,
58+ Package : annotations .PackageName ,
59+ Annotations : annotations ,
60+ }
5461 for _ , o := range objs {
5562 bundle .Add (o )
5663 }
64+
65+ if annotations == nil {
66+ return bundle
67+ }
68+ bundle .Channels = strings .Split (annotations .Channels , "," )
69+
5770 return bundle
5871}
5972
60- func NewBundleFromStrings (name , pkgName string , channels []string , objs []string ) (* Bundle , error ) {
73+ func NewBundleFromStrings (name , version , pkg , defaultChannel , channels , objs string ) (* Bundle , error ) {
74+ objStrs , err := BundleStringToObjectStrings (objs )
75+ if err != nil {
76+ return nil , err
77+ }
78+
6179 unstObjs := []* unstructured.Unstructured {}
62- for _ , o := range objs {
80+ for _ , o := range objStrs {
6381 dec := yaml .NewYAMLOrJSONDecoder (strings .NewReader (o ), 10 )
6482 unst := & unstructured.Unstructured {}
6583 if err := dec .Decode (unst ); err != nil {
6684 return nil , err
6785 }
6886 unstObjs = append (unstObjs , unst )
6987 }
70- return NewBundle (name , pkgName , channels , unstObjs ... ), nil
88+
89+ annotations := & Annotations {
90+ PackageName : pkg ,
91+ Channels : channels ,
92+ DefaultChannelName : defaultChannel ,
93+ }
94+ bundle := NewBundle (name , annotations , unstObjs ... )
95+ bundle .version = version
96+
97+ return bundle , nil
7198}
7299
73100func (b * Bundle ) Size () int {
@@ -86,10 +113,20 @@ func (b *Bundle) ClusterServiceVersion() (*ClusterServiceVersion, error) {
86113}
87114
88115func (b * Bundle ) Version () (string , error ) {
89- if err := b .cache (); err != nil {
116+ if b .version != "" {
117+ return b .version , nil
118+ }
119+
120+ var err error
121+ if err = b .cache (); err != nil {
90122 return "" , err
91123 }
92- return b .csv .GetVersion ()
124+
125+ if b .csv != nil {
126+ b .version , err = b .csv .GetVersion ()
127+ }
128+
129+ return b .version , err
93130}
94131
95132func (b * Bundle ) SkipRange () (string , error ) {
@@ -226,29 +263,33 @@ func (b *Bundle) AllProvidedAPIsInBundle() error {
226263 return nil
227264}
228265
229- func (b * Bundle ) Serialize () (csvName , bundleImage string , csvBytes []byte , bundleBytes []byte , err error ) {
266+ func (b * Bundle ) Serialize () (csvName , bundleImage string , csvBytes []byte , bundleBytes []byte , annotationBytes [] byte , err error ) {
230267 csvCount := 0
231268 for _ , obj := range b .Objects {
232269 objBytes , err := runtime .Encode (unstructured .UnstructuredJSONScheme , obj )
233270 if err != nil {
234- return "" , "" , nil , nil , err
271+ return "" , "" , nil , nil , nil , err
235272 }
236273 bundleBytes = append (bundleBytes , objBytes ... )
237274
238275 if obj .GroupVersionKind ().Kind == "ClusterServiceVersion" {
239276 csvName = obj .GetName ()
240277 csvBytes , err = runtime .Encode (unstructured .UnstructuredJSONScheme , obj )
241278 if err != nil {
242- return "" , "" , nil , nil , err
279+ return "" , "" , nil , nil , nil , err
243280 }
244281 csvCount += 1
245282 if csvCount > 1 {
246- return "" , "" , nil , nil , fmt .Errorf ("two csvs found in one bundle" )
283+ return "" , "" , nil , nil , nil , fmt .Errorf ("two csvs found in one bundle" )
247284 }
248285 }
249286 }
250287
251- return csvName , b .BundleImage , csvBytes , bundleBytes , nil
288+ if b .Annotations != nil {
289+ annotationBytes , err = json .Marshal (b .Annotations )
290+ }
291+
292+ return csvName , b .BundleImage , csvBytes , bundleBytes , annotationBytes , nil
252293}
253294
254295func (b * Bundle ) Images () (map [string ]struct {}, error ) {
0 commit comments