@@ -42,20 +42,57 @@ type Plain struct {
4242}
4343
4444func RegistryV1ToHelmChart (ctx context.Context , rv1 fs.FS , installNamespace string , watchNamespaces []string ) (* chart.Chart , error ) {
45+ reg , err := ParseFS (ctx , rv1 )
46+ if err != nil {
47+ return nil , err
48+ }
49+
50+ plain , err := Convert (reg , installNamespace , watchNamespaces )
51+ if err != nil {
52+ return nil , err
53+ }
54+
55+ chrt := & chart.Chart {Metadata : & chart.Metadata {}}
56+ chrt .Metadata .Annotations = reg .CSV .GetAnnotations ()
57+ for _ , obj := range plain .Objects {
58+ jsonData , err := json .Marshal (obj )
59+ if err != nil {
60+ return nil , err
61+ }
62+ hash := sha256 .Sum256 (jsonData )
63+ chrt .Templates = append (chrt .Templates , & chart.File {
64+ Name : fmt .Sprintf ("object-%x.json" , hash [0 :8 ]),
65+ Data : jsonData ,
66+ })
67+ }
68+
69+ return chrt , nil
70+ }
71+
72+ // ParseFS converts the rv1 filesystem into a RegistryV1.
73+ // ParseFS expects the filesystem to conform to the registry+v1 format:
74+ // metadata/annotations.yaml
75+ // manifests/
76+ // - csv.yaml
77+ // - ...
78+ //
79+ // manifests directory does not contain subdirectories
80+ func ParseFS (ctx context.Context , rv1 fs.FS ) (RegistryV1 , error ) {
4581 l := log .FromContext (ctx )
4682
4783 reg := RegistryV1 {}
4884 annotationsFileData , err := fs .ReadFile (rv1 , filepath .Join ("metadata" , "annotations.yaml" ))
4985 if err != nil {
50- return nil , err
86+ return reg , err
5187 }
5288 annotationsFile := registry.AnnotationsFile {}
5389 if err := yaml .Unmarshal (annotationsFileData , & annotationsFile ); err != nil {
54- return nil , err
90+ return reg , err
5591 }
5692 reg .PackageName = annotationsFile .Annotations .PackageName
5793
5894 const manifestsDir = "manifests"
95+ foundCSV := false
5996 if err := fs .WalkDir (rv1 , manifestsDir , func (path string , e fs.DirEntry , err error ) error {
6097 if err != nil {
6198 return err
@@ -91,6 +128,7 @@ func RegistryV1ToHelmChart(ctx context.Context, rv1 fs.FS, installNamespace stri
91128 return err
92129 }
93130 reg .CSV = csv
131+ foundCSV = true
94132 default :
95133 reg .Others = append (reg .Others , * info .Object .(* unstructured.Unstructured ))
96134 }
@@ -100,14 +138,18 @@ func RegistryV1ToHelmChart(ctx context.Context, rv1 fs.FS, installNamespace stri
100138 }
101139 return nil
102140 }); err != nil {
103- return nil , err
141+ return reg , err
142+ }
143+
144+ if ! foundCSV {
145+ return reg , fmt .Errorf ("no ClusterServiceVersion found in %q" , manifestsDir )
104146 }
105147
106148 if err := copyMetadataPropertiesToCSV (& reg .CSV , rv1 ); err != nil {
107- return nil , err
149+ return reg , err
108150 }
109151
110- return toChart ( reg , installNamespace , watchNamespaces )
152+ return reg , nil
111153}
112154
113155// copyMetadataPropertiesToCSV copies properties from `metadata/propeties.yaml` (in the filesystem fsys) into
@@ -158,29 +200,6 @@ func copyMetadataPropertiesToCSV(csv *v1alpha1.ClusterServiceVersion, fsys fs.FS
158200 return nil
159201}
160202
161- func toChart (in RegistryV1 , installNamespace string , watchNamespaces []string ) (* chart.Chart , error ) {
162- plain , err := Convert (in , installNamespace , watchNamespaces )
163- if err != nil {
164- return nil , err
165- }
166-
167- chrt := & chart.Chart {Metadata : & chart.Metadata {}}
168- chrt .Metadata .Annotations = in .CSV .GetAnnotations ()
169- for _ , obj := range plain .Objects {
170- jsonData , err := json .Marshal (obj )
171- if err != nil {
172- return nil , err
173- }
174- hash := sha256 .Sum256 (jsonData )
175- chrt .Templates = append (chrt .Templates , & chart.File {
176- Name : fmt .Sprintf ("object-%x.json" , hash [0 :8 ]),
177- Data : jsonData ,
178- })
179- }
180-
181- return chrt , nil
182- }
183-
184203func validateTargetNamespaces (supportedInstallModes sets.Set [string ], installNamespace string , targetNamespaces []string ) error {
185204 set := sets .New [string ](targetNamespaces ... )
186205 switch {
0 commit comments