Skip to content

Commit da3baf6

Browse files
committed
Fixed #27260 Use NewArtifactEvent by injecting dependency in NewArtifactStore
Signed-off-by: ByoungUk Lee <[email protected]>
1 parent c204651 commit da3baf6

File tree

3 files changed

+79
-76
lines changed

3 files changed

+79
-76
lines changed

libpod/runtime.go

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -531,7 +531,15 @@ func makeRuntime(ctx context.Context, runtime *Runtime) (retErr error) {
531531

532532
// Using sync once value to only init the store exactly once and only when it will be actually be used.
533533
runtime.ArtifactStore = sync.OnceValues(func() (*artStore.ArtifactStore, error) {
534-
return artStore.NewArtifactStore(filepath.Join(runtime.storageConfig.GraphRoot, "artifacts"), runtime.SystemContext())
534+
artifactEventCallBack := func(status, name, digest string, attributes map[string]string) {
535+
eventStatus, err := events.StringToStatus(status)
536+
if err != nil {
537+
logrus.Errorf("Unknown artifact event status %q: %v", status, err)
538+
return
539+
}
540+
runtime.NewArtifactEvent(eventStatus, name, digest, attributes)
541+
}
542+
return artStore.NewArtifactStore(filepath.Join(runtime.storageConfig.GraphRoot, "artifacts"), runtime.SystemContext(), artifactEventCallBack)
535543
})
536544
}
537545

pkg/domain/infra/abi/artifact.go

Lines changed: 2 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@ import (
1010
"os"
1111
"time"
1212

13-
"github.com/containers/podman/v5/libpod/events"
1413
"github.com/containers/podman/v5/pkg/domain/entities"
1514
"github.com/containers/podman/v5/pkg/libartifact/types"
1615
"github.com/opencontainers/go-digest"
@@ -87,8 +86,6 @@ func (ir *ImageEngine) ArtifactPull(ctx context.Context, name string, opts entit
8786
if err != nil {
8887
return nil, err
8988
}
90-
91-
ir.Libpod.NewArtifactEvent(events.Pull, name, artifactDigest.String(), nil)
9289
return &entities.ArtifactPullReport{
9390
ArtifactDigest: &artifactDigest,
9491
}, nil
@@ -134,7 +131,6 @@ func (ir *ImageEngine) ArtifactRm(ctx context.Context, opts entities.ArtifactRem
134131
}
135132
return nil, err
136133
}
137-
ir.Libpod.NewArtifactEvent(events.Remove, "", namesOrDigest, nil)
138134
artifactDigests = append(artifactDigests, artifactDigest)
139135
}
140136
artifactRemoveReport := entities.ArtifactRemoveReport{
@@ -200,8 +196,6 @@ func (ir *ImageEngine) ArtifactPush(ctx context.Context, name string, opts entit
200196
if err != nil {
201197
return nil, err
202198
}
203-
204-
ir.Libpod.NewArtifactEvent(events.Push, name, artifactDigest.String(), nil)
205199
return &entities.ArtifactPushReport{
206200
ArtifactDigest: &artifactDigest,
207201
}, nil
@@ -232,9 +226,6 @@ func (ir *ImageEngine) ArtifactAdd(ctx context.Context, name string, artifactBlo
232226
if err != nil {
233227
return nil, err
234228
}
235-
ir.Libpod.NewArtifactEvent(events.Add, name, artifactDigest.String(), map[string]string{
236-
"files": fmt.Sprintf("%d", len(artifactBlobs)),
237-
})
238229
return &entities.ArtifactAddReport{
239230
ArtifactDigest: artifactDigest,
240231
}, nil
@@ -251,13 +242,7 @@ func (ir *ImageEngine) ArtifactExtract(ctx context.Context, name string, target
251242
Title: opts.Title,
252243
},
253244
}
254-
if err := artStore.Extract(ctx, name, target, &extractOpt); err != nil {
255-
return err
256-
}
257-
ir.Libpod.NewArtifactEvent(events.Extract, name, opts.Digest, map[string]string{
258-
"target": target,
259-
})
260-
return nil
245+
return artStore.Extract(ctx, name, target, &extractOpt)
261246
}
262247

263248
func (ir *ImageEngine) ArtifactExtractTarStream(ctx context.Context, w io.Writer, name string, opts entities.ArtifactExtractOptions) error {
@@ -272,12 +257,5 @@ func (ir *ImageEngine) ArtifactExtractTarStream(ctx context.Context, w io.Writer
272257
},
273258
ExcludeTitle: opts.ExcludeTitle,
274259
}
275-
276-
if err = artStore.ExtractTarStream(ctx, w, name, &extractOpt); err != nil {
277-
return err
278-
}
279-
ir.Libpod.NewArtifactEvent(events.Extract, name, opts.Digest, map[string]string{
280-
"format": "tar-stream",
281-
})
282-
return nil
260+
return artStore.ExtractTarStream(ctx, w, name, &extractOpt)
283261
}

