github.com/rawahars/moby@v24.0.4+incompatible/daemon/containerd/image_events.go (about)

     1  package containerd
     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  	ctx := context.TODO()
    13  	attributes := map[string]string{}
    14  
    15  	img, err := i.GetImage(ctx, imageID, imagetypes.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  	actor := events.Actor{
    25  		ID:         imageID,
    26  		Attributes: attributes,
    27  	}
    28  
    29  	i.eventsService.Log(action, events.ImageEventType, actor)
    30  }
    31  
    32  // copyAttributes guarantees that labels are not mutated by event triggers.
    33  func copyAttributes(attributes, labels map[string]string) {
    34  	if labels == nil {
    35  		return
    36  	}
    37  	for k, v := range labels {
    38  		attributes[k] = v
    39  	}
    40  }