Address comments from the MR

Signed-off-by: Alexandr Danilin <alexandr.danilin@ooma.com>
pull/31524/head
Alexandr Danilin 7 months ago
parent bc6b6199ba
commit 862a5dfb4f

@ -262,6 +262,8 @@ func GetLocalPath(repo, chartpath string) (string, error) {
return depPath, nil return depPath, nil
} }
// IsLocalDependency checks if the repository path is a local file dependency.
// Local dependencies use the "file://" protocol prefix.
func IsLocalDependency(repositoryPath string) bool { func IsLocalDependency(repositoryPath string) bool {
return strings.HasPrefix(repositoryPath, "file://") return strings.HasPrefix(repositoryPath, "file://")
} }

@ -23,6 +23,7 @@ import (
"io" "io"
stdfs "io/fs" stdfs "io/fs"
"log" "log"
"log/slog"
"net/url" "net/url"
"os" "os"
"path/filepath" "path/filepath"
@ -192,29 +193,10 @@ func (m *Manager) Update() error {
} }
} }
// Check for circular dependencies in local dependencies
if err := m.checkCircularDeps(req, nil); err != nil {
return err
}
// Do resolution for each local dependency first. Local dependencies may // Do resolution for each local dependency first. Local dependencies may
// have their own dependencies which must be resolved. // have their own dependencies which must be resolved.
for _, dep := range req { if err := m.updateLocalDeps(req, nil); err != nil {
if !resolver.IsLocalDependency(dep.Repository) { return err
continue
}
chartpath, err := resolver.GetLocalPath(dep.Repository, m.ChartPath)
if err != nil {
return err
}
man := *m
// no need to update repositories, it is already done in main chart
man.SkipUpdate = true
man.ChartPath = chartpath
err = man.Update()
if err != nil {
return err
}
} }
// Now we need to find out which version of a chart best satisfies the // Now we need to find out which version of a chart best satisfies the
@ -946,14 +928,14 @@ func key(name string) (string, error) {
return hex.EncodeToString(hash.Sum(nil)), nil return hex.EncodeToString(hash.Sum(nil)), nil
} }
// checkCircularDeps checks local dependencies for circular dependency issue. // updateLocalDeps recursively updates local dependencies and detects circular dependencies.
// When local charts depend on each other, helm will quit at the very beginning with the clear message. func (m *Manager) updateLocalDeps(deps []*chart.Dependency, chain []string) error {
func (m *Manager) checkCircularDeps(deps []*chart.Dependency, chain []string) error {
absPath, err := filepath.Abs(m.ChartPath) absPath, err := filepath.Abs(m.ChartPath)
if err != nil { if err != nil {
return err return err
} }
// Check for circular dependency
for i, visited := range chain { for i, visited := range chain {
if visited == absPath { if visited == absPath {
cycle := append(chain[i:], absPath) cycle := append(chain[i:], absPath)
@ -961,10 +943,7 @@ func (m *Manager) checkCircularDeps(deps []*chart.Dependency, chain []string) er
} }
} }
// Create a new chain with the current path to avoid modifying the caller's slice chain = append(chain, absPath)
newChain := make([]string, len(chain)+1)
copy(newChain, chain)
newChain[len(chain)] = absPath
for _, dep := range deps { for _, dep := range deps {
if !resolver.IsLocalDependency(dep.Repository) { if !resolver.IsLocalDependency(dep.Repository) {
@ -974,13 +953,22 @@ func (m *Manager) checkCircularDeps(deps []*chart.Dependency, chain []string) er
if err != nil { if err != nil {
return err return err
} }
c, err := loader.LoadDir(chartpath) c, err := loader.LoadDir(chartpath)
if err != nil { if err != nil {
return err return err
} }
man := *m man := *m
man.SkipUpdate = true
man.ChartPath = chartpath man.ChartPath = chartpath
if err := man.checkCircularDeps(c.Metadata.Dependencies, newChain); err != nil { slog.Debug("Recursively updating dependencies for local chart", "chart", dep.Name, "path", chartpath)
if err := man.updateLocalDeps(c.Metadata.Dependencies, chain); err != nil {
return err
}
if err := man.Update(); err != nil {
return err return err
} }
} }

Loading…
Cancel
Save