diff --git a/azure/backend.go b/azure/backend.go index efa8be10b..92eca993e 100644 --- a/azure/backend.go +++ b/azure/backend.go @@ -42,8 +42,9 @@ import ( ) const ( - singleContainerName = "single--container--aci" + singleContainerTag = "single--container--aci" composeContainerSeparator = "_" + statusUnknown = "Unknown" ) // ErrNoSuchContainer is returned when the mentioned container does not exist @@ -128,40 +129,50 @@ func (cs *aciContainerService) List(ctx context.Context, _ bool) ([]containers.C return []containers.Container{}, err } + if _, ok := group.Tags[singleContainerTag]; ok { + if group.Containers == nil || len(*group.Containers) < 1 { + return []containers.Container{}, fmt.Errorf("no containers to run") + } + container := (*group.Containers)[0] + c := getContainer(*containerGroup.Name, group.IPAddress, container) + res = append(res, c) + continue + } + for _, container := range *group.Containers { var containerID string // don't list sidecar container if *container.Name == convert.ComposeDNSSidecarName { continue } - if *container.Name == singleContainerName { - containerID = *containerGroup.Name - } else { - containerID = *containerGroup.Name + composeContainerSeparator + *container.Name - } - status := "Unknown" - if container.InstanceView != nil && container.InstanceView.CurrentState != nil { - status = *container.InstanceView.CurrentState.State - } - - res = append(res, containers.Container{ - ID: containerID, - Image: *container.Image, - Status: status, - Ports: convert.ToPorts(group.IPAddress, *container.Ports), - }) + containerID = *containerGroup.Name + composeContainerSeparator + *container.Name + c := getContainer(containerID, group.IPAddress, container) + res = append(res, c) } } return res, nil } +func getContainer(containerID string, ipAddress *containerinstance.IPAddress, container containerinstance.Container) containers.Container { + status := statusUnknown + if container.InstanceView != nil && container.InstanceView.CurrentState != nil { + status = *container.InstanceView.CurrentState.State + } + return containers.Container{ + ID: containerID, + Image: *container.Image, + Status: status, + Ports: convert.ToPorts(ipAddress, *container.Ports), + } +} + func (cs *aciContainerService) Run(ctx context.Context, r containers.ContainerConfig) error { if strings.Contains(r.ID, composeContainerSeparator) { return errors.New(fmt.Sprintf("invalid container name. ACI container name cannot include %q", composeContainerSeparator)) } - project, err := convert.ContainerToComposeProject(r, singleContainerName) + project, err := convert.ContainerToComposeProject(r) if err != nil { return err } @@ -172,6 +183,11 @@ func (cs *aciContainerService) Run(ctx context.Context, r containers.ContainerCo return err } + if groupDefinition.Tags == nil { + groupDefinition.Tags = make(map[string]*string, 1) + } + groupDefinition.Tags[singleContainerTag] = to.StringPtr("") + return createACIContainers(ctx, cs.ctx, groupDefinition) } @@ -179,14 +195,13 @@ func (cs *aciContainerService) Stop(ctx context.Context, containerName string, t return errdefs.ErrNotImplemented } -func getGroupAndContainerName(containerID string) (groupName string, containerName string) { +func getGroupAndContainerName(containerID string) (string, string) { tokens := strings.Split(containerID, composeContainerSeparator) - groupName = tokens[0] + groupName := tokens[0] + containerName := groupName if len(tokens) > 1 { containerName = tokens[len(tokens)-1] groupName = containerID[:len(containerID)-(len(containerName)+1)] - } else { - containerName = singleContainerName } return groupName, containerName } diff --git a/azure/backend_test.go b/azure/backend_test.go index 8092c8c52..022eac56c 100644 --- a/azure/backend_test.go +++ b/azure/backend_test.go @@ -34,7 +34,7 @@ type BackendSuiteTest struct { func (suite *BackendSuiteTest) TestGetContainerName() { group, container := getGroupAndContainerName("docker1234") Expect(group).To(Equal("docker1234")) - Expect(container).To(Equal(singleContainerName)) + Expect(container).To(Equal("docker1234")) group, container = getGroupAndContainerName("compose_service1") Expect(group).To(Equal("compose")) diff --git a/azure/convert/container.go b/azure/convert/container.go index 024f928c1..e684deddf 100644 --- a/azure/convert/container.go +++ b/azure/convert/container.go @@ -10,7 +10,7 @@ import ( ) // ContainerToComposeProject convert container config to compose project -func ContainerToComposeProject(r containers.ContainerConfig, containerID string) (types.Project, error) { +func ContainerToComposeProject(r containers.ContainerConfig) (types.Project, error) { var ports []types.ServicePortConfig for _, p := range r.Ports { ports = append(ports, types.ServicePortConfig{ @@ -28,7 +28,7 @@ func ContainerToComposeProject(r containers.ContainerConfig, containerID string) Name: r.ID, Services: []types.ServiceConfig{ { - Name: containerID, + Name: r.ID, Image: r.Image, Ports: ports, Labels: r.Labels, diff --git a/azure/convert/container_test.go b/azure/convert/container_test.go index e8220ffd6..8b05cf9f8 100644 --- a/azure/convert/container_test.go +++ b/azure/convert/container_test.go @@ -37,10 +37,10 @@ func (suite *ContainerConvertTestSuite) TestConvertContainerEnvironment() { ID: "container1", Environment: []string{"key1=value1", "key2", "key3=value3"}, } - project, err := ContainerToComposeProject(container, "ID") + project, err := ContainerToComposeProject(container) Expect(err).To(BeNil()) service1 := project.Services[0] - Expect(service1.Name).To(Equal("ID")) + Expect(service1.Name).To(Equal(container.ID)) Expect(service1.Environment).To(Equal(types.MappingWithEquals{ "key1": to.StringPtr("value1"), "key2": nil,