Skip to content

Commit c846c1a

Browse files
MeNsaaHdjeebus
andauthored
Fix helm local dependency build (#393)
Signed-off-by: Mmadu Manasseh <manasseh.mmadu@zapier.com> Co-authored-by: joe.lombrozo <joe.lombrozo@zapier.com>
1 parent 678d020 commit c846c1a

File tree

3 files changed

+364
-5
lines changed

3 files changed

+364
-5
lines changed

pkg/argo_client/manifests.go

Lines changed: 76 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ import (
2626
"github.com/zapier/kubechecks/pkg/git"
2727
"github.com/zapier/kubechecks/pkg/kustomize"
2828
"github.com/zapier/kubechecks/pkg/vcs"
29+
"gopkg.in/yaml.v3"
2930
"sigs.k8s.io/kustomize/kyaml/filesys"
3031
)
3132

@@ -358,6 +359,61 @@ func copyFile(srcpath, dstpath string) error {
358359
return err
359360
}
360361

362+
// processLocalHelmDependency handles copying local helm dependencies to the temp directory
363+
func processLocalHelmDependency(
364+
srcAppPath string,
365+
destAppDir string,
366+
dependencyPath string,
367+
) error {
368+
// Remove the file:// prefix if present
369+
cleanPath := strings.TrimPrefix(dependencyPath, "file://")
370+
371+
// Resolve the absolute path of the dependency
372+
absDepPath := filepath.Join(srcAppPath, cleanPath)
373+
374+
// Create the destination path in the temp directory
375+
destDepPath := filepath.Join(destAppDir, cleanPath)
376+
377+
// Create the charts directory if it doesn't exist
378+
if err := os.MkdirAll(destDepPath, os.ModePerm); err != nil {
379+
return errors.Wrapf(err, "failed to create charts directory %s", destDepPath)
380+
}
381+
382+
// Copy the entire dependency directory
383+
log.Debug().Msgf("copying helm dependency from %s to %s", absDepPath, destDepPath)
384+
if err := copyDir(filesys.MakeFsOnDisk(), absDepPath, destDepPath); err != nil {
385+
return errors.Wrapf(err, "failed to copy helm dependency from %s to %s", absDepPath, destDepPath)
386+
}
387+
388+
return nil
389+
}
390+
391+
// parseChartYAML reads and parses a Chart.yaml file to extract dependencies
392+
func parseChartYAML(chartPath string) ([]struct {
393+
Name string `yaml:"name"`
394+
Version string `yaml:"version"`
395+
Repository string `yaml:"repository"`
396+
}, error) {
397+
content, err := os.ReadFile(chartPath)
398+
if err != nil {
399+
return nil, errors.Wrap(err, "failed to read Chart.yaml")
400+
}
401+
402+
var chart struct {
403+
Dependencies []struct {
404+
Name string `yaml:"name"`
405+
Version string `yaml:"version"`
406+
Repository string `yaml:"repository"`
407+
} `yaml:"dependencies"`
408+
}
409+
410+
if err := yaml.Unmarshal(content, &chart); err != nil {
411+
return nil, errors.Wrap(err, "failed to parse Chart.yaml")
412+
}
413+
414+
return chart.Dependencies, nil
415+
}
416+
361417
func packageApp(
362418
ctx context.Context,
363419
source v1alpha1.ApplicationSource,
@@ -385,7 +441,6 @@ func packageApp(
385441
relKustPath := filepath.Join(source.Path, "kustomization.yaml")
386442
absKustPath := filepath.Join(destDir, relKustPath)
387443
if fsIface.Exists(absKustPath) {
388-
389444
files, _, err := kustomize.ProcessKustomizationFile(sourceFS, relKustPath)
390445
if err != nil {
391446
return "", errors.Wrap(err, "failed to process kustomization dependencies")
@@ -398,7 +453,27 @@ func packageApp(
398453
}
399454
}
400455

456+
// Process helm dependencies
401457
if source.Helm != nil {
458+
// Handle local helm dependencies from Chart.yaml
459+
chartPath := filepath.Join(srcAppPath, "Chart.yaml")
460+
if _, err := os.Stat(chartPath); err == nil {
461+
log.Debug().Msg("processing helm dependencies")
462+
deps, err := parseChartYAML(chartPath)
463+
if err != nil {
464+
return "", errors.Wrap(err, "failed to parse Chart.yaml")
465+
}
466+
467+
for _, dep := range deps {
468+
if strings.HasPrefix(dep.Repository, "file://") {
469+
log.Debug().Msgf("processing local helm dependency %s", dep.Repository)
470+
if err := processLocalHelmDependency(srcAppPath, destAppDir, dep.Repository); err != nil {
471+
return "", errors.Wrapf(err, "failed to process local helm dependency %s", dep.Name)
472+
}
473+
}
474+
}
475+
}
476+
402477
refsByName := make(map[string]v1alpha1.ApplicationSource)
403478
for _, ref := range refs {
404479
refsByName[ref.Ref] = ref

0 commit comments

Comments
 (0)