From b2ae05aabf3217683879d1d0f26bb8bf45d899df Mon Sep 17 00:00:00 2001 From: Aditya Rajan Date: Wed, 1 Sep 2021 12:44:58 +0530 Subject: [PATCH] runtime: Adds RemoveManifestLists and (*ManifestList) Remove Adding `Remove` to *ManifestLists and RemoveManifestLists which takes list of manifests and remove them in an iterative manner. Signed-off-by: Aditya Rajan --- libimage/.image.go.swp | Bin 0 -> 16384 bytes libimage/manifest_list.go | 5 +++ libimage/runtime.go | 83 ++++++++++++++++++++++++++++++++++++++ 3 files changed, 88 insertions(+) create mode 100644 libimage/.image.go.swp diff --git a/libimage/.image.go.swp b/libimage/.image.go.swp new file mode 100644 index 0000000000000000000000000000000000000000..d5682ee7511890c972934349e0a7c8371d219aa6 GIT binary patch literal 16384 zcmeHNTdZ7F8D3Z~J;fqa0ye%Z#?;QV(>@R zQL`ucI;ZRMuYZ02|F6rxrgQUs?GyagV1?mxEn{kbORW567rTAO4komCM5I)){iiD< zrN#DbsP-`@ur*eCz12;6{Z^<31C_RtxDyXVPqys8K~G&-R@I_npkm;iGO(N7va)~qlnU49M||9#cW z_kaB7abN!EO#Q!oeb1L)nyLSnukZWv&<~K0bII4A^Yt&!)W7BH&-?o0Ug5m|KYjh# zy#Gx74qtxIf8Xf~^8UWK`l=YH7^oPi7^oPi7^oPi7^oPi7^oPi7^oQdzhpo}j9rJB zT}St2y#MF-|JOgj*q?yk0>1!W0)7H~5BN6lE#Oh$Uf_D*kJm8vGVnC;7_bH$1a<>g z1G|71b~APXI1iiw?gl;u>;*2ppRu=qp8-z;UkA~p-xDmJkxVV$CZvZ3U7;r0a z82A{l2e=CO`}-LC3-D*)IpAx+7l6+LZ@!nYe*nJ+UITsyTmZfV^nts9HgFu+1N{0u z;0|~RcoCQYtH561jdwHlH{h?pE5LVv3&2-_CxPPt171KrV+h;@G=TlUM}dp5>l463 zz%9V_z#f3?eJwEmImec?%tkupabPn1u%jp2^y7?RE$&H^8P*-8A+N{$fGuutUtVwU z2hT*djc>}&vQ~>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fy!V2=4@Wmn+1AGr&+!X4ZWjU?k|o&MC#CwxjnBxpIWPefN{PH7t?X^mq+-bXZDBW}A3sn3#sk-hs6?5rYJ0m~Gk%IVCJHVY;KBr4AR~>a7g!Q!A$? zCX)l-r@`y)7UXS3YUAGANmK`U#mE{N?^Kj$%q^I%bnaoz$?c%4Zmn|Qkz$kUXVC{gS} z!qZ&zaF0`N>X76T6_m-NyvvF9iDTBRQ;#sEx3-{6i*?^s@=xs};SLzn&512J51ne` zTzQ@}#O?yZN?i-$VCfl`je|Th1LxiHwpBHp)Eo23Z!2lRU=UmkvM`H2mmw8n7ybF1 zXJZ7G%B^G9CU1yQI)MN7@j?MVFVbD*%%@E-)sg>f@->@N;2SIE>~eGET!Z}=!KDw+ literal 0 HcmV?d00001 diff --git a/libimage/manifest_list.go b/libimage/manifest_list.go index 4e8959004..3e3c38508 100644 --- a/libimage/manifest_list.go +++ b/libimage/manifest_list.go @@ -104,6 +104,11 @@ func (i *Image) ToManifestList() (*ManifestList, error) { return &ManifestList{image: i, list: list}, nil } +// Remove the current manifest list. +func (m *ManifestList) Remove(ctx context.Context, rmMap map[string]*RemoveImageReport, ref string) ([]string, error) { + return m.image.remove(ctx, rmMap, ref, &RemoveImagesOptions{Force: true}) +} + // LookupInstance looks up an instance of the manifest list matching the // specified platform. The local machine's platform is used if left empty. func (m *ManifestList) LookupInstance(ctx context.Context, architecture, os, variant string) (*Image, error) { diff --git a/libimage/runtime.go b/libimage/runtime.go index 7d3fbf3f2..78f39db74 100644 --- a/libimage/runtime.go +++ b/libimage/runtime.go @@ -676,3 +676,86 @@ func (r *Runtime) RemoveImages(ctx context.Context, names []string, options *Rem return reports, rmErrors } + +// Takes list of names as argument and removes one or more manifest lists. +func (r *Runtime) RemoveManifestLists(ctx context.Context, names []string) (reports []*RemoveImageReport, rmErrors []error) { + type deleteMe struct { + manifestList *ManifestList + manifestTagName string + manifestID string + } + + appendError := func(err error) { + rmErrors = append(rmErrors, err) + } + + deleteMap := make(map[string]*deleteMe) // ID -> deleteMe + toDelete := []string{} + + if len(names) > 0 { + // Look up the manifest list one-by-one. That allows for removing + // manifest lists that have been looked up successfully while reporting + // lookup errors at the end. + for _, name := range names { + m, err := r.LookupManifestList(name) + if err != nil { + appendError(err) + continue + } + dm, exists := deleteMap[name] + if !exists { + // empty ref if name does not matches any tag + // if ref empty all the manifest list with same id will be removed + manifestName := "" + toDelete = append(toDelete, name) + for _, manifestTag := range m.image.Names() { + if strings.Contains(manifestTag, name) || manifestTag == name { + manifestName = manifestTag + } + } + dm = &deleteMe{manifestList: m, manifestTagName: manifestName, manifestID: m.image.ID()} + deleteMap[name] = dm + } + } + + } + + // Return early if there's no manifest lists to delete. + if len(deleteMap) == 0 { + return nil, rmErrors + } + + // Now remove the manifest lists in the given order. + rmMap := make(map[string]*RemoveImageReport) + orderedIDs := []string{} + visitedIDs := make(map[string]bool) + for _, name := range toDelete { + del, exists := deleteMap[name] + if !exists { + appendError(errors.Errorf("internal error: ID %s not in found in image-deletion map", del.manifestID)) + continue + } + processedIDs, err := del.manifestList.Remove(ctx, rmMap, del.manifestTagName) + if err != nil { + appendError(err) + } + // NOTE: make sure to add given ID only once to orderedIDs. + for _, id := range processedIDs { + if visited := visitedIDs[id]; visited { + continue + } + orderedIDs = append(orderedIDs, id) + visitedIDs[id] = true + } + } + + // Finally, we can assemble the reports slice. + for _, id := range orderedIDs { + report, exists := rmMap[id] + if exists { + reports = append(reports, report) + } + } + + return reports, rmErrors +}