Skip to content

Commit

Permalink
Refactor new_container_pull to generate fully backward compatible for…
Browse files Browse the repository at this point in the history
…mat (bazelbuild#974)

* refactor puller.go to generate fully backward compatible format

* implement manifest.json symlink creation

* refactor variable names and add error checks for manifest list length

* modify manifest list check to prevent manifest panic

* refactor error message

* add comma
  • Loading branch information
xwinxu authored and k8s-ci-robot committed Jul 15, 2019
1 parent 8eb250b commit 77718a2
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 7 deletions.
3 changes: 1 addition & 2 deletions container/go/cmd/puller/puller.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@
// For the format specification, if the format is:
// 1. 'docker': image is pulled as tarball and may be used with `docker load -i`.
// 2. 'oci' (default): image will be pulled as a collection of files in OCI layout to directory.
// 3. 'both': both formats of image are pulled.
// Unlike regular docker pull, the format this package uses is proprietary.

package main
Expand All @@ -31,8 +30,8 @@ import (

"github.com/pkg/errors"

"github.com/bazelbuild/rules_docker/container/go/pkg/oci"
"github.com/bazelbuild/rules_docker/container/go/pkg/compat"
"github.com/bazelbuild/rules_docker/container/go/pkg/oci"
"github.com/google/go-containerregistry/pkg/authn"
"github.com/google/go-containerregistry/pkg/name"
v1 "github.com/google/go-containerregistry/pkg/v1"
Expand Down
1 change: 1 addition & 0 deletions container/go/pkg/compat/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ go_library(
visibility = ["//visibility:public"],
deps = [
"@com_github_google_go_containerregistry//pkg/v1:go_default_library",
"@com_github_google_go_containerregistry//pkg/v1/layout:go_default_library",
"@com_github_google_go_containerregistry//pkg/v1/partial:go_default_library",
"@com_github_google_go_containerregistry//pkg/v1/types:go_default_library",
"@com_github_google_go_containerregistry//pkg/v1/validate:go_default_library",
Expand Down
33 changes: 28 additions & 5 deletions container/go/pkg/compat/ociToLegacy.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,17 +19,20 @@
package compat

import (
"log"
ospkg "os"
"path"
"strconv"

v1 "github.com/google/go-containerregistry/pkg/v1"
"github.com/google/go-containerregistry/pkg/v1/layout"
"github.com/pkg/errors"
)

// Extension for layers and config files that are made symlinks
const targzExt = ".tar.gz"
const configExt = "config.json"
const compressedLayerExt = ".tar.gz"
const legacyConfigFile = "config.json"
const legacyManifestFile = "manifest.json"

// generateSymlinks safely generates a symbolic link at dst pointing to src.
func generateSymlinks(src, dst string) error {
Expand Down Expand Up @@ -63,7 +66,7 @@ func LegacyFromOCIImage(img v1.Image, srcDir, dstDir string) error {
return errors.Wrap(err, "failed to get the config file's hash information for image")
}
configPath := path.Join(targetDir, config.Hex)
dstLink := path.Join(dstDir, configExt)
dstLink := path.Join(dstDir, legacyConfigFile)
if err = generateSymlinks(configPath, dstLink); err != nil {
return errors.Wrap(err, "failed to generate config.json symlink")
}
Expand All @@ -80,15 +83,35 @@ func LegacyFromOCIImage(img v1.Image, srcDir, dstDir string) error {
layerDigest, err := layer.Digest()
if err != nil {
return errors.Wrap(err, "failed to fetch the layer's digest")
}
}

layerPath = path.Join(targetDir, layerDigest.Hex)
out := strconv.Itoa(i) + targzExt
out := strconv.Itoa(i) + compressedLayerExt
dstLink = path.Join(dstDir, out)
if err = generateSymlinks(layerPath, dstLink); err != nil {
return errors.Wrapf(err, "failed to generate legacy symlink for layer %d with digest %s", i, layerDigest)
}
}

// symlink for the image manifest pulled into OCI layout to .json, so must rename the file from being
// named after its sha256 digest under blobs/sha256 to manifest.json
imgIndex, err := layout.ImageIndexFromPath(srcDir)
if err != nil {
return errors.Wrapf(err, "unable to open image as index from %s", srcDir)
}
manifest, err := imgIndex.IndexManifest()
if err != nil {
return errors.Wrap(err, "unable to get manifest from image index")
}
if len(manifest.Manifests) != 1 {
log.Fatalf("Image index read from %s had unexpected number of manifests: got %d, want 1", srcDir, len(manifest.Manifests))
}
manifestHex := manifest.Manifests[0].Digest.Hex
dstLink = path.Join(dstDir, legacyManifestFile)
manifestPath := path.Join(targetDir, manifestHex)
if err = generateSymlinks(manifestPath, dstLink); err != nil {
return errors.Wrapf(err, "failed to generate %s symlink", legacyManifestFile)
}

return nil
}

0 comments on commit 77718a2

Please sign in to comment.