浏览代码

ACI Container ids work fine between ps, log & exec, either from single container (docker run) of multi-container compose stack

Guillaume Tardif 5 年之前
父节点
当前提交
13db3437fd
共有 4 个文件被更改,包括 57 次插入17 次删除
  1. 27 4
      azure/backend.go
  2. 24 0
      azure/backend_test.go
  3. 0 5
      azure/convert/convert.go
  4. 6 8
      tests/aci-e2e/e2e-aci.go

+ 27 - 4
azure/backend.go

@@ -21,6 +21,8 @@ import (
 	"github.com/docker/api/context/store"
 )
 
+const singleContainerName = "single--container--aci"
+
 func init() {
 	backend.Register("aci", "aci", func(ctx context.Context) (backend.Service, error) {
 		return New(ctx)
@@ -110,12 +112,18 @@ func (cs *aciContainerService) List(ctx context.Context) ([]containers.Container
 		}
 
 		for _, container := range *group.Containers {
+			var containerID string
+			if *container.Name == singleContainerName {
+				containerID = *containerGroup.Name
+			} else {
+				containerID = *containerGroup.Name + "_" + *container.Name
+			}
 			status := "Unknown"
 			if container.InstanceView != nil && container.InstanceView.CurrentState != nil {
 				status = *container.InstanceView.CurrentState.State
 			}
 			res = append(res, containers.Container{
-				ID:     *container.Name,
+				ID:     containerID,
 				Image:  *container.Image,
 				Status: status,
 			})
@@ -138,7 +146,7 @@ func (cs *aciContainerService) Run(ctx context.Context, r containers.ContainerCo
 		Config: types.Config{
 			Services: []types.ServiceConfig{
 				{
-					Name:  r.ID,
+					Name:  singleContainerName,
 					Image: r.Image,
 					Ports: ports,
 				},
@@ -155,8 +163,21 @@ func (cs *aciContainerService) Run(ctx context.Context, r containers.ContainerCo
 	return createACIContainers(ctx, cs.ctx, groupDefinition)
 }
 
+func getGrouNameContainername(containerID string) (groupName string, containerName string) {
+	tokens := strings.Split(containerID, "_")
+	groupName = tokens[0]
+	if len(tokens) > 1 {
+		containerName = tokens[len(tokens)-1]
+		groupName = containerID[:len(containerID)-(len(containerName)+1)]
+	} else {
+		containerName = singleContainerName
+	}
+	return groupName, containerName
+}
+
 func (cs *aciContainerService) Exec(ctx context.Context, name string, command string, reader io.Reader, writer io.Writer) error {
-	containerExecResponse, err := execACIContainer(ctx, cs.ctx, command, name, name)
+	groupName, containerAciName := getGrouNameContainername(name)
+	containerExecResponse, err := execACIContainer(ctx, cs.ctx, command, groupName, containerAciName)
 	if err != nil {
 		return err
 	}
@@ -171,7 +192,8 @@ func (cs *aciContainerService) Exec(ctx context.Context, name string, command st
 }
 
 func (cs *aciContainerService) Logs(ctx context.Context, containerName string, req containers.LogsRequest) error {
-	logs, err := getACIContainerLogs(ctx, cs.ctx, containerName, containerName)
+	groupName, containerAciName := getGrouNameContainername(containerName)
+	logs, err := getACIContainerLogs(ctx, cs.ctx, groupName, containerAciName)
 	if err != nil {
 		return err
 	}
@@ -204,6 +226,7 @@ func (cs *aciComposeService) Up(ctx context.Context, opts compose.ProjectOptions
 	}
 	logrus.Debugf("Up on project with name %q\n", project.Name)
 	groupDefinition, err := convert.ToContainerGroup(cs.ctx, *project)
+
 	if err != nil {
 		return err
 	}

+ 24 - 0
azure/backend_test.go

@@ -0,0 +1,24 @@
+package azure
+
+import (
+	"testing"
+
+	. "github.com/onsi/gomega"
+)
+
+// TestGetContainerName ensures we can read container group name / container name from a containerID
+func TestGetContainerName(t *testing.T) {
+	RegisterTestingT(t)
+
+	group, container := getGrouNameContainername("docker1234")
+	Expect(group).To(Equal("docker1234"))
+	Expect(container).To(Equal(singleContainerName))
+
+	group, container = getGrouNameContainername("compose_service1")
+	Expect(group).To(Equal("compose"))
+	Expect(container).To(Equal("service1"))
+
+	group, container = getGrouNameContainername("compose_stack_service1")
+	Expect(group).To(Equal("compose_stack"))
+	Expect(container).To(Equal("service1"))
+}

+ 0 - 5
azure/convert/convert.go

@@ -12,7 +12,6 @@ import (
 	"github.com/compose-spec/compose-go/types"
 	"github.com/docker/api/compose"
 	"github.com/docker/api/context/store"
-	"github.com/sirupsen/logrus"
 )
 
 const (
@@ -20,7 +19,6 @@ const (
 	volumeDriveroptsShareNameKey   = "share_name"
 	volumeDriveroptsAccountNameKey = "storage_account_name"
 	volumeDriveroptsAccountKeyKey  = "storage_account_key"
-	singleContainerName            = "single--container--aci"
 	secretInlineMark               = "inline:"
 )
 
@@ -56,9 +54,6 @@ func ToContainerGroup(aciContext store.AciContext, p compose.Project) (container
 
 	for _, s := range project.Services {
 		service := serviceConfigAciHelper(s)
-		if s.Name != singleContainerName {
-			logrus.Debugf("Adding %q\n", service.Name)
-		}
 		containerDefinition, err := service.getAciContainer(volumesCache)
 		if err != nil {
 			return containerinstance.ContainerGroup{}, err

+ 6 - 8
tests/aci-e2e/e2e-aci.go

@@ -89,7 +89,7 @@ func main() {
 	*/
 
 	It("deploys a compose app", func() {
-		NewDockerCommand("compose", "up", "-f", "./composefiles/aci-demo/aci_demo_port.yaml").ExecOrDie()
+		NewDockerCommand("compose", "up", "-f", "./composefiles/aci-demo/aci_demo_port.yaml", "--name", "acidemo").ExecOrDie()
 		//Expect(output).To(ContainSubstring("Successfully deployed"))
 		output := NewDockerCommand("ps").ExecOrDie()
 		Lines := Lines(output)
@@ -115,15 +115,13 @@ func main() {
 		*/
 	})
 
-	/*
-		It("get logs from web service", func() {
-			output := NewDockerCommand("logs", "aci-demo_web").ExecOrDie()
-			Expect(output).To(ContainSubstring("Listening on port 80"))
-		})
-	*/
+	It("get logs from web service", func() {
+		output := NewDockerCommand("logs", "acidemo_web").ExecOrDie()
+		Expect(output).To(ContainSubstring("Listening on port 80"))
+	})
 
 	It("shutdown compose app", func() {
-		NewDockerCommand("compose", "down", "-f", "./composefiles/aci-demo/aci_demo_port.yaml").ExecOrDie()
+		NewDockerCommand("compose", "down", "-f", "./composefiles/aci-demo/aci_demo_port.yaml", "--name", "acidemo").ExecOrDie()
 	})
 	It("switches back to default context", func() {
 		output := NewCommand("docker", "context", "use", "default").ExecOrDie()