Skip to content

Commit 37d2e85

Browse files
committed
Handle valuesFiles from cozypkg.cozystack.io/values-files annotation
Signed-off-by: Andrei Kvapil <kvapss@gmail.com>
1 parent 29deec6 commit 37d2e85

1 file changed

Lines changed: 48 additions & 25 deletions

File tree

main.go

Lines changed: 48 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -245,6 +245,7 @@ func resolveChartRef(ctx context.Context, cl client.Client, hr *v2.HelmRelease)
245245
switch refKind {
246246
case "ExternalArtifact":
247247
// ExternalArtifact - fetch the resource to extract chart name from artifact path
248+
// Note: For valuesFiles, we now use annotation, so this is mainly for chartName/chartVersion
248249
extArtifactGVR := schema.GroupVersionResource{
249250
Group: "source.toolkit.fluxcd.io", Version: "v1", Resource: "externalartifacts",
250251
}
@@ -255,6 +256,8 @@ func resolveChartRef(ctx context.Context, cl client.Client, hr *v2.HelmRelease)
255256
Kind: "ExternalArtifact",
256257
})
257258
if err := cl.Get(ctx, types.NamespacedName{Namespace: refNS, Name: refName}, extArtifact); err != nil {
259+
// If ExternalArtifact is not found, return error - caller should handle this gracefully
260+
// For valuesFiles, we use annotation instead, so this is mainly for chartName
258261
return "", "", nil, fmt.Errorf("failed to get ExternalArtifact %s/%s: %w", refNS, refName, err)
259262
}
260263

@@ -271,6 +274,7 @@ func resolveChartRef(ctx context.Context, cl client.Client, hr *v2.HelmRelease)
271274
return "", "", nil, fmt.Errorf("ExternalArtifact %s/%s has unexpected artifact path format: %s", refNS, refName, artifactPath)
272275
}
273276
chartName = parts[2]
277+
// ExternalArtifact doesn't provide valuesFiles - we use annotation instead
274278
return chartName, "", nil, nil
275279

276280
case "HelmChart":
@@ -351,21 +355,32 @@ func findArtifactGeneratorForExternalArtifact(ctx context.Context, cl client.Cli
351355
return nil, fmt.Errorf("ArtifactGenerator for ExternalArtifact %s/%s not found", extArtifactNS, extArtifactName)
352356
}
353357

