Skip to content

Commit

Permalink
Merge pull request #153 from q384566678/config-type
Browse files Browse the repository at this point in the history
image: code optimization
  • Loading branch information
Zhou Hao authored Jan 24, 2018
2 parents af57c3b + 42074d0 commit 974de27
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 30 deletions.
8 changes: 3 additions & 5 deletions image/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,10 +31,8 @@ import (
"github.com/pkg/errors"
)

type config v1.Image

func findConfig(w walker, d *v1.Descriptor) (*config, error) {
var c config
func findConfig(w walker, d *v1.Descriptor) (*v1.Image, error) {
var c v1.Image
cpath := filepath.Join("blobs", string(d.Digest.Algorithm()), d.Digest.Hex())

switch err := w.walk(func(path string, info os.FileInfo, r io.Reader) error {
Expand Down Expand Up @@ -65,7 +63,7 @@ func findConfig(w walker, d *v1.Descriptor) (*config, error) {
}
}

func (c *config) runtimeSpec(rootfs string) (*specs.Spec, error) {
func runtimeSpec(c *v1.Image, rootfs string) (*specs.Spec, error) {
if c.OS != "linux" {
return nil, fmt.Errorf("%s: unsupported OS", c.OS)
}
Expand Down
24 changes: 12 additions & 12 deletions image/image.go
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ func validate(w walker, refs []string, out *log.Logger) error {
return err
}

if err := m.validate(w); err != nil {
if err := validateManifest(m, w); err != nil {
return err
}
}
Expand All @@ -130,7 +130,7 @@ func validate(w walker, refs []string, out *log.Logger) error {
return err
}

if err := m.validate(w); err != nil {
if err := validateManifest(m, w); err != nil {
return err
}
}
Expand Down Expand Up @@ -198,11 +198,11 @@ func unpack(w walker, dest, platform string, refs []string) error {
return err
}

if err := m.validate(w); err != nil {
if err := validateManifest(m, w); err != nil {
return err
}

return m.unpack(w, dest)
return unpackManifest(m, w, dest)
}

if ref.MediaType == validRefMediaTypes[1] {
Expand All @@ -221,7 +221,7 @@ func unpack(w walker, dest, platform string, refs []string) error {
}

for _, m := range manifests {
return m.unpack(w, dest)
return unpackManifest(m, w, dest)
}
}

Expand Down Expand Up @@ -282,7 +282,7 @@ func createRuntimeBundle(w walker, dest, rootfs, platform string, refs []string)
return err
}

if err := m.validate(w); err != nil {
if err := validateManifest(m, w); err != nil {
return err
}

Expand Down Expand Up @@ -312,7 +312,7 @@ func createRuntimeBundle(w walker, dest, rootfs, platform string, refs []string)
return nil
}

func createBundle(w walker, m *manifest, dest, rootfs string) (retErr error) {
func createBundle(w walker, m *v1.Manifest, dest, rootfs string) (retErr error) {
c, err := findConfig(w, &m.Config)
if err != nil {
return err
Expand All @@ -335,11 +335,11 @@ func createBundle(w walker, m *manifest, dest, rootfs string) (retErr error) {
}
}

if err = m.unpack(w, filepath.Join(dest, rootfs)); err != nil {
if err = unpackManifest(m, w, filepath.Join(dest, rootfs)); err != nil {
return err
}

spec, err := c.runtimeSpec(rootfs)
spec, err := runtimeSpec(c, rootfs)
if err != nil {
return err
}
Expand All @@ -354,8 +354,8 @@ func createBundle(w walker, m *manifest, dest, rootfs string) (retErr error) {
}

// filertManifest returns a filtered list of manifests
func filterManifest(w walker, Manifests []v1.Descriptor, platform string) ([]*manifest, error) {
var manifests []*manifest
func filterManifest(w walker, Manifests []v1.Descriptor, platform string) ([]*v1.Manifest, error) {
var manifests []*v1.Manifest

argsParts := strings.Split(platform, ":")
if len(argsParts) != 2 {
Expand All @@ -373,7 +373,7 @@ func filterManifest(w walker, Manifests []v1.Descriptor, platform string) ([]*ma
return manifests, err
}

if err := m.validate(w); err != nil {
if err := validateManifest(m, w); err != nil {
return manifests, err
}
if strings.EqualFold(manifest.Platform.OS, argsParts[0]) && strings.EqualFold(manifest.Platform.Architecture, argsParts[1]) {
Expand Down
13 changes: 4 additions & 9 deletions image/manifest.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,13 +35,8 @@ import (
"github.com/sirupsen/logrus"
)

type manifest struct {
Config v1.Descriptor `json:"config"`
Layers []v1.Descriptor `json:"layers"`
}

func findManifest(w walker, d *v1.Descriptor) (*manifest, error) {
var m manifest
func findManifest(w walker, d *v1.Descriptor) (*v1.Manifest, error) {
var m v1.Manifest
mpath := filepath.Join("blobs", string(d.Digest.Algorithm()), d.Digest.Hex())

switch err := w.walk(func(path string, info os.FileInfo, r io.Reader) error {
Expand Down Expand Up @@ -73,7 +68,7 @@ func findManifest(w walker, d *v1.Descriptor) (*manifest, error) {
}
}

func (m *manifest) validate(w walker) error {
func validateManifest(m *v1.Manifest, w walker) error {
if err := validateDescriptor(&m.Config, w, []string{v1.MediaTypeImageConfig}); err != nil {
return errors.Wrap(err, "config validation failed")
}
Expand All @@ -94,7 +89,7 @@ func (m *manifest) validate(w walker) error {
return nil
}

func (m *manifest) unpack(w walker, dest string) (retErr error) {
func unpackManifest(m *v1.Manifest, w walker, dest string) (retErr error) {
// error out if the dest directory is not empty
s, err := ioutil.ReadDir(dest)
if err != nil && !os.IsNotExist(err) { // We'll create the dir later
Expand Down
8 changes: 4 additions & 4 deletions image/manifest_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -141,15 +141,15 @@ func testUnpackLayer(t *testing.T, compression string, invalid bool) {
mediatype += "+" + compression
}

testManifest := manifest{
testManifest := v1.Manifest{
Layers: []v1.Descriptor{
{
MediaType: mediatype,
Digest: digester.Digest(),
},
},
}
err = testManifest.unpack(newPathWalker(tmp1), filepath.Join(tmp1, "rootfs"))
err = unpackManifest(&testManifest, newPathWalker(tmp1), filepath.Join(tmp1, "rootfs"))
if err != nil {
t.Fatal(errors.Wrapf(err, "%q / %s", blobPath, compression))
}
Expand Down Expand Up @@ -212,15 +212,15 @@ func TestUnpackLayerRemovePartiallyUnpackedFile(t *testing.T) {
t.Fatal(err)
}

testManifest := manifest{
testManifest := v1.Manifest{
Layers: []v1.Descriptor{
{
MediaType: "application/vnd.oci.image.layer.v1.tar+gzip",
Digest: digester.Digest(),
},
},
}
err = testManifest.unpack(newPathWalker(tmp1), filepath.Join(tmp1, "rootfs"))
err = unpackManifest(&testManifest, newPathWalker(tmp1), filepath.Join(tmp1, "rootfs"))
if err != nil && !strings.Contains(err.Error(), "duplicate entry for") {
t.Fatal(err)
}
Expand Down

0 comments on commit 974de27

Please sign in to comment.