Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update generated key with latest timestamp when index is changed #325

Merged
merged 2 commits into from
Mar 12, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,11 @@ and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.

## [Unreleased]

### Changed

- `generated` timestamp field in the index file is now updated with current time
on push, reindex and delete.

## [0.16.0] - 2023-12-07

### Added
Expand Down
1 change: 1 addition & 0 deletions cmd/helm-s3/delete.go
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,7 @@ func (act *deleteAction) run(ctx context.Context) error {
if err != nil {
return err
}
idx.UpdateGeneratedTime()

idxReader, err := idx.Reader()
if err != nil {
Expand Down
1 change: 1 addition & 0 deletions cmd/helm-s3/push.go
Original file line number Diff line number Diff line change
Expand Up @@ -211,6 +211,7 @@ func (act *pushAction) run(ctx context.Context) error {
return errors.WithMessage(err, "add/replace chart in the index")
}
idx.SortEntries()
idx.UpdateGeneratedTime()

idxReader, err := idx.Reader()
if err != nil {
Expand Down
1 change: 1 addition & 0 deletions cmd/helm-s3/reindex.go
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,7 @@ func (act *reindexAction) run(ctx context.Context) error {
}
}
idx.SortEntries()
idx.UpdateGeneratedTime()

builtIndex <- idx
}()
Expand Down
3 changes: 3 additions & 0 deletions internal/helmutil/index.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,9 @@ type Index interface {
// SortEntries sorts the entries by version in descending order.
SortEntries()

// UpdateGeneratedTime updates time when the index was generated.
UpdateGeneratedTime()

// MarshalBinary encodes index to a binary form.
MarshalBinary() (data []byte, err error)

Expand Down
4 changes: 4 additions & 0 deletions internal/helmutil/index_v2.go
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,10 @@ func (idx *IndexV2) SortEntries() {
idx.index.SortEntries()
}

func (idx *IndexV2) UpdateGeneratedTime() {
idx.index.Generated = time.Now().UTC()
}

func (idx *IndexV2) MarshalBinary() (data []byte, err error) {
return yaml.Marshal(idx.index)
}
Expand Down
13 changes: 13 additions & 0 deletions internal/helmutil/index_v2_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -122,3 +122,16 @@ func TestIndexV2_AddOrReplace(t *testing.T) {
assert.Equal(t, "sha256:222", i.index.Entries["foo"][0].Digest)
})
}

func TestIndexV2_UpdateGeneratedTime(t *testing.T) {
idx := IndexV2{
index: &repo.IndexFile{
APIVersion: "foo",
Generated: time.Date(2018, 01, 01, 0, 0, 0, 0, time.UTC),
},
}
generatedOld := idx.index.Generated
idx.UpdateGeneratedTime()
generatedNew := idx.index.Generated
assert.True(t, generatedNew.After(generatedOld), "Expected %s greater than %s", generatedNew.String(), generatedOld.String())
}
4 changes: 4 additions & 0 deletions internal/helmutil/index_v3.go
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,10 @@ func (idx *IndexV3) SortEntries() {
idx.index.SortEntries()
}

func (idx *IndexV3) UpdateGeneratedTime() {
idx.index.Generated = time.Now().UTC()
}

func (idx *IndexV3) MarshalBinary() (data []byte, err error) {
return yaml.Marshal(idx.index)
}
Expand Down
13 changes: 13 additions & 0 deletions internal/helmutil/index_v3_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -122,3 +122,16 @@ func TestIndexV3_AddOrReplace(t *testing.T) {
assert.Equal(t, "sha256:222", i.index.Entries["foo"][0].Digest)
})
}

func TestIndexV3_UpdateGeneratedTime(t *testing.T) {
idx := IndexV3{
index: &repo.IndexFile{
APIVersion: "foo",
Generated: time.Date(2018, 01, 01, 0, 0, 0, 0, time.UTC),
},
}
generatedOld := idx.index.Generated
idx.UpdateGeneratedTime()
generatedNew := idx.index.Generated
assert.True(t, generatedNew.After(generatedOld), "Expected %s greater than %s", generatedNew.String(), generatedOld.String())
}
36 changes: 31 additions & 5 deletions tests/e2e/push_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,28 @@ func TestPush(t *testing.T) {
setupRepo(t, repoName, repoDir)
defer teardownRepo(t, repoName)

tmpdir, err := os.MkdirTemp("", t.Name())
require.NoError(t, err)
defer os.RemoveAll(tmpdir)

// Fetch the repo index before push to get generated time.

indexFile := filepath.Join(tmpdir, "index.yaml")

err = mc.FGetObject(repoName, repoDir+"/index.yaml", indexFile, minio.GetObjectOptions{})
require.NoError(t, err)

idx, err := repo.LoadIndexFile(indexFile)
require.NoError(t, err)

assert.NotEmpty(t, idx.Generated)

idxGeneratedTimeBeforePush := idx.Generated

// Push chart.

cmd, stdout, stderr := command(fmt.Sprintf("helm s3 push %s %s", chartFilepath, repoName))
err := cmd.Run()
err = cmd.Run()
assert.NoError(t, err)
assertEmptyOutput(t, nil, stderr)
assert.Contains(t, stdout.String(), "Successfully uploaded the chart to the repository.")
Expand All @@ -64,10 +84,6 @@ func TestPush(t *testing.T) {

// Check that pushed chart can be fetched.

tmpdir, err := os.MkdirTemp("", t.Name())
require.NoError(t, err)
defer os.RemoveAll(tmpdir)

cmd, stdout, stderr = command(fmt.Sprintf("helm fetch %s/%s --version %s --destination %s", repoName, chartName, chartVersion, tmpdir))
err = cmd.Run()
assert.NoError(t, err)
Expand All @@ -83,6 +99,16 @@ func TestPush(t *testing.T) {

expected = "The chart already exists in the repository and cannot be overwritten without an explicit intent."
assert.Contains(t, stderr.String(), expected)

// Fetch the repo index again and check that generated time was updated.

err = mc.FGetObject(repoName, repoDir+"/index.yaml", indexFile, minio.GetObjectOptions{})
require.NoError(t, err)

idx, err = repo.LoadIndexFile(indexFile)
require.NoError(t, err)

assert.Greater(t, idx.Generated, idxGeneratedTimeBeforePush)
}

func TestPushContentType(t *testing.T) {
Expand Down