pkg/libartifact/store/store.go

Lines changed: 68 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -40,17 +40,20 @@ var (
4040
ErrEmptyArtifactName = errors.New("artifact name cannot be empty")
4141
)
4242

43+
type EventCallback func(eventType, name, digest string, attrs map[string]string)
44+
4345
const ManifestSchemaVersion = 2
4446

4547
type ArtifactStore struct {
4648
SystemContext *types.SystemContext
4749
storePath string
4850
lock *lockfile.LockFile
51+
eventCallBack EventCallback
4952
}
5053

5154
// NewArtifactStore is a constructor for artifact stores. Most artifact dealings depend on this. Store path is
5255
// the filesystem location.
53-
func NewArtifactStore(storePath string, sc *types.SystemContext) (*ArtifactStore, error) {
56+
func NewArtifactStore(storePath string, sc *types.SystemContext, eventCallBack EventCallback) (*ArtifactStore, error) {
5457
if storePath == "" {
5558
return nil, errors.New("store path cannot be empty")
5659
}
@@ -63,6 +66,7 @@ func NewArtifactStore(storePath string, sc *types.SystemContext) (*ArtifactStore
6366
artifactStore := &ArtifactStore{
6467
storePath: storePath,
6568
SystemContext: sc,
69+
eventCallBack: eventCallBack,
6670
}
6771

6872
// if the storage dir does not exist, we need to create it.
@@ -116,7 +120,12 @@ func (as ArtifactStore) Remove(ctx context.Context, name string) (*digest.Digest
116120
if err != nil {
117121
return nil, err
118122
}
119-
return artifactDigest, ir.DeleteImage(ctx, as.SystemContext)
123+
err = ir.DeleteImage(ctx, as.SystemContext)
124+
if err != nil {
125+
return nil, err
126+
}
127+
as.eventCallBack("remove", name, artifactDigest.String(), nil)
128+
return artifactDigest, nil
120129
}
121130

122131
// Inspect an artifact in a local store
@@ -173,7 +182,9 @@ func (as ArtifactStore) Pull(ctx context.Context, name string, opts libimage.Cop
173182
if err != nil {
174183
return "", err
175184
}
176-
return digest.FromBytes(artifactBytes), nil
185+
artifactDigest := digest.FromBytes(artifactBytes)
186+
as.eventCallBack("pull", name, artifactDigest.String(), nil)
187+
return artifactDigest, nil
177188
}
178189

179190
// Push an artifact to an image registry
@@ -207,6 +218,7 @@ func (as ArtifactStore) Push(ctx context.Context, src, dest string, opts libimag
207218
return "", err
208219
}
209220
artifactDigest := digest.FromBytes(artifactBytes)
221+
as.eventCallBack("push", src, artifactDigest.String(), nil)
210222
return artifactDigest, nil
211223
}
212224

@@ -400,6 +412,9 @@ func (as ArtifactStore) Add(ctx context.Context, dest string, artifactBlobs []en
400412
}
401413
}
402414
}
415+
as.eventCallBack("add", dest, artifactManifestDigest.String(), map[string]string{
416+
"files": fmt.Sprintf("%d", len(artifactBlobs)),
417+
})
403418
return &artifactManifestDigest, nil
404419
}
405420

