Skip to content

Commit

Permalink
Merge pull request moby#49014 from thaJeztah/eventlog_context
Browse files Browse the repository at this point in the history
daemon: ImageService.LogImageEvent: pass through context
  • Loading branch information
thaJeztah authored Jan 8, 2025
2 parents d127f16 + ee1a15a commit 50212d2
Show file tree
Hide file tree
Showing 21 changed files with 32 additions and 33 deletions.
6 changes: 2 additions & 4 deletions daemon/build.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
2 changes: 1 addition & 1 deletion daemon/containerd/image_builder.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
4 changes: 2 additions & 2 deletions daemon/containerd/image_delete.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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})
}

Expand Down
4 changes: 2 additions & 2 deletions daemon/containerd/image_events.go
Original file line number Diff line number Diff line change
Expand Up @@ -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{})
Expand Down
4 changes: 2 additions & 2 deletions daemon/containerd/image_exporter.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

Expand Down Expand Up @@ -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.
Expand Down
2 changes: 1 addition & 1 deletion daemon/containerd/image_import.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion daemon/containerd/image_pull.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down
2 changes: 1 addition & 1 deletion daemon/containerd/image_push.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down
5 changes: 2 additions & 3 deletions daemon/containerd/image_tag.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand All @@ -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) {
Expand Down
2 changes: 1 addition & 1 deletion daemon/image_service.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
2 changes: 1 addition & 1 deletion daemon/images/image_commit.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
8 changes: 4 additions & 4 deletions daemon/images/image_delete.go
Original file line number Diff line number Diff line change
Expand Up @@ -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())
Expand Down Expand Up @@ -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)})
}
}
Expand Down Expand Up @@ -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),
})
Expand Down Expand Up @@ -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()})
Expand Down
4 changes: 2 additions & 2 deletions daemon/images/image_events.go
Original file line number Diff line number Diff line change
Expand Up @@ -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{})
Expand Down
2 changes: 1 addition & 1 deletion daemon/images/image_import.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
2 changes: 1 addition & 1 deletion daemon/images/image_tag.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
2 changes: 1 addition & 1 deletion distribution/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion distribution/pull.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion distribution/push.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

Expand Down
2 changes: 1 addition & 1 deletion image/tarexport/load.go
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
2 changes: 1 addition & 1 deletion image/tarexport/save.go
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
4 changes: 3 additions & 1 deletion image/tarexport/tarexport.go
Original file line number Diff line number Diff line change
@@ -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"
Expand Down Expand Up @@ -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
Expand Down

0 comments on commit 50212d2

Please sign in to comment.