From 9c04b8dfd7605813c013de7c4f88e68c052b84fa Mon Sep 17 00:00:00 2001 From: "Alex Ellis (OpenFaaS Ltd)" Date: Thu, 31 Dec 2020 18:54:23 +0000 Subject: [PATCH] Reduce duplication of pre-pull logic Signed-off-by: Alex Ellis (OpenFaaS Ltd) --- pkg/provider/handlers/deploy.go | 25 +++++++------------------ pkg/provider/handlers/update.go | 2 +- 2 files changed, 8 insertions(+), 19 deletions(-) diff --git a/pkg/provider/handlers/deploy.go b/pkg/provider/handlers/deploy.go index 1067f77..c8213ff 100644 --- a/pkg/provider/handlers/deploy.go +++ b/pkg/provider/handlers/deploy.go @@ -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() @@ -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() diff --git a/pkg/provider/handlers/update.go b/pkg/provider/handlers/update.go index 1d7a5f3..b32dea9 100644 --- a/pkg/provider/handlers/update.go +++ b/pkg/provider/handlers/update.go @@ -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) }