Skip to content
This repository was archived by the owner on Jul 16, 2021. It is now read-only.

Commit 0686901

Browse files
authored
fixes fetching relative chart urls from index (#388)
* fixes fetching relative chart urls from index Helm supports repository indexes that use relative URLs to chart packages (formed by omitting the --url flag in `helm repo index`). This patch enables Monocular to do the same. * clarification for possible invalid relative URL * use raw repo URL string to form url.URL var When passing the url.URL pointer, we're continuously appending to the same path. By re-creating the url.URL var each time we ensure we only append to the base repo URL.
1 parent 022ca60 commit 0686901

File tree

5 files changed

+34
-20
lines changed

5 files changed

+34
-20
lines changed

src/api/data/cache/cache.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -178,7 +178,7 @@ func (c *cachedCharts) Refresh() error {
178178

179179
// 3.1 - parallellize processing
180180
for _, chart := range charts {
181-
go processChartMetadata(chart, ch)
181+
go processChartMetadata(chart, repo.URL, ch)
182182
}
183183
// 3.2 Channel drain
184184
for range charts {
@@ -207,7 +207,7 @@ type chanItem struct {
207207
// Counting semaphore, 25 downloads max in paralell
208208
var tokens = make(chan struct{}, 15)
209209

210-
func processChartMetadata(chart *swaggermodels.ChartPackage, out chan<- chanItem) {
210+
func processChartMetadata(chart *swaggermodels.ChartPackage, repoURL string, out chan<- chanItem) {
211211
tokens <- struct{}{}
212212
// Semaphore control channel
213213
defer func() {
@@ -231,7 +231,7 @@ func processChartMetadata(chart *swaggermodels.ChartPackage, out chan<- chanItem
231231
"version": *chart.Version,
232232
}).Info("Local cache missing")
233233

234-
err := charthelper.DownloadAndExtractChartTarball(chart)
234+
err := charthelper.DownloadAndExtractChartTarball(chart, repoURL)
235235
if err != nil {
236236
log.WithFields(log.Fields{
237237
"error": err,

src/api/data/cache/cache_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,7 @@ func TestCachedChartsRefresh(t *testing.T) {
8080
// Stubs Download and processing
8181
DownloadAndExtractChartTarballOrig := charthelper.DownloadAndExtractChartTarball
8282
defer func() { charthelper.DownloadAndExtractChartTarball = DownloadAndExtractChartTarballOrig }()
83-
charthelper.DownloadAndExtractChartTarball = func(chart *swaggermodels.ChartPackage) error { return nil }
83+
charthelper.DownloadAndExtractChartTarball = func(chart *swaggermodels.ChartPackage, repoURL string) error { return nil }
8484

8585
DownloadAndProcessChartIconOrig := charthelper.DownloadAndProcessChartIcon
8686
defer func() { charthelper.DownloadAndProcessChartIcon = DownloadAndProcessChartIconOrig }()
@@ -120,7 +120,7 @@ func TestCachedChartsRefreshErrorDownloadingPackage(t *testing.T) {
120120
DownloadAndExtractChartTarballOrig := charthelper.DownloadAndExtractChartTarball
121121
defer func() { charthelper.DownloadAndExtractChartTarball = DownloadAndExtractChartTarballOrig }()
122122
knownError := errors.New("error on DownloadAndExtractChartTarball")
123-
charthelper.DownloadAndExtractChartTarball = func(chart *swaggermodels.ChartPackage) error {
123+
charthelper.DownloadAndExtractChartTarball = func(chart *swaggermodels.ChartPackage, repoURL string) error {
124124
return knownError
125125
}
126126

src/api/data/cache/charthelper/chart_package_helper.go

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,9 @@ import (
77
"io"
88
"io/ioutil"
99
"net/http"
10+
"net/url"
1011
"os"
12+
"path"
1113
"path/filepath"
1214
"strings"
1315
"time"
@@ -22,7 +24,7 @@ const defaultTimeout time.Duration = 10 * time.Second
2224

2325
// DownloadAndExtractChartTarball the chart tar file linked by metadata.Urls and store
2426
// the wanted files (i.e README.md) under chartDataDir
25-
var DownloadAndExtractChartTarball = func(chart *models.ChartPackage) (err error) {
27+
var DownloadAndExtractChartTarball = func(chart *models.ChartPackage, repoURL string) (err error) {
2628
if err := ensureChartDataDir(chart); err != nil {
2729
return err
2830
}
@@ -34,7 +36,7 @@ var DownloadAndExtractChartTarball = func(chart *models.ChartPackage) (err error
3436
}()
3537

3638
if !tarballExists(chart) {
37-
if err := downloadTarball(chart); err != nil {
39+
if err := downloadTarball(chart, repoURL); err != nil {
3840
return err
3941
}
4042
}
@@ -53,8 +55,17 @@ var tarballExists = func(chart *models.ChartPackage) bool {
5355

5456
// Downloads the tar.gz file associated with the chart version exposed by the index
5557
// in order to extract specific files for caching
56-
var downloadTarball = func(chart *models.ChartPackage) error {
58+
var downloadTarball = func(chart *models.ChartPackage, repoURL string) error {
5759
source := chart.Urls[0]
60+
if _, err := url.ParseRequestURI(source); err != nil {
61+
// If the chart URL is not absolute, join with repo URL. It's fine if the
62+
// URL we build here is invalid as we can catch this error when actually
63+
// making the request
64+
u, _ := url.Parse(repoURL)
65+
u.Path = path.Join(u.Path, source)
66+
source = u.String()
67+
}
68+
5869
destination := TarballPath(chart)
5970

6071
// Create output

src/api/data/cache/charthelper/chart_package_helper_test.go

Lines changed: 15 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -14,10 +14,18 @@ import (
1414
"github.com/kubernetes-helm/monocular/src/api/swagger/models"
1515
)
1616

17+
var repoURL = "http://storage.googleapis.com/kubernetes-charts/"
18+
1719
func TestDownloadAndExtractChartTarballOk(t *testing.T) {
1820
chart, err := getTestChart()
1921
assert.NoErr(t, err)
20-
assert.NoErr(t, DownloadAndExtractChartTarball(chart))
22+
assert.NoErr(t, DownloadAndExtractChartTarball(chart, repoURL))
23+
24+
// Relative URLs
25+
chart, err = getTestChart()
26+
assert.NoErr(t, err)
27+
chart.Urls[0] = "drupal-0.3.0.tgz"
28+
assert.NoErr(t, DownloadAndExtractChartTarball(chart, repoURL))
2129
}
2230

2331
func TestDownloadAndExtractChartTarballErrorCantWrite(t *testing.T) {
@@ -27,22 +35,22 @@ func TestDownloadAndExtractChartTarballErrorCantWrite(t *testing.T) {
2735

2836
chart, err := getTestChart()
2937
assert.NoErr(t, err)
30-
err = DownloadAndExtractChartTarball(chart)
38+
err = DownloadAndExtractChartTarball(chart, repoURL)
3139
assert.ExistsErr(t, err, "trying to create a non valid directory")
3240
}
3341

3442
func TestDownloadAndExtractChartTarballErrorDownload(t *testing.T) {
3543
// Stubs
3644
downloadTarballOrig := downloadTarball
3745
defer func() { downloadTarball = downloadTarballOrig }()
38-
downloadTarball = func(chart *models.ChartPackage) error { return errors.New("Can't download") }
46+
downloadTarball = func(chart *models.ChartPackage, url string) error { return errors.New("Can't download") }
3947
tarballExistsOrig := tarballExists
4048
defer func() { tarballExists = tarballExistsOrig }()
4149
tarballExists = func(chart *models.ChartPackage) bool { return false }
4250
// EO stubs
4351
chart, err := getTestChart()
4452
assert.NoErr(t, err)
45-
err = DownloadAndExtractChartTarball(chart)
53+
err = DownloadAndExtractChartTarball(chart, repoURL)
4654
assert.ExistsErr(t, err, "Error downloading the tar file")
4755
_, err = os.Stat(chartDataDir(chart))
4856
assert.ExistsErr(t, err, "chart data dir has been removed")
@@ -54,7 +62,7 @@ func TestDownloadAndExtractChartTarballErrorExtract(t *testing.T) {
5462
extractFilesFromTarball = func(chart *models.ChartPackage) error { return errors.New("Can't download") }
5563
chart, err := getTestChart()
5664
assert.NoErr(t, err)
57-
err = DownloadAndExtractChartTarball(chart)
65+
err = DownloadAndExtractChartTarball(chart, repoURL)
5866
assert.ExistsErr(t, err, "Error extracting tar file content")
5967
_, err = os.Stat(chartDataDir(chart))
6068
assert.ExistsErr(t, err, "chart data dir has been removed")
@@ -79,7 +87,7 @@ func TestDownloadTarballCreatesFileInDestination(t *testing.T) {
7987
return pathExists
8088
}
8189

82-
err = downloadTarball(chart)
90+
err = downloadTarball(chart, repoURL)
8391
assert.NoErr(t, err)
8492
_, err = os.Stat(filepath.Join(chartDataDir(chart), "chart.tgz"))
8593
assert.NoErr(t, err)
@@ -94,14 +102,9 @@ func TestDownloadTarballErrorDownloading(t *testing.T) {
94102
TarballPath = func(chart *models.ChartPackage) string {
95103
return filepath.Join(randomPath, "myFile.tar.gz")
96104
}
97-
// Invald protocol
98-
chart.Urls[0] = "./invalid-protocol/bogusUrl"
99-
err = downloadTarball(chart)
100-
assert.ExistsErr(t, err, "Invalid protocol")
101-
102105
// 404
103106
chart.Urls[0] = "http://localhost/bogusUrl"
104-
err = downloadTarball(chart)
107+
err = downloadTarball(chart, repoURL)
105108
assert.ExistsErr(t, err, "Cant copy")
106109
}
107110

9.51 MB
Binary file not shown.

0 commit comments

Comments
 (0)