Merge pull request #70 from docker/better_container_ids

ACI Container ids work fine between ps, log & exec, either from single container (docker run) of multi-container compose stack
This commit is contained in:
Guillaume Tardif 2020-05-07 12:19:00 +02:00 committed by GitHub
commit 90273ec801
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
33 changed files with 57 additions and 17 deletions

View File

@ -21,6 +21,8 @@ import (
"github.com/docker/api/context/store" "github.com/docker/api/context/store"
) )
const singleContainerName = "single--container--aci"
func init() { func init() {
backend.Register("aci", "aci", func(ctx context.Context) (backend.Service, error) { backend.Register("aci", "aci", func(ctx context.Context) (backend.Service, error) {
return New(ctx) return New(ctx)
@ -110,12 +112,18 @@ func (cs *aciContainerService) List(ctx context.Context) ([]containers.Container
} }
for _, container := range *group.Containers { for _, container := range *group.Containers {
var containerID string
if *container.Name == singleContainerName {
containerID = *containerGroup.Name
} else {
containerID = *containerGroup.Name + "_" + *container.Name
}
status := "Unknown" status := "Unknown"
if container.InstanceView != nil && container.InstanceView.CurrentState != nil { if container.InstanceView != nil && container.InstanceView.CurrentState != nil {
status = *container.InstanceView.CurrentState.State status = *container.InstanceView.CurrentState.State
} }
res = append(res, containers.Container{ res = append(res, containers.Container{
ID: *container.Name, ID: containerID,
Image: *container.Image, Image: *container.Image,
Status: status, Status: status,
}) })
@ -138,7 +146,7 @@ func (cs *aciContainerService) Run(ctx context.Context, r containers.ContainerCo
Config: types.Config{ Config: types.Config{
Services: []types.ServiceConfig{ Services: []types.ServiceConfig{
{ {
Name: r.ID, Name: singleContainerName,
Image: r.Image, Image: r.Image,
Ports: ports, Ports: ports,
}, },
@ -155,8 +163,21 @@ func (cs *aciContainerService) Run(ctx context.Context, r containers.ContainerCo
return createACIContainers(ctx, cs.ctx, groupDefinition) 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 { 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 { if err != nil {
return err 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 { 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 { if err != nil {
return err 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) logrus.Debugf("Up on project with name %q\n", project.Name)
groupDefinition, err := convert.ToContainerGroup(cs.ctx, *project) groupDefinition, err := convert.ToContainerGroup(cs.ctx, *project)
if err != nil { if err != nil {
return err return err
} }

24
azure/backend_test.go Normal file
View File

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

View File

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

View File

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

View File

Before

Width:  |  Height:  |  Size: 11 KiB

After

Width:  |  Height:  |  Size: 11 KiB

View File

Before

Width:  |  Height:  |  Size: 67 KiB

After

Width:  |  Height:  |  Size: 67 KiB

View File

Before

Width:  |  Height:  |  Size: 15 KiB

After

Width:  |  Height:  |  Size: 15 KiB

View File

Before

Width:  |  Height:  |  Size: 14 KiB

After

Width:  |  Height:  |  Size: 14 KiB

View File

Before

Width:  |  Height:  |  Size: 85 KiB

After

Width:  |  Height:  |  Size: 85 KiB

View File

Before

Width:  |  Height:  |  Size: 1.5 KiB

After

Width:  |  Height:  |  Size: 1.5 KiB

View File

Before

Width:  |  Height:  |  Size: 1.2 KiB

After

Width:  |  Height:  |  Size: 1.2 KiB

View File

Before

Width:  |  Height:  |  Size: 1.2 KiB

After

Width:  |  Height:  |  Size: 1.2 KiB

View File

Before

Width:  |  Height:  |  Size: 10 KiB

After

Width:  |  Height:  |  Size: 10 KiB