-
Notifications
You must be signed in to change notification settings - Fork 78
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Implementation of a blob gatherer for images in the local cache
- Loading branch information
Showing
30 changed files
with
418 additions
and
30 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,97 @@ | ||
package archive | ||
|
||
import ( | ||
"fmt" | ||
"os" | ||
"path/filepath" | ||
"strings" | ||
|
||
digest "github.com/opencontainers/go-digest" | ||
) | ||
|
||
const ( | ||
repositoriesSubFolder = "docker/registry/v2/repositories" | ||
) | ||
|
||
type StoreBlobGatherer struct { | ||
localStorage string | ||
} | ||
|
||
func NewStoreBlobGatherer(localStorageLocation string) BlobsGatherer { | ||
return StoreBlobGatherer{ | ||
localStorage: localStorageLocation, | ||
} | ||
} | ||
func (o StoreBlobGatherer) GatherBlobs(imgRef string) (map[string]string, error) { | ||
blobs := map[string]string{} | ||
imageSubPath, err := imagePathComponents(imgRef) | ||
if err != nil { | ||
return nil, err | ||
} | ||
imagePath := filepath.Join(o.localStorage, repositoriesSubFolder, imageSubPath) | ||
|
||
err = filepath.Walk(imagePath, func(path string, info os.FileInfo, err error) error { | ||
if err != nil { | ||
return err | ||
} | ||
if !info.IsDir() && info.Name() == "link" { | ||
possibleDigest := filepath.Base(filepath.Dir(path)) | ||
|
||
if _, err := digest.Parse("sha256:" + possibleDigest); err == nil { | ||
blobs[possibleDigest] = "" | ||
} else { | ||
// this was for a tag | ||
return nil | ||
} | ||
} | ||
return nil | ||
}) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
return blobs, nil | ||
} | ||
|
||
func imagePathComponents(imgRef string) (string, error) { | ||
var imageName []string | ||
if strings.Contains(imgRef, "://") { | ||
transportAndRef := strings.Split(imgRef, "://") | ||
imgRef = transportAndRef[1] | ||
} | ||
|
||
imageName = strings.Split(imgRef, "/") | ||
|
||
if len(imageName) > 2 { | ||
imgRef = strings.Join(imageName[1:], "/") | ||
} else if len(imageName) == 1 { | ||
imgRef = imageName[0] | ||
} else { | ||
return "", fmt.Errorf("unable to parse image %s correctly", imgRef) | ||
} | ||
|
||
if strings.Contains(imgRef, "@") { | ||
nameAndDigest := strings.Split(imgRef, "@") | ||
imgRef = nameAndDigest[0] | ||
} | ||
|
||
if strings.Contains(imgRef, ":") { | ||
nameAndTag := strings.Split(imgRef, ":") | ||
imgRef = nameAndTag[0] | ||
} | ||
return imgRef, nil | ||
} | ||
|
||
func isImageByDigest(imgRef string) bool { | ||
return strings.Contains(imgRef, "@") | ||
} | ||
|
||
// func imageHash(imgRef string) string { | ||
// var hash string | ||
// imgSplit := strings.Split(imgRef, "@") | ||
// if len(imgSplit) > 1 { | ||
// hash = strings.Split(imgSplit[1], ":")[1] | ||
// } | ||
|
||
// return hash | ||
// } |
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,88 @@ | ||
package archive | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/stretchr/testify/assert" | ||
) | ||
|
||
func TestStoreBlobGatherer_imagePathComponents(t *testing.T) { | ||
imgRefs := []string{ | ||
"docker://localhost:5000/ubi8/ubi:latest", | ||
"docker://localhost:5000/ubi8/ubi@sha256:db870970ba330193164dacc88657df261d75bce1552ea474dbc7cf08b2fae2ed", | ||
"docker://registry.redhat.io/ubi8/ubi:latest", | ||
"docker://registry.redhat.io/ubi8/ubi@sha256:db870970ba330193164dacc88657df261d75bce1552ea474dbc7cf08b2fae2ed", | ||
"localhost:5000/ubi8/ubi:latest", | ||
"localhost:5000/ubi8/ubi@sha256:db870970ba330193164dacc88657df261d75bce1552ea474dbc7cf08b2fae2ed", | ||
"file:///tmp/ubi8/ubi", | ||
"oci:///tmp/ubi8/ubi", | ||
} | ||
expectedPathComponents := []string{ | ||
"ubi8/ubi", | ||
"ubi8/ubi", | ||
"ubi8/ubi", | ||
"ubi8/ubi", | ||
"ubi8/ubi", | ||
"ubi8/ubi", | ||
"tmp/ubi8/ubi", | ||
"tmp/ubi8/ubi", | ||
} | ||
|
||
for i, imgRef := range imgRefs { | ||
actualPathComponents, err := imagePathComponents(imgRef) | ||
if err != nil { | ||
t.Fatal(err) | ||
} | ||
if actualPathComponents != expectedPathComponents[i] { | ||
t.Errorf("imagePathComponents() returned unexpected value for %q: got %q, want %q", imgRef, actualPathComponents, expectedPathComponents[i]) | ||
} | ||
} | ||
} | ||
|
||
func TestStoreBlobGatherer_isImageByDigest(t *testing.T) { | ||
imgRefs := []string{ | ||
"docker://localhost:5000/ubi8/ubi:latest", | ||
"docker://localhost:5000/ubi8/ubi@sha256:db870970ba330193164dacc88657df261d75bce1552ea474dbc7cf08b2fae2ed", | ||
} | ||
expectedisByDigest := []bool{ | ||
false, | ||
true, | ||
} | ||
|
||
for i, imgRef := range imgRefs { | ||
actualIsByDigest := isImageByDigest(imgRef) | ||
|
||
if actualIsByDigest != expectedisByDigest[i] { | ||
t.Errorf("isImageByDigest() returned unexpected value for %q: got %v, want %v", imgRef, actualIsByDigest, expectedisByDigest[i]) | ||
} | ||
} | ||
} | ||
|
||
func TestStoreBlobGatherer_GatherBlobs(t *testing.T) { | ||
|
||
// Create a new StoreBlobGatherer with the temporary directory as the local storage location | ||
gatherer := NewStoreBlobGatherer("../../tests/cache-fake") | ||
|
||
// Call GatherBlobs with a test image reference | ||
actualBlobs, err := gatherer.GatherBlobs("docker://localhost:5000/ubi8/ubi:latest") | ||
if err != nil { | ||
t.Fatal(err) | ||
} | ||
|
||
// Check that the returned blobs map contains the expected key-value pair | ||
expectedBlobs := map[string]string{ | ||
"db870970ba330193164dacc88657df261d75bce1552ea474dbc7cf08b2fae2ed": "", | ||
"e6c589cf5f402a60a83a01653304d7a8dcdd47b93a395a797b5622a18904bd66": "", | ||
"9b6fa335dba394d437930ad79e308e01da4f624328e49d00c0ff44775d2e4769": "", | ||
"6376a0276facf61d87fdf7c6f21d761ee25ba8ceba934d64752d43e84fe0cb98": "", | ||
"2e39d55595ea56337b5b788e96e6afdec3db09d2759d903cbe120468187c4644": "", | ||
"4c0f6aace7053de3b9c1476b33c9a763e45a099c8c7ae9117773c9a8e5b8506b": "", | ||
"53c56977ccd20c0d87df0ad52036c55b27201e1a63874c2644383d0c532f5aee": "", | ||
"6e1ac33d11e06db5e850fec4a1ec07f6c2ab15f130c2fdf0f9d0d0a5c83651e7": "", | ||
"94343313ec1512ab02267e4bc3ce09eecb01fda5bf26c56e2f028ecc72e80b18": "", | ||
"cfaa7496ab546c36ab14859f93fbd2d8a3588b344b18d5fbe74dd834e4a6f7eb": "", | ||
"e1bb0572465a9e03d7af5024abb36d7227b5bf133c448b54656d908982127874": "", | ||
"f992cb38fce665360a4d07f6f78db864a1f6e20a7ad304219f7f81d7fe608d97": "", | ||
} | ||
assert.Equal(t, expectedBlobs, actualBlobs) | ||
} |
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,5 @@ | ||
package archive | ||
|
||
type BlobsGatherer interface { | ||
GatherBlobs(imgRef string) (map[string]string, error) | ||
} |
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
1 change: 1 addition & 0 deletions
1
.../ubi/_layers/sha256/2e39d55595ea56337b5b788e96e6afdec3db09d2759d903cbe120468187c4644/link
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 @@ | ||
sha256:2e39d55595ea56337b5b788e96e6afdec3db09d2759d903cbe120468187c4644 |
1 change: 1 addition & 0 deletions
1
.../ubi/_layers/sha256/4c0f6aace7053de3b9c1476b33c9a763e45a099c8c7ae9117773c9a8e5b8506b/link
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 @@ | ||
sha256:4c0f6aace7053de3b9c1476b33c9a763e45a099c8c7ae9117773c9a8e5b8506b |
1 change: 1 addition & 0 deletions
1
.../ubi/_layers/sha256/53c56977ccd20c0d87df0ad52036c55b27201e1a63874c2644383d0c532f5aee/link
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 @@ | ||
sha256:53c56977ccd20c0d87df0ad52036c55b27201e1a63874c2644383d0c532f5aee |
1 change: 1 addition & 0 deletions
1
.../ubi/_layers/sha256/6e1ac33d11e06db5e850fec4a1ec07f6c2ab15f130c2fdf0f9d0d0a5c83651e7/link
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 @@ | ||
sha256:6e1ac33d11e06db5e850fec4a1ec07f6c2ab15f130c2fdf0f9d0d0a5c83651e7 |
1 change: 1 addition & 0 deletions
1
.../ubi/_layers/sha256/94343313ec1512ab02267e4bc3ce09eecb01fda5bf26c56e2f028ecc72e80b18/link
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 @@ | ||
sha256:94343313ec1512ab02267e4bc3ce09eecb01fda5bf26c56e2f028ecc72e80b18 |
1 change: 1 addition & 0 deletions
1
.../ubi/_layers/sha256/cfaa7496ab546c36ab14859f93fbd2d8a3588b344b18d5fbe74dd834e4a6f7eb/link
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 @@ | ||
sha256:cfaa7496ab546c36ab14859f93fbd2d8a3588b344b18d5fbe74dd834e4a6f7eb |
1 change: 1 addition & 0 deletions
1
.../ubi/_layers/sha256/e1bb0572465a9e03d7af5024abb36d7227b5bf133c448b54656d908982127874/link
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 @@ | ||
sha256:e1bb0572465a9e03d7af5024abb36d7227b5bf133c448b54656d908982127874 |
1 change: 1 addition & 0 deletions
1
.../ubi/_layers/sha256/f992cb38fce665360a4d07f6f78db864a1f6e20a7ad304219f7f81d7fe608d97/link
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 @@ | ||
sha256:f992cb38fce665360a4d07f6f78db864a1f6e20a7ad304219f7f81d7fe608d97 |
1 change: 1 addition & 0 deletions
1
...ts/revisions/sha256/6376a0276facf61d87fdf7c6f21d761ee25ba8ceba934d64752d43e84fe0cb98/link
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 @@ | ||
sha256:6376a0276facf61d87fdf7c6f21d761ee25ba8ceba934d64752d43e84fe0cb98 |
1 change: 1 addition & 0 deletions
1
...ts/revisions/sha256/9b6fa335dba394d437930ad79e308e01da4f624328e49d00c0ff44775d2e4769/link
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 @@ | ||
sha256:9b6fa335dba394d437930ad79e308e01da4f624328e49d00c0ff44775d2e4769 |
1 change: 1 addition & 0 deletions
1
...ts/revisions/sha256/db870970ba330193164dacc88657df261d75bce1552ea474dbc7cf08b2fae2ed/link
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 @@ | ||
sha256:db870970ba330193164dacc88657df261d75bce1552ea474dbc7cf08b2fae2ed |
1 change: 1 addition & 0 deletions
1
...ts/revisions/sha256/e6c589cf5f402a60a83a01653304d7a8dcdd47b93a395a797b5622a18904bd66/link
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 @@ | ||
sha256:e6c589cf5f402a60a83a01653304d7a8dcdd47b93a395a797b5622a18904bd66 |
1 change: 1 addition & 0 deletions
1
...s/cache-fake/docker/registry/v2/repositories/ubi8/ubi/_manifests/tags/latest/current/link
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 @@ | ||
sha256:db870970ba330193164dacc88657df261d75bce1552ea474dbc7cf08b2fae2ed |
1 change: 1 addition & 0 deletions
1
...latest/index/sha256/db870970ba330193164dacc88657df261d75bce1552ea474dbc7cf08b2fae2ed/link
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 @@ | ||
sha256:db870970ba330193164dacc88657df261d75bce1552ea474dbc7cf08b2fae2ed |
Binary file added
BIN
+19.6 MB
...cker/registry/v2/repositories/ubi8/ubi/_uploads/97e2891e-4cb2-4289-a87a-e9b8cd006d20/data
Binary file not shown.
Binary file added
BIN
+108 Bytes
...2/repositories/ubi8/ubi/_uploads/97e2891e-4cb2-4289-a87a-e9b8cd006d20/hashstates/sha256/0
Binary file not shown.
1 change: 1 addition & 0 deletions
1
...registry/v2/repositories/ubi8/ubi/_uploads/97e2891e-4cb2-4289-a87a-e9b8cd006d20/startedat
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 @@ | ||
2023-11-09T17:47:04Z |
Oops, something went wrong.