Relax single container name

Signed-off-by: Ulysses Souza <ulyssessouza@gmail.com>
This commit is contained in:
Ulysses Souza 2020-07-08 23:31:01 +02:00
parent 78cb0a87c7
commit 3b85582c38
4 changed files with 42 additions and 27 deletions

View File

@ -43,8 +43,9 @@ import (
)
const (
singleContainerName = "single--container--aci"
singleContainerTag = "single--container--aci"
composeContainerSeparator = "_"
statusUnknown = "Unknown"
)
// ErrNoSuchContainer is returned when the mentioned container does not exist
@ -129,40 +130,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
}
@ -173,6 +184,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)
}
@ -180,14 +196,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
}

View File

@ -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"))

View File

@ -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,

View File

@ -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,