diff --git a/pkg/repo/index.go b/pkg/repo/index.go index feee9cdaa..6e2c1920d 100644 --- a/pkg/repo/index.go +++ b/pkg/repo/index.go @@ -23,6 +23,7 @@ type ChartRef struct { URL string `yaml:"url"` Created string `yaml:"created,omitempty"` Removed bool `yaml:"removed,omitempty"` + Digest string `yaml:"digest,omitempty"` Chartfile chart.Chartfile `yaml:"chartfile"` } diff --git a/pkg/repo/index_test.go b/pkg/repo/index_test.go index 41b04d2c4..0b5590c96 100644 --- a/pkg/repo/index_test.go +++ b/pkg/repo/index_test.go @@ -52,5 +52,6 @@ func TestDownloadIndexFile(t *testing.T) { t.Errorf("Expected 2 entries in index file but got %v", numEntries) } os.Remove(path) + os.Remove(dirName) } diff --git a/pkg/repo/repo.go b/pkg/repo/repo.go index 2ad6e3a94..7f2ce4d01 100644 --- a/pkg/repo/repo.go +++ b/pkg/repo/repo.go @@ -1,11 +1,14 @@ package repo import ( + "crypto/sha1" "errors" + "fmt" "io/ioutil" "os" "path/filepath" "strings" + "time" "gopkg.in/yaml.v2" @@ -113,16 +116,17 @@ func (r *ChartRepository) Index() error { return err } chartfile := ch.Chartfile() + hash, err := generateDigest(path) + if err != nil { + return err + } key := chartfile.Name + "-" + chartfile.Version if r.IndexFile.Entries == nil { r.IndexFile.Entries = make(map[string]*ChartRef) } - entry := &ChartRef{Chartfile: *chartfile, Name: chartfile.Name, URL: r.URL, Created: "", Removed: false} - - //TODO: generate hash of contents of chart and add to the entry - //TODO: Set created timestamp + entry := &ChartRef{Chartfile: *chartfile, Name: chartfile.Name, URL: r.URL, Created: time.Now().UTC().String(), Digest: hash, Removed: false} r.IndexFile.Entries[key] = entry @@ -134,3 +138,19 @@ func (r *ChartRepository) Index() error { return nil } + +func generateDigest(path string) (string, error) { + f, err := os.Open(path) + if err != nil { + return "", err + } + + b, err := ioutil.ReadAll(f) + if err != nil { + return "", err + } + + result := sha1.Sum(b) + + return fmt.Sprintf("%x", result), nil +} diff --git a/pkg/repo/repo_test.go b/pkg/repo/repo_test.go index 2ab4dcf65..f765091fd 100644 --- a/pkg/repo/repo_test.go +++ b/pkg/repo/repo_test.go @@ -5,6 +5,7 @@ import ( "path/filepath" "reflect" "testing" + "time" ) const testfile = "testdata/local-index.yaml" @@ -110,10 +111,28 @@ func TestIndex(t *testing.T) { t.Errorf("Error loading index file %v", err) } - numEntries := len(actual.Entries) + entries := actual.Entries + numEntries := len(entries) if numEntries != 2 { t.Errorf("Expected 2 charts to be listed in index file but got %v", numEntries) } + var empty time.Time + for chartName, details := range entries { + if details == nil { + t.Errorf("Chart Entry is not filled out for %s", chartName) + } + + if details.Created == empty.String() { + t.Errorf("Created timestamp under %s chart entry is nil", chartName) + } + + if details.Digest == "" { + t.Errorf("Digest was not set for %s", chartName) + } + } + + //TODO: test update case + os.Remove(tempIndexPath) // clean up }