github.com/toplink-cn/moby@v0.0.0-20240305205811-460b4aebdf81/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/backend"
     7  	"github.com/docker/docker/api/types/events"
     8  )
     9  
    10  // LogImageEvent generates an event related to an image with only the default attributes.
    11  func (i *ImageService) LogImageEvent(imageID, refName string, action events.Action) {
    12  	ctx := context.TODO()
    13  	attributes := map[string]string{}
    14  
    15  	img, err := i.GetImage(ctx, imageID, backend.GetImageOpts{})
    16  	if err == nil && img.Config != nil {
    17  		// image has not been removed yet.
    18  		// it could be missing if the event is `delete`.
    19  		copyAttributes(attributes, img.Config.Labels)
    20  	}
    21  	if refName != "" {
    22  		attributes["name"] = refName
    23  	}
    24  	i.eventsService.Log(action, events.ImageEventType, events.Actor{
    25  		ID:         imageID,
    26  		Attributes: attributes,
    27  	})
    28  }
    29  
    30  // copyAttributes guarantees that labels are not mutated by event triggers.
    31  func copyAttributes(attributes, labels map[string]string) {
    32  	if labels == nil {
    33  		return
    34  	}
    35  	for k, v := range labels {
    36  		attributes[k] = v
    37  	}
    38  }