Skip to content

Commit 366bc3c

Browse files
authored
Merge pull request #304 from ktock/esgzcmp
estargz: support compressed input blob
2 parents 93ec784 + df0415b commit 366bc3c

File tree

9 files changed

+396
-242
lines changed

9 files changed

+396
-242
lines changed

Dockerfile

+1-1
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
# See the License for the specific language governing permissions and
1313
# limitations under the License.
1414

15-
ARG CONTAINERD_VERSION=v1.5.0-rc.0
15+
ARG CONTAINERD_VERSION=v1.5.0-rc.2
1616
ARG RUNC_VERSION=v1.0.0-rc93
1717
ARG CNI_PLUGINS_VERSION=v0.9.1
1818
ARG NERDCTL_VERSION=0.7.3

cmd/ctr-remote/commands/flags.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -260,7 +260,7 @@ func withCNI(clicontext *cli.Context) (specOpt oci.SpecOpts, done func() error,
260260
cleanups = append(cleanups, ns.Remove)
261261

262262
// Configure the namespace with CNI plugins
263-
var cniopts []gocni.CNIOpt
263+
var cniopts []gocni.Opt
264264
if cdir := clicontext.String("cni-plugin-conf-dir"); cdir != "" {
265265
cniopts = append(cniopts, gocni.WithPluginConfDir(cdir))
266266
}

estargz/build.go

+49-5
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ import (
3737
"sync"
3838

3939
"github.com/containerd/stargz-snapshotter/estargz/errorutil"
40+
"github.com/klauspost/compress/zstd"
4041
digest "github.com/opencontainers/go-digest"
4142
"github.com/pkg/errors"
4243
"golang.org/x/sync/errgroup"
@@ -112,11 +113,11 @@ func (b *Blob) TOCDigest() digest.Digest {
112113
return b.tocDigest
113114
}
114115

115-
// Build builds an eStargz blob which is an extended version of stargz, from tar blob passed
116-
// through the argument. If there are some prioritized files are listed in the option, these
117-
// files are grouped as "prioritized" and can be used for runtime optimization (e.g. prefetch).
118-
// This function builds a blob in parallel, with dividing that blob into several (at least the
119-
// number of runtime.GOMAXPROCS(0)) sub-blobs.
116+
// Build builds an eStargz blob which is an extended version of stargz, from a blob (gzip, zstd
117+
// or plain tar) passed through the argument. If there are some prioritized files are listed in
118+
// the option, these files are grouped as "prioritized" and can be used for runtime optimization
119+
// (e.g. prefetch). This function builds a blob in parallel, with dividing that blob into several
120+
// (at least the number of runtime.GOMAXPROCS(0)) sub-blobs.
120121
func Build(tarBlob *io.SectionReader, opt ...Option) (_ *Blob, rErr error) {
121122
var opts options
122123
opts.compressionLevel = gzip.BestCompression // BestCompression by default
@@ -133,6 +134,10 @@ func Build(tarBlob *io.SectionReader, opt ...Option) (_ *Blob, rErr error) {
133134
}
134135
}
135136
}()
137+
tarBlob, err := decompressBlob(tarBlob, layerFiles)
138+
if err != nil {
139+
return nil, err
140+
}
136141
entries, err := sortEntries(tarBlob, opts.prioritizedFiles, opts.missedPrioritizedFiles)
137142
if err != nil {
138143
return nil, err
@@ -593,3 +598,42 @@ func (cr *countReader) currentPos() int64 {
593598

594599
return *cr.cPos
595600
}
601+
602+
func decompressBlob(org *io.SectionReader, tmp *tempFiles) (*io.SectionReader, error) {
603+
if org.Size() < 4 {
604+
return org, nil
605+
}
606+
src := make([]byte, 4)
607+
if _, err := org.Read(src); err != nil && err != io.EOF {
608+
return nil, err
609+
}
610+
var dR io.Reader
611+
if bytes.Equal([]byte{0x1F, 0x8B, 0x08}, src[:3]) {
612+
// gzip
613+
dgR, err := gzip.NewReader(io.NewSectionReader(org, 0, org.Size()))
614+
if err != nil {
615+
return nil, err
616+
}
617+
defer dgR.Close()
618+
dR = io.Reader(dgR)
619+
} else if bytes.Equal([]byte{0x28, 0xb5, 0x2f, 0xfd}, src[:4]) {
620+
// zstd
621+
dzR, err := zstd.NewReader(io.NewSectionReader(org, 0, org.Size()))
622+
if err != nil {
623+
return nil, err
624+
}
625+
defer dzR.Close()
626+
dR = io.Reader(dzR)
627+
} else {
628+
// uncompressed
629+
return io.NewSectionReader(org, 0, org.Size()), nil
630+
}
631+
b, err := tmp.TempFile("", "uncompresseddata")
632+
if err != nil {
633+
return nil, err
634+
}
635+
if _, err := io.Copy(b, dR); err != nil {
636+
return nil, err
637+
}
638+
return fileSectionReader(b)
639+
}

0 commit comments

Comments
 (0)