Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(hatchery): always pull image #6455

Merged
merged 1 commit into from
Feb 15, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 1 addition & 6 deletions engine/hatchery/kubernetes/kubernetes.go
Original file line number Diff line number Diff line change
Expand Up @@ -265,11 +265,6 @@ func (h *HatcheryKubernetes) SpawnWorker(ctx context.Context, spawnArgs hatchery
i++
}

pullPolicy := "IfNotPresent"
if strings.HasSuffix(spawnArgs.Model.ModelDocker.Image, ":latest") {
pullPolicy = "Always"
}

// Create secret for worker config
configSecretName, err := h.createConfigSecret(ctx, workerConfig)
if err != nil {
Expand Down Expand Up @@ -307,7 +302,7 @@ func (h *HatcheryKubernetes) SpawnWorker(ctx context.Context, spawnArgs hatchery
{
Name: spawnArgs.WorkerName,
Image: spawnArgs.Model.ModelDocker.Image,
ImagePullPolicy: apiv1.PullPolicy(pullPolicy),
ImagePullPolicy: apiv1.PullAlways,
Env: envs,
Command: strings.Fields(spawnArgs.Model.ModelDocker.Shell),
Args: []string{cmd},
Expand Down
59 changes: 17 additions & 42 deletions engine/hatchery/swarm/swarm_util_create.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import (
"regexp"
"strings"

types "github.com/docker/docker/api/types"
"github.com/docker/docker/api/types"
"github.com/docker/docker/api/types/container"
"github.com/docker/docker/api/types/mount"
"github.com/docker/docker/api/types/network"
Expand All @@ -19,7 +19,7 @@ import (
"github.com/ovh/cds/sdk/telemetry"
)

//create the docker bridge
// create the docker bridge
func (h *HatcherySwarm) createNetwork(ctx context.Context, dockerClient *dockerClient, name string) error {
ctx, end := telemetry.Span(ctx, "swarm.createNetwork", telemetry.Tag("network", name))
defer end()
Expand Down Expand Up @@ -49,7 +49,7 @@ type containerArgs struct {
entryPoint strslice.StrSlice
}

//shortcut to create+start(=run) a container
// shortcut to create+start(=run) a container
func (h *HatcherySwarm) createAndStartContainer(ctx context.Context, dockerClient *dockerClient, cArgs containerArgs, spawnArgs hatchery.SpawnArguments) error {
if spawnArgs.Model == nil {
return sdk.WithStack(sdk.ErrNotFound)
Expand Down Expand Up @@ -100,47 +100,22 @@ func (h *HatcherySwarm) createAndStartContainer(ctx context.Context, dockerClien
}
}

_, next := telemetry.Span(ctx, "swarm.dockerClient.ImageList")
// Check the images to know if we had to pull or not
images, errl := dockerClient.ImageList(ctx, types.ImageListOptions{All: true})
if errl != nil {
log.Warn(ctx, "unable to list images: %s", errl)
}
next()
hatchery.SendSpawnInfo(ctx, h, spawnArgs.JobID, sdk.SpawnMsgNew(*sdk.MsgSpawnInfoHatcheryStartDockerPull, h.Name(), cArgs.image))

var imageFound bool
checkImage:
for _, img := range images {
for _, t := range img.RepoTags {
if cArgs.image == t {
imageFound = true
break checkImage
}
}
}

if strings.HasSuffix(cArgs.image, ":latest") {
imageFound = false
}

if !imageFound {
hatchery.SendSpawnInfo(ctx, h, spawnArgs.JobID, sdk.SpawnMsgNew(*sdk.MsgSpawnInfoHatcheryStartDockerPull, h.Name(), cArgs.image))

_, next := telemetry.Span(ctx, "swarm.dockerClient.pullImage", telemetry.Tag("image", cArgs.image))
if err := h.pullImage(dockerClient,
cArgs.image,
timeoutPullImage,
*spawnArgs.Model); err != nil {
next()

spawnMsg := sdk.SpawnMsgNew(*sdk.MsgSpawnInfoHatcheryEndDockerPullErr, h.Name(), cArgs.image, sdk.Cause(err))
hatchery.SendSpawnInfo(ctx, h, spawnArgs.JobID, spawnMsg)
return sdk.WrapError(err, "unable to pull image %s on %s", cArgs.image, dockerClient.name)
}
_, next := telemetry.Span(ctx, "swarm.dockerClient.pullImage", telemetry.Tag("image", cArgs.image))
if err := h.pullImage(dockerClient,
cArgs.image,
timeoutPullImage,
*spawnArgs.Model); err != nil {
next()

hatchery.SendSpawnInfo(ctx, h, spawnArgs.JobID, sdk.SpawnMsgNew(*sdk.MsgSpawnInfoHatcheryEndDockerPull, h.Name(), cArgs.image))
spawnMsg := sdk.SpawnMsgNew(*sdk.MsgSpawnInfoHatcheryEndDockerPullErr, h.Name(), cArgs.image, sdk.Cause(err))
hatchery.SendSpawnInfo(ctx, h, spawnArgs.JobID, spawnMsg)
return sdk.WrapError(err, "unable to pull image %s on %s", cArgs.image, dockerClient.name)
}
next()

hatchery.SendSpawnInfo(ctx, h, spawnArgs.JobID, sdk.SpawnMsgNew(*sdk.MsgSpawnInfoHatcheryEndDockerPull, h.Name(), cArgs.image))

_, next = telemetry.Span(ctx, "swarm.dockerClient.ContainerCreate", telemetry.Tag(telemetry.TagWorker, cArgs.name), telemetry.Tag("network", fmt.Sprintf("%v", networkingConfig)))
c, err := dockerClient.ContainerCreate(ctx, config, hostConfig, networkingConfig, name)
Expand Down Expand Up @@ -217,7 +192,7 @@ func (h *HatcherySwarm) computeDockerOptsOnModelRequirement(d *dockerOpts, req s
} else if opt == "--privileged" {
d.privileged = true
} else {
return fmt.Errorf("Options not supported: %s", opt)
return fmt.Errorf("options not supported: %s", opt)
}
}
return nil
Expand Down Expand Up @@ -252,5 +227,5 @@ func (d *dockerOpts) computeDockerOptsPorts(arg string) error {
nat.PortBinding{HostIP: "0.0.0.0", HostPort: s[1]})
return nil // no error
}
return fmt.Errorf("Wrong format of ports arguments. Example: --port=8081:8182/tcp")
return fmt.Errorf("wrong format of ports arguments. Example: --port=8081:8182/tcp")
}