Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

support gzip on tarball.ImageFromPath #1858

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
25 changes: 24 additions & 1 deletion pkg/v1/tarball/image.go
Expand Up @@ -17,6 +17,7 @@ package tarball
import (
"archive/tar"
"bytes"
"compress/gzip"
"encoding/json"
"errors"
"fmt"
Expand All @@ -27,6 +28,7 @@ import (
"sync"

comp "github.com/google/go-containerregistry/internal/compression"
ggzip "github.com/google/go-containerregistry/internal/gzip"
"github.com/google/go-containerregistry/pkg/compression"
"github.com/google/go-containerregistry/pkg/name"
v1 "github.com/google/go-containerregistry/pkg/v1"
Expand Down Expand Up @@ -230,7 +232,28 @@ func extractFileFromTar(opener Opener, filePath string) (io.ReadCloser, error) {
}
}()

tf := tar.NewReader(f)
var tf *tar.Reader
magicHeader := make([]byte, len(ggzip.MagicHeader))
n, err := f.Read(magicHeader)
if n == 0 && err == io.EOF {
return nil, errors.New("invalid tar or tar.zip header")
}
if err != nil {
return nil, err
}
// ggzip.Is will eat two bytes, so used the MultiReader
combinedReader := io.MultiReader(bytes.NewReader(magicHeader[:n]), f)
if bytes.Equal(magicHeader, ggzip.MagicHeader) {
gzipReader, err := gzip.NewReader(combinedReader)
if err != nil {
return nil, err
}
defer gzipReader.Close()
tf = tar.NewReader(gzipReader)
} else {
tf = tar.NewReader(combinedReader)
}

for {
hdr, err := tf.Next()
if errors.Is(err, io.EOF) {
Expand Down
43 changes: 23 additions & 20 deletions pkg/v1/tarball/image_test.go
Expand Up @@ -24,28 +24,31 @@ import (
)

func TestManifestAndConfig(t *testing.T) {
img, err := ImageFromPath("testdata/test_image_1.tar", nil)
if err != nil {
t.Fatalf("Error loading image: %v", err)
}
manifest, err := img.Manifest()
if err != nil {
t.Fatalf("Error loading manifest: %v", err)
}
if len(manifest.Layers) != 1 {
t.Fatalf("layers should be 1, got %d", len(manifest.Layers))
}
imgList := []string{"testdata/test_image_1.tar", "testdata/test_image_1.tar.gz"}
for _, file := range imgList {
img, err := ImageFromPath(file, nil)
if err != nil {
t.Fatalf("Error loading image: %v", err)
}
manifest, err := img.Manifest()
if err != nil {
t.Fatalf("Error loading manifest: %v", err)
}
if len(manifest.Layers) != 1 {
t.Fatalf("layers should be 1, got %d", len(manifest.Layers))
}

config, err := img.ConfigFile()
if err != nil {
t.Fatalf("Error loading config file: %v", err)
}
if len(config.History) != 1 {
t.Fatalf("history length should be 1, got %d", len(config.History))
}
config, err := img.ConfigFile()
if err != nil {
t.Fatalf("Error loading config file: %v", err)
}
if len(config.History) != 1 {
t.Fatalf("history length should be 1, got %d", len(config.History))
}

if err := validate.Image(img); err != nil {
t.Errorf("Validate() = %v", err)
if err := validate.Image(img); err != nil {
t.Errorf("Validate() = %v", err)
}
}
}

Expand Down
Binary file added pkg/v1/tarball/testdata/test_image_1.tar.gz
Binary file not shown.