Skip to content

Commit

Permalink
with lazy resolve
Browse files Browse the repository at this point in the history
Signed-off-by: Avi Deitcher <avi@deitcher.net>
  • Loading branch information
deitch committed Dec 21, 2020
1 parent 9f927af commit f336189
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 29 deletions.
11 changes: 2 additions & 9 deletions pkg/v1/layout/write.go
Original file line number Diff line number Diff line change
Expand Up @@ -173,11 +173,7 @@ func (l Path) replaceDescriptor(append mutate.Appendable, matcher match.Matcher,
Add: append,
Descriptor: *desc,
}
ii, err = mutate.RemoveManifests(ii, matcher)
if err != nil {
return err
}
ii = mutate.AppendManifests(ii, add)
ii = mutate.AppendManifests(mutate.RemoveManifests(ii, matcher), add)

index, err := ii.IndexManifest()
if err != nil {
Expand All @@ -198,10 +194,7 @@ func (l Path) RemoveDescriptors(matcher match.Matcher) error {
if err != nil {
return err
}
ii, err = mutate.RemoveManifests(ii, matcher)
if err != nil {
return err
}
ii = mutate.RemoveManifests(ii, matcher)

index, err := ii.IndexManifest()
if err != nil {
Expand Down
13 changes: 13 additions & 0 deletions pkg/v1/mutate/index.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import (

"github.com/google/go-containerregistry/pkg/logs"
v1 "github.com/google/go-containerregistry/pkg/v1"
"github.com/google/go-containerregistry/pkg/v1/match"
"github.com/google/go-containerregistry/pkg/v1/partial"
"github.com/google/go-containerregistry/pkg/v1/types"
)
Expand Down Expand Up @@ -58,6 +59,8 @@ type index struct {
adds []IndexAddendum
// replaces are applied before adds
replaces []v1.Descriptor
// removes are removed after replaces and before adds
removes []match.Matcher

computed bool
manifest *v1.IndexManifest
Expand Down Expand Up @@ -96,6 +99,16 @@ func (i *index) compute() error {
manifests = i.replaces
}

var cleanedManifests []v1.Descriptor
for _, r := range i.removes {
for _, m := range manifests {
if !r(m) {
cleanedManifests = append(cleanedManifests, m)
}
}
manifests = cleanedManifests
}

for _, add := range i.adds {
desc, err := computeDescriptor(add)
if err != nil {
Expand Down
20 changes: 4 additions & 16 deletions pkg/v1/mutate/mutate.go
Original file line number Diff line number Diff line change
Expand Up @@ -94,23 +94,11 @@ func AppendManifests(base v1.ImageIndex, adds ...IndexAddendum) v1.ImageIndex {
}

// RemoveManifests removes any descriptors that match the match.Matcher.
func RemoveManifests(base v1.ImageIndex, matcher match.Matcher) (v1.ImageIndex, error) {
ii, err := base.IndexManifest()
if err != nil {
return base, err
}

// create a list without any existing ones
manifests := []v1.Descriptor{}
for _, manifest := range ii.Manifests {
if !matcher(manifest) {
manifests = append(manifests, manifest)
}
}
func RemoveManifests(base v1.ImageIndex, matcher match.Matcher) v1.ImageIndex {
return &index{
base: base,
replaces: manifests,
}, nil
base: base,
removes: []match.Matcher{matcher},
}
}

// Config mutates the provided v1.Image to have the provided v1.Config
Expand Down
5 changes: 1 addition & 4 deletions pkg/v1/mutate/mutate_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -503,10 +503,7 @@ func TestRemoveManifests(t *testing.T) {
t.Fatalf("mismatched manifests on setup, had %d, expected %d", len(manifest.Manifests), count)
}
digest := manifest.Manifests[i].Digest
ii, err = mutate.RemoveManifests(ii, match.Digests(digest))
if err != nil {
t.Fatal(err)
}
ii = mutate.RemoveManifests(ii, match.Digests(digest))
manifest, err = ii.IndexManifest()
if err != nil {
t.Fatal(err)
Expand Down

0 comments on commit f336189

Please sign in to comment.