mirror of https://github.com/docker/compose.git
Merge pull request #380 from docker/remove-single-container-fixed-name
Relax single container name and replace by using tags
This commit is contained in:
commit
eb5965c962
|
@ -42,8 +42,9 @@ import (
|
||||||
)
|
)
|
||||||
|
|
||||||
const (
|
const (
|
||||||
singleContainerName = "single--container--aci"
|
singleContainerTag = "single--container--aci"
|
||||||
composeContainerSeparator = "_"
|
composeContainerSeparator = "_"
|
||||||
|
statusUnknown = "Unknown"
|
||||||
)
|
)
|
||||||
|
|
||||||
// ErrNoSuchContainer is returned when the mentioned container does not exist
|
// 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
|
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 {
|
for _, container := range *group.Containers {
|
||||||
var containerID string
|
var containerID string
|
||||||
// don't list sidecar container
|
// don't list sidecar container
|
||||||
if *container.Name == convert.ComposeDNSSidecarName {
|
if *container.Name == convert.ComposeDNSSidecarName {
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
if *container.Name == singleContainerName {
|
containerID = *containerGroup.Name + composeContainerSeparator + *container.Name
|
||||||
containerID = *containerGroup.Name
|
c := getContainer(containerID, group.IPAddress, container)
|
||||||
} else {
|
res = append(res, c)
|
||||||
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),
|
|
||||||
})
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return res, nil
|
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 {
|
func (cs *aciContainerService) Run(ctx context.Context, r containers.ContainerConfig) error {
|
||||||
if strings.Contains(r.ID, composeContainerSeparator) {
|
if strings.Contains(r.ID, composeContainerSeparator) {
|
||||||
return errors.New(fmt.Sprintf("invalid container name. ACI container name cannot include %q", 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 {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
@ -172,6 +183,11 @@ func (cs *aciContainerService) Run(ctx context.Context, r containers.ContainerCo
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if groupDefinition.Tags == nil {
|
||||||
|
groupDefinition.Tags = make(map[string]*string, 1)
|
||||||
|
}
|
||||||
|
groupDefinition.Tags[singleContainerTag] = to.StringPtr("")
|
||||||
|
|
||||||
return createACIContainers(ctx, cs.ctx, groupDefinition)
|
return createACIContainers(ctx, cs.ctx, groupDefinition)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -179,14 +195,13 @@ func (cs *aciContainerService) Stop(ctx context.Context, containerName string, t
|
||||||
return errdefs.ErrNotImplemented
|
return errdefs.ErrNotImplemented
|
||||||
}
|
}
|
||||||
|
|
||||||
func getGroupAndContainerName(containerID string) (groupName string, containerName string) {
|
func getGroupAndContainerName(containerID string) (string, string) {
|
||||||
tokens := strings.Split(containerID, composeContainerSeparator)
|
tokens := strings.Split(containerID, composeContainerSeparator)
|
||||||
groupName = tokens[0]
|
groupName := tokens[0]
|
||||||
|
containerName := groupName
|
||||||
if len(tokens) > 1 {
|
if len(tokens) > 1 {
|
||||||
containerName = tokens[len(tokens)-1]
|
containerName = tokens[len(tokens)-1]
|
||||||
groupName = containerID[:len(containerID)-(len(containerName)+1)]
|
groupName = containerID[:len(containerID)-(len(containerName)+1)]
|
||||||
} else {
|
|
||||||
containerName = singleContainerName
|
|
||||||
}
|
}
|
||||||
return groupName, containerName
|
return groupName, containerName
|
||||||
}
|
}
|
||||||
|
|
|
@ -34,7 +34,7 @@ type BackendSuiteTest struct {
|
||||||
func (suite *BackendSuiteTest) TestGetContainerName() {
|
func (suite *BackendSuiteTest) TestGetContainerName() {
|
||||||
group, container := getGroupAndContainerName("docker1234")
|
group, container := getGroupAndContainerName("docker1234")
|
||||||
Expect(group).To(Equal("docker1234"))
|
Expect(group).To(Equal("docker1234"))
|
||||||
Expect(container).To(Equal(singleContainerName))
|
Expect(container).To(Equal("docker1234"))
|
||||||
|
|
||||||
group, container = getGroupAndContainerName("compose_service1")
|
group, container = getGroupAndContainerName("compose_service1")
|
||||||
Expect(group).To(Equal("compose"))
|
Expect(group).To(Equal("compose"))
|
||||||
|
|
|
@ -10,7 +10,7 @@ import (
|
||||||
)
|
)
|
||||||
|
|
||||||
// ContainerToComposeProject convert container config to compose project
|
// 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
|
var ports []types.ServicePortConfig
|
||||||
for _, p := range r.Ports {
|
for _, p := range r.Ports {
|
||||||
ports = append(ports, types.ServicePortConfig{
|
ports = append(ports, types.ServicePortConfig{
|
||||||
|
@ -28,7 +28,7 @@ func ContainerToComposeProject(r containers.ContainerConfig, containerID string)
|
||||||
Name: r.ID,
|
Name: r.ID,
|
||||||
Services: []types.ServiceConfig{
|
Services: []types.ServiceConfig{
|
||||||
{
|
{
|
||||||
Name: containerID,
|
Name: r.ID,
|
||||||
Image: r.Image,
|
Image: r.Image,
|
||||||
Ports: ports,
|
Ports: ports,
|
||||||
Labels: r.Labels,
|
Labels: r.Labels,
|
||||||
|
|
|
@ -37,10 +37,10 @@ func (suite *ContainerConvertTestSuite) TestConvertContainerEnvironment() {
|
||||||
ID: "container1",
|
ID: "container1",
|
||||||
Environment: []string{"key1=value1", "key2", "key3=value3"},
|
Environment: []string{"key1=value1", "key2", "key3=value3"},
|
||||||
}
|
}
|
||||||
project, err := ContainerToComposeProject(container, "ID")
|
project, err := ContainerToComposeProject(container)
|
||||||
Expect(err).To(BeNil())
|
Expect(err).To(BeNil())
|
||||||
service1 := project.Services[0]
|
service1 := project.Services[0]
|
||||||
Expect(service1.Name).To(Equal("ID"))
|
Expect(service1.Name).To(Equal(container.ID))
|
||||||
Expect(service1.Environment).To(Equal(types.MappingWithEquals{
|
Expect(service1.Environment).To(Equal(types.MappingWithEquals{
|
||||||
"key1": to.StringPtr("value1"),
|
"key1": to.StringPtr("value1"),
|
||||||
"key2": nil,
|
"key2": nil,
|
||||||
|
|
Loading…
Reference in New Issue