-
Notifications
You must be signed in to change notification settings - Fork 361
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1393 from lyft/precompute-digests
Precompute digests option prior to registry upload
- Loading branch information
Showing
6 changed files
with
99 additions
and
20 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
Hello |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
package streamdigest | ||
|
||
import ( | ||
"fmt" | ||
"io" | ||
"io/ioutil" | ||
"os" | ||
|
||
"github.com/containers/image/v5/internal/putblobdigest" | ||
"github.com/containers/image/v5/internal/tmpdir" | ||
"github.com/containers/image/v5/types" | ||
) | ||
|
||
// ComputeBlobInfo streams a blob to a temporary file and populates Digest and Size in inputInfo. | ||
// The temporary file is returned as an io.Reader along with a cleanup function. | ||
// It is the caller's responsibility to call the cleanup function, which closes and removes the temporary file. | ||
// If an error occurs, inputInfo is not modified. | ||
func ComputeBlobInfo(sys *types.SystemContext, stream io.Reader, inputInfo *types.BlobInfo) (io.Reader, func(), error) { | ||
diskBlob, err := ioutil.TempFile(tmpdir.TemporaryDirectoryForBigFiles(sys), "stream-blob") | ||
if err != nil { | ||
return nil, nil, fmt.Errorf("creating temporary on-disk layer: %w", err) | ||
} | ||
cleanup := func() { | ||
diskBlob.Close() | ||
os.Remove(diskBlob.Name()) | ||
} | ||
digester, stream := putblobdigest.DigestIfCanonicalUnknown(stream, *inputInfo) | ||
written, err := io.Copy(diskBlob, stream) | ||
if err != nil { | ||
cleanup() | ||
return nil, nil, fmt.Errorf("writing to temporary on-disk layer: %w", err) | ||
} | ||
_, err = diskBlob.Seek(0, io.SeekStart) | ||
if err != nil { | ||
cleanup() | ||
return nil, nil, fmt.Errorf("rewinding temporary on-disk layer: %w", err) | ||
} | ||
inputInfo.Digest = digester.Digest() | ||
inputInfo.Size = written | ||
return diskBlob, cleanup, nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
package streamdigest | ||
|
||
import ( | ||
"io/ioutil" | ||
"os" | ||
"testing" | ||
|
||
"github.com/containers/image/v5/types" | ||
"github.com/stretchr/testify/assert" | ||
"github.com/stretchr/testify/require" | ||
) | ||
|
||
func TestComputeBlobInfo(t *testing.T) { | ||
inputInfo := types.BlobInfo{Digest: "", Size: -1} | ||
fixtureFname := "fixtures/Hello.uncompressed" | ||
fixtureInfo := types.BlobInfo{Digest: "sha256:185f8db32271fe25f561a6fc938b2e264306ec304eda518007d1764826381969", Size: 5} | ||
fixtureBytes := []byte("Hello") | ||
|
||
// open fixture | ||
stream, err := os.Open(fixtureFname) | ||
require.NoError(t, err, fixtureFname) | ||
defer stream.Close() | ||
|
||
// fill in Digest and Size for inputInfo | ||
streamCopy, cleanup, err := ComputeBlobInfo(nil, stream, &inputInfo) | ||
require.NoError(t, err) | ||
defer cleanup() | ||
|
||
// ensure inputInfo has been filled in with Digest and Size of fixture | ||
assert.Equal(t, inputInfo, fixtureInfo) | ||
|
||
// ensure streamCopy is the same as fixture | ||
b, err := ioutil.ReadAll(streamCopy) | ||
require.NoError(t, err) | ||
assert.Equal(t, b, fixtureBytes) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters