瀏覽代碼

Merge pull request #45241 from rumpl/c8d-image-events

c8d: Add the events services to the containerd image service
Sebastiaan van Stijn 2 年之前
父節點
當前提交
7c93e4a09b
共有 5 個文件被更改,包括 62 次插入20 次删除
  1. 34 7
      daemon/containerd/image_events.go
  2. 18 6
      daemon/containerd/service.go
  3. 8 1
      daemon/daemon.go
  4. 0 1
      daemon/image_service.go
  5. 2 5
      daemon/images/image_events.go

+ 34 - 7
daemon/containerd/image_events.go

@@ -1,13 +1,40 @@
 package containerd
 package containerd
 
 
-// LogImageEvent generates an event related to an image with only the
-// default attributes.
+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) {
 func (i *ImageService) LogImageEvent(imageID, refName, action string) {
-	panic("not implemented")
+	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)
 }
 }
 
 
-// LogImageEventWithAttributes generates an event related to an image with
-// specific given attributes.
-func (i *ImageService) LogImageEventWithAttributes(imageID, refName, action string, attributes map[string]string) {
-	panic("not implemented")
+// 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
+	}
 }
 }

+ 18 - 6
daemon/containerd/service.go

@@ -10,6 +10,7 @@ import (
 	"github.com/containerd/containerd/remotes/docker"
 	"github.com/containerd/containerd/remotes/docker"
 	"github.com/containerd/containerd/snapshots"
 	"github.com/containerd/containerd/snapshots"
 	"github.com/docker/docker/container"
 	"github.com/docker/docker/container"
+	daemonevents "github.com/docker/docker/daemon/events"
 	"github.com/docker/docker/daemon/images"
 	"github.com/docker/docker/daemon/images"
 	"github.com/docker/docker/errdefs"
 	"github.com/docker/docker/errdefs"
 	"github.com/docker/docker/image"
 	"github.com/docker/docker/image"
@@ -27,6 +28,7 @@ type ImageService struct {
 	snapshotter     string
 	snapshotter     string
 	registryHosts   RegistryHostsProvider
 	registryHosts   RegistryHostsProvider
 	registryService RegistryConfigProvider
 	registryService RegistryConfigProvider
+	eventsService   *daemonevents.Events
 }
 }
 
 
 type RegistryHostsProvider interface {
 type RegistryHostsProvider interface {
@@ -37,14 +39,24 @@ type RegistryConfigProvider interface {
 	IsInsecureRegistry(host string) bool
 	IsInsecureRegistry(host string) bool
 }
 }
 
 
+type ImageServiceConfig struct {
+	Client        *containerd.Client
+	Containers    container.Store
+	Snapshotter   string
+	HostsProvider RegistryHostsProvider
+	Registry      RegistryConfigProvider
+	EventsService *daemonevents.Events
+}
+
 // NewService creates a new ImageService.
 // NewService creates a new ImageService.
-func NewService(c *containerd.Client, containers container.Store, snapshotter string, hostsProvider RegistryHostsProvider, registry RegistryConfigProvider) *ImageService {
+func NewService(config ImageServiceConfig) *ImageService {
 	return &ImageService{
 	return &ImageService{
-		client:          c,
-		containers:      containers,
-		snapshotter:     snapshotter,
-		registryHosts:   hostsProvider,
-		registryService: registry,
+		client:          config.Client,
+		containers:      config.Containers,
+		snapshotter:     config.Snapshotter,
+		registryHosts:   config.HostsProvider,
+		registryService: config.Registry,
+		eventsService:   config.EventsService,
 	}
 	}
 }
 }
 
 

+ 8 - 1
daemon/daemon.go

@@ -1012,7 +1012,14 @@ func NewDaemon(ctx context.Context, config *config.Config, pluginStore *plugin.S
 		if err := configureKernelSecuritySupport(config, driverName); err != nil {
 		if err := configureKernelSecuritySupport(config, driverName); err != nil {
 			return nil, err
 			return nil, err
 		}
 		}
-		d.imageService = ctrd.NewService(d.containerdCli, d.containers, driverName, d, d.registryService)
+		d.imageService = ctrd.NewService(ctrd.ImageServiceConfig{
+			Client:        d.containerdCli,
+			Containers:    d.containers,
+			Snapshotter:   driverName,
+			HostsProvider: d,
+			Registry:      d.registryService,
+			EventsService: d.EventsService,
+		})
 	} else {
 	} else {
 		layerStore, err := layer.NewStoreFromOptions(layer.StoreOptions{
 		layerStore, err := layer.NewStoreFromOptions(layer.StoreOptions{
 			Root:                      config.Root,
 			Root:                      config.Root,

+ 0 - 1
daemon/image_service.go

@@ -35,7 +35,6 @@ type ImageService interface {
 	LoadImage(ctx context.Context, inTar io.ReadCloser, outStream io.Writer, quiet bool) error
 	LoadImage(ctx context.Context, inTar io.ReadCloser, outStream io.Writer, quiet bool) error
 	Images(ctx context.Context, opts types.ImageListOptions) ([]*types.ImageSummary, error)
 	Images(ctx context.Context, opts types.ImageListOptions) ([]*types.ImageSummary, error)
 	LogImageEvent(imageID, refName, action string)
 	LogImageEvent(imageID, refName, action string)
-	LogImageEventWithAttributes(imageID, refName, action string, attributes map[string]string)
 	CountImages() int
 	CountImages() int
 	ImagesPrune(ctx context.Context, pruneFilters filters.Args) (*types.ImagesPruneReport, error)
 	ImagesPrune(ctx context.Context, pruneFilters filters.Args) (*types.ImagesPruneReport, error)
 	ImportImage(ctx context.Context, ref reference.Named, platform *v1.Platform, msg string, layerReader io.Reader, changes []string) (image.ID, error)
 	ImportImage(ctx context.Context, ref reference.Named, platform *v1.Platform, msg string, layerReader io.Reader, changes []string) (image.ID, error)

+ 2 - 5
daemon/images/image_events.go

@@ -9,12 +9,9 @@ import (
 
 
 // LogImageEvent generates an event related to an image with only the default attributes.
 // LogImageEvent generates an event related to an image with only the default attributes.
 func (i *ImageService) LogImageEvent(imageID, refName, action string) {
 func (i *ImageService) LogImageEvent(imageID, refName, action string) {
-	i.LogImageEventWithAttributes(imageID, refName, action, map[string]string{})
-}
-
-// LogImageEventWithAttributes generates an event related to an image with specific given attributes.
-func (i *ImageService) LogImageEventWithAttributes(imageID, refName, action string, attributes map[string]string) {
 	ctx := context.TODO()
 	ctx := context.TODO()
+	attributes := map[string]string{}
+
 	img, err := i.GetImage(ctx, imageID, imagetypes.GetImageOpts{})
 	img, err := i.GetImage(ctx, imageID, imagetypes.GetImageOpts{})
 	if err == nil && img.Config != nil {
 	if err == nil && img.Config != nil {
 		// image has not been removed yet.
 		// image has not been removed yet.