fix(helm): fix 'helm dep up' to check lock digest

When `helm dependency update` is run helm will now correctly check the
digest of the old and new lock file dependencies instead of the digest
of `requirements.yaml` when choosing whether or not to write
`requirements.lock` to disk.

Closes #4011

Signed-off-by: Travis Thompson <trthomps@confluent.io>
pull/4918/head
Travis Thompson 7 years ago
parent 46e66d4712
commit f2eb84a494
No known key found for this signature in database
GPG Key ID: B1D28BD6CD62A5A3

@ -156,15 +156,14 @@ func (m *Manager) Update() error {
return err
}
// If the lock file hasn't changed, don't write a new one.
oldLock, err := chartutil.LoadRequirementsLock(c)
if err == nil && oldLock.Digest == lock.Digest {
return nil
}
// Finally, we need to write the lockfile.
// Finally, we need to write the lockfile if the dependencies changed.
if oldLock, err := chartutil.LoadRequirementsLock(c); err == nil {
if changed, err := dependenciesChanged(oldLock.Dependencies, lock.Dependencies); err == nil && changed {
return writeLock(m.ChartPath, lock)
}
}
return nil
}
func (m *Manager) loadChartDir() (*chart.Chart, error) {
if fi, err := os.Stat(m.ChartPath); err != nil {
@ -591,6 +590,22 @@ func (m *Manager) loadChartRepositories() (map[string]*repo.ChartRepository, err
return indices, nil
}
// dependenciesChanged compares the chksum of old and new dependencies and returns
// true or false if there's a difference
func dependenciesChanged(oldDep, newDep []*chartutil.Dependency) (bool, error) {
newDigest, err := resolver.HashReq(&chartutil.Requirements{Dependencies: newDep})
if err != nil {
return false, err
}
oldDigest, err := resolver.HashReq(&chartutil.Requirements{Dependencies: oldDep})
if err != nil {
return false, err
}
return newDigest != oldDigest, nil
}
// writeLock writes a lockfile to disk
func writeLock(chartpath string, lock *chartutil.RequirementsLock) error {
data, err := yaml.Marshal(lock)

@ -18,6 +18,7 @@ package downloader
import (
"bytes"
"reflect"
"strconv"
"testing"
"k8s.io/helm/pkg/chartutil"
@ -168,3 +169,60 @@ func TestGetRepoNames(t *testing.T) {
}
}
}
func TestDependenciesChanged(t *testing.T) {
tests := []struct {
name string
expect bool
oldDep []*chartutil.Dependency
newDep []*chartutil.Dependency
}{
{
name: "did not change",
expect: false,
oldDep: []*chartutil.Dependency{
{
Name: "test",
Version: "1.0.0",
Repository: "http://example.com",
},
},
newDep: []*chartutil.Dependency{
{
Name: "test",
Version: "1.0.0",
Repository: "http://example.com",
},
},
},
{
name: "did change",
expect: true,
oldDep: []*chartutil.Dependency{
{
Name: "test",
Version: "1.0.0",
Repository: "http://example.com",
},
},
newDep: []*chartutil.Dependency{
{
Name: "test",
Version: "1.1.0",
Repository: "http://example.com",
},
},
},
}
for _, tt := range tests {
changed, err := dependenciesChanged(tt.oldDep, tt.newDep)
if err != nil {
t.Fatal(err)
}
if changed != tt.expect {
t.Errorf("%s: Expected %s, got %s", tt.name, strconv.FormatBool(tt.expect), strconv.FormatBool(changed))
}
}
}

Loading…
Cancel
Save