diff --git a/pkg/repo/v1/index.go b/pkg/repo/v1/index.go index adc245d43..bfd6e953d 100644 --- a/pkg/repo/v1/index.go +++ b/pkg/repo/v1/index.go @@ -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 -} +} \ No newline at end of file diff --git a/pkg/repo/v1/index_test.go b/pkg/repo/v1/index_test.go index df63f07a2..b9d65a8ae 100644 --- a/pkg/repo/v1/index_test.go +++ b/pkg/repo/v1/index_test.go @@ -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