Skip to content

Commit 8b0a936

Browse files
committed
don't duplicate pre-release charts in index.yaml
Prior to this commit chart-releaser would incorrectly parse pre-release versions, e.g. v25.1.1-beta2, due to naively splitting on the last index. This would result in calling `IndexFile.Get` with just `beta2` and trigger a reinsertion of the pre-release version upon every call to `cr index`. This commit removes the file name based parsing altogether and instead preemptively parses the chart metadata to avoid such complications all together. It should be noted this commit highlights a pre-existing issue more clearly but intentionally does not fix it: chart-releaser can *not* update an existing index.yaml entry. Signed-off-by: Chris Seto <[email protected]>
1 parent 2181d19 commit 8b0a936

File tree

2 files changed

+35
-59
lines changed

2 files changed

+35
-59
lines changed

pkg/releaser/releaser.go

Lines changed: 17 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -168,14 +168,13 @@ func (r *Releaser) UpdateIndexFile() (bool, error) {
168168
if filepath.Ext(name) != chartAssetFileExtension {
169169
continue
170170
}
171-
baseName := strings.TrimSuffix(name, filepath.Ext(name))
172-
tagParts := r.splitPackageNameAndVersion(baseName)
173-
packageName, packageVersion := tagParts[0], tagParts[1]
174-
fmt.Printf("Found %s-%s.tgz\n", packageName, packageVersion)
175-
if _, err := indexFile.Get(packageName, packageVersion); err != nil {
176-
if err := r.addToIndexFile(indexFile, downloadURL.String()); err != nil {
177-
return false, err
178-
}
171+
fmt.Printf("Found %s\n", name)
172+
updated, err := r.maybeAddToIndexFile(indexFile, downloadURL.String())
173+
if err != nil {
174+
return false, err
175+
}
176+
177+
if updated {
179178
update = true
180179
break
181180
}
@@ -255,20 +254,26 @@ func (r *Releaser) splitPackageNameAndVersion(pkg string) []string {
255254
return []string{pkg[0:delimIndex], pkg[delimIndex+1:]}
256255
}
257256

258-
func (r *Releaser) addToIndexFile(indexFile *repo.IndexFile, url string) error {
257+
func (r *Releaser) maybeAddToIndexFile(indexFile *repo.IndexFile, url string) (bool, error) {
259258
arch := filepath.Join(r.config.PackagePath, filepath.Base(url))
260259

261260
// extract chart metadata
262261
fmt.Printf("Extracting chart metadata from %s\n", arch)
263262
c, err := loader.LoadFile(arch)
264263
if err != nil {
265-
return errors.Wrapf(err, "%s is not a helm chart package", arch)
264+
return false, errors.Wrapf(err, "%s is not a helm chart package", arch)
266265
}
266+
267+
if _, err := indexFile.Get(c.Name(), c.Metadata.Version); err == nil {
268+
fmt.Printf("version %s of %s already present in index file", c.Metadata.Version, c.Name())
269+
return false, nil
270+
}
271+
267272
// calculate hash
268273
fmt.Printf("Calculating Hash for %s\n", arch)
269274
hash, err := provenance.DigestFile(arch)
270275
if err != nil {
271-
return err
276+
return false, err
272277
}
273278

274279
// remove url name from url as helm's index library
@@ -284,7 +289,7 @@ func (r *Releaser) addToIndexFile(indexFile *repo.IndexFile, url string) error {
284289
}
285290

286291
// Add to index
287-
return indexFile.MustAdd(c.Metadata, filepath.Base(arch), strings.Join(s, "/"), hash)
292+
return true, indexFile.MustAdd(c.Metadata, filepath.Base(arch), strings.Join(s, "/"), hash)
288293
}
289294

290295
// CreateReleases finds and uploads Helm chart packages to GitHub

pkg/releaser/releaser_test.go

Lines changed: 18 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -246,44 +246,7 @@ func TestReleaser_UpdateIndexFileGenerated(t *testing.T) {
246246
}
247247
}
248248

249-
func TestReleaser_splitPackageNameAndVersion(t *testing.T) {
250-
tests := []struct {
251-
name string
252-
pkg string
253-
expected []string
254-
}{
255-
{
256-
"no-hyphen",
257-
"foo",
258-
nil,
259-
},
260-
{
261-
"one-hyphen",
262-
"foo-1.2.3",
263-
[]string{"foo", "1.2.3"},
264-
},
265-
{
266-
"two-hyphens",
267-
"foo-bar-1.2.3",
268-
[]string{"foo-bar", "1.2.3"},
269-
},
270-
}
271-
for _, tt := range tests {
272-
t.Run(tt.name, func(t *testing.T) {
273-
r := &Releaser{}
274-
if tt.expected == nil {
275-
assert.Panics(t, func() {
276-
r.splitPackageNameAndVersion(tt.pkg)
277-
}, "slice bounds out of range")
278-
} else {
279-
actual := r.splitPackageNameAndVersion(tt.pkg)
280-
assert.Equal(t, tt.expected, actual)
281-
}
282-
})
283-
}
284-
}
285-
286-
func TestReleaser_addToIndexFile(t *testing.T) {
249+
func TestReleaser_maybeAddToIndexFile(t *testing.T) {
287250
tests := []struct {
288251
name string
289252
chart string
@@ -336,20 +299,28 @@ func TestReleaser_addToIndexFile(t *testing.T) {
336299
t.Run(tt.name, func(t *testing.T) {
337300
indexFile := repo.NewIndexFile()
338301
url := fmt.Sprintf("https://myrepo/charts/%s-%s.tgz", tt.chart, tt.version)
339-
err := tt.releaser.addToIndexFile(indexFile, url)
302+
updated, err := tt.releaser.maybeAddToIndexFile(indexFile, url)
340303
if tt.error {
341304
assert.Error(t, err)
305+
assert.False(t, updated)
342306
assert.False(t, indexFile.Has(tt.chart, tt.version))
343-
} else {
344-
assert.True(t, indexFile.Has(tt.chart, tt.version))
307+
return
308+
}
345309

346-
indexEntry, _ := indexFile.Get(tt.chart, tt.version)
347-
if tt.packagesWithIndex {
348-
assert.Equal(t, filepath.Base(url), indexEntry.URLs[0])
349-
} else {
350-
assert.Equal(t, url, indexEntry.URLs[0])
351-
}
310+
assert.True(t, updated)
311+
assert.True(t, indexFile.Has(tt.chart, tt.version))
312+
313+
indexEntry, _ := indexFile.Get(tt.chart, tt.version)
314+
if tt.packagesWithIndex {
315+
assert.Equal(t, filepath.Base(url), indexEntry.URLs[0])
316+
} else {
317+
assert.Equal(t, url, indexEntry.URLs[0])
352318
}
319+
320+
// Second time around is a no-op.
321+
updated, err = tt.releaser.maybeAddToIndexFile(indexFile, url)
322+
assert.NoError(t, err)
323+
assert.False(t, updated)
353324
})
354325
}
355326
}

0 commit comments

Comments
 (0)