diff --git a/executors/docker/executor_docker.go b/executors/docker/executor_docker.go index 7447b0ec71cb9b8b4dd70ab20c391623e07b4520..34a9fd90f2b509f0d8658c66726d734c0371205c 100644 --- a/executors/docker/executor_docker.go +++ b/executors/docker/executor_docker.go @@ -66,6 +66,13 @@ type executor struct { usedImagesLock sync.RWMutex } +type containerRuntimeSpecification struct { + containerType string + cmd []string + allowedInternalImages []string + imageDefinition common.Image +} + func init() { runnerFolder, err := osext.ExecutableFolder() if err != nil { @@ -835,8 +842,8 @@ func (e *executor) getValidContainers(containers []string) []string { return newContainers } -func (e *executor) createAttachableContainer(containerType string, imageDefinition common.Image, cmd []string, allowedInternalImages []string) (*types.ContainerJSON, error) { - config := e.newContainerConfig(cmd, containerType) +func (e *executor) createAttachableContainer(specs containerRuntimeSpecification) (*types.ContainerJSON, error) { + config := e.newContainerConfig(specs.cmd, specs.containerType) config.Tty = false config.AttachStdin = true config.AttachStdout = true @@ -844,11 +851,11 @@ func (e *executor) createAttachableContainer(containerType string, imageDefiniti config.OpenStdin = true config.StdinOnce = true - return e.createContainer(containerType, imageDefinition, allowedInternalImages, config) + return e.createContainer(specs, config) } -func (e *executor) createExecutableContainer(containerType string, imageDefinition common.Image, cmd []string, allowedInternalImages []string) (*types.ContainerJSON, error) { - config := e.newContainerConfig(cmd, containerType) +func (e *executor) createExecutableContainer(specs containerRuntimeSpecification) (*types.ContainerJSON, error) { + config := e.newContainerConfig(specs.cmd, specs.containerType) config.Tty = true config.AttachStdin = false config.AttachStdout = false @@ -856,7 +863,7 @@ func (e *executor) createExecutableContainer(containerType string, imageDefiniti config.OpenStdin = false config.StdinOnce = false - return e.createContainer(containerType, imageDefinition, allowedInternalImages, config) + return e.createContainer(specs, config) } func (e *executor) newContainerConfig(cmd []string, containerType string) *container.Config { @@ -867,8 +874,8 @@ func (e *executor) newContainerConfig(cmd []string, containerType string) *conta } } -func (e *executor) createContainer(containerType string, imageDefinition common.Image, allowedInternalImages []string, config *container.Config) (*types.ContainerJSON, error) { - image, err := e.expandAndGetDockerImage(imageDefinition.Name, allowedInternalImages) +func (e *executor) createContainer(specs containerRuntimeSpecification, config *container.Config) (*types.ContainerJSON, error) { + image, err := e.expandAndGetDockerImage(specs.imageDefinition.Name, specs.allowedInternalImages) if err != nil { return nil, err } @@ -882,9 +889,9 @@ func (e *executor) createContainer(containerType string, imageDefinition common. // Always create unique, but sequential name containerIndex := len(e.builds) containerName := e.Build.ProjectUniqueName() + "-" + - containerType + "-" + strconv.Itoa(containerIndex) + specs.containerType + "-" + strconv.Itoa(containerIndex) - config.Entrypoint = e.overwriteEntrypoint(&imageDefinition) + config.Entrypoint = e.overwriteEntrypoint(&specs.imageDefinition) nanoCPUs, err := e.Config.Docker.GetNanoCPUs() if err != nil { diff --git a/executors/docker/executor_docker_command.go b/executors/docker/executor_docker_command.go index a2074c7b34a5add03712d4c566ce7f7884560060..ebd530a72be1e5d5c1fd94d28ce8a87f851808fc 100644 --- a/executors/docker/executor_docker_command.go +++ b/executors/docker/executor_docker_command.go @@ -57,7 +57,12 @@ func (s *commandExecutor) requestNewPredefinedContainer() (*types.ContainerJSON, Name: prebuildImage.ID, } - containerJSON, err := s.createAttachableContainer("predefined", buildImage, common.ContainerCommandBuild, []string{prebuildImage.ID}) + containerJSON, err := s.createAttachableContainer(containerRuntimeSpecification{ + containerType: "predefined", + cmd: common.ContainerCommandBuild, + allowedInternalImages: []string{prebuildImage.ID}, + imageDefinition: buildImage, + }) if err != nil { return nil, err } @@ -73,15 +78,22 @@ func (s *commandExecutor) requestBuildContainer() (*types.ContainerJSON, error) return s.buildContainer, nil } + runtimeSpecs := containerRuntimeSpecification{ + containerType: "build", + cmd: s.BuildShell.DockerCommand, + allowedInternalImages: []string{}, + imageDefinition: s.Build.Image, + } + var err error switch s.Build.GetDockerStrategy() { case common.DockerAttach: - s.buildContainer, err = s.createAttachableContainer("build", s.Build.Image, s.BuildShell.DockerCommand, []string{}) + s.buildContainer, err = s.createAttachableContainer(runtimeSpecs) if err != nil { return nil, err } case common.DockerExec: - s.buildContainer, err = s.createExecutableContainer("build", s.Build.Image, s.BuildShell.DockerCommand, []string{}) + s.buildContainer, err = s.createExecutableContainer(runtimeSpecs) if err != nil { return nil, err } diff --git a/executors/docker/executor_docker_ssh.go b/executors/docker/executor_docker_ssh.go index d8a75fe9e0953f5d0fa6fb19bfb7968cdfe00749..13e3968b1b0566c5a20093eca217de4cb4292620 100644 --- a/executors/docker/executor_docker_ssh.go +++ b/executors/docker/executor_docker_ssh.go @@ -30,7 +30,12 @@ func (s *sshExecutor) Prepare(options common.ExecutorPrepareOptions) error { s.Debugln("Starting SSH command...") // Start build container which will run actual build - container, err := s.createAttachableContainer("build", s.Build.Image, []string{}, []string{}) + container, err := s.createAttachableContainer(containerRuntimeSpecification{ + containerType: "build", + cmd: []string{}, + allowedInternalImages: []string{}, + imageDefinition: s.Build.Image, + }) if err != nil { return err } diff --git a/executors/docker/executor_docker_test.go b/executors/docker/executor_docker_test.go index 20babda22907f3d739508a5c1a5623e1bc88bd16..851e20f898f192c3448d70fe060eb9192ad69dd9 100644 --- a/executors/docker/executor_docker_test.go +++ b/executors/docker/executor_docker_test.go @@ -887,7 +887,12 @@ func TestDockerWatchOn_1_12_4(t *testing.T) { err := e.connectDocker() assert.NoError(t, err) - container, err := e.createAttachableContainer("build", common.Image{Name: common.TestAlpineImage}, []string{"/bin/sh"}, []string{}) + container, err := e.createAttachableContainer(containerRuntimeSpecification{ + containerType: "build", + cmd: []string{"/bin/sh"}, + allowedInternalImages: []string{}, + imageDefinition: common.Image{Name: common.TestAlpineImage}, + }) assert.NoError(t, err) assert.NotNil(t, container) @@ -965,7 +970,12 @@ func testDockerConfigurationWithJobContainer(t *testing.T, dockerConfig *common. c.On("ContainerInspect", mock.Anything, "abc"). Return(types.ContainerJSON{}, nil).Once() - _, err := e.createAttachableContainer("build", common.Image{Name: "alpine"}, []string{"/bin/sh"}, []string{}) + _, err := e.createAttachableContainer(containerRuntimeSpecification{ + containerType: "build", + cmd: []string{"/bin/sh"}, + allowedInternalImages: []string{}, + imageDefinition: common.Image{Name: "alpine"}, + }) assert.NoError(t, err, "Should create container without errors") }