Skip to content

Commit 1136f3d

Browse files
update hauler store remove to handle registry reference as part of string (backport #705) (#706)
Signed-off-by: Camryn Carter <camryn.carter@ranchergovernment.com> Co-authored-by: Camryn Carter <camryn.carter@ranchergovernment.com>
1 parent f190939 commit 1136f3d

2 files changed

Lines changed: 41 additions & 1 deletion

File tree

cmd/hauler/cli/store/remove.go

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ import (
1212
ocispec "github.com/opencontainers/image-spec/specs-go/v1"
1313

1414
"hauler.dev/go/hauler/v2/internal/flags"
15+
"hauler.dev/go/hauler/v2/pkg/consts"
1516
"hauler.dev/go/hauler/v2/pkg/log"
1617
"hauler.dev/go/hauler/v2/pkg/store"
1718
)
@@ -50,7 +51,12 @@ func RemoveCmd(ctx context.Context, o *flags.RemoveOpts, s *store.Layout, ref st
5051
var matches []match
5152

5253
if err := s.Walk(func(reference string, desc ocispec.Descriptor) error {
53-
if !strings.Contains(reference, ref) {
54+
registryRef := desc.Annotations[consts.ContainerdImageNameKey]
55+
if registryRef == "" {
56+
registryRef = desc.Annotations[ocispec.AnnotationRefName]
57+
}
58+
59+
if !strings.Contains(reference, ref) && !strings.Contains(registryRef, ref) {
5460
return nil
5561
}
5662

cmd/hauler/cli/store/remove_test.go

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -125,6 +125,40 @@ func TestRemoveCmd_NotFound(t *testing.T) {
125125
}
126126
}
127127

128+
// TestRemoveCmd_ContainerdImageName confirms that a
129+
// registry-prefixed ref (which only appears in the io.containerd.image.name
130+
// annotation, not the registry-stripped org.opencontainers.image.ref.name
131+
// used to key the store's nameMap) still matches for removal.
132+
func TestRemoveCmd_ContainerdImageName(t *testing.T) {
133+
ctx := newTestContext(t)
134+
s := newTestStore(t)
135+
host, rOpts := newLocalhostRegistry(t)
136+
seedImage(t, host, "test/repo", "v1", rOpts...)
137+
138+
rso := defaultRootOpts(s.Root)
139+
ro := defaultCliOpts()
140+
141+
if err := storeImage(ctx, s, v1.Image{Name: host + "/test/repo:v1"}, "", false, rso, ro, ""); err != nil {
142+
t.Fatalf("storeImage: %v", err)
143+
}
144+
145+
if n := countArtifactsInStore(t, s); n == 0 {
146+
t.Fatal("expected at least 1 artifact after storeImage, got 0")
147+
}
148+
149+
// The registry-qualified ref only lives in io.containerd.image.name;
150+
// org.opencontainers.image.ref.name (and the nameMap key derived from it)
151+
// only holds the registry-stripped short form "test/repo:v1".
152+
fullRef := host + "/test/repo:v1"
153+
if err := RemoveCmd(ctx, &flags.RemoveOpts{Force: true}, s, fullRef); err != nil {
154+
t.Fatalf("RemoveCmd with fully-qualified ref: %v", err)
155+
}
156+
157+
if n := countArtifactsInStore(t, s); n != 0 {
158+
t.Errorf("expected 0 artifacts after removal by containerd image name, got %d", n)
159+
}
160+
}
161+
128162
func TestRemoveCmd_Force_MultipleMatches(t *testing.T) {
129163
ctx := newTestContext(t)
130164
s := newTestStore(t)

0 commit comments

Comments
 (0)