image_events.go 1018 B

1234567891011121314151617181920212223242526272829303132333435363738
  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 string, action events.Action) {
  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. i.eventsService.Log(action, events.ImageEventType, events.Actor{
  21. ID: imageID,
  22. Attributes: attributes,
  23. })
  24. }
  25. // copyAttributes guarantees that labels are not mutated by event triggers.
  26. func copyAttributes(attributes, labels map[string]string) {
  27. if labels == nil {
  28. return
  29. }
  30. for k, v := range labels {
  31. attributes[k] = v
  32. }
  33. }