Skip to content

Commit

Permalink
improve: skip unchanged manifest list images
Browse files Browse the repository at this point in the history
  • Loading branch information
hhyasdf committed Jul 7, 2023
1 parent 0303aee commit bad4855
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 10 deletions.
29 changes: 24 additions & 5 deletions pkg/sync/destination.go
Original file line number Diff line number Diff line change
Expand Up @@ -106,17 +106,36 @@ func (i *ImageDestination) CheckManifestChanged(destManifestBytes []byte, tagOrD
}

func (i *ImageDestination) GetManifest(tagOrDigest string) []byte {
var err error
var srcRef types.ImageReference
var convertDigest *digest.Digest

if len(tagOrDigest) != 0 {
_, err := digest.Parse(tagOrDigest)
manifestURL := i.registry + "/" + i.repository + ":" + tagOrDigest
if err == nil {
// has digest
manifestURL = i.registry + "/" + i.repository + "@" + tagOrDigest
}

// create source to check manifest
srcRef, err = docker.ParseReference("//" + manifestURL)
if err != nil {
return nil
}

tmp := digest.Digest(tagOrDigest)
convertDigest = &tmp
} else {
srcRef = i.ref
}

// create source to check manifest
source, err := i.ref.NewImageSource(i.ctx, i.sysctx)
source, err := srcRef.NewImageSource(i.ctx, i.sysctx)
if err != nil {
// if the source cannot be created, manifest not exist
return nil
}

tDigest := digest.Digest(tagOrDigest)
tManifestByte, _, err := source.GetManifest(i.ctx, &tDigest)
tManifestByte, _, err := source.GetManifest(i.ctx, convertDigest)
if err != nil {
// if error happens, it's considered that the manifest not exist
return nil
Expand Down
14 changes: 9 additions & 5 deletions pkg/sync/task.go
Original file line number Diff line number Diff line change
Expand Up @@ -90,9 +90,13 @@ func (t *Task) Run() error {
}

func (t *Task) SyncListTypeImageByManifest(manifestBytes []byte, subManifestInfoSlice []*ManifestInfo) error {
// TODO: ignore list-type image if not changed
// Docker manifest list (and its tag) might still exist even it's deleted, we will always update it because we
// cannot make sure if it can be ignored.
if changed := t.destination.CheckManifestChanged(manifestBytes, ""); !changed {
// do nothing if manifest is unchanged
t.Infof("Dest manifest list %s/%s:%s is unchanged, will do nothing",
t.destination.GetRegistry(), t.destination.GetRepository(), t.destination.GetTag())
return nil
}

for _, mfstInfo := range subManifestInfoSlice {
if err := t.SyncNonListTypeImageByManifest(mfstInfo.obj, mfstInfo.bytes,
mfstInfo.digest.String(), mfstInfo.digest.String(), true); err != nil {
Expand All @@ -112,8 +116,8 @@ func (t *Task) SyncNonListTypeImageByManifest(manifestObj interface{}, manifestB
srcTagOrDigest, dstTagOrDigest string, belongsToList bool) error {

if changed := t.destination.CheckManifestChanged(manifestBytes, dstTagOrDigest); !changed {
// do nothing if manifest is not changed
t.Infof("Dest manifest %s/%s:%s is not changed, will do nothing",
// do nothing if manifest is unchanged
t.Infof("Dest manifest %s/%s:%s is unchanged, will do nothing",
t.destination.GetRegistry(), t.destination.GetRepository(), dstTagOrDigest)
return nil
}
Expand Down

0 comments on commit bad4855

Please sign in to comment.