Merge pull request #45266 from rumpl/c8d-tag-delete-events

c8d: Send an event when an image is tagged or deleted
This commit is contained in:
Sebastiaan van Stijn 2023-04-05 09:10:43 +02:00 committed by GitHub
commit c2c83e16a2
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 14 additions and 1 deletions

View file

@ -45,7 +45,7 @@ import (
// TODO(thaJeztah): implement ImageDelete "force" options; see https://github.com/moby/moby/issues/43850
// TODO(thaJeztah): implement ImageDelete "prune" options; see https://github.com/moby/moby/issues/43849
// TODO(thaJeztah): add support for image delete using image (short)ID; see https://github.com/moby/moby/issues/43854
// TODO(thaJeztah): mage delete should send image "untag" events and prometheus counters; see https://github.com/moby/moby/issues/43855
// TODO(thaJeztah): image delete should send prometheus counters; see https://github.com/moby/moby/issues/45268
func (i *ImageService) ImageDelete(ctx context.Context, imageRef string, force, prune bool) ([]types.ImageDeleteResponseItem, error) {
parsedRef, err := reference.ParseNormalizedNamed(imageRef)
if err != nil {
@ -53,10 +53,19 @@ func (i *ImageService) ImageDelete(ctx context.Context, imageRef string, force,
}
ref := reference.TagNameOnly(parsedRef)
desc, err := i.resolveDescriptor(ctx, imageRef)
if err != nil {
return nil, err
}
imgID := string(desc.Digest)
err = i.client.ImageService().Delete(ctx, ref.String(), images.SynchronousDelete())
if err != nil {
return nil, err
}
i.LogImageEvent(imgID, imgID, "untag")
i.LogImageEvent(imgID, imgID, "delete")
return []types.ImageDeleteResponseItem{{Untagged: reference.FamiliarString(parsedRef)}}, nil
}

View file

@ -39,6 +39,7 @@ func (i *ImageService) TagImage(ctx context.Context, imageID image.ID, newTag re
// Check if image we would replace already resolves to the same target.
// No need to do anything.
if replacedImg.Target.Digest == target.Digest {
i.LogImageEvent(imageID.String(), reference.FamiliarString(newTag), "tag")
return nil
}
@ -59,12 +60,15 @@ func (i *ImageService) TagImage(ctx context.Context, imageID image.ID, newTag re
})
logger.Info("image created")
defer i.LogImageEvent(imageID.String(), reference.FamiliarString(newTag), "tag")
// The tag succeeded, check if the source image is dangling
sourceDanglingImg, err := is.Get(context.Background(), danglingImageName(target.Digest))
if err != nil {
if !cerrdefs.IsNotFound(err) {
logger.WithError(err).Warn("unexpected error when checking if source image is dangling")
}
return nil
}