From d544832bf88a1c2b5fdbb5991a52d2b628b4c388 Mon Sep 17 00:00:00 2001 From: Guiheux Steven Date: Wed, 15 Feb 2023 10:39:08 +0100 Subject: [PATCH] feat: always pull image (#6455) --- engine/hatchery/kubernetes/kubernetes.go | 7 +-- engine/hatchery/swarm/swarm_util_create.go | 59 +++++++--------------- 2 files changed, 18 insertions(+), 48 deletions(-) diff --git a/engine/hatchery/kubernetes/kubernetes.go b/engine/hatchery/kubernetes/kubernetes.go index 8033801a54..e066cac442 100644 --- a/engine/hatchery/kubernetes/kubernetes.go +++ b/engine/hatchery/kubernetes/kubernetes.go @@ -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 { @@ -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}, diff --git a/engine/hatchery/swarm/swarm_util_create.go b/engine/hatchery/swarm/swarm_util_create.go index a4928529af..d5fde77289 100644 --- a/engine/hatchery/swarm/swarm_util_create.go +++ b/engine/hatchery/swarm/swarm_util_create.go @@ -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" @@ -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() @@ -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) @@ -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) @@ -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 @@ -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") }