@@ -42,20 +42,57 @@ type Plain struct {
42
42
}
43
43
44
44
func 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 ) {
45
81
l := log .FromContext (ctx )
46
82
47
83
reg := RegistryV1 {}
48
84
annotationsFileData , err := fs .ReadFile (rv1 , filepath .Join ("metadata" , "annotations.yaml" ))
49
85
if err != nil {
50
- return nil , err
86
+ return reg , err
51
87
}
52
88
annotationsFile := registry.AnnotationsFile {}
53
89
if err := yaml .Unmarshal (annotationsFileData , & annotationsFile ); err != nil {
54
- return nil , err
90
+ return reg , err
55
91
}
56
92
reg .PackageName = annotationsFile .Annotations .PackageName
57
93
58
94
const manifestsDir = "manifests"
95
+ foundCSV := false
59
96
if err := fs .WalkDir (rv1 , manifestsDir , func (path string , e fs.DirEntry , err error ) error {
60
97
if err != nil {
61
98
return err
@@ -91,6 +128,7 @@ func RegistryV1ToHelmChart(ctx context.Context, rv1 fs.FS, installNamespace stri
91
128
return err
92
129
}
93
130
reg .CSV = csv
131
+ foundCSV = true
94
132
default :
95
133
reg .Others = append (reg .Others , * info .Object .(* unstructured.Unstructured ))
96
134
}
@@ -100,14 +138,18 @@ func RegistryV1ToHelmChart(ctx context.Context, rv1 fs.FS, installNamespace stri
100
138
}
101
139
return nil
102
140
}); 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 )
104
146
}
105
147
106
148
if err := copyMetadataPropertiesToCSV (& reg .CSV , rv1 ); err != nil {
107
- return nil , err
149
+ return reg , err
108
150
}
109
151
110
- return toChart ( reg , installNamespace , watchNamespaces )
152
+ return reg , nil
111
153
}
112
154
113
155
// copyMetadataPropertiesToCSV copies properties from `metadata/propeties.yaml` (in the filesystem fsys) into
@@ -158,29 +200,6 @@ func copyMetadataPropertiesToCSV(csv *v1alpha1.ClusterServiceVersion, fsys fs.FS
158
200
return nil
159
201
}
160
202
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
-
184
203
func validateTargetNamespaces (supportedInstallModes sets.Set [string ], installNamespace string , targetNamespaces []string ) error {
185
204
set := sets .New [string ](targetNamespaces ... )
186
205
switch {
0 commit comments