From bad485565356125c85ec0b43fa5f77c22377a510 Mon Sep 17 00:00:00 2001 From: hhyasdf <552483776@qq.com> Date: Fri, 7 Jul 2023 19:18:44 +0800 Subject: [PATCH] improve: skip unchanged manifest list images --- pkg/sync/destination.go | 29 ++++++++++++++++++++++++----- pkg/sync/task.go | 14 +++++++++----- 2 files changed, 33 insertions(+), 10 deletions(-) diff --git a/pkg/sync/destination.go b/pkg/sync/destination.go index 651964e..a1368ff 100644 --- a/pkg/sync/destination.go +++ b/pkg/sync/destination.go @@ -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 diff --git a/pkg/sync/task.go b/pkg/sync/task.go index 12b2b1f..964252d 100644 --- a/pkg/sync/task.go +++ b/pkg/sync/task.go @@ -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 { @@ -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 }