Skip to content

Commit

Permalink
fix(sync): better cleaning sync's download dir (#2273)
Browse files Browse the repository at this point in the history
added cleanup in the case of copy.Image() failures.

Signed-off-by: Petu Eusebiu <[email protected]>
  • Loading branch information
eusebiu-constantin-petu-dbk authored Feb 29, 2024
1 parent 6561e9f commit 740eae8
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 4 deletions.
21 changes: 17 additions & 4 deletions pkg/extensions/sync/destination.go
Original file line number Diff line number Diff line change
Expand Up @@ -186,6 +186,12 @@ func (registry *DestinationRegistry) CommitImage(imageReference types.ImageRefer
return nil
}

func (registry *DestinationRegistry) CleanupImage(imageReference types.ImageReference, repo, reference string) error {
tmpDir := getTempRootDirFromImageReference(imageReference, repo, reference)

return os.RemoveAll(tmpDir)
}

func (registry *DestinationRegistry) copyManifest(repo string, manifestContent []byte, reference string,
tempImageStore storageTypes.ImageStore,
) error {
Expand Down Expand Up @@ -275,17 +281,24 @@ func (registry *DestinationRegistry) copyBlob(repo string, blobDigest digest.Dig
return err
}

// use only with local imageReferences.
func getImageStoreFromImageReference(imageReference types.ImageReference, repo, reference string,
) storageTypes.ImageStore {
var tempRootDir string
tmpRootDir := getTempRootDirFromImageReference(imageReference, repo, reference)

return getImageStore(tmpRootDir)
}

func getTempRootDirFromImageReference(imageReference types.ImageReference, repo, reference string) string {
var tmpRootDir string

if strings.HasSuffix(imageReference.StringWithinTransport(), reference) {
tempRootDir = strings.ReplaceAll(imageReference.StringWithinTransport(), fmt.Sprintf("%s:%s", repo, reference), "")
tmpRootDir = strings.ReplaceAll(imageReference.StringWithinTransport(), fmt.Sprintf("%s:%s", repo, reference), "")
} else {
tempRootDir = strings.ReplaceAll(imageReference.StringWithinTransport(), fmt.Sprintf("%s:", repo), "")
tmpRootDir = strings.ReplaceAll(imageReference.StringWithinTransport(), fmt.Sprintf("%s:", repo), "")
}

return getImageStore(tempRootDir)
return tmpRootDir
}

func getImageStore(rootDir string) storageTypes.ImageStore {
Expand Down
7 changes: 7 additions & 0 deletions pkg/extensions/sync/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -441,6 +441,13 @@ func (service *BaseService) syncTag(ctx context.Context, destinationRepo, remote

_, err = copy.Image(ctx, policyContext, localImageRef, remoteImageRef, &copyOptions)
if err != nil {
// cleanup in cases of copy.Image errors while copying.
if cErr := service.destination.CleanupImage(localImageRef, destinationRepo, tag); cErr != nil {
service.log.Error().Err(err).Str("errortype", common.TypeOf(err)).
Str("local image", fmt.Sprintf("%s:%s", destinationRepo, tag)).
Msg("couldn't cleanup temp local image")
}

service.log.Error().Err(err).Str("errortype", common.TypeOf(err)).
Str("remote image", remoteImageRef.DockerReference().String()).
Str("local image", fmt.Sprintf("%s:%s", destinationRepo, tag)).Msg("coulnd't sync image")
Expand Down
2 changes: 2 additions & 0 deletions pkg/extensions/sync/sync.go
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,8 @@ type Destination interface {
CanSkipImage(repo, tag string, imageDigest digest.Digest) (bool, error)
// CommitImage moves a synced repo/ref from temporary oci layout to ImageStore
CommitImage(imageReference types.ImageReference, repo, tag string) error
// Removes image reference, used when copy.Image() errors out
CleanupImage(imageReference types.ImageReference, repo, reference string) error
}

type TaskGenerator struct {
Expand Down

0 comments on commit 740eae8

Please sign in to comment.