Skip to content

Commit e8ce6c8

Browse files
author
Per Goncalves da Silva
committed
Decompose RegistryV1ToHelmChart function
Signed-off-by: Per Goncalves da Silva <[email protected]>
1 parent c0a2964 commit e8ce6c8

File tree

2 files changed

+77
-95
lines changed

2 files changed

+77
-95
lines changed

internal/rukpak/convert/registryv1.go

Lines changed: 33 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -42,16 +42,44 @@ type Plain struct {
4242
}
4343

4444
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+
func ParseFS(ctx context.Context, rv1 fs.FS) (RegistryV1, error) {
4573
l := log.FromContext(ctx)
4674

4775
reg := RegistryV1{}
4876
annotationsFileData, err := fs.ReadFile(rv1, filepath.Join("metadata", "annotations.yaml"))
4977
if err != nil {
50-
return nil, err
78+
return reg, err
5179
}
5280
annotationsFile := registry.AnnotationsFile{}
5381
if err := yaml.Unmarshal(annotationsFileData, &annotationsFile); err != nil {
54-
return nil, err
82+
return reg, err
5583
}
5684
reg.PackageName = annotationsFile.Annotations.PackageName
5785

@@ -100,14 +128,14 @@ func RegistryV1ToHelmChart(ctx context.Context, rv1 fs.FS, installNamespace stri
100128
}
101129
return nil
102130
}); err != nil {
103-
return nil, err
131+
return reg, err
104132
}
105133

106134
if err := copyMetadataPropertiesToCSV(&reg.CSV, rv1); err != nil {
107-
return nil, err
135+
return reg, err
108136
}
109137

110-
return toChart(reg, installNamespace, watchNamespaces)
138+
return reg, nil
111139
}
112140

113141
// copyMetadataPropertiesToCSV copies properties from `metadata/propeties.yaml` (in the filesystem fsys) into
@@ -158,29 +186,6 @@ func copyMetadataPropertiesToCSV(csv *v1alpha1.ClusterServiceVersion, fsys fs.FS
158186
return nil
159187
}
160188

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-
184189
func validateTargetNamespaces(supportedInstallModes sets.Set[string], installNamespace string, targetNamespaces []string) error {
185190
set := sets.New[string](targetNamespaces...)
186191
switch {

0 commit comments

Comments
 (0)