Skip to content

Commit

Permalink
don't fetch tags when exact version is used in HelmRepository
Browse files Browse the repository at this point in the history
Taking this shortcut has two benefits:

1. It allows charts to be fetched from AWS's public container registry
   at public.ecr.aws
2. It makes reconciling a HelmChart faster by skipping one or more
   potentially expensive API calls to the registry.

refs #845
  • Loading branch information
makkes committed Jul 26, 2022
1 parent cf1a27c commit e5d93e5
Show file tree
Hide file tree
Showing 2 changed files with 64 additions and 40 deletions.
19 changes: 17 additions & 2 deletions internal/helm/repository/oci_chart_repository.go
Original file line number Diff line number Diff line change
Expand Up @@ -131,10 +131,25 @@ func NewOCIChartRepository(repositoryURL string, chartRepoOpts ...OCIChartReposi
// stable version will be returned and prerelease versions will be ignored.
// adapted from https://github.com/helm/helm/blob/49819b4ef782e80b0c7f78c30bd76b51ebb56dc8/pkg/downloader/chart_downloader.go#L162
func (r *OCIChartRepository) GetChartVersion(name, ver string) (*repo.ChartVersion, error) {
// Find chart versions matching the given name.
// Either in an index file or from a registry.

cpURL := r.URL
cpURL.Path = path.Join(cpURL.Path, name)

// if ver is a valid semver version, take a shortcut here so we don't need to list all tags which can be an
// expensive operation.
if _, err := version.ParseVersion(ver); err == nil {
return &repo.ChartVersion{
URLs: []string{fmt.Sprintf("%s:%s", cpURL.String(), ver)},
Metadata: &chart.Metadata{
Name: name,
Version: ver,
},
}, nil
}

// ver doesn't denote a concrete version so we interpret it as a semver range and try to find the best-matching
// version from the list of tags in the registry.

cvs, err := r.getTags(cpURL.String())
if err != nil {
return nil, fmt.Errorf("could not get tags for %q: %s", name, err)
Expand Down
85 changes: 47 additions & 38 deletions internal/helm/repository/oci_chart_repository_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -118,67 +118,76 @@ func TestOCIChartRepository_Get(t *testing.T) {
testURL := "oci://localhost:5000/my_repo"

testCases := []struct {
name string
url string
version string
expected string
expectedErr string
name string
registryClient RegistryClient
url string
version string
expected string
expectedErr string
}{
{
name: "should return latest stable version",
version: "",
url: testURL,
expected: "1.0.0",
name: "should return latest stable version",
registryClient: registryClient,
version: "",
url: testURL,
expected: "1.0.0",
},
{
name: "should return latest stable version (asterisk)",
version: "*",
url: testURL,
expected: "1.0.0",
name: "should return latest stable version (asterisk)",
registryClient: registryClient,
version: "*",
url: testURL,
expected: "1.0.0",
},
{
name: "should return latest stable version (semver range)",
version: ">=0.1.5",
url: testURL,
expected: "1.0.0",
name: "should return latest stable version (semver range)",
registryClient: registryClient,
version: ">=0.1.5",
url: testURL,
expected: "1.0.0",
},
{
name: "should return 0.2.0 (semver range)",
version: "0.2.x",
url: testURL,
expected: "0.2.0",
name: "should return 0.2.0 (semver range)",
registryClient: registryClient,
version: "0.2.x",
url: testURL,
expected: "0.2.0",
},
{
name: "should return a perfect match",
version: "0.1.0",
url: testURL,
expected: "0.1.0",
name: "should return a perfect match",
registryClient: nil,
version: "0.1.0",
url: testURL,
expected: "0.1.0",
},
{
name: "should return 0.10.0",
version: "0.*",
url: testURL,
expected: "0.10.0",
name: "should return 0.10.0",
registryClient: registryClient,
version: "0.*",
url: testURL,
expected: "0.10.0",
},
{
name: "should an error for unfunfilled range",
version: ">2.0.0",
url: testURL,
expectedErr: "could not locate a version matching provided version string >2.0.0",
name: "should an error for unfunfilled range",
registryClient: registryClient,
version: ">2.0.0",
url: testURL,
expectedErr: "could not locate a version matching provided version string >2.0.0",
},
{
name: "shouldn't error out with trailing slash",
version: "",
url: "oci://localhost:5000/my_repo/",
expected: "1.0.0",
name: "shouldn't error out with trailing slash",
registryClient: registryClient,
version: "",
url: "oci://localhost:5000/my_repo/",
expected: "1.0.0",
},
}

for _, tc := range testCases {

t.Run(tc.name, func(t *testing.T) {
g := NewWithT(t)
r, err := NewOCIChartRepository(tc.url, WithOCIRegistryClient(registryClient), WithOCIGetter(providers))
r, err := NewOCIChartRepository(tc.url, WithOCIRegistryClient(tc.registryClient), WithOCIGetter(providers))
g.Expect(err).ToNot(HaveOccurred())
g.Expect(r).ToNot(BeNil())

Expand Down

0 comments on commit e5d93e5

Please sign in to comment.