Skip to content

Commit 9c04b8d

Browse files
committed
Reduce duplication of pre-pull logic
Signed-off-by: Alex Ellis (OpenFaaS Ltd) <[email protected]>
1 parent c493613 commit 9c04b8d

File tree

2 files changed

+8
-19
lines changed

2 files changed

+8
-19
lines changed

pkg/provider/handlers/deploy.go

Lines changed: 7 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -72,11 +72,11 @@ func MakeDeployHandler(client *containerd.Client, cni gocni.CNI, secretMountPath
7272
// prepull is an optimization which means an image can be pulled before a deployment
7373
// request, since a deployment request first deletes the active function before
7474
// trying to deploy a new one.
75-
func prepull(ctx context.Context, req types.FunctionDeployment, client *containerd.Client, alwaysPull bool) error {
75+
func prepull(ctx context.Context, req types.FunctionDeployment, client *containerd.Client, alwaysPull bool) (containerd.Image, error) {
7676
start := time.Now()
7777
r, err := reference.ParseNormalizedNamed(req.Image)
7878
if err != nil {
79-
return err
79+
return nil, err
8080
}
8181

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

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

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

97-
return nil
97+
return image, nil
9898
}
9999

100100
func deploy(ctx context.Context, req types.FunctionDeployment, client *containerd.Client, cni gocni.CNI, secretMountPath string, alwaysPull bool) error {
101-
start := time.Now()
102-
103-
r, err := reference.ParseNormalizedNamed(req.Image)
104-
if err != nil {
105-
return err
106-
}
107-
108-
imgRef := reference.TagNameOnly(r).String()
109101

110102
snapshotter := ""
111103
if val, ok := os.LookupEnv("snapshotter"); ok {
112104
snapshotter = val
113105
}
114106

115-
image, err := service.PrepareImage(ctx, client, imgRef, snapshotter, alwaysPull)
107+
image, err := prepull(ctx, req, client, alwaysPull)
116108
if err != nil {
117-
return errors.Wrapf(err, "unable to pull image %s", imgRef)
109+
return err
118110
}
119111

120-
size, _ := image.Size(ctx)
121-
log.Printf("[deploy] Deploy %s size: %d, took: %fs\n", image.Name(), size, time.Since(start).Seconds())
122-
123112
envs := prepareEnv(req.EnvProcess, req.EnvVars)
124113
mounts := getMounts()
125114

pkg/provider/handlers/update.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ func MakeUpdateHandler(client *containerd.Client, cni gocni.CNI, secretMountPath
5757

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

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

0 commit comments

Comments
 (0)