Make `version` optional for local chart dependencies

Fixes #10095
Signed-off-by: Felipe Santos <felipecassiors@gmail.com>
pull/10096/head
Felipe Santos 4 years ago
parent 0d0f91d1ce
commit bb66fb3365

@ -70,7 +70,7 @@ the dependency charts stored locally. The path should start with a prefix of
If the dependency chart is retrieved locally, it is not required to have the If the dependency chart is retrieved locally, it is not required to have the
repository added to helm by "helm add repo". Version matching is also supported repository added to helm by "helm add repo". Version matching is also supported
for this case. for this case, but optional.
` `
const dependencyListDesc = ` const dependencyListDesc = `

@ -58,31 +58,36 @@ func (r *Resolver) Resolve(reqs []*chart.Dependency, repoNames map[string]string
locked := make([]*chart.Dependency, len(reqs)) locked := make([]*chart.Dependency, len(reqs))
missing := []string{} missing := []string{}
for i, d := range reqs { for i, d := range reqs {
constraint, err := semver.NewConstraint(d.Version) var constraint *semver.Constraints
if err != nil { var chartpath string
return nil, errors.Wrapf(err, "dependency %q has an invalid version/constraint format", d.Name) var err error
}
if d.Repository == "" { // If version is defined
// Local chart subfolder if d.Version != "" {
if _, err := GetLocalPath(filepath.Join("charts", d.Name), r.chartpath); err != nil { constraint, err = semver.NewConstraint(d.Version)
return nil, err if err != nil {
} return nil, errors.Wrapf(err, "dependency %q has an invalid version/constraint format", d.Name)
locked[i] = &chart.Dependency{
Name: d.Name,
Repository: "",
Version: d.Version,
} }
continue
} }
if strings.HasPrefix(d.Repository, "file://") {
chartpath, err := GetLocalPath(d.Repository, r.chartpath) // Local chart
if err != nil { if d.Repository == "" || strings.HasPrefix(d.Repository, "file://") {
return nil, err
if d.Repository == "" {
// From charts subfolder
chartpath, err = GetLocalPath(filepath.Join("charts", d.Name), r.chartpath)
if err != nil {
return nil, err
}
} else {
// From file:// repository
chartpath, err = GetLocalPath(d.Repository, r.chartpath)
if err != nil {
return nil, err
}
} }
// Load chart to validate the version
ch, err := loader.LoadDir(chartpath) ch, err := loader.LoadDir(chartpath)
if err != nil { if err != nil {
return nil, err return nil, err
@ -90,11 +95,15 @@ func (r *Resolver) Resolve(reqs []*chart.Dependency, repoNames map[string]string
v, err := semver.NewVersion(ch.Metadata.Version) v, err := semver.NewVersion(ch.Metadata.Version)
if err != nil { if err != nil {
// Not a legit entry. // If version is not set and the version from the local chart is invalid
if d.Version == "" {
missing = append(missing, d.Name)
}
continue continue
} }
if !constraint.Check(v) { // If version is set but does not match the local chart
if d.Version != "" && !constraint.Check(v) {
missing = append(missing, d.Name) missing = append(missing, d.Name)
continue continue
} }

@ -137,6 +137,28 @@ func TestResolve(t *testing.T) {
}, },
err: true, err: true,
}, },
{
name: "no version set for local chart under charts path",
req: []*chart.Dependency{
{Name: "localdependency", Repository: "", Version: ""},
},
expect: &chart.Lock{
Dependencies: []*chart.Dependency{
{Name: "localdependency", Repository: "", Version: "0.1.0"},
},
},
},
{
name: "no version set for local chart from file://",
req: []*chart.Dependency{
{Name: "localdependency", Repository: "file://base", Version: ""},
},
expect: &chart.Lock{
Dependencies: []*chart.Dependency{
{Name: "localdependency", Repository: "file://base", Version: "0.1.0"},
},
},
},
} }
repoNames := map[string]string{"alpine": "kubernetes-charts", "redis": "kubernetes-charts"} repoNames := map[string]string{"alpine": "kubernetes-charts", "redis": "kubernetes-charts"}

Loading…
Cancel
Save