github.com/moby/docker@v26.1.3+incompatible/daemon/containerd/image_events.go (about)

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