Skip to content
Snippets Groups Projects
Unverified Commit cacd5618 authored by Steve Azzopardi's avatar Steve Azzopardi
Browse files

Extract createContainer params to struct

The method defenition is getting a bit long and hard to read, create a
new struct `containerRuntimeSpecification` that wraps those parameters
into a sturct. This is different form container config because this
specifies the runtime config.
parent e9e8db31
No related branches found
No related tags found
No related merge requests found
......@@ -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 {
......
......@@ -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
}
......
......@@ -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
}
......
......@@ -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")
}
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment