Skip to content

Commit

Permalink
Return bundle location from PullBundle and DownloadDefault
Browse files Browse the repository at this point in the history
This allows to remove a bit of redundant code.
  • Loading branch information
cfergeau authored and praveenkumar committed Sep 19, 2022
1 parent 35faa01 commit 73a6d0b
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 22 deletions.
18 changes: 11 additions & 7 deletions pkg/crc/image/image.go
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,7 @@ func GetPresetName(imageName string) crcpreset.Preset {
return preset
}

func PullBundle(preset crcpreset.Preset, imageURI string) error {
func PullBundle(preset crcpreset.Preset, imageURI string) (string, error) {
if imageURI == "" {
imageURI = defaultURI(preset)
}
Expand All @@ -138,33 +138,37 @@ func PullBundle(preset crcpreset.Preset, imageURI string) error {
}
destDir, err := os.MkdirTemp(constants.MachineCacheDir, "tmpBundleImage")
if err != nil {
return err
return "", err
}
defer os.RemoveAll(destDir)
imgManifest, err := imgHandler.copyImage(destDir, os.Stdout)
if err != nil {
return err
return "", err
}

logging.Info("Extracting the image bundle layer...")
imgLayer, err := getLayerPath(imgManifest, 0, "application/vnd.oci.image.layer.v1.tar+gzip")
if err != nil {
return err
return "", err
}
fileList, err := extract.Uncompress(filepath.Join(destDir, imgLayer), constants.MachineCacheDir, true)
if err != nil {
return err
return "", err
}
logging.Debugf("Bundle and sign path: %v", fileList)

logging.Info("Verifying the bundle signature...")
if len(fileList) != 2 {
return fmt.Errorf("image layer contains more files than expected: %v", fileList)
return "", fmt.Errorf("image layer contains more files than expected: %v", fileList)
}
bundleFilePath, sigFilePath := fileList[0], fileList[1]
if !strings.HasSuffix(sigFilePath, ".crcbundle.sig") {
sigFilePath, bundleFilePath = fileList[0], fileList[1]
}

return gpg.Verify(bundleFilePath, sigFilePath)
if err := gpg.Verify(bundleFilePath, sigFilePath); err != nil {
return "", err
}

return bundleFilePath, nil
}
23 changes: 8 additions & 15 deletions pkg/crc/machine/bundle/metadata.go
Original file line number Diff line number Diff line change
Expand Up @@ -286,23 +286,20 @@ func getBundleDownloadInfo(preset crcPreset.Preset) (*download.RemoteFile, error
return downloadInfo, nil
}

func DownloadDefault(preset crcPreset.Preset) error {
func DownloadDefault(preset crcPreset.Preset) (string, error) {
downloadInfo, err := getBundleDownloadInfo(preset)
if err != nil {
return err
return "", err
}
bundleNameFromURI, err := downloadInfo.GetSourceFilename()
if err != nil {
return err
return "", err
}
if constants.GetDefaultBundle(preset) != bundleNameFromURI {
return fmt.Errorf("expected %s but found %s in Makefile", bundleNameFromURI, constants.GetDefaultBundle(preset))
}
if _, err := downloadInfo.Download(constants.GetDefaultBundlePath(preset), 0664); err != nil {
return err
return "", fmt.Errorf("expected %s but found %s in Makefile", bundleNameFromURI, constants.GetDefaultBundle(preset))
}

return nil
return downloadInfo.Download(constants.GetDefaultBundlePath(preset), 0664)
}

func Download(preset crcPreset.Preset, bundleURI string) (string, error) {
Expand All @@ -313,19 +310,15 @@ func Download(preset crcPreset.Preset, bundleURI string) (string, error) {
// bundles, their sha256sums are known and can be checked.
if bundleURI == constants.GetDefaultBundlePath(preset) {
if preset == crcPreset.OpenShift {
return bundleURI, DownloadDefault(preset)
return DownloadDefault(preset)
}
return bundleURI, image.PullBundle(preset, "")
return image.PullBundle(preset, "")
}
switch {
case strings.HasPrefix(bundleURI, "http://"), strings.HasPrefix(bundleURI, "https://"):
return download.Download(bundleURI, constants.MachineCacheDir, 0644, nil)
case strings.HasPrefix(bundleURI, "docker://"):
if err := image.PullBundle(preset, bundleURI); err != nil {
return "", err
}
bundlePath := filepath.Join(constants.MachineCacheDir, GetBundleNameFromURI(bundleURI))
return bundlePath, nil
return image.PullBundle(preset, bundleURI)
}
// the `bundleURI` parameter turned out to be a local path
return bundleURI, nil
Expand Down

0 comments on commit 73a6d0b

Please sign in to comment.