Skip to content

Commit 5849a6f

Browse files
committed
feat(import): import single-layer container images directly
Add support to import and overlay single-layer container images. This allows for a different model of software distribution in the container image ecosystem. --- apt-get install openssl curl ca-certificates (becomes) import: - path: docker://ghcr.io/homebrew/core/openssl/1.1:1.1.1k dest: / - path: docker://ghcr.io/homebrew/core/curl:8.0.1 dest: / - path: docker://ghcr.io/homebrew/core/ca-certificates:2022-10-11 dest: / Signed-off-by: Ramkumar Chinchani <[email protected]>
1 parent 8d233ed commit 5849a6f

File tree

6 files changed

+202
-110
lines changed

6 files changed

+202
-110
lines changed

pkg/oci/oci.go

+115
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,20 @@ package oci
22

33
import (
44
"context"
5+
"os"
6+
"path"
7+
"runtime"
8+
"sync"
59

10+
"github.com/klauspost/pgzip"
11+
"github.com/opencontainers/go-digest"
612
ispec "github.com/opencontainers/image-spec/specs-go/v1"
13+
"github.com/opencontainers/umoci"
714
"github.com/opencontainers/umoci/oci/casext"
15+
"github.com/opencontainers/umoci/oci/layer"
816
"github.com/pkg/errors"
17+
"stackerbuild.io/stacker/pkg/log"
18+
"stackerbuild.io/stacker/pkg/squashfs"
919
)
1020

