-
Notifications
You must be signed in to change notification settings - Fork 165
/
builder_builder.go
422 lines (357 loc) · 10.2 KB
/
builder_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
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
package cnb
import (
"archive/tar"
"bytes"
"io"
"sort"
"strings"
"time"
"github.com/BurntSushi/toml"
"github.com/Masterminds/semver/v3"
"github.com/buildpacks/imgutil/layer"
v1 "github.com/google/go-containerregistry/pkg/v1"
"github.com/google/go-containerregistry/pkg/v1/mutate"
"github.com/google/go-containerregistry/pkg/v1/tarball"
"github.com/pkg/errors"
buildapi "github.com/pivotal/kpack/pkg/apis/build/v1alpha2"
corev1alpha1 "github.com/pivotal/kpack/pkg/apis/core/v1alpha1"
"github.com/pivotal/kpack/pkg/registry/imagehelpers"
)
const (
workspaceDir = "/workspace"
layersDir = "/layers"
cnbDir = "/cnb"
platformDir = "/platform"
platformEnvDir = platformDir + "/env"
buildpacksDir = "/cnb/buildpacks"
orderTomlPath = "/cnb/order.toml"
stackTomlPath = "/cnb/stack.toml"
relaxedMixinMinPlatformAPI = "0.7"
)
var (
normalizedTime = time.Date(1980, time.January, 1, 0, 0, 1, 0, time.UTC)
supportedPlatformApis = []string{"0.3", "0.4", "0.5", "0.6", "0.7", "0.8"}
)
type builderBlder struct {
baseImage v1.Image
lifecycleLayer v1.Layer
LifecycleMetadata LifecycleMetadata
stackId string
order []corev1alpha1.OrderEntry
buildpackLayers map[DescriptiveBuildpackInfo]buildpackLayer
cnbUserId int
cnbGroupId int
kpackVersion string
runImage string
mixins []string
os string
additionalLabels map[string]string
}
func newBuilderBldr(kpackVersion string) *builderBlder {
return &builderBlder{
buildpackLayers: map[DescriptiveBuildpackInfo]buildpackLayer{},
kpackVersion: kpackVersion,
}
}
func (bb *builderBlder) AddStack(baseImage v1.Image, clusterStack *buildapi.ClusterStack) error {
file, err := baseImage.ConfigFile()
if err != nil {
return err
}
bb.os = file.OS
bb.baseImage = baseImage
bb.stackId = clusterStack.Status.Id
bb.mixins = clusterStack.Status.Mixins
bb.cnbUserId = clusterStack.Status.UserID
bb.cnbGroupId = clusterStack.Status.GroupID
return nil
}
func (bb *builderBlder) AddLifecycle(lifecycleLayer v1.Layer, lifecycleMetadata LifecycleMetadata) {
bb.lifecycleLayer = lifecycleLayer
bb.LifecycleMetadata = lifecycleMetadata
}
func (bb *builderBlder) AddGroup(buildpacks ...RemoteBuildpackRef) {
group := make([]corev1alpha1.BuildpackRef, 0, len(buildpacks))
for _, b := range buildpacks {
group = append(group, b.buildpackRef())
for _, layer := range b.Layers {
bb.buildpackLayers[layer.BuildpackInfo] = layer
}
}
bb.order = append(bb.order, corev1alpha1.OrderEntry{Group: group})
}
func (bb *builderBlder) AddAdditionalLabels(additionalLabels map[string]string) {
bb.additionalLabels = additionalLabels
}
func (bb *builderBlder) WriteableImage() (v1.Image, error) {
buildpacks := bb.buildpacks()
err := bb.validateBuilder(buildpacks)
if err != nil {
return nil, err
}
buildpackLayerMetadata := BuildpackLayerMetadata{}
buildpackLayers := make([]v1.Layer, 0, len(bb.buildpackLayers))
for _, key := range buildpacks {
layer := bb.buildpackLayers[key]
buildpackLayerMetadata.add(layer)
buildpackLayers = append(buildpackLayers, layer.v1Layer)
}
defaultLayer, err := bb.defaultDirsLayer()
if err != nil {
return nil, err
}
stackLayer, err := bb.stackLayer()
if err != nil {
return nil, err
}
orderLayer, err := bb.orderLayer()
if err != nil {
return nil, err
}
image, err := mutate.AppendLayers(bb.baseImage,
deduplicateLayers(layers(
[]v1.Layer{
defaultLayer,
bb.lifecycleLayer,
},
buildpackLayers,
[]v1.Layer{
stackLayer,
orderLayer,
},
))...)
if err != nil {
return nil, err
}
image, err = imagehelpers.SetWorkingDir(image, layersDir)
if err != nil {
return nil, err
}
image, err = imagehelpers.SetStringLabel(image, lifecycleVersionLabel, bb.LifecycleMetadata.Version)
if err != nil {
return nil, err
}
image, err = imagehelpers.SetStringLabels(image, bb.additionalLabels)
if err != nil {
return nil, err
}
return imagehelpers.SetLabels(image, map[string]interface{}{
buildpackOrderLabel: bb.order,
buildpackLayersLabel: buildpackLayerMetadata,
lifecycleApisLabel: bb.LifecycleMetadata.APIs,
buildpackMetadataLabel: BuilderImageMetadata{
Description: "Custom Builder built with kpack",
Stack: StackMetadata{
RunImage: RunImageMetadata{
Image: bb.runImage,
Mirrors: nil,
},
},
Lifecycle: bb.LifecycleMetadata,
CreatedBy: CreatorMetadata{
Name: "kpack Builder",
Version: bb.kpackVersion,
},
Buildpacks: buildpacks,
},
})
}
func (bb *builderBlder) AddRunImage(runImage string) {
bb.runImage = runImage
}
func (bb *builderBlder) validateBuilder(sortedBuildpacks []DescriptiveBuildpackInfo) error {
platformApis := append(bb.LifecycleMetadata.APIs.Platform.Deprecated, bb.LifecycleMetadata.APIs.Platform.Supported...)
err := validatePlatformApis(platformApis)
if err != nil {
return err
}
buildpackApis := append(bb.LifecycleMetadata.APIs.Buildpack.Deprecated, bb.LifecycleMetadata.APIs.Buildpack.Supported...)
for _, bpInfo := range sortedBuildpacks {
bpLayerInfo := bb.buildpackLayers[bpInfo].BuildpackLayerInfo
err := bpLayerInfo.supports(buildpackApis, bb.stackId, bb.mixins, relaxedMixinContract(platformApis))
if err != nil {
return errors.Wrapf(err, "validating buildpack %s", bpInfo)
}
}
return nil
}
func validatePlatformApis(builderSupportedApis []string) error {
for _, api := range supportedPlatformApis {
if present(builderSupportedApis, api) {
return nil
}
}
return errors.Errorf("unsupported platform apis in kpack lifecycle: %s, expecting one of: %s", strings.Join(builderSupportedApis, ", "), strings.Join(supportedPlatformApis, ", "))
}
func relaxedMixinContract(builderSupportedApis []string) bool {
for _, api := range builderSupportedApis {
if semver.MustParse(api).Compare(semver.MustParse(relaxedMixinMinPlatformAPI)) >= 0 {
return true
}
}
return false
}
func (bb *builderBlder) buildpacks() []DescriptiveBuildpackInfo {
return deterministicSortBySize(bb.buildpackLayers)
}
func (bb *builderBlder) stackLayer() (v1.Layer, error) {
type tomlRunImage struct {
Image string `toml:"image"`
}
type tomlStackFile struct {
RunImage tomlRunImage `toml:"run-image"`
}
stackBuf := &bytes.Buffer{}
stackFile := tomlStackFile{
RunImage: tomlRunImage{
Image: bb.runImage,
},
}
err := toml.NewEncoder(stackBuf).Encode(stackFile)
if err != nil {
return nil, err
}
return bb.singeFileLayer(stackTomlPath, stackBuf.Bytes())
}
func (bb *builderBlder) orderLayer() (v1.Layer, error) {
type tomlBuildpack struct {
ID string `toml:"id"`
Version string `toml:"version"`
Optional bool `toml:"optional,omitempty"`
}
type tomlOrderEntry struct {
Group []tomlBuildpack `toml:"group"`
}
type tomlOrder []tomlOrderEntry
type tomlOrderFile struct {
Order tomlOrder `toml:"order"`
}
orderBuf := &bytes.Buffer{}
order := make(tomlOrder, 0, len(bb.order))
for _, o := range bb.order {
bps := make([]tomlBuildpack, 0, len(o.Group))
for _, b := range o.Group {
bps = append(bps, tomlBuildpack{
ID: b.Id,
Version: b.Version,
Optional: b.Optional,
})
}
order = append(order, tomlOrderEntry{Group: bps})
}
err := toml.NewEncoder(orderBuf).Encode(tomlOrderFile{order})
if err != nil {
return nil, err
}
return bb.singeFileLayer(orderTomlPath, orderBuf.Bytes())
}
func (bb *builderBlder) singeFileLayer(file string, contents []byte) (v1.Layer, error) {
b := &bytes.Buffer{}
w := bb.layerWriter(b)
if err := w.WriteHeader(&tar.Header{
Name: file,
Size: int64(len(contents)),
Mode: 0644,
ModTime: normalizedTime,
}); err != nil {
return nil, err
}
if _, err := w.Write(contents); err != nil {
return nil, err
}
if err := w.Close(); err != nil {
return nil, err
}
return tarball.LayerFromReader(b)
}
func (bb *builderBlder) defaultDirsLayer() (v1.Layer, error) {
dirs := []*tar.Header{
bb.kpackOwnedDir(workspaceDir),
bb.kpackOwnedDir(layersDir),
bb.rootOwnedDir(cnbDir),
bb.rootOwnedDir(buildpacksDir),
bb.rootOwnedDir(platformDir),
bb.rootOwnedDir(platformEnvDir),
}
b := &bytes.Buffer{}
tw := bb.layerWriter(b)
for _, header := range dirs {
if err := tw.WriteHeader(header); err != nil {
return nil, errors.Wrapf(err, "creating %s dir in layer", header.Name)
}
}
if err := tw.Close(); err != nil {
return nil, err
}
return tarball.LayerFromReader(b)
}
func (bb *builderBlder) kpackOwnedDir(path string) *tar.Header {
return &tar.Header{
Typeflag: tar.TypeDir,
Name: path,
Mode: 0755,
ModTime: normalizedTime,
Uid: bb.cnbUserId,
Gid: bb.cnbGroupId,
}
}
func (bb *builderBlder) rootOwnedDir(path string) *tar.Header {
return &tar.Header{
Typeflag: tar.TypeDir,
Name: path,
Mode: 0755,
ModTime: normalizedTime,
}
}
func (bb *builderBlder) layerWriter(fileWriter io.Writer) layerWriter {
if bb.os == "windows" {
return layer.NewWindowsWriter(fileWriter)
}
return tar.NewWriter(fileWriter)
}
type layerWriter interface {
WriteHeader(hdr *tar.Header) error
Write(b []byte) (int, error)
Close() error
}
func deterministicSortBySize(layers map[DescriptiveBuildpackInfo]buildpackLayer) []DescriptiveBuildpackInfo {
keys := make([]DescriptiveBuildpackInfo, 0, len(layers))
sizes := make(map[DescriptiveBuildpackInfo]int64, len(layers))
for k, layer := range layers {
keys = append(keys, k)
size, _ := layer.v1Layer.Size()
sizes[k] = size
}
sort.Slice(keys, func(i, j int) bool {
sizeI := sizes[keys[i]]
sizeJ := sizes[keys[j]]
if sizeI == sizeJ {
return keys[i].String() > keys[j].String()
}
return sizeI > sizeJ
})
return keys
}
func layers(layers ...[]v1.Layer) []v1.Layer {
var appendedLayers []v1.Layer
for _, l := range layers {
appendedLayers = append(appendedLayers, l...)
}
return appendedLayers
}
func deduplicateLayers(layers []v1.Layer) []v1.Layer {
layerMap := map[v1.Hash]struct{}{}
res := make([]v1.Layer, 0)
for _, l := range layers {
diffId, err := l.DiffID()
if err != nil {
res = append(res, l)
continue
}
if _, ok := layerMap[diffId]; !ok {
res = append(res, l)
layerMap[diffId] = struct{}{}
}
}
return res
}