@@ -519,11 +534,10 @@ func (as ArtifactStore) Extract(ctx context.Context, nameOrDigest string, target
519534
} else {
520535
digest = arty.Manifest.Layers[0].Digest
521536
}
522-
523-
return copyTrustedImageBlobToFile(ctx, imgSrc, digest, target)
524-
}
525-
526-
if len(options.Digest) > 0 || len(options.Title) > 0 {
537+
if err := copyTrustedImageBlobToFile(ctx, imgSrc, digest, target); err != nil {
538+
return err
539+
}
540+
} else if len(options.Digest) > 0 || len(options.Title) > 0 {
527541
digest, err := findDigest(arty, &options.FilterBlobOptions)
528542
if err != nil {
529543
return err
@@ -536,23 +550,26 @@ func (as ArtifactStore) Extract(ctx context.Context, nameOrDigest string, target
536550
if err != nil {
537551
return err
538552
}
539-
540-
return copyTrustedImageBlobToFile(ctx, imgSrc, digest, filepath.Join(target, filename))
541-
}
542-
543-
for _, l := range arty.Manifest.Layers {
544-
title := l.Annotations[specV1.AnnotationTitle]
545-
filename, err := generateArtifactBlobName(title, l.Digest)
546-
if err != nil {
553+
if err := copyTrustedImageBlobToFile(ctx, imgSrc, digest, filepath.Join(target, filename)); err != nil {
547554
return err
548555
}
556+
} else {
557+
for _, l := range arty.Manifest.Layers {
558+
title := l.Annotations[specV1.AnnotationTitle]
559+
filename, err := generateArtifactBlobName(title, l.Digest)
560+
if err != nil {
561+
return err
562+
}
549563

550-
err = copyTrustedImageBlobToFile(ctx, imgSrc, l.Digest, filepath.Join(target, filename))
551-
if err != nil {
552-
return err
564+
err = copyTrustedImageBlobToFile(ctx, imgSrc, l.Digest, filepath.Join(target, filename))
565+
if err != nil {
566+
return err
567+
}
553568
}
554569
}
555570

571+
attrs := map[string]string{"target": target}
572+
as.eventCallBack("extract", arty.Name, options.Digest, attrs)
556573
return nil
557574
}
558575

@@ -594,49 +611,49 @@ func (as ArtifactStore) ExtractTarStream(ctx context.Context, w io.Writer, nameO
594611
if err != nil {
595612
return err
596613
}
614+
} else {
615+
artifactBlobCount := len(arty.Manifest.Layers)
597616

598-
return nil
599-
}
617+
type blob struct {
618+
name string
619+
digest digest.Digest
620+
}
621+
blobs := make([]blob, 0, artifactBlobCount)
600622

601-
artifactBlobCount := len(arty.Manifest.Layers)
623+
// Gather blob details and return error on any illegal names
624+
for _, l := range arty.Manifest.Layers {
625+
title := l.Annotations[specV1.AnnotationTitle]
626+
digest := l.Digest
627+
var name string
602628

603-
type blob struct {
604-
name string
605-
digest digest.Digest
606-
}
607-
blobs := make([]blob, 0, artifactBlobCount)
629+
if artifactBlobCount != 1 || !options.ExcludeTitle {
630+
name, err = generateArtifactBlobName(title, digest)
631+
if err != nil {
632+
return err
633+
}
634+
}
608635

609-
// Gather blob details and return error on any illegal names
610-
for _, l := range arty.Manifest.Layers {
611-
title := l.Annotations[specV1.AnnotationTitle]
612-
digest := l.Digest
613-
var name string
636+
blobs = append(blobs, blob{
637+
name: name,
638+
digest: digest,
639+
})
640+
}
641+
642+
// Wrap io.Writer in a tar.Writer
643+
tw := tar.NewWriter(w)
644+
defer tw.Close()
614645

615-
if artifactBlobCount != 1 || !options.ExcludeTitle {
616-
name, err = generateArtifactBlobName(title, digest)
646+
// Write each blob to tar.Writer then close
647+
for _, b := range blobs {
648+
err := copyTrustedImageBlobToTarStream(ctx, imgSrc, b.digest, b.name, tw)
617649
if err != nil {
618650
return err
619651
}
620652
}
621-
622-
blobs = append(blobs, blob{
623-
name: name,
624-
digest: digest,
625-
})
626-
}
627-
628-
// Wrap io.Writer in a tar.Writer
629-
tw := tar.NewWriter(w)
630-
defer tw.Close()
631-
632-
// Write each blob to tar.Writer then close
633-
for _, b := range blobs {
634-
err := copyTrustedImageBlobToTarStream(ctx, imgSrc, b.digest, b.name, tw)
635-
if err != nil {
636-
return err
637-
}
638653
}
639654

655+
attrs := map[string]string{"format": "tar-stream"}
656+
as.eventCallBack("extract", arty.Name, options.Digest, attrs)
640657
return nil
641658
}
642659

0 commit comments

Comments
 (0)