reid 2 лет назад
Родитель
Сommit
a7abaa5e42
1 измененных файлов с 9 добавлено и 5 удалено
  1. 9 5
      docker/docker.go

+ 9 - 5
docker/docker.go

@@ -21,6 +21,7 @@ var (
 	EventBus = make(chan structs.Event, 100)
 )
 
+// return the container status of a slice of ships
 func GetShipStatus(patps []string) (map[string]string, error) {
 	statuses := make(map[string]string)
 	cli, err := client.NewClientWithOpts(client.FromEnv, client.WithAPIVersionNegotiation())
@@ -142,6 +143,7 @@ func StartContainer(containerName string, containerType string) (structs.Contain
 	desiredTag := containerInfo["tag"]
 	desiredHash := containerInfo["hash"]
 	desiredRepo := containerInfo["repo"]
+	desiredImage := fmt.Sprintf("%s:%s@sha256:%s", containerDetails.Config.Image, desiredTag, desiredHash)
 	if desiredTag == "" || desiredHash == "" {
 		err = fmt.Errorf("Version info has not been retrieved!")
 		return containerState, err
@@ -165,7 +167,7 @@ func StartContainer(containerName string, containerType string) (structs.Contain
 	}
 	if !imageExistsLocally {
 		// pull the image if it doesn't exist locally
-		_, err = cli.ImagePull(ctx, desiredRepo+":"+desiredTag, types.ImagePullOptions{})
+		_, err = cli.ImagePull(ctx, desiredImage, types.ImagePullOptions{})
 		if err != nil {
 			return containerState, err
 		}
@@ -183,7 +185,7 @@ func StartContainer(containerName string, containerType string) (structs.Contain
 		if err != nil {
 			return containerState, err
 		}
-		msg := fmt.Sprintf("%s started with image %s:%s", containerName, containerType, desiredTag)
+		msg := fmt.Sprintf("%s started with image %s", containerName, desiredImage)
 		logger.Info(msg)
 	case existingContainer.State == "exited":
 		// if the container exists but is stopped, start it
@@ -204,7 +206,7 @@ func StartContainer(containerName string, containerType string) (structs.Contain
 				return containerState, err
 			}
 			_, err = cli.ContainerCreate(ctx, &container.Config{
-				Image: containerType + ":" + desiredTag,
+				Image: desiredImage,
 			}, nil, nil, nil, containerName)
 			if err != nil {
 				return containerState, err
@@ -213,7 +215,7 @@ func StartContainer(containerName string, containerType string) (structs.Contain
 			if err != nil {
 				return containerState, err
 			}
-			msg := fmt.Sprintf("Restarted %s with image %s:%s", containerName, containerType, desiredTag)
+			msg := fmt.Sprintf("Restarted %s with image %s", containerName, desiredImage)
 			logger.Info(msg)
 		} else {
 			msg := fmt.Sprintf("%s is already running with the correct tag: %s", containerName, desiredTag)
@@ -227,7 +229,7 @@ func StartContainer(containerName string, containerType string) (structs.Contain
 	containerState = structs.ContainerState{
 		ID:        containerDetails.ID,
 		Name:      containerName,
-		Image:     fmt.Sprintf("%s:%s@sha256:%s", containerDetails.Config.Image, desiredTag, desiredHash),
+		Image:     desiredImage,
 		Status:    containerDetails.State.Status,
 		CreatedAt: containerDetails.Created,
 	}
@@ -312,6 +314,7 @@ func StopContainerByName(containerName string) error {
 	return fmt.Errorf("container with name %s not found", containerName)
 }
 
+// subscribe to docker events and feed them into eventbus
 func DockerListener() {
 	ctx := context.Background()
 	cli, err := client.NewClientWithOpts(client.FromEnv)
@@ -331,6 +334,7 @@ func DockerListener() {
 	}
 }
 
+// periodically poll docker in case we miss something
 func DockerPoller() {
 	ticker := time.NewTicker(10 * time.Second)
 	for {