Skip to content

Commit a595e0c

Browse files
committed
Fix: Changed implementation to allow non standard convention for chart tgz names
1 parent 231bbb9 commit a595e0c

File tree

1 file changed

+42
-27
lines changed

1 file changed

+42
-27
lines changed

pkg/clients/helm/client.go

Lines changed: 42 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,6 @@ import (
2020
"fmt"
2121
"net/url"
2222
"os"
23-
"path"
2423
"path/filepath"
2524
"strings"
2625

@@ -180,11 +179,7 @@ func (hc *client) pullLatestChartVersion(spec *v1beta1.ChartSpec, creds *RepoCre
180179
}
181180
}()
182181

183-
if err := hc.pullChart(spec, creds, tmpDir); err != nil {
184-
return "", err
185-
}
186-
187-
chartFileName, err := getChartFileName(tmpDir)
182+
chartFileName, err := hc.pullChart(spec, creds, tmpDir)
188183
if err != nil {
189184
return "", err
190185
}
@@ -196,7 +191,17 @@ func (hc *client) pullLatestChartVersion(spec *v1beta1.ChartSpec, creds *RepoCre
196191
return chartFilePath, nil
197192
}
198193

199-
func (hc *client) pullChart(spec *v1beta1.ChartSpec, creds *RepoCreds, chartDir string) error {
194+
func (hc *client) pullChart(spec *v1beta1.ChartSpec, creds *RepoCreds, chartDir string) (string, error) {
195+
tmpDir, err := os.MkdirTemp(chartDir, "")
196+
if err != nil {
197+
return "", err
198+
}
199+
defer func() {
200+
if err := os.RemoveAll(tmpDir); err != nil {
201+
hc.log.WithValues("tmpDir", tmpDir).Info("failed to remove temporary directory")
202+
}
203+
}()
204+
200205
pc := hc.pullClient
201206

202207
chartRef := spec.URL
@@ -211,29 +216,40 @@ func (hc *client) pullChart(spec *v1beta1.ChartSpec, creds *RepoCreds, chartDir
211216
} else if registry.IsOCI(spec.URL) {
212217
ociURL, version, err := resolveOCIChartVersion(spec.URL)
213218
if err != nil {
214-
return err
219+
return "", err
215220
}
216221
pc.Version = version
217222
chartRef = ociURL.String()
218223
}
219224
pc.Username = creds.Username
220225
pc.Password = creds.Password
221226

222-
pc.DestDir = chartDir
227+
pc.DestDir = tmpDir
223228

224229
if creds.Username != "" && creds.Password != "" {
225230
err := hc.login(spec, creds, pc.InsecureSkipTLSverify)
226231
if err != nil {
227-
return err
232+
return "", err
228233
}
229234
}
230235

231236
o, err := pc.Run(chartRef)
232237
hc.log.Debug(o)
238+
239+
if err != nil {
240+
return "", errors.Wrap(err, errFailedToPullChart)
241+
}
242+
243+
chartFileName, err := getChartFileName(tmpDir)
233244
if err != nil {
234-
return errors.Wrap(err, errFailedToPullChart)
245+
return "", err
235246
}
236-
return nil
247+
248+
chartFilePath := filepath.Join(chartDir, chartFileName)
249+
if err := os.Rename(filepath.Join(tmpDir, chartFileName), chartFilePath); err != nil {
250+
return "", err
251+
}
252+
return chartFilePath, nil
237253
}
238254

239255
func (hc *client) login(spec *v1beta1.ChartSpec, creds *RepoCreds, insecure bool) error {
@@ -257,14 +273,15 @@ func (hc *client) login(spec *v1beta1.ChartSpec, creds *RepoCreds, insecure bool
257273
func (hc *client) PullAndLoadChart(spec *v1beta1.ChartSpec, creds *RepoCreds) (*chart.Chart, error) {
258274
var chartFilePath string
259275
var err error
276+
260277
switch {
261278
case spec.URL == "" && (spec.Version == "" || spec.Version == devel):
262279
chartFilePath, err = hc.pullLatestChartVersion(spec, creds)
263280
if err != nil {
264281
return nil, err
265282
}
266283
case registry.IsOCI(spec.URL):
267-
u, v, err := resolveOCIChartVersion(spec.URL)
284+
_, v, err := resolveOCIChartVersion(spec.URL)
268285
if err != nil {
269286
return nil, err
270287
}
@@ -275,24 +292,27 @@ func (hc *client) PullAndLoadChart(spec *v1beta1.ChartSpec, creds *RepoCreds) (*
275292
return nil, err
276293
}
277294
} else {
278-
chartFilePath = resolveChartFilePath(path.Base(u.Path), v)
295+
chartFilePath, err = hc.pullChart(spec, creds, chartCache)
296+
if err != nil {
297+
return nil, err
298+
}
279299
}
280300
case spec.URL != "":
281-
u, err := url.Parse(spec.URL)
301+
chartFilePath, err = hc.pullChart(spec, creds, chartCache)
282302
if err != nil {
283-
return nil, errors.Wrap(err, errFailedToParseURL)
303+
return nil, err
284304
}
285-
chartFilePath = filepath.Join(chartCache, path.Base(u.Path))
286305
default:
287-
chartFilePath = resolveChartFilePath(spec.Name, spec.Version)
306+
chartFilePath, err = hc.pullChart(spec, creds, chartCache)
307+
if err != nil {
308+
return nil, err
309+
}
288310
}
289311

290312
if _, err := os.Stat(chartFilePath); os.IsNotExist(err) {
291-
if err = hc.pullChart(spec, creds, chartCache); err != nil {
292-
return nil, err
293-
}
294-
} else if err != nil {
295313
return nil, errors.Wrap(err, errFailedToCheckIfLocalChartExists)
314+
} else if err != nil {
315+
return nil, err
296316
}
297317

298318
chart, err := loader.Load(chartFilePath)
@@ -359,11 +379,6 @@ func resolveOCIChartVersion(chartURL string) (*url.URL, string, error) {
359379
return ociURL, "", nil
360380
}
361381

362-
func resolveChartFilePath(name string, version string) string {
363-
filename := fmt.Sprintf("%s-%s.tgz", name, version)
364-
return filepath.Join(chartCache, filename)
365-
}
366-
367382
func resolveOCIChartRef(repository string, name string) string {
368383
return strings.Join([]string{strings.TrimSuffix(repository, "/"), name}, "/")
369384
}

0 commit comments

Comments
 (0)