image_events.go 1021 B

12345678910111213141516171819202122232425262728293031323334353637383940
  1. package containerd
  2. import (
  3. "context"
  4. "github.com/docker/docker/api/types/events"
  5. imagetypes "github.com/docker/docker/api/types/image"
  6. )
  7. // LogImageEvent generates an event related to an image with only the default attributes.
  8. func (i *ImageService) LogImageEvent(imageID, refName, action string) {
  9. ctx := context.TODO()
  10. attributes := map[string]string{}
  11. img, err := i.GetImage(ctx, imageID, imagetypes.GetImageOpts{})
  12. if err == nil && img.Config != nil {
  13. // image has not been removed yet.
  14. // it could be missing if the event is `delete`.
  15. copyAttributes(attributes, img.Config.Labels)
  16. }
  17. if refName != "" {
  18. attributes["name"] = refName
  19. }
  20. actor := events.Actor{
  21. ID: imageID,
  22. Attributes: attributes,
  23. }
  24. i.eventsService.Log(action, events.ImageEventType, actor)
  25. }
  26. // copyAttributes guarantees that labels are not mutated by event triggers.
  27. func copyAttributes(attributes, labels map[string]string) {
  28. if labels == nil {
  29. return
  30. }
  31. for k, v := range labels {
  32. attributes[k] = v
  33. }
  34. }