浏览代码

daemon: inline some variables when emitting events

Signed-off-by: Sebastiaan van Stijn <github@gone.nl>
Sebastiaan van Stijn 1 年之前
父节点
当前提交
10a3a3bc49

+ 2 - 4
daemon/containerd/image_events.go

@@ -21,12 +21,10 @@ func (i *ImageService) LogImageEvent(imageID, refName, action string) {
 	if refName != "" {
 		attributes["name"] = refName
 	}
-	actor := events.Actor{
+	i.eventsService.Log(action, events.ImageEventType, events.Actor{
 		ID:         imageID,
 		Attributes: attributes,
-	}
-
-	i.eventsService.Log(action, events.ImageEventType, actor)
+	})
 }
 
 // copyAttributes guarantees that labels are not mutated by event triggers.

+ 6 - 10
daemon/exec.go

@@ -137,11 +137,9 @@ func (daemon *Daemon) ContainerExecCreate(name string, config *types.ExecConfig)
 	}
 
 	daemon.registerExecCommand(cntr, execConfig)
-
-	attributes := map[string]string{
+	daemon.LogContainerEventWithAttributes(cntr, "exec_create: "+execConfig.Entrypoint+" "+strings.Join(execConfig.Args, " "), map[string]string{
 		"execID": execConfig.ID,
-	}
-	daemon.LogContainerEventWithAttributes(cntr, "exec_create: "+execConfig.Entrypoint+" "+strings.Join(execConfig.Args, " "), attributes)
+	})
 
 	return execConfig.ID, nil
 }
@@ -175,10 +173,9 @@ func (daemon *Daemon) ContainerExecStart(ctx context.Context, name string, optio
 	ec.Unlock()
 
 	log.G(ctx).Debugf("starting exec command %s in container %s", ec.ID, ec.Container.ID)
-	attributes := map[string]string{
+	daemon.LogContainerEventWithAttributes(ec.Container, "exec_start: "+ec.Entrypoint+" "+strings.Join(ec.Args, " "), map[string]string{
 		"execID": ec.ID,
-	}
-	daemon.LogContainerEventWithAttributes(ec.Container, "exec_start: "+ec.Entrypoint+" "+strings.Join(ec.Args, " "), attributes)
+	})
 
 	defer func() {
 		if err != nil {
@@ -311,10 +308,9 @@ func (daemon *Daemon) ContainerExecStart(ctx context.Context, name string, optio
 			if _, ok := err.(term.EscapeError); !ok {
 				return errdefs.System(errors.Wrap(err, "exec attach failed"))
 			}
-			attributes := map[string]string{
+			daemon.LogContainerEventWithAttributes(ec.Container, "exec_detach", map[string]string{
 				"execID": ec.ID,
-			}
-			daemon.LogContainerEventWithAttributes(ec.Container, "exec_detach", attributes)
+			})
 		}
 	}
 	return nil

+ 2 - 3
daemon/health.go

@@ -87,10 +87,9 @@ func (p *cmdProbe) run(ctx context.Context, d *Daemon, cntr *container.Container
 	execConfig.Env = container.ReplaceOrAppendEnvValues(cntr.CreateDaemonEnvironment(execConfig.Tty, linkedEnv), execConfig.Env)
 
 	d.registerExecCommand(cntr, execConfig)
-	attributes := map[string]string{
+	d.LogContainerEventWithAttributes(cntr, "exec_create: "+execConfig.Entrypoint+" "+strings.Join(execConfig.Args, " "), map[string]string{
 		"execID": execConfig.ID,
-	}
-	d.LogContainerEventWithAttributes(cntr, "exec_create: "+execConfig.Entrypoint+" "+strings.Join(execConfig.Args, " "), attributes)
+	})
 
 	output := &limitedBuffer{}
 	probeCtx, cancelProbe := context.WithCancel(ctx)

+ 4 - 7
daemon/images/image_delete.go

@@ -243,18 +243,15 @@ func (i *ImageService) removeImageRef(ref reference.Named) (reference.Named, err
 // daemon's event service. An "Untagged" types.ImageDeleteResponseItem is added to the
 // given list of records.
 func (i *ImageService) removeAllReferencesToImageID(imgID image.ID, records *[]types.ImageDeleteResponseItem) error {
-	imageRefs := i.referenceStore.References(imgID.Digest())
-
-	for _, imageRef := range imageRefs {
+	for _, imageRef := range i.referenceStore.References(imgID.Digest()) {
 		parsedRef, err := i.removeImageRef(imageRef)
 		if err != nil {
 			return err
 		}
-
-		untaggedRecord := types.ImageDeleteResponseItem{Untagged: reference.FamiliarString(parsedRef)}
-
 		i.LogImageEvent(imgID.String(), imgID.String(), "untag")
-		*records = append(*records, untaggedRecord)
+		*records = append(*records, types.ImageDeleteResponseItem{
+			Untagged: reference.FamiliarString(parsedRef),
+		})
 	}
 
 	return nil

+ 2 - 4
daemon/images/image_events.go

@@ -21,12 +21,10 @@ func (i *ImageService) LogImageEvent(imageID, refName, action string) {
 	if refName != "" {
 		attributes["name"] = refName
 	}
-	actor := events.Actor{
+	i.eventsService.Log(action, events.ImageEventType, events.Actor{
 		ID:         imageID,
 		Attributes: attributes,
-	}
-
-	i.eventsService.Log(action, events.ImageEventType, actor)
+	})
 }
 
 // copyAttributes guarantees that labels are not mutated by event triggers.

+ 2 - 3
daemon/monitor.go

@@ -219,11 +219,10 @@ func (daemon *Daemon) ProcessEvent(id string, e libcontainerdtypes.EventType, ei
 				}()
 			}
 		}
-		attributes := map[string]string{
+		daemon.LogContainerEventWithAttributes(c, "exec_die", map[string]string{
 			"execID":   ei.ProcessID,
 			"exitCode": strconv.Itoa(exitCode),
-		}
-		daemon.LogContainerEventWithAttributes(c, "exec_die", attributes)
+		})
 	case libcontainerdtypes.EventStart:
 		c.Lock()
 		defer c.Unlock()

+ 2 - 3
daemon/resize.go

@@ -25,11 +25,10 @@ func (daemon *Daemon) ContainerResize(name string, height, width int) error {
 	}
 
 	if err = tsk.Resize(context.Background(), uint32(width), uint32(height)); err == nil {
-		attributes := map[string]string{
+		daemon.LogContainerEventWithAttributes(container, "resize", map[string]string{
 			"height": strconv.Itoa(height),
 			"width":  strconv.Itoa(width),
-		}
-		daemon.LogContainerEventWithAttributes(container, "resize", attributes)
+		})
 	}
 	return err
 }

+ 2 - 3
daemon/volumes_unix.go

@@ -73,14 +73,13 @@ func (daemon *Daemon) setupMounts(c *container.Container) ([]container.Mount, er
 				mnt.ReadOnlyForceRecursive = m.Spec.BindOptions.ReadOnlyForceRecursive
 			}
 			if m.Volume != nil {
-				attributes := map[string]string{
+				daemon.LogVolumeEvent(m.Volume.Name(), "mount", map[string]string{
 					"driver":      m.Volume.DriverName(),
 					"container":   c.ID,
 					"destination": m.Destination,
 					"read/write":  strconv.FormatBool(m.RW),
 					"propagation": string(m.Propagation),
-				}
-				daemon.LogVolumeEvent(m.Volume.Name(), "mount", attributes)
+				})
 			}
 			mounts = append(mounts, mnt)
 		}