moby/daemon/containerd/image_events.go
Djordje Lukic 15b9176d53
Add the events services to the containerd image service
No events are sent yet, these will come at a later stage.

Signed-off-by: Djordje Lukic <djordje.lukic@docker.com>
2023-03-30 17:48:51 +02:00

40 lines
1,021 B
Go

package containerd
import (
"context"
"github.com/docker/docker/api/types/events"
imagetypes "github.com/docker/docker/api/types/image"
)
// LogImageEvent generates an event related to an image with only the default attributes.
func (i *ImageService) LogImageEvent(imageID, refName, action string) {
ctx := context.TODO()
attributes := map[string]string{}
img, err := i.GetImage(ctx, imageID, imagetypes.GetImageOpts{})
if err == nil && img.Config != nil {
// image has not been removed yet.
// it could be missing if the event is `delete`.
copyAttributes(attributes, img.Config.Labels)
}
if refName != "" {
attributes["name"] = refName
}
actor := events.Actor{
ID: imageID,
Attributes: attributes,
}
i.eventsService.Log(action, events.ImageEventType, actor)
}
// copyAttributes guarantees that labels are not mutated by event triggers.
func copyAttributes(attributes, labels map[string]string) {
if labels == nil {
return
}
for k, v := range labels {
attributes[k] = v
}
}