fix: resolve duplicate LoadIndexFileForEntries definition and review feedback

- Remove duplicate LoadIndexFileForEntries function (compile error)
- Keep single []string signature, using loadIndex which validates and
  normalizes all entries on unmarshal before filtering
- Treat empty names slice the same as nil (load all entries)
- Capture and check error from LoadIndexFile in partial match test

Co-authored-by: Cloud-Architect-Emma <emmanuela_prince@yahoo.com>
Signed-off-by: Cloud-Architect-Emma <emmanuela_prince@yahoo.com>
pull/32172/head
Cloud-Architect-Emma 4 months ago
parent 4943d368bc
commit 0bdb3dd6e8

@ -107,13 +107,15 @@ func LoadIndexFile(path string) (*IndexFile, error) {
return LoadIndexFileForEntries(path, nil)
}
// LoadIndexFileForEntries loads an index file but only validates and retains
// entries matching the provided chart names. Unmatched entries are discarded
// after unmarshal without validation, reducing memory and CPU overhead.
// If names is nil, all entries are loaded (equivalent to LoadIndexFile).
// LoadIndexFileForEntries loads an index file but only retains entries
// matching the provided chart names. All entries are still validated and
// normalized (via loadIndex) before unmatched entries are discarded,
// reducing the retained heap for large repositories when filtering to a
// small subset of charts.
// If names is nil or empty, all entries are loaded (equivalent to LoadIndexFile).
func LoadIndexFileForEntries(path string, names []string) (*IndexFile, error) {
var entries map[string]struct{}
if names != nil {
if len(names) > 0 {
entries = make(map[string]struct{}, len(names))
for _, n := range names {
entries[n] = struct{}{}
@ -138,41 +140,6 @@ func LoadIndexFileForEntries(path string, names []string) (*IndexFile, error) {
return i, nil
}
// LoadIndexFileForEntries loads an index file but only validates and retains
// entries matching the provided chart names. Unmatched entries are discarded
// after unmarshal without validation, reducing memory and CPU overhead.
// If entries is nil, all entries are loaded (equivalent to LoadIndexFile).
func LoadIndexFileForEntries(path string, entries map[string]struct{}) (*IndexFile, error) {
i := &IndexFile{}
b, err := os.ReadFile(path)
if err != nil {
return i, err
}
if err := yaml.Unmarshal(b, i); err != nil {
return i, err
}
if entries != nil {
for name := range i.Entries {
if _, ok := entries[name]; !ok {
delete(i.Entries, name)
}
}
}
for _, cvs := range i.Entries {
for _, cv := range cvs {
if cv.APIVersion == "" {
cv.APIVersion = chart.APIVersionV1
}
if err := cv.Validate(); err != nil {
return i, err
}
}
}
i.SortEntries()
return i, nil
}
// MustAdd adds a file to the index
// This can leave the index in an unsorted state
func (i IndexFile) MustAdd(md *chart.Metadata, filename, baseURL, digest string) error {
@ -490,4 +457,4 @@ func ignoreSkippableChartValidationError(err error) error {
}
return err
}
}

@ -798,7 +798,10 @@ func TestLoadIndexFileForEntries(t *testing.T) {
// partial match retains only matched entries
t.Run("partial match", func(t *testing.T) {
full, _ := LoadIndexFile("testdata/local-index.yaml")
full, err := LoadIndexFile("testdata/local-index.yaml")
if err != nil {
t.Fatal(err)
}
var firstName string
for k := range full.Entries {
firstName = k

Loading…
Cancel
Save