forked from buildpacks/pack
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcreate_builder.go
329 lines (271 loc) · 9.97 KB
/
create_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
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
package pack
import (
"context"
"fmt"
"github.com/buildpacks/pack/config"
"github.com/Masterminds/semver"
"github.com/buildpacks/imgutil"
"github.com/pkg/errors"
pubbldr "github.com/buildpacks/pack/builder"
"github.com/buildpacks/pack/internal/builder"
"github.com/buildpacks/pack/internal/buildpack"
"github.com/buildpacks/pack/internal/dist"
"github.com/buildpacks/pack/internal/image"
"github.com/buildpacks/pack/internal/paths"
"github.com/buildpacks/pack/internal/style"
)
// CreateBuilderOptions is a configuration object used to change the behavior of
// CreateBuilder.
type CreateBuilderOptions struct {
// The base directory to use to resolve relative assets
RelativeBaseDir string
// Name of the builder.
BuilderName string
// Configuration that defines the functionality a builder provides.
Config pubbldr.Config
// Skip building image locally, directly publish to a registry.
// Requires BuilderName to be a valid registry location.
Publish bool
// Buildpack registry name. Defines where all registry buildpacks will be pulled from.
Registry string
// Strategy for updating images before a build.
PullPolicy config.PullPolicy
}
// CreateBuilder creates and saves a builder image to a registry with the provided options.
// If any configuration is invalid, it will error and exit without creating any images.
func (c *Client) CreateBuilder(ctx context.Context, opts CreateBuilderOptions) error {
if err := c.validateConfig(ctx, opts); err != nil {
return err
}
bldr, err := c.createBaseBuilder(ctx, opts)
if err != nil {
return errors.Wrap(err, "failed to create builder")
}
if err := c.addBuildpacksToBuilder(ctx, opts, bldr); err != nil {
return errors.Wrap(err, "failed to add buildpacks to builder")
}
bldr.SetOrder(opts.Config.Order)
bldr.SetStack(opts.Config.Stack)
return bldr.Save(c.logger, builder.CreatorMetadata{Version: Version})
}
func (c *Client) validateConfig(ctx context.Context, opts CreateBuilderOptions) error {
if err := pubbldr.ValidateConfig(opts.Config); err != nil {
return errors.Wrap(err, "invalid builder config")
}
if err := c.validateRunImageConfig(ctx, opts); err != nil {
return errors.Wrap(err, "invalid run image config")
}
return nil
}
func (c *Client) validateRunImageConfig(ctx context.Context, opts CreateBuilderOptions) error {
var runImages []imgutil.Image
for _, i := range append([]string{opts.Config.Stack.RunImage}, opts.Config.Stack.RunImageMirrors...) {
if !opts.Publish {
img, err := c.imageFetcher.Fetch(ctx, i, true, opts.PullPolicy)
if err != nil {
if errors.Cause(err) != image.ErrNotFound {
return errors.Wrap(err, "failed to fetch image")
}
} else {
runImages = append(runImages, img)
continue
}
}
img, err := c.imageFetcher.Fetch(ctx, i, false, opts.PullPolicy)
if err != nil {
if errors.Cause(err) != image.ErrNotFound {
return errors.Wrap(err, "failed to fetch image")
}
c.logger.Warnf("run image %s is not accessible", style.Symbol(i))
} else {
runImages = append(runImages, img)
}
}
for _, img := range runImages {
stackID, err := img.Label("io.buildpacks.stack.id")
if err != nil {
return errors.Wrap(err, "failed to label image")
}
if stackID != opts.Config.Stack.ID {
return fmt.Errorf(
"stack %s from builder config is incompatible with stack %s from run image %s",
style.Symbol(opts.Config.Stack.ID),
style.Symbol(stackID),
style.Symbol(img.Name()),
)
}
}
return nil
}
func (c *Client) createBaseBuilder(ctx context.Context, opts CreateBuilderOptions) (*builder.Builder, error) {
baseImage, err := c.imageFetcher.Fetch(ctx, opts.Config.Stack.BuildImage, !opts.Publish, opts.PullPolicy)
if err != nil {
return nil, errors.Wrap(err, "fetch build image")
}
c.logger.Debugf("Creating builder %s from build-image %s", style.Symbol(opts.BuilderName), style.Symbol(baseImage.Name()))
bldr, err := builder.New(baseImage, opts.BuilderName)
if err != nil {
return nil, errors.Wrap(err, "invalid build-image")
}
os, err := baseImage.OS()
if err != nil {
return nil, errors.Wrap(err, "lookup image OS")
}
if os == "windows" && !c.experimental {
return nil, NewExperimentError("Windows containers support is currently experimental.")
}
bldr.SetDescription(opts.Config.Description)
if bldr.StackID != opts.Config.Stack.ID {
return nil, fmt.Errorf(
"stack %s from builder config is incompatible with stack %s from build image",
style.Symbol(opts.Config.Stack.ID),
style.Symbol(bldr.StackID),
)
}
lifecycle, err := c.fetchLifecycle(ctx, opts.Config.Lifecycle, opts.RelativeBaseDir, os)
if err != nil {
return nil, errors.Wrap(err, "fetch lifecycle")
}
bldr.SetLifecycle(lifecycle)
return bldr, nil
}
func (c *Client) fetchLifecycle(ctx context.Context, config pubbldr.LifecycleConfig, relativeBaseDir, os string) (builder.Lifecycle, error) {
if config.Version != "" && config.URI != "" {
return nil, errors.Errorf(
"%s can only declare %s or %s, not both",
style.Symbol("lifecycle"), style.Symbol("version"), style.Symbol("uri"),
)
}
var uri string
var err error
switch {
case config.Version != "":
v, err := semver.NewVersion(config.Version)
if err != nil {
return nil, errors.Wrapf(err, "%s must be a valid semver", style.Symbol("lifecycle.version"))
}
uri = uriFromLifecycleVersion(*v, os)
case config.URI != "":
uri, err = paths.FilePathToURI(config.URI, relativeBaseDir)
if err != nil {
return nil, err
}
default:
uri = uriFromLifecycleVersion(*semver.MustParse(builder.DefaultLifecycleVersion), os)
}
blob, err := c.downloader.Download(ctx, uri)
if err != nil {
return nil, errors.Wrap(err, "downloading lifecycle")
}
lifecycle, err := builder.NewLifecycle(blob)
if err != nil {
return nil, errors.Wrap(err, "invalid lifecycle")
}
return lifecycle, nil
}
func (c *Client) addBuildpacksToBuilder(ctx context.Context, opts CreateBuilderOptions, bldr *builder.Builder) error {
for _, b := range opts.Config.Buildpacks {
c.logger.Debugf("Looking up buildpack %s", style.Symbol(b.DisplayString()))
var err error
var locatorType buildpack.LocatorType
if b.URI == "" && b.ImageName != "" {
c.logger.Warn("The 'image' key is deprecated. Use 'uri=\"docker://...\"' instead.")
b.URI = b.ImageName
locatorType = buildpack.PackageLocator
} else {
locatorType, err = buildpack.GetLocatorType(b.URI, opts.RelativeBaseDir, []dist.BuildpackInfo{})
if err != nil {
return err
}
}
var mainBP dist.Buildpack
var depBPs []dist.Buildpack
switch locatorType {
case buildpack.PackageLocator:
imageName := buildpack.ParsePackageLocator(b.URI)
c.logger.Debugf("Downloading buildpack from image: %s", style.Symbol(imageName))
mainBP, depBPs, err = extractPackagedBuildpacks(ctx, imageName, c.imageFetcher, opts.Publish, opts.PullPolicy)
if err != nil {
return err
}
case buildpack.RegistryLocator:
c.logger.Debugf("Downloading buildpack from registry: %s", style.Symbol(b.URI))
registryCache, err := c.getRegistry(c.logger, opts.Registry)
if err != nil {
return errors.Wrapf(err, "invalid registry '%s'", opts.Registry)
}
registryBp, err := registryCache.LocateBuildpack(b.URI)
if err != nil {
return errors.Wrapf(err, "locating in registry %s", style.Symbol(b.URI))
}
mainBP, depBPs, err = extractPackagedBuildpacks(ctx, registryBp.Address, c.imageFetcher, opts.Publish, opts.PullPolicy)
if err != nil {
return errors.Wrapf(err, "extracting from registry %s", style.Symbol(b.URI))
}
case buildpack.URILocator:
b.URI, err = paths.FilePathToURI(b.URI, opts.RelativeBaseDir)
if err != nil {
return errors.Wrapf(err, "making absolute: %s", style.Symbol(b.URI))
}
c.logger.Debugf("Downloading buildpack from URI: %s", style.Symbol(b.URI))
err = ensureBPSupport(b.URI)
if err != nil {
return err
}
blob, err := c.downloader.Download(ctx, b.URI)
if err != nil {
return errors.Wrapf(err, "downloading buildpack from %s", style.Symbol(b.URI))
}
imageOS, err := bldr.Image().OS()
if err != nil {
return errors.Wrapf(err, "getting OS from %s", style.Symbol(bldr.Image().Name()))
}
mainBP, depBPs, err = decomposeBuildpack(blob, imageOS)
if err != nil {
return errors.Wrapf(err, "extracting from %s", style.Symbol(b.URI))
}
default:
return fmt.Errorf("error reading %s: invalid locator: %s", b.URI, locatorType)
}
err = validateBuildpack(mainBP, b.URI, b.ID, b.Version)
if err != nil {
return errors.Wrap(err, "invalid buildpack")
}
bpDesc := mainBP.Descriptor()
for _, deprecatedAPI := range bldr.LifecycleDescriptor().APIs.Buildpack.Deprecated {
if deprecatedAPI.Equal(bpDesc.API) {
c.logger.Warnf("Buildpack %s is using deprecated Buildpacks API version %s", style.Symbol(bpDesc.Info.FullName()), style.Symbol(bpDesc.API.String()))
break
}
}
for _, bp := range append([]dist.Buildpack{mainBP}, depBPs...) {
bldr.AddBuildpack(bp)
}
}
return nil
}
func validateBuildpack(bp dist.Buildpack, source, expectedID, expectedBPVersion string) error {
if expectedID != "" && bp.Descriptor().Info.ID != expectedID {
return fmt.Errorf(
"buildpack from URI %s has ID %s which does not match ID %s from builder config",
style.Symbol(source),
style.Symbol(bp.Descriptor().Info.ID),
style.Symbol(expectedID),
)
}
if expectedBPVersion != "" && bp.Descriptor().Info.Version != expectedBPVersion {
return fmt.Errorf(
"buildpack from URI %s has version %s which does not match version %s from builder config",
style.Symbol(source),
style.Symbol(bp.Descriptor().Info.Version),
style.Symbol(expectedBPVersion),
)
}
return nil
}
func uriFromLifecycleVersion(version semver.Version, os string) string {
if os == "windows" {
return fmt.Sprintf("https://github.com/buildpacks/lifecycle/releases/download/v%s/lifecycle-v%s+windows.x86-64.tgz", version.String(), version.String())
}
return fmt.Sprintf("https://github.com/buildpacks/lifecycle/releases/download/v%s/lifecycle-v%s+linux.x86-64.tgz", version.String(), version.String())
}