mirror of
https://github.com/project-zot/zot.git
synced 2024-12-16 21:56:37 -05:00
fix(sync): better cleaning sync's download dir (#2273)
added cleanup in the case of copy.Image() failures. Signed-off-by: Petu Eusebiu <peusebiu@cisco.com>
This commit is contained in:
parent
6561e9f527
commit
740eae8f26
3 changed files with 26 additions and 4 deletions
|
@ -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 {
|
||||
|
@ -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 {
|
||||
|
|
|
@ -441,6 +441,13 @@ func (service *BaseService) syncTag(ctx context.Context, destinationRepo, remote
|
|||
|
||||
_, err = copy.Image(ctx, policyContext, localImageRef, remoteImageRef, ©Options)
|
||||
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")
|
||||
|
|
|
@ -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 {
|
||||
|
|
Loading…
Reference in a new issue