This repository has been archived by the owner on Nov 16, 2020. It is now read-only.
/
builder.go
149 lines (124 loc) · 4.53 KB
/
builder.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
///////////////////////////////////////////////////////////////////////
// Copyright (c) 2017 VMware, Inc. All Rights Reserved.
// SPDX-License-Identifier: Apache-2.0
///////////////////////////////////////////////////////////////////////
package functions
import (
"bytes"
"compress/gzip"
"context"
"io"
"io/ioutil"
"os"
"strings"
"github.com/docker/docker/api/types"
"github.com/docker/docker/api/types/container"
docker "github.com/docker/docker/client"
"github.com/go-openapi/swag"
"github.com/pkg/errors"
log "github.com/sirupsen/logrus"
"github.com/vmware/dispatch/pkg/images"
"github.com/vmware/dispatch/pkg/trace"
"github.com/vmware/dispatch/pkg/utils"
)
// DockerImageBuilder builds function images
type DockerImageBuilder struct {
ImageRegistry string
RegistryAuth string
PushImages bool
PullImages bool
docker docker.CommonAPIClient
}
// NewDockerImageBuilder is the constructor for the DockerImageBuilder
func NewDockerImageBuilder(imageRegistry, registryAuth string, docker docker.CommonAPIClient) *DockerImageBuilder {
return &DockerImageBuilder{
ImageRegistry: imageRegistry,
RegistryAuth: registryAuth,
docker: docker,
PushImages: true,
PullImages: true,
}
}
const (
functionTemplateLabel = "io.dispatchframework.functionTemplate"
functionTemplateDirDefault = "/function-template"
)
func (ib *DockerImageBuilder) copyFunctionTemplate(tmpDir string, image string) error {
log.Debugf("Creating a container for image: %s", image)
resp, err := ib.docker.ContainerCreate(context.Background(), &container.Config{
Image: image,
}, nil, nil, "")
if err != nil {
return errors.Wrapf(err, "failed to create container for image '%s'", image)
}
defer ib.docker.ContainerRemove(context.Background(), resp.ID, types.ContainerRemoveOptions{})
ic, err := ib.docker.ContainerInspect(context.Background(), resp.ID)
if err != nil {
return errors.Wrapf(err, "failed to inspect image container, id='%s', image='%s'", resp.ID, image)
}
functionTemplateDir := ic.Config.Labels[functionTemplateLabel]
if functionTemplateDir == "" {
functionTemplateDir = functionTemplateDirDefault
}
readCloser, _, err := ib.docker.CopyFromContainer(context.Background(), resp.ID, functionTemplateDir)
defer readCloser.Close()
return utils.Untar(tmpDir, strings.TrimPrefix(functionTemplateDir, "/")+"/", readCloser)
}
// BuildImage packages a function into a docker image. It also adds any FaaS specfic image layers
func (ib *DockerImageBuilder) BuildImage(ctx context.Context, f *Function, code []byte) (string, error) {
span, ctx := trace.Trace(ctx, "")
defer span.Finish()
name := imageName(ib.ImageRegistry, f.FaasID)
log.Debugf("Building image '%s'", name)
tmpDir, err := ioutil.TempDir("", "func-build")
if err != nil {
return "", errors.Wrap(err, "failed to create a temp dir")
}
defer os.RemoveAll(tmpDir)
log.Debugf("Created tmpDir: %s", tmpDir)
if ib.PullImages {
opts := types.ImagePullOptions{RegistryAuth: ib.RegistryAuth}
if err := images.DockerError(ib.docker.ImagePull(ctx, f.ImageURL, opts)); err != nil {
return "", errors.Wrapf(err, "failed to pull image '%s'", f.ImageURL)
}
}
if err := writeSourceDir(tmpDir, code); err != nil {
return "", errors.Wrap(err, "failed to write dockerfile")
}
if err := ib.copyFunctionTemplate(tmpDir, f.ImageURL); err != nil {
return "", err
}
buildArgs := map[string]*string{
"IMAGE": swag.String(f.ImageURL),
"HANDLER": swag.String(f.Handler),
}
err = images.BuildAndPushFromDir(ctx, ib.docker, tmpDir, name, ib.RegistryAuth, ib.PushImages, buildArgs)
return name, err
}
func writeSourceDir(destDir string, code []byte) error {
r, err := tarStream(code)
if err != nil {
return errors.Wrapf(err, "failed to get the tar stream, writing source dir to '%s'", destDir)
}
err = utils.Untar(destDir, "/", r)
return errors.Wrapf(err, "failed to untar, writing source dir to '%s'", destDir)
}
func tarStream(bs []byte) (io.Reader, error) {
gr, err := gzip.NewReader(bytes.NewReader(bs))
if err != nil {
return nil, errors.Wrap(err, "failed to read gzip stream")
}
return gr, nil
}
func imageName(registry, fnID string) string {
return registry + "/func-" + fnID + ":latest"
}
// RemoveImage removes a function image from the docker host
func (ib *DockerImageBuilder) RemoveImage(ctx context.Context, f *Function) error {
span, ctx := trace.Trace(ctx, "")
defer span.Finish()
if err := images.Remove(ctx, ib.docker, f.FunctionImageURL); err != nil {
return errors.Wrapf(err, "failed to delete docker image for function %s", f.Name)
}
return nil
}