feat(server): Apply GZIP compression to all image layers

This fixes #62
This commit is contained in:
Vincent Ambo 2019-10-11 01:28:38 +01:00 committed by Vincent Ambo
parent bf2718cebb
commit 0693e371d6
4 changed files with 15 additions and 9 deletions

View file

@ -126,9 +126,9 @@ let
# Image layer that contains the symlink forest created above. This # Image layer that contains the symlink forest created above. This
# must be included in the image to ensure that the filesystem has a # must be included in the image to ensure that the filesystem has a
# useful layout at runtime. # useful layout at runtime.
symlinkLayer = runCommand "symlink-layer.tar" {} '' symlinkLayer = runCommand "symlink-layer.tar.gz" {} ''
cp -r ${contentsEnv}/ ./layer cp -r ${contentsEnv}/ ./layer
tar --transform='s|^\./||' -C layer --sort=name --mtime="@$SOURCE_DATE_EPOCH" --owner=0 --group=0 -cf $out . tar --transform='s|^\./||' -C layer --sort=name --mtime="@$SOURCE_DATE_EPOCH" --owner=0 --group=0 -czf $out .
''; '';
# Metadata about the symlink layer which is required for serving it. # Metadata about the symlink layer which is required for serving it.

View file

@ -9,6 +9,7 @@ package builder
import ( import (
"archive/tar" "archive/tar"
"compress/gzip"
"io" "io"
"os" "os"
"path/filepath" "path/filepath"
@ -16,10 +17,11 @@ import (
"github.com/google/nixery/server/layers" "github.com/google/nixery/server/layers"
) )
// Create a new tarball from each of the paths in the list and write the tarball // Create a new compressed tarball from each of the paths in the list
// to the supplied writer. // and write it to the supplied writer.
func tarStorePaths(l *layers.Layer, w io.Writer) error { func packStorePaths(l *layers.Layer, w io.Writer) error {
t := tar.NewWriter(w) gz := gzip.NewWriter(w)
t := tar.NewWriter(gz)
for _, path := range l.Contents { for _, path := range l.Contents {
err := filepath.Walk(path, tarStorePath(t)) err := filepath.Walk(path, tarStorePath(t))
@ -32,6 +34,10 @@ func tarStorePaths(l *layers.Layer, w io.Writer) error {
return err return err
} }
if err := gz.Close(); err != nil {
return err
}
return nil return nil
} }

View file

@ -270,7 +270,7 @@ func prepareLayers(ctx context.Context, s *State, image *Image, result *ImageRes
} else { } else {
lh := l.Hash() lh := l.Hash()
lw := func(w io.Writer) error { lw := func(w io.Writer) error {
return tarStorePaths(&l, w) return packStorePaths(&l, w)
} }
entry, err := uploadHashLayer(ctx, s, lh, lw) entry, err := uploadHashLayer(ctx, s, lh, lw)

View file

@ -15,7 +15,7 @@ const (
// media types // media types
manifestType = "application/vnd.docker.distribution.manifest.v2+json" manifestType = "application/vnd.docker.distribution.manifest.v2+json"
layerType = "application/vnd.docker.image.rootfs.diff.tar" layerType = "application/vnd.docker.image.rootfs.diff.tar.gzip"
configType = "application/vnd.docker.container.image.v1+json" configType = "application/vnd.docker.container.image.v1+json"
// image config constants // image config constants
@ -102,7 +102,7 @@ func Manifest(layers []Entry) (json.RawMessage, ConfigLayer) {
hashes := make([]string, len(layers)) hashes := make([]string, len(layers))
for i, l := range layers { for i, l := range layers {
l.MediaType = "application/vnd.docker.image.rootfs.diff.tar" l.MediaType = layerType
layers[i] = l layers[i] = l
hashes[i] = l.Digest hashes[i] = l.Digest
} }