Skip to content

Commit

Permalink
Better sparse image for podman
Browse files Browse the repository at this point in the history
This library uses the fact that uploaded tar may miss some layers when
pushed to docker provided the missing layers are already in daemon.
It works also with podman but the layers have to be present in the tar
at least as empty entries.

Signed-off-by: Matej Vasek <[email protected]>
  • Loading branch information
matejvasek committed Sep 24, 2021
1 parent 66aea98 commit d200acd
Showing 1 changed file with 20 additions and 13 deletions.
33 changes: 20 additions & 13 deletions local/local.go
Original file line number Diff line number Diff line change
Expand Up @@ -511,23 +511,30 @@ func (i *Image) doSave() (types.ImageInspect, error) {
return types.ImageInspect{}, err
}

var blankIdx int
var layerPaths []string
for _, path := range i.layerPaths {
if path == "" {
layerPaths = append(layerPaths, "")
continue
}
layerName := fmt.Sprintf("/%x.tar", sha256.Sum256([]byte(path)))
f, err := os.Open(filepath.Clean(path))
if err != nil {
return types.ImageInspect{}, err
}
defer f.Close()
if err := addFileToTar(tw, layerName, f); err != nil {
return types.ImageInspect{}, err
layerName := fmt.Sprintf("blank_%d", blankIdx)
blankIdx++
hdr := &tar.Header{Name: layerName, Mode: 0644, Size: 0}
if err := tw.WriteHeader(hdr); err != nil {
return types.ImageInspect{}, err
}
layerPaths = append(layerPaths, layerName)
} else {
layerName := fmt.Sprintf("/%x.tar", sha256.Sum256([]byte(path)))
f, err := os.Open(filepath.Clean(path))
if err != nil {
return types.ImageInspect{}, err
}
defer f.Close()
if err := addFileToTar(tw, layerName, f); err != nil {
return types.ImageInspect{}, err
}
f.Close()
layerPaths = append(layerPaths, layerName)
}
f.Close()
layerPaths = append(layerPaths, layerName)
}

manifest, err := json.Marshal([]map[string]interface{}{
Expand Down

0 comments on commit d200acd

Please sign in to comment.