354-
// getChartInfo gets chart name and version from chart or chartRef.
355-
func getChartInfo(ctx context.Context, cl client.Client, hr *v2.HelmRelease) (chartName, chartVersion string, err error) {
356-
if hr.Spec.ChartRef != nil {
357-
name, version, _, err := resolveChartRef(ctx, cl, hr)
358-
if err != nil {
359-
return "", "", err
360-
}
361-
return name, version, nil
358+
// getChartInfo gets chart name and version from local chart's Chart.yaml.
359+
func getChartInfo(chartDir string) (chartName, chartVersion string, err error) {
360+
if chartDir == "" {
361+
return "", "", fmt.Errorf("chartDir is empty")
362362
}
363363

364-
if hr.Spec.Chart != nil {
365-
return hr.Spec.Chart.Spec.Chart, hr.Spec.Chart.Spec.Version, nil
364+
chartYamlPath := filepath.Join(chartDir, "Chart.yaml")
365+
data, err := os.ReadFile(chartYamlPath)
366+
if err != nil {
367+
return "", "", fmt.Errorf("failed to read Chart.yaml from %s: %w", chartDir, err)
366368
}
367369

368-
return "", "", fmt.Errorf("neither chart nor chartRef is set")
370+
// Parse Chart.yaml
371+
var chartMeta struct {
372+
Name string `yaml:"name"`
373+
Version string `yaml:"version"`
374+
}
375+
if err := sigsyaml.Unmarshal(data, &chartMeta); err != nil {
376+
return "", "", fmt.Errorf("failed to parse Chart.yaml: %w", err)
377+
}
378+
379+
if chartMeta.Name == "" {
380+
return "", "", fmt.Errorf("chart name is empty in Chart.yaml")
381+
}
382+
383+
return chartMeta.Name, chartMeta.Version, nil
369384
}
370385

371386
// mergedValues merges valuesFiles and inline .spec.values in the given HelmRelease.
@@ -374,14 +389,22 @@ func mergedValues(ctx context.Context, cl client.Client, hr *v2.HelmRelease, cha
374389

375390
var valuesFiles []string
376391

377-
// Get valuesFiles from chart or chartRef
378-
if hr.Spec.ChartRef != nil {
379-
_, _, vf, err := resolveChartRef(ctx, cl, hr)
380-
if err != nil {
381-
return nil, fmt.Errorf("failed to resolve chartRef: %w", err)
392+
// First, try to get valuesFiles from annotation (set by operator)
393+
if ann := hr.GetAnnotations(); ann != nil {
394+
if vfStr, ok := ann["cozypkg.cozystack.io/values-files"]; ok {
395+
// Format: comma-separated string, e.g., "values.yaml,values-cilium.yaml"
396+
if vfStr != "" {
397+
valuesFiles = strings.Split(vfStr, ",")
398+
// Trim whitespace from each value
399+
for i, vf := range valuesFiles {
400+
valuesFiles[i] = strings.TrimSpace(vf)
401+
}
402+
}
382403
}
383-
valuesFiles = vf
384-
} else if hr.Spec.Chart != nil {
404+
}
405+
406+
// Fallback: Get valuesFiles from chart spec if annotation not found
407+
if len(valuesFiles) == 0 && hr.Spec.Chart != nil {
385408
valuesFiles = hr.Spec.Chart.Spec.ValuesFiles
386409
}
387410

@@ -723,7 +746,7 @@ func cmdApply() *cobra.Command {
723746
var chartVer, cfgDigest string
724747
if !plain {
725748
cfgDigest = fluxchartutil.DigestValues(digest.Canonical, vals).String()
726-
_, ver, err := getChartInfo(ctx, cl, hr)
749+
_, ver, err := getChartInfo(chartDir)
727750
if err == nil {
728751
chartVer = ver
729752
}
@@ -747,7 +770,7 @@ func cmdApply() *cobra.Command {
747770
}
748771

749772
if !plain {
750-
markSuccess(ctx, cl, rec, hr, chartVer, cfgDigest)
773+
markSuccess(ctx, cl, rec, hr, chartVer, cfgDigest, chartDir)
751774
}
752775
return nil
753776
})
@@ -1091,8 +1114,8 @@ func cmdList() *cobra.Command {
10911114
}
10921115

10931116
// newHistoryEntry creates a v2.Snapshot for status.history.
1094-
func newHistoryEntry(ctx context.Context, cl client.Client, hr *v2.HelmRelease, chartVersion, cfgDigest string) *v2.Snapshot {
1095-
chartName, _, err := getChartInfo(ctx, cl, hr)
1117+
func newHistoryEntry(hr *v2.HelmRelease, chartVersion, cfgDigest string, chartDir string) *v2.Snapshot {
1118+
chartName, _, err := getChartInfo(chartDir)
10961119
if err != nil {
10971120
// Log the error and fall back to the HelmRelease name.
10981121
log.Printf("could not get chart info for %s/%s: %v", hr.Namespace, hr.Name, err)
@@ -1112,8 +1135,8 @@ func newHistoryEntry(ctx context.Context, cl client.Client, hr *v2.HelmRelease,
11121135
}
11131136

11141137
// markSuccess sets Ready=True and emits a normal event.
1115-
func markSuccess(ctx context.Context, cl client.Client, rec record.EventRecorder, hr *v2.HelmRelease, chartVer, cfgDigest string) {
1116-
chartName, _, err := getChartInfo(ctx, cl, hr)
1138+
func markSuccess(ctx context.Context, cl client.Client, rec record.EventRecorder, hr *v2.HelmRelease, chartVer, cfgDigest string, chartDir string) {
1139+
chartName, _, err := getChartInfo(chartDir)
11171140
if err != nil {
11181141
// Log the error and fall back to the HelmRelease name for the message.
11191142
log.Printf("could not get chart info for %s/%s: %v", hr.Namespace, hr.Name, err)
@@ -1124,7 +1147,7 @@ func markSuccess(ctx context.Context, cl client.Client, rec record.EventRecorder
11241147
conditions.MarkTrue(hr, v2.ReleasedCondition, v2.UpgradeSucceededReason, msg)
11251148
conditions.MarkTrue(hr, fluxmeta.ReadyCondition, v2.UpgradeSucceededReason, msg)
11261149

1127-
hr.Status.History = append(hr.Status.History, newHistoryEntry(ctx, cl, hr, chartVer, cfgDigest))
1150+
hr.Status.History = append(hr.Status.History, newHistoryEntry(hr, chartVer, cfgDigest, chartDir))
11281151
hr.Status.Failures = 0
11291152
hr.Status.ObservedGeneration = hr.Generation
11301153
_ = cl.Status().Update(ctx, hr)

0 commit comments

Comments
 (0)