diff --git a/daemon/build.go b/daemon/build.go index f80f622a0a0c9..cae0cda850eaa 100644 --- a/daemon/build.go +++ b/daemon/build.go @@ -13,14 +13,12 @@ import ( // When no tag is given, buildkit doesn't call the image service so it has no // way of knowing the image was created. func (daemon *Daemon) ImageExportedByBuildkit(ctx context.Context, id string, desc ocispec.Descriptor) { - daemon.imageService.LogImageEvent(id, id, events.ActionCreate) + daemon.imageService.LogImageEvent(ctx, id, id, events.ActionCreate) } // ImageNamedByBuildkit is a callback that is called when an image is tagged by buildkit. // Note: It is only called if the buildkit didn't call the image service itself to perform the tagging. // Currently this only happens when the containerd image store is used. func (daemon *Daemon) ImageNamedByBuildkit(ctx context.Context, ref reference.NamedTagged, desc ocispec.Descriptor) { - id := desc.Digest.String() - name := reference.FamiliarString(ref) - daemon.imageService.LogImageEvent(id, name, events.ActionTag) + daemon.imageService.LogImageEvent(ctx, desc.Digest.String(), reference.FamiliarString(ref), events.ActionTag) } diff --git a/daemon/containerd/image_builder.go b/daemon/containerd/image_builder.go index 60a4a6757a449..a1b7a757b4844 100644 --- a/daemon/containerd/image_builder.go +++ b/daemon/containerd/image_builder.go @@ -524,7 +524,7 @@ func (i *ImageService) createImageOCI(ctx context.Context, imgToCreate imagespec } id := image.ID(img.Target.Digest) - i.LogImageEvent(id.String(), id.String(), events.ActionCreate) + i.LogImageEvent(ctx, id.String(), id.String(), events.ActionCreate) if err := i.unpackImage(ctx, i.StorageDriver(), img, manifestDesc); err != nil { return "", err diff --git a/daemon/containerd/image_delete.go b/daemon/containerd/image_delete.go index 638f8ae15093e..5c4e93090acd1 100644 --- a/daemon/containerd/image_delete.go +++ b/daemon/containerd/image_delete.go @@ -235,7 +235,7 @@ func (i *ImageService) deleteAll(ctx context.Context, imgID image.ID, all []c8di return records, err } } - i.LogImageEvent(imgID.String(), imgID.String(), events.ActionDelete) + i.LogImageEvent(ctx, imgID.String(), imgID.String(), events.ActionDelete) records = append(records, imagetypes.DeleteResponse{Deleted: imgID.String()}) for _, parent := range parents { @@ -248,7 +248,7 @@ func (i *ImageService) deleteAll(ctx context.Context, imgID image.ID, all []c8di break } parentID := parent.Target.Digest.String() - i.LogImageEvent(parentID, parentID, events.ActionDelete) + i.LogImageEvent(ctx, parentID, parentID, events.ActionDelete) records = append(records, imagetypes.DeleteResponse{Deleted: parentID}) } diff --git a/daemon/containerd/image_events.go b/daemon/containerd/image_events.go index cd8bc84131f7f..b07c11807d8b8 100644 --- a/daemon/containerd/image_events.go +++ b/daemon/containerd/image_events.go @@ -9,8 +9,8 @@ import ( ) // LogImageEvent generates an event related to an image with only the default attributes. -func (i *ImageService) LogImageEvent(imageID, refName string, action events.Action) { - ctx := context.TODO() +func (i *ImageService) LogImageEvent(ctx context.Context, imageID, refName string, action events.Action) { + ctx = context.WithoutCancel(ctx) attributes := map[string]string{} img, err := i.GetImage(ctx, imageID, backend.GetImageOpts{}) diff --git a/daemon/containerd/image_exporter.go b/daemon/containerd/image_exporter.go index f2bfe2afc824e..945fcdc6f7f86 100644 --- a/daemon/containerd/image_exporter.go +++ b/daemon/containerd/image_exporter.go @@ -121,7 +121,7 @@ func (i *ImageService) ExportImage(ctx context.Context, names []string, platform }).Debug("export image without name") } - i.LogImageEvent(target.Digest.String(), target.Digest.String(), events.ActionSave) + i.LogImageEvent(ctx, target.Digest.String(), target.Digest.String(), events.ActionSave) return nil } @@ -368,7 +368,7 @@ func (i *ImageService) LoadImage(ctx context.Context, inTar io.ReadCloser, platf }) fmt.Fprintf(progress, "%s: %s\n", loadedMsg, name) - i.LogImageEvent(img.Target.Digest.String(), img.Target.Digest.String(), events.ActionLoad) + i.LogImageEvent(ctx, img.Target.Digest.String(), img.Target.Digest.String(), events.ActionLoad) if err != nil { // The image failed to unpack, but is already imported, log the error but don't fail the whole load. diff --git a/daemon/containerd/image_import.go b/daemon/containerd/image_import.go index 31c2903eacaaf..3f20ae90b1037 100644 --- a/daemon/containerd/image_import.go +++ b/daemon/containerd/image_import.go @@ -154,7 +154,7 @@ func (i *ImageService) ImportImage(ctx context.Context, ref reference.Named, pla if err != nil { logger.WithError(err).Debug("failed to unpack image") } else { - i.LogImageEvent(id.String(), id.String(), events.ActionImport) + i.LogImageEvent(ctx, id.String(), id.String(), events.ActionImport) } return id, err diff --git a/daemon/containerd/image_pull.go b/daemon/containerd/image_pull.go index e7d59634b66ea..376f76d3df462 100644 --- a/daemon/containerd/image_pull.go +++ b/daemon/containerd/image_pull.go @@ -246,7 +246,7 @@ func (i *ImageService) pullTag(ctx context.Context, ref reference.Named, platfor logger.WithError(err).Warn("unexpected error while removing outdated dangling image reference") } - i.LogImageEvent(reference.FamiliarString(ref), reference.FamiliarName(ref), events.ActionPull) + i.LogImageEvent(ctx, reference.FamiliarString(ref), reference.FamiliarName(ref), events.ActionPull) outNewImg = img return nil } diff --git a/daemon/containerd/image_push.go b/daemon/containerd/image_push.go index c652863b5c45a..674777673a3f5 100644 --- a/daemon/containerd/image_push.go +++ b/daemon/containerd/image_push.go @@ -209,7 +209,7 @@ func (i *ImageService) pushRef(ctx context.Context, targetRef reference.Named, p appendDistributionSourceLabel(ctx, realStore, targetRef, target) - i.LogImageEvent(reference.FamiliarString(targetRef), reference.FamiliarName(targetRef), events.ActionPush) + i.LogImageEvent(ctx, reference.FamiliarString(targetRef), reference.FamiliarName(targetRef), events.ActionPush) return nil } diff --git a/daemon/containerd/image_tag.go b/daemon/containerd/image_tag.go index b8d6819ccd349..7219e0941da39 100644 --- a/daemon/containerd/image_tag.go +++ b/daemon/containerd/image_tag.go @@ -58,7 +58,7 @@ func (i *ImageService) createOrReplaceImage(ctx context.Context, newImg c8dimage // No need to do anything. if replacedImg.Target.Digest == newImg.Target.Digest { if !creatingDangling { - i.LogImageEvent(replacedImg.Target.Digest.String(), imageFamiliarName(newImg), events.ActionTag) + i.LogImageEvent(ctx, replacedImg.Target.Digest.String(), imageFamiliarName(newImg), events.ActionTag) } return nil } @@ -81,8 +81,7 @@ func (i *ImageService) createOrReplaceImage(ctx context.Context, newImg c8dimage logger.Info("image created") if !creatingDangling { - ctx := context.WithoutCancel(ctx) - defer i.LogImageEvent(string(newImg.Target.Digest), imageFamiliarName(newImg), events.ActionTag) + defer i.LogImageEvent(ctx, string(newImg.Target.Digest), imageFamiliarName(newImg), events.ActionTag) if err := i.images.Delete(ctx, danglingName); err != nil { if !cerrdefs.IsNotFound(err) { diff --git a/daemon/image_service.go b/daemon/image_service.go index 098907cb88892..4b1ca07b1a0f3 100644 --- a/daemon/image_service.go +++ b/daemon/image_service.go @@ -34,7 +34,7 @@ type ImageService interface { PerformWithBaseFS(ctx context.Context, c *container.Container, fn func(string) error) error LoadImage(ctx context.Context, inTar io.ReadCloser, platform *ocispec.Platform, outStream io.Writer, quiet bool) error Images(ctx context.Context, opts imagetype.ListOptions) ([]*imagetype.Summary, error) - LogImageEvent(imageID, refName string, action events.Action) + LogImageEvent(ctx context.Context, imageID, refName string, action events.Action) CountImages(ctx context.Context) int ImagesPrune(ctx context.Context, pruneFilters filters.Args) (*imagetype.PruneReport, error) ImportImage(ctx context.Context, ref reference.Named, platform *ocispec.Platform, msg string, layerReader io.Reader, changes []string) (image.ID, error) diff --git a/daemon/images/image_commit.go b/daemon/images/image_commit.go index daf7162f78d55..27b7ea3fc03f1 100644 --- a/daemon/images/image_commit.go +++ b/daemon/images/image_commit.go @@ -64,7 +64,7 @@ func (i *ImageService) CommitImage(ctx context.Context, c backend.CommitConfig) return "", err } - i.LogImageEvent(id.String(), id.String(), events.ActionCreate) + i.LogImageEvent(ctx, id.String(), id.String(), events.ActionCreate) if err := i.imageStore.SetBuiltLocally(id); err != nil { return "", err diff --git a/daemon/images/image_delete.go b/daemon/images/image_delete.go index f185d6e2fc5a9..00d84ee4f3fdb 100644 --- a/daemon/images/image_delete.go +++ b/daemon/images/image_delete.go @@ -107,7 +107,7 @@ func (i *ImageService) ImageDelete(ctx context.Context, imageRef string, force, untaggedRecord := imagetypes.DeleteResponse{Untagged: reference.FamiliarString(parsedRef)} - i.LogImageEvent(imgID.String(), imgID.String(), events.ActionUnTag) + i.LogImageEvent(ctx, imgID.String(), imgID.String(), events.ActionUnTag) records = append(records, untaggedRecord) repoRefs = i.referenceStore.References(imgID.Digest()) @@ -164,7 +164,7 @@ func (i *ImageService) ImageDelete(ctx context.Context, imageRef string, force, if err != nil { return nil, err } - i.LogImageEvent(imgID.String(), imgID.String(), events.ActionUnTag) + i.LogImageEvent(ctx, imgID.String(), imgID.String(), events.ActionUnTag) records = append(records, imagetypes.DeleteResponse{Untagged: reference.FamiliarString(parsedRef)}) } } @@ -245,7 +245,7 @@ func (i *ImageService) removeAllReferencesToImageID(imgID image.ID, records *[]i if err != nil { return err } - i.LogImageEvent(imgID.String(), imgID.String(), events.ActionUnTag) + i.LogImageEvent(context.TODO(), imgID.String(), imgID.String(), events.ActionUnTag) *records = append(*records, imagetypes.DeleteResponse{ Untagged: reference.FamiliarString(parsedRef), }) @@ -322,7 +322,7 @@ func (i *ImageService) imageDeleteHelper(imgID image.ID, records *[]imagetypes.D return err } - i.LogImageEvent(imgID.String(), imgID.String(), events.ActionDelete) + i.LogImageEvent(context.TODO(), imgID.String(), imgID.String(), events.ActionDelete) *records = append(*records, imagetypes.DeleteResponse{Deleted: imgID.String()}) for _, removedLayer := range removedLayers { *records = append(*records, imagetypes.DeleteResponse{Deleted: removedLayer.ChainID.String()}) diff --git a/daemon/images/image_events.go b/daemon/images/image_events.go index b87e366776be5..a80aac86e6e66 100644 --- a/daemon/images/image_events.go +++ b/daemon/images/image_events.go @@ -8,8 +8,8 @@ import ( ) // LogImageEvent generates an event related to an image with only the default attributes. -func (i *ImageService) LogImageEvent(imageID, refName string, action events.Action) { - ctx := context.TODO() +func (i *ImageService) LogImageEvent(ctx context.Context, imageID, refName string, action events.Action) { + ctx = context.WithoutCancel(ctx) attributes := map[string]string{} img, err := i.GetImage(ctx, imageID, backend.GetImageOpts{}) diff --git a/daemon/images/image_import.go b/daemon/images/image_import.go index de8bd9a6c1e0e..fc2000637e96d 100644 --- a/daemon/images/image_import.go +++ b/daemon/images/image_import.go @@ -85,6 +85,6 @@ func (i *ImageService) ImportImage(ctx context.Context, newRef reference.Named, } } - i.LogImageEvent(id.String(), id.String(), events.ActionImport) + i.LogImageEvent(ctx, id.String(), id.String(), events.ActionImport) return id, nil } diff --git a/daemon/images/image_tag.go b/daemon/images/image_tag.go index 98ed08d77c59d..e4e50eff83153 100644 --- a/daemon/images/image_tag.go +++ b/daemon/images/image_tag.go @@ -17,6 +17,6 @@ func (i *ImageService) TagImage(ctx context.Context, imageID image.ID, newTag re if err := i.imageStore.SetLastUpdated(imageID); err != nil { return err } - i.LogImageEvent(imageID.String(), reference.FamiliarString(newTag), events.ActionTag) + i.LogImageEvent(ctx, imageID.String(), reference.FamiliarString(newTag), events.ActionTag) return nil } diff --git a/distribution/config.go b/distribution/config.go index ddb6314ffbaa4..11d27e3ec4c80 100644 --- a/distribution/config.go +++ b/distribution/config.go @@ -38,7 +38,7 @@ type Config struct { // and endpoint lookup. RegistryService RegistryResolver // ImageEventLogger notifies events for a given image - ImageEventLogger func(id, name string, action events.Action) + ImageEventLogger func(ctx context.Context, id, name string, action events.Action) // MetadataStore is the storage backend for distribution-specific // metadata. MetadataStore metadata.Store diff --git a/distribution/pull.go b/distribution/pull.go index 0131824e60761..db9b23539ebd6 100644 --- a/distribution/pull.go +++ b/distribution/pull.go @@ -24,7 +24,7 @@ func Pull(ctx context.Context, ref reference.Named, config *ImagePullConfig, loc }) if err == nil { - config.ImageEventLogger(reference.FamiliarString(ref), reference.FamiliarName(repoInfo.Name), events.ActionPull) + config.ImageEventLogger(ctx, reference.FamiliarString(ref), reference.FamiliarName(repoInfo.Name), events.ActionPull) } return err diff --git a/distribution/push.go b/distribution/push.go index fa4f7c5a12b6a..efeb9fe902912 100644 --- a/distribution/push.go +++ b/distribution/push.go @@ -78,7 +78,7 @@ func Push(ctx context.Context, ref reference.Named, config *ImagePushConfig) err return err } - config.ImageEventLogger(reference.FamiliarString(ref), reference.FamiliarName(repoInfo.Name), events.ActionPush) + config.ImageEventLogger(ctx, reference.FamiliarString(ref), reference.FamiliarName(repoInfo.Name), events.ActionPush) return nil } diff --git a/image/tarexport/load.go b/image/tarexport/load.go index 03641466b9a96..2bbdde0226043 100644 --- a/image/tarexport/load.go +++ b/image/tarexport/load.go @@ -159,7 +159,7 @@ func (l *tarexporter) Load(ctx context.Context, inTar io.ReadCloser, outStream i } parentLinks = append(parentLinks, parentLink{imgID, m.Parent}) - l.loggerImgEvent.LogImageEvent(imgID.String(), imgID.String(), events.ActionLoad) + l.loggerImgEvent.LogImageEvent(ctx, imgID.String(), imgID.String(), events.ActionLoad) } for _, p := range validatedParentLinks(parentLinks) { diff --git a/image/tarexport/save.go b/image/tarexport/save.go index 49f3b871ed107..53b2acefa2b76 100644 --- a/image/tarexport/save.go +++ b/image/tarexport/save.go @@ -316,7 +316,7 @@ func (s *saveSession) save(ctx context.Context, outStream io.Writer) error { parentID, _ := s.is.GetParent(id) parentLinks = append(parentLinks, parentLink{id, parentID}) - s.tarexporter.loggerImgEvent.LogImageEvent(id.String(), id.String(), events.ActionSave) + s.tarexporter.loggerImgEvent.LogImageEvent(ctx, id.String(), id.String(), events.ActionSave) } for i, p := range validatedParentLinks(parentLinks) { diff --git a/image/tarexport/tarexport.go b/image/tarexport/tarexport.go index 000307ac5da14..ab1860ee36b77 100644 --- a/image/tarexport/tarexport.go +++ b/image/tarexport/tarexport.go @@ -1,6 +1,8 @@ package tarexport // import "github.com/docker/docker/image/tarexport" import ( + "context" + "github.com/containerd/platforms" "github.com/docker/distribution" "github.com/docker/docker/api/types/events" @@ -37,7 +39,7 @@ type tarexporter struct { // LogImageEvent defines interface for event generation related to image tar(load and save) operations type LogImageEvent interface { // LogImageEvent generates an event related to an image operation - LogImageEvent(imageID, refName string, action events.Action) + LogImageEvent(ctx context.Context, imageID, refName string, action events.Action) } // NewTarExporter returns new Exporter for tar packages