Skip to content

Commit

Permalink
Reduce duplication of pre-pull logic
Browse files Browse the repository at this point in the history
Signed-off-by: Alex Ellis (OpenFaaS Ltd) <[email protected]>
  • Loading branch information
alexellis committed Dec 31, 2020
1 parent c493613 commit 9c04b8d
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 19 deletions.
25 changes: 7 additions & 18 deletions pkg/provider/handlers/deploy.go
Original file line number Diff line number Diff line change
Expand Up @@ -72,11 +72,11 @@ func MakeDeployHandler(client *containerd.Client, cni gocni.CNI, secretMountPath
// prepull is an optimization which means an image can be pulled before a deployment
// request, since a deployment request first deletes the active function before
// trying to deploy a new one.
func prepull(ctx context.Context, req types.FunctionDeployment, client *containerd.Client, alwaysPull bool) error {
func prepull(ctx context.Context, req types.FunctionDeployment, client *containerd.Client, alwaysPull bool) (containerd.Image, error) {
start := time.Now()
r, err := reference.ParseNormalizedNamed(req.Image)
if err != nil {
return err
return nil, err
}

imgRef := reference.TagNameOnly(r).String()
Expand All @@ -88,38 +88,27 @@ func prepull(ctx context.Context, req types.FunctionDeployment, client *containe

image, err := service.PrepareImage(ctx, client, imgRef, snapshotter, alwaysPull)
if err != nil {
return errors.Wrapf(err, "unable to pull image %s", imgRef)
return nil, errors.Wrapf(err, "unable to pull image %s", imgRef)
}

size, _ := image.Size(ctx)
log.Printf("[Prepull] Deploy %s size: %d, took: %fs\n", image.Name(), size, time.Since(start).Seconds())
log.Printf("Image for: %s size: %d, took: %fs\n", image.Name(), size, time.Since(start).Seconds())

return nil
return image, nil
}

func deploy(ctx context.Context, req types.FunctionDeployment, client *containerd.Client, cni gocni.CNI, secretMountPath string, alwaysPull bool) error {
start := time.Now()

r, err := reference.ParseNormalizedNamed(req.Image)
if err != nil {
return err
}

imgRef := reference.TagNameOnly(r).String()

snapshotter := ""
if val, ok := os.LookupEnv("snapshotter"); ok {
snapshotter = val
}

image, err := service.PrepareImage(ctx, client, imgRef, snapshotter, alwaysPull)
image, err := prepull(ctx, req, client, alwaysPull)
if err != nil {
return errors.Wrapf(err, "unable to pull image %s", imgRef)
return err
}

size, _ := image.Size(ctx)
log.Printf("[deploy] Deploy %s size: %d, took: %fs\n", image.Name(), size, time.Since(start).Seconds())

envs := prepareEnv(req.EnvProcess, req.EnvVars)
mounts := getMounts()

Expand Down
2 changes: 1 addition & 1 deletion pkg/provider/handlers/update.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ func MakeUpdateHandler(client *containerd.Client, cni gocni.CNI, secretMountPath

ctx := namespaces.WithNamespace(context.Background(), faasd.FunctionNamespace)

if err := prepull(ctx, req, client, alwaysPull); err != nil {
if _, err := prepull(ctx, req, client, alwaysPull); err != nil {
log.Printf("[Update] error with pre-pull: %s, %s\n", name, err)
http.Error(w, err.Error(), http.StatusInternalServerError)
}
Expand Down

0 comments on commit 9c04b8d

Please sign in to comment.