1121
func LookupManifest(oci casext.Engine, tag string) (ispec.Manifest, error) {
@@ -76,3 +86,108 @@ func UpdateImageConfig(oci casext.Engine, name string, newConfig ispec.Image, ne
7686

7787
return desc, nil
7888
}
89+
90+
func hasDirEntries(dir string) bool {
91+
ents, err := os.ReadDir(dir)
92+
if err != nil {
93+
return false
94+
}
95+
return len(ents) != 0
96+
}
97+
98+
var tarEx sync.Mutex
99+
100+
// UnpackOne - unpack a single layer (Descriptor) found in ociDir to extractDir
101+
//
102+
// The result of calling unpackOne is either error or the contents available
103+
// at the provided extractDir. The extractDir should be either empty or
104+
// fully populated with this layer.
105+
func UnpackOne(l ispec.Descriptor, ociDir string, extractDir string) error {
106+
// population of a dir is not atomic, at least for tar extraction.
107+
// As a result, we could hasDirEntries(extractDir) at the same time that
108+
// something is un-populating that dir due to a failed extraction (like
109+
// os.RemoveAll below).
110+
// There needs to be a lock on the extract dir (scoped to the overlay storage backend).
111+
// A sync.RWMutex would work well here since it is safe to check as long
112+
// as no one is populating or unpopulating.
113+
if hasDirEntries(extractDir) {
114+
// the directory was already populated.
115+
return nil
116+
}
117+
118+
if squashfs.IsSquashfsMediaType(l.MediaType) {
119+
return squashfs.ExtractSingleSquash(
120+
path.Join(ociDir, "blobs", "sha256", l.Digest.Encoded()), extractDir)
121+
}
122+
switch l.MediaType {
123+
case ispec.MediaTypeImageLayer, ispec.MediaTypeImageLayerGzip:
124+
tarEx.Lock()
125+
defer tarEx.Unlock()
126+
127+
oci, err := umoci.OpenLayout(ociDir)
128+
if err != nil {
129+
return err
130+
}
131+
defer oci.Close()
132+
133+
compressed, err := oci.GetBlob(context.Background(), l.Digest)
134+
if err != nil {
135+
return err
136+
}
137+
defer compressed.Close()
138+
139+
uncompressed, err := pgzip.NewReader(compressed)
140+
if err != nil {
141+
return err
142+
}
143+
144+
err = layer.UnpackLayer(extractDir, uncompressed, nil)
145+
if err != nil {
146+
if rmErr := os.RemoveAll(extractDir); rmErr != nil {
147+
log.Errorf("Failed to remove dir '%s' after failed extraction: %v", extractDir, rmErr)
148+
}
149+
}
150+
return err
151+
}
152+
return errors.Errorf("unknown media type %s", l.MediaType)
153+
}
154+
155+
// Unpack an image with "tag" from "ociLayout" into paths returned by "pathfunc"
156+
func Unpack(ociLayout, tag string, pathfunc func(digest.Digest) string) (int, error) {
157+
oci, err := umoci.OpenLayout(ociLayout)
158+
if err != nil {
159+
return -1, err
160+
}
161+
defer oci.Close()
162+
163+
manifest, err := LookupManifest(oci, tag)
164+
if err != nil {
165+
return -1, err
166+
}
167+
168+
pool := NewThreadPool(runtime.NumCPU())
169+
170+
seen := map[digest.Digest]bool{}
171+
for _, curLayer := range manifest.Layers {
172+
// avoid calling UnpackOne twice for the same digest
173+
if seen[curLayer.Digest] {
174+
continue
175+
}
176+
seen[curLayer.Digest] = true
177+
178+
// copy layer to avoid race on pool access.
179+
l := curLayer
180+
pool.Add(func(ctx context.Context) error {
181+
return UnpackOne(l, ociLayout, pathfunc(l.Digest))
182+
})
183+
}
184+
185+
pool.DoneAddingJobs()
186+
187+
err = pool.Run()
188+
if err != nil {
189+
return -1, err
190+
}
191+
192+
return len(manifest.Layers), nil
193+
}

pkg/overlay/pool.go renamed to pkg/oci/pool.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package overlay
1+
package oci
22

33
import (
44
"context"

pkg/overlay/overlay.go

+2-9
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ import (
1414
ispec "github.com/opencontainers/image-spec/specs-go/v1"
1515
"github.com/pkg/errors"
1616
"golang.org/x/sys/unix"
17+
"stackerbuild.io/stacker/pkg/oci"
1718
"stackerbuild.io/stacker/pkg/types"
1819
)
1920

@@ -142,14 +143,6 @@ func (o *overlay) SetupEmptyRootfs(name string) error {
142143
return ovl.write(o.config, name)
143144
}
144145

145-
func hasDirEntries(dir string) bool {
146-
ents, err := os.ReadDir(dir)
147-
if err != nil {
148-
return false
149-
}
150-
return len(ents) != 0
151-
}
152-
153146
func (o *overlay) snapshot(source string, target string) error {
154147
err := o.Create(target)
155148
if err != nil {
@@ -168,7 +161,7 @@ func (o *overlay) snapshot(source string, target string) error {
168161
}
169162
ociDir := path.Join(o.config.StackerDir, "layer-bases", "oci")
170163
for _, layer := range manifest.Layers {
171-
err := unpackOne(layer, ociDir, overlayPath(o.config.RootFSDir, layer.Digest, "overlay"))
164+
err := oci.UnpackOne(layer, ociDir, overlayPath(o.config.RootFSDir, layer.Digest, "overlay"))
172165
if err != nil {
173166
return errors.Wrapf(err, "Failed mounting %#v", layer)
174167
}

pkg/overlay/pack.go

+21-100
Original file line numberDiff line numberDiff line change
@@ -8,12 +8,9 @@ import (
88
"os"
99
"path"
1010
"path/filepath"
11-
"runtime"
1211
"strings"
13-
"sync"
1412
"time"
1513

16-
"github.com/klauspost/pgzip"
1714
"github.com/opencontainers/go-digest"
1815
ispec "github.com/opencontainers/image-spec/specs-go/v1"
1916
"github.com/opencontainers/umoci"
@@ -24,14 +21,13 @@ import (
2421
"github.com/pkg/xattr"
2522
"stackerbuild.io/stacker/pkg/lib"
2623
"stackerbuild.io/stacker/pkg/log"
24+
"stackerbuild.io/stacker/pkg/oci"
2725
stackeroci "stackerbuild.io/stacker/pkg/oci"
2826
"stackerbuild.io/stacker/pkg/squashfs"
2927
"stackerbuild.io/stacker/pkg/storage"
3028
"stackerbuild.io/stacker/pkg/types"
3129
)
3230

33-
var tarEx sync.Mutex
34-
3531
// Container image layers are often tar.gz, however there is nothing in the
3632
// spec or documentation which standardizes compression params which can cause
3733
// different layer hashes even for the same tar. So picking compression params
@@ -51,56 +47,6 @@ func overlayPath(rootfs string, d digest.Digest, subdirs ...string) string {
5147
return path.Join(dirs...)
5248
}
5349

54-
func (o *overlay) Unpack(tag, name string) error {
55-
cacheDir := path.Join(o.config.StackerDir, "layer-bases", "oci")
56-
oci, err := umoci.OpenLayout(cacheDir)
57-
if err != nil {
58-
return err
59-
}
60-
defer oci.Close()
61-
62-
manifest, err := stackeroci.LookupManifest(oci, tag)
63-
if err != nil {
64-
return err
65-
}
66-
67-
pool := NewThreadPool(runtime.NumCPU())
68-
69-
seen := map[digest.Digest]bool{}
70-
for _, curLayer := range manifest.Layers {
71-
// avoid calling unpackOne twice for the same digest
72-
if seen[curLayer.Digest] {
73-
continue
74-
}
75-
seen[curLayer.Digest] = true
76-
77-
// copy layer to avoid race on pool access.
78-
l := curLayer
79-
pool.Add(func(ctx context.Context) error {
80-
return unpackOne(l, cacheDir, overlayPath(o.config.RootFSDir, l.Digest, "overlay"))
81-
})
82-
}
83-
84-
pool.DoneAddingJobs()
85-
86-
err = pool.Run()
87-
if err != nil {
88-
return err
89-
}
90-
91-
err = o.Create(name)
92-
if err != nil {
93-
return err
94-
}
95-
96-
ovl, err := newOverlayMetadataFromOCI(oci, tag)
97-
if err != nil {
98-
return err
99-
}
100-
101-
return ovl.write(o.config, name)
102-
}
103-
10450
func ConvertAndOutput(config types.StackerConfig, tag, name string, layerType types.LayerType) error {
10551
cacheDir := path.Join(config.StackerDir, "layer-bases", "oci")
10652
cacheOCI, err := umoci.OpenLayout(cacheDir)
@@ -681,57 +627,32 @@ func repackOverlay(config types.StackerConfig, name string, layerTypes []types.L
681627
return ovl.write(config, name)
682628
}
683629

684-
// unpackOne - unpack a single layer (Descriptor) found in ociDir to extractDir
685-
//
686-
// The result of calling unpackOne is either error or the contents available
687-
// at the provided extractDir. The extractDir should be either empty or
688-
// fully populated with this layer.
689-
func unpackOne(l ispec.Descriptor, ociDir string, extractDir string) error {
690-
// population of a dir is not atomic, at least for tar extraction.
691-
// As a result, we could hasDirEntries(extractDir) at the same time that
692-
// something is un-populating that dir due to a failed extraction (like
693-
// os.RemoveAll below).
694-
// There needs to be a lock on the extract dir (scoped to the overlay storage backend).
695-
// A sync.RWMutex would work well here since it is safe to check as long
696-
// as no one is populating or unpopulating.
697-
if hasDirEntries(extractDir) {
698-
// the directory was already populated.
699-
return nil
700-
}
630+
func (o *overlay) Unpack(tag, name string) error {
631+
cacheDir := path.Join(o.config.StackerDir, "layer-bases", "oci")
701632

702-
if squashfs.IsSquashfsMediaType(l.MediaType) {
703-
return squashfs.ExtractSingleSquash(
704-
path.Join(ociDir, "blobs", "sha256", l.Digest.Encoded()), extractDir)
633+
pathfunc := func(digest digest.Digest) string {
634+
return overlayPath(o.config.RootFSDir, digest, "overlay")
705635
}
706-
switch l.MediaType {
707-
case ispec.MediaTypeImageLayer, ispec.MediaTypeImageLayerGzip:
708-
tarEx.Lock()
709-
defer tarEx.Unlock()
710636

711-
oci, err := umoci.OpenLayout(ociDir)
712-
if err != nil {
713-
return err
714-
}
715-
defer oci.Close()
637+
_, err := oci.Unpack(cacheDir, tag, pathfunc)
638+
if err != nil {
639+
return err
640+
}
716641

717-
compressed, err := oci.GetBlob(context.Background(), l.Digest)
718-
if err != nil {
719-
return err
720-
}
721-
defer compressed.Close()
642+
err = o.Create(name)
643+
if err != nil {
644+
return err
645+
}
722646

723-
uncompressed, err := pgzip.NewReader(compressed)
724-
if err != nil {
725-
return err
726-
}
647+
oci, err := umoci.OpenLayout(cacheDir)
648+
if err != nil {
649+
return err
650+
}
727651

728-
err = layer.UnpackLayer(extractDir, uncompressed, nil)
729-
if err != nil {
730-
if rmErr := os.RemoveAll(extractDir); rmErr != nil {
731-
log.Errorf("Failed to remove dir '%s' after failed extraction: %v", extractDir, rmErr)
732-
}
733-
}
652+
ovl, err := newOverlayMetadataFromOCI(oci, tag)
653+
if err != nil {
734654
return err
735655
}
736-
return errors.Errorf("unknown media type %s", l.MediaType)
656+
657+
return ovl.write(o.config, name)
737658
}

pkg/stacker/import.go

+33
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ import (
1313
"github.com/vbatts/go-mtree"
1414
"stackerbuild.io/stacker/pkg/lib"
1515
"stackerbuild.io/stacker/pkg/log"
16+
"stackerbuild.io/stacker/pkg/oci"
1617
"stackerbuild.io/stacker/pkg/types"
1718
)
1819

@@ -294,6 +295,38 @@ func acquireUrl(c types.StackerConfig, storage types.Storage, i string, cache st
294295
}
295296

296297
return p, nil
298+
} else if url.Scheme == "docker" {
299+
if idest != "" && idest[len(idest)-1:] != "/" {
300+
return "", errors.Errorf("The destination path must be directory: %s", idest)
301+
}
302+
303+
is := types.ImageSource{Type: "docker", Url: i}
304+
if err := importContainersImage(is, c, false); err != nil {
305+
return "", err
306+
}
307+
308+
tag, err := is.ParseTag()
309+
if err != nil {
310+
return "", err
311+
}
312+
313+
pathfunc := func(digest digest.Digest) string {
314+
_ = os.Remove(cache)
315+
return cache
316+
}
317+
318+
ociDir := path.Join(c.StackerDir, "layer-bases", "oci")
319+
320+
n, err := oci.Unpack(ociDir, tag, pathfunc)
321+
if err != nil {
322+
return "", err
323+
}
324+
325+
if n > 1 {
326+
return "", errors.Errorf("Currently supporting single-layer container image imports")
327+
}
328+
329+
return cache, nil
297330
}
298331

299332
return "", errors.Errorf("unsupported url scheme %s", i)

0 commit comments

Comments
 (0)