2022-07-18 10:51:49 +00:00
|
|
|
package containerd
|
|
|
|
|
2023-03-30 14:11:02 +00:00
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
|
2023-11-17 05:55:54 +00:00
|
|
|
"github.com/containerd/containerd/images"
|
2024-01-20 15:01:43 +00:00
|
|
|
"github.com/docker/docker/api/types/backend"
|
2023-03-30 14:11:02 +00:00
|
|
|
"github.com/docker/docker/api/types/events"
|
|
|
|
)
|
|
|
|
|
|
|
|
// LogImageEvent generates an event related to an image with only the default attributes.
|
2023-08-26 13:24:46 +00:00
|
|
|
func (i *ImageService) LogImageEvent(imageID, refName string, action events.Action) {
|
2023-03-30 14:11:02 +00:00
|
|
|
ctx := context.TODO()
|
|
|
|
attributes := map[string]string{}
|
|
|
|
|
2024-01-20 15:01:43 +00:00
|
|
|
img, err := i.GetImage(ctx, imageID, backend.GetImageOpts{})
|
2023-03-30 14:11:02 +00:00
|
|
|
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
|
|
|
|
}
|
2023-08-26 16:25:27 +00:00
|
|
|
i.eventsService.Log(action, events.ImageEventType, events.Actor{
|
2023-03-30 14:11:02 +00:00
|
|
|
ID: imageID,
|
|
|
|
Attributes: attributes,
|
2023-08-26 16:25:27 +00:00
|
|
|
})
|
2022-07-18 10:51:49 +00:00
|
|
|
}
|
|
|
|
|
2023-11-17 05:55:54 +00:00
|
|
|
// logImageEvent generates an event related to an image with only name attribute.
|
|
|
|
func (i *ImageService) logImageEvent(img images.Image, refName string, action events.Action) {
|
|
|
|
attributes := map[string]string{}
|
|
|
|
if refName != "" {
|
|
|
|
attributes["name"] = refName
|
|
|
|
}
|
|
|
|
i.eventsService.Log(action, events.ImageEventType, events.Actor{
|
|
|
|
ID: img.Target.Digest.String(),
|
|
|
|
Attributes: attributes,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2023-03-30 14:11:02 +00:00
|
|
|
// 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
|
|
|
|
}
|
2022-07-18 10:51:49 +00:00
|
|
|
}
|