Skip to content

Commit 41695ca

Browse files
redbeamanjannath
authored andcommitted
bundle download & decompression: added Context argument
to allow stopping using API when these operations are running
1 parent f71c018 commit 41695ca

File tree

14 files changed

+101
-72
lines changed

14 files changed

+101
-72
lines changed

cmd/crc-embedder/cmd/embed.go

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package cmd
22

33
import (
4+
"context"
45
"fmt"
56
"os"
67
"path"
@@ -162,7 +163,7 @@ func downloadDataFiles(goos string, components []string, destDir string) ([]stri
162163
if !shouldDownload(components, componentName) {
163164
continue
164165
}
165-
filename, err := download.Download(dl.url, destDir, dl.permissions, nil)
166+
filename, err := download.Download(context.TODO(), dl.url, destDir, dl.permissions, nil)
166167
if err != nil {
167168
return nil, err
168169
}

pkg/compress/compress_test.go

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package compress
22

33
import (
4+
"context"
45
"fmt"
56
"os"
67
"path/filepath"
@@ -33,7 +34,7 @@ func testCompress(t *testing.T, baseDir string) {
3334

3435
destDir := t.TempDir()
3536

36-
fileList, err := extract.Uncompress(testArchiveName, destDir)
37+
fileList, err := extract.Uncompress(context.TODO(), testArchiveName, destDir)
3738
require.NoError(t, err)
3839

3940
_, d := filepath.Split(baseDir)

pkg/crc/cache/cache.go

+3-2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package cache
22

33
import (
4+
"context"
45
"fmt"
56
"os"
67
"path/filepath"
@@ -121,7 +122,7 @@ func (c *Cache) cacheExecutable() error {
121122
// Check the file is tarball or not
122123
if isTarball(assetTmpFile) {
123124
// Extract the tarball and put it the cache directory.
124-
extractedFiles, err = extract.UncompressWithFilter(assetTmpFile, tmpDir,
125+
extractedFiles, err = extract.UncompressWithFilter(context.TODO(), assetTmpFile, tmpDir,
125126
func(filename string) bool { return filepath.Base(filename) == c.GetExecutableName() })
126127
if err != nil {
127128
return errors.Wrapf(err, "Cannot uncompress '%s'", assetTmpFile)
@@ -153,7 +154,7 @@ func (c *Cache) getExecutable(destDir string) (string, error) {
153154
destPath := filepath.Join(destDir, archiveName)
154155
err := embed.Extract(archiveName, destPath)
155156
if err != nil {
156-
return download.Download(c.archiveURL, destDir, 0600, nil)
157+
return download.Download(context.TODO(), c.archiveURL, destDir, 0600, nil)
157158
}
158159

159160
return destPath, err

pkg/crc/image/image.go

+8-5
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ func (img *imageHandler) policyContext() (*signature.PolicyContext, error) {
5353
}
5454

5555
// copyImage pulls the image from the registry and puts it to destination path
56-
func (img *imageHandler) copyImage(destPath string, reportWriter io.Writer) (*v1.Manifest, error) {
56+
func (img *imageHandler) copyImage(ctx context.Context, destPath string, reportWriter io.Writer) (*v1.Manifest, error) {
5757
// Source Image from docker transport
5858
srcImg := img.imageURI
5959
srcRef, err := docker.ParseReference(srcImg)
@@ -71,7 +71,10 @@ func (img *imageHandler) copyImage(destPath string, reportWriter io.Writer) (*v1
7171
return nil, err
7272
}
7373

74-
manifestData, err := copy.Image(context.Background(), policyContext,
74+
if ctx == nil {
75+
panic("ctx is nil, this should not happen")
76+
}
77+
manifestData, err := copy.Image(ctx, policyContext,
7578
destRef, srcRef, &copy.Options{
7679
ReportWriter: reportWriter,
7780
})
@@ -113,7 +116,7 @@ func GetPresetName(imageName string) crcpreset.Preset {
113116
return preset
114117
}
115118

116-
func PullBundle(imageURI string) (string, error) {
119+
func PullBundle(ctx context.Context, imageURI string) (string, error) {
117120
imgHandler := imageHandler{
118121
imageURI: strings.TrimPrefix(imageURI, "docker:"),
119122
}
@@ -122,7 +125,7 @@ func PullBundle(imageURI string) (string, error) {
122125
return "", err
123126
}
124127
defer os.RemoveAll(destDir)
125-
imgManifest, err := imgHandler.copyImage(destDir, os.Stdout)
128+
imgManifest, err := imgHandler.copyImage(ctx, destDir, os.Stdout)
126129
if err != nil {
127130
return "", err
128131
}
@@ -132,7 +135,7 @@ func PullBundle(imageURI string) (string, error) {
132135
if err != nil {
133136
return "", err
134137
}
135-
fileList, err := extract.Uncompress(filepath.Join(destDir, imgLayer), constants.MachineCacheDir)
138+
fileList, err := extract.Uncompress(ctx, filepath.Join(destDir, imgLayer), constants.MachineCacheDir)
136139
if err != nil {
137140
return "", err
138141
}

pkg/crc/machine/bundle/metadata.go

+9-9
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package bundle
22

33
import (
4+
"context"
45
"encoding/json"
56
"fmt"
67
"io"
@@ -337,16 +338,15 @@ func getVerifiedHash(url string, file string) (string, error) {
337338
return "", fmt.Errorf("%s hash is missing or shasums are malformed", file)
338339
}
339340

340-
func downloadDefault(preset crcPreset.Preset) (string, error) {
341+
func downloadDefault(ctx context.Context, preset crcPreset.Preset) (string, error) {
341342
downloadInfo, err := getBundleDownloadInfo(preset)
342343
if err != nil {
343344
return "", err
344345
}
345-
346-
return downloadInfo.Download(constants.GetDefaultBundlePath(preset), 0664)
346+
return downloadInfo.Download(ctx, constants.GetDefaultBundlePath(preset), 0664)
347347
}
348348

349-
func Download(preset crcPreset.Preset, bundleURI string, enableBundleQuayFallback bool) (string, error) {
349+
func Download(ctx context.Context, preset crcPreset.Preset, bundleURI string, enableBundleQuayFallback bool) (string, error) {
350350
// If we are asked to download
351351
// ~/.crc/cache/crc_podman_libvirt_4.1.1.crcbundle, this means we want
352352
// are downloading the default bundle for this release. This uses a
@@ -355,23 +355,23 @@ func Download(preset crcPreset.Preset, bundleURI string, enableBundleQuayFallbac
355355
if bundleURI == constants.GetDefaultBundlePath(preset) {
356356
switch preset {
357357
case crcPreset.OpenShift, crcPreset.Microshift:
358-
downloadedBundlePath, err := downloadDefault(preset)
358+
downloadedBundlePath, err := downloadDefault(ctx, preset)
359359
if err != nil && enableBundleQuayFallback {
360360
logging.Info("Unable to download bundle from mirror, falling back to quay")
361-
return image.PullBundle(constants.GetDefaultBundleImageRegistry(preset))
361+
return image.PullBundle(ctx, constants.GetDefaultBundleImageRegistry(preset))
362362
}
363363
return downloadedBundlePath, err
364364
case crcPreset.OKD:
365365
fallthrough
366366
default:
367-
return image.PullBundle(constants.GetDefaultBundleImageRegistry(preset))
367+
return image.PullBundle(ctx, constants.GetDefaultBundleImageRegistry(preset))
368368
}
369369
}
370370
switch {
371371
case strings.HasPrefix(bundleURI, "http://"), strings.HasPrefix(bundleURI, "https://"):
372-
return download.Download(bundleURI, constants.MachineCacheDir, 0644, nil)
372+
return download.Download(ctx, bundleURI, constants.MachineCacheDir, 0644, nil)
373373
case strings.HasPrefix(bundleURI, "docker://"):
374-
return image.PullBundle(bundleURI)
374+
return image.PullBundle(ctx, bundleURI)
375375
}
376376
// the `bundleURI` parameter turned out to be a local path
377377
return bundleURI, nil

pkg/crc/machine/bundle/repository.go

+4-4
Original file line numberDiff line numberDiff line change
@@ -124,7 +124,7 @@ func (bundle *CrcBundleInfo) createSymlinkOrCopyPodmanRemote(binDir string) erro
124124
return bundle.copyExecutableFromBundle(binDir, PodmanExecutable, constants.PodmanRemoteExecutableName)
125125
}
126126

127-
func (repo *Repository) Extract(path string) error {
127+
func (repo *Repository) Extract(ctx context.Context, path string) error {
128128
bundleName := filepath.Base(path)
129129

130130
tmpDir := filepath.Join(repo.CacheDir, "tmp-extract")
@@ -133,7 +133,7 @@ func (repo *Repository) Extract(path string) error {
133133
_ = os.RemoveAll(tmpDir) // clean up after using it
134134
}()
135135

136-
if _, err := extract.Uncompress(path, tmpDir); err != nil {
136+
if _, err := extract.Uncompress(ctx, path, tmpDir); err != nil {
137137
return err
138138
}
139139

@@ -198,8 +198,8 @@ func Use(bundleName string) (*CrcBundleInfo, error) {
198198
return defaultRepo.Use(bundleName)
199199
}
200200

201-
func Extract(path string) (*CrcBundleInfo, error) {
202-
if err := defaultRepo.Extract(path); err != nil {
201+
func Extract(ctx context.Context, path string) (*CrcBundleInfo, error) {
202+
if err := defaultRepo.Extract(ctx, path); err != nil {
203203
return nil, err
204204
}
205205
return defaultRepo.Get(filepath.Base(path))

pkg/crc/machine/bundle/repository_test.go

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package bundle
22

33
import (
4+
"context"
45
"os"
56
"path/filepath"
67
"runtime"
@@ -39,7 +40,7 @@ func TestExtract(t *testing.T) {
3940
OcBinDir: ocBinDir,
4041
}
4142

42-
assert.NoError(t, repo.Extract(filepath.Join("testdata", testBundle(t))))
43+
assert.NoError(t, repo.Extract(context.TODO(), filepath.Join("testdata", testBundle(t))))
4344

4445
bundle, err := repo.Get(testBundle(t))
4546
assert.NoError(t, err)

pkg/crc/machine/start.go

+5-5
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ import (
1414
"github.com/crc-org/crc/v2/pkg/crc/cluster"
1515
"github.com/crc-org/crc/v2/pkg/crc/constants"
1616
crcerrors "github.com/crc-org/crc/v2/pkg/crc/errors"
17-
logging "github.com/crc-org/crc/v2/pkg/crc/logging"
17+
"github.com/crc-org/crc/v2/pkg/crc/logging"
1818
"github.com/crc-org/crc/v2/pkg/crc/machine/bundle"
1919
"github.com/crc-org/crc/v2/pkg/crc/machine/config"
2020
"github.com/crc-org/crc/v2/pkg/crc/machine/state"
@@ -41,20 +41,20 @@ import (
4141

4242
const minimumMemoryForMonitoring = 14336
4343

44-
func getCrcBundleInfo(preset crcPreset.Preset, bundleName, bundlePath string, enableBundleQuayFallback bool) (*bundle.CrcBundleInfo, error) {
44+
func getCrcBundleInfo(ctx context.Context, preset crcPreset.Preset, bundleName, bundlePath string, enableBundleQuayFallback bool) (*bundle.CrcBundleInfo, error) {
4545
bundleInfo, err := bundle.Use(bundleName)
4646
if err == nil {
4747
logging.Infof("Loading bundle: %s...", bundleName)
4848
return bundleInfo, nil
4949
}
5050
logging.Debugf("Failed to load bundle %s: %v", bundleName, err)
5151
logging.Infof("Downloading bundle: %s...", bundleName)
52-
bundlePath, err = bundle.Download(preset, bundlePath, enableBundleQuayFallback)
52+
bundlePath, err = bundle.Download(ctx, preset, bundlePath, enableBundleQuayFallback)
5353
if err != nil {
5454
return nil, err
5555
}
5656
logging.Infof("Extracting bundle: %s...", bundleName)
57-
if _, err := bundle.Extract(bundlePath); err != nil {
57+
if _, err := bundle.Extract(ctx, bundlePath); err != nil {
5858
return nil, err
5959
}
6060
return bundle.Use(bundleName)
@@ -279,7 +279,7 @@ func (client *client) Start(ctx context.Context, startConfig types.StartConfig)
279279
}
280280

281281
bundleName := bundle.GetBundleNameWithoutExtension(bundle.GetBundleNameFromURI(startConfig.BundlePath))
282-
crcBundleMetadata, err := getCrcBundleInfo(startConfig.Preset, bundleName, startConfig.BundlePath, startConfig.EnableBundleQuayFallback)
282+
crcBundleMetadata, err := getCrcBundleInfo(ctx, startConfig.Preset, bundleName, startConfig.BundlePath, startConfig.EnableBundleQuayFallback)
283283
if err != nil {
284284
return nil, errors.Wrap(err, "Error getting bundle metadata")
285285
}

pkg/crc/preflight/preflight_checks_common.go

+3-2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package preflight
22

33
import (
4+
"context"
45
"fmt"
56
"os"
67
"path/filepath"
@@ -112,12 +113,12 @@ func fixBundleExtracted(bundlePath string, preset crcpreset.Preset, enableBundle
112113
}
113114
var err error
114115
logging.Infof("Downloading bundle: %s...", bundlePath)
115-
if bundlePath, err = bundle.Download(preset, bundlePath, enableBundleQuayFallback); err != nil {
116+
if bundlePath, err = bundle.Download(context.TODO(), preset, bundlePath, enableBundleQuayFallback); err != nil {
116117
return err
117118
}
118119

119120
logging.Infof("Uncompressing %s", bundlePath)
120-
if _, err := bundle.Extract(bundlePath); err != nil {
121+
if _, err := bundle.Extract(context.TODO(), bundlePath); err != nil {
121122
if errors.Is(err, os.ErrNotExist) {
122123
return errors.Wrap(err, "Use `crc setup -b <bundle-path>`")
123124
}

pkg/download/download.go

+11-4
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package download
22

33
import (
4+
"context"
45
"crypto/sha256"
56
"encoding/hex"
67
"fmt"
@@ -60,7 +61,7 @@ loop:
6061

6162
// Download function takes sha256sum as hex decoded byte
6263
// something like hex.DecodeString("33daf4c03f86120fdfdc66bddf6bfff4661c7ca11c5d")
63-
func Download(uri, destination string, mode os.FileMode, sha256sum []byte) (string, error) {
64+
func Download(ctx context.Context, uri, destination string, mode os.FileMode, sha256sum []byte) (string, error) {
6465
logging.Debugf("Downloading %s to %s", uri, destination)
6566

6667
client := grab.NewClient()
@@ -70,6 +71,12 @@ func Download(uri, destination string, mode os.FileMode, sha256sum []byte) (stri
7071
if err != nil {
7172
return "", errors.Wrapf(err, "unable to get request from %s", uri)
7273
}
74+
75+
if ctx == nil {
76+
panic("ctx is nil, this should not happen")
77+
}
78+
req = req.WithContext(ctx)
79+
7380
if sha256sum != nil {
7481
req.SetChecksum(sha256.New(), sha256sum, true)
7582
}
@@ -131,12 +138,12 @@ func NewRemoteFile(uri, sha256sum string) *RemoteFile {
131138

132139
}
133140

134-
func (r *RemoteFile) Download(bundlePath string, mode os.FileMode) (string, error) {
135-
sha256, err := hex.DecodeString(r.sha256sum)
141+
func (r *RemoteFile) Download(ctx context.Context, bundlePath string, mode os.FileMode) (string, error) {
142+
sha256bytes, err := hex.DecodeString(r.sha256sum)
136143
if err != nil {
137144
return "", err
138145
}
139-
return Download(r.URI, bundlePath, mode, sha256)
146+
return Download(ctx, r.URI, bundlePath, mode, sha256bytes)
140147
}
141148

142149
func (r *RemoteFile) GetSha256Sum() string {

0 commit comments

Comments
 (0)