github.com/Heebron/moby@v0.0.0-20221111184709-6eab4f55faf7/daemon/images/image_events.go (about)

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