forked from buildpacks/lifecycle
-
Notifications
You must be signed in to change notification settings - Fork 0
/
rebaser.go
126 lines (98 loc) · 3.54 KB
/
rebaser.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
package lifecycle
import (
"encoding/json"
"fmt"
"sort"
"strings"
"github.com/buildpacks/imgutil"
"github.com/pkg/errors"
"github.com/buildpacks/lifecycle/api"
"github.com/buildpacks/lifecycle/platform"
)
type Rebaser struct {
Logger Logger
PlatformAPI *api.Version
}
type RebaseReport struct {
Image platform.ImageReport `toml:"image"`
}
func (r *Rebaser) Rebase(appImage imgutil.Image, newBaseImage imgutil.Image, additionalNames []string) (RebaseReport, error) {
var origMetadata platform.LayersMetadataCompat
if err := DecodeLabel(appImage, platform.LayerMetadataLabel, &origMetadata); err != nil {
return RebaseReport{}, errors.Wrap(err, "get image metadata")
}
appStackID, err := appImage.Label(platform.StackIDLabel)
if err != nil {
return RebaseReport{}, errors.Wrap(err, "get app image stack")
}
newBaseStackID, err := newBaseImage.Label(platform.StackIDLabel)
if err != nil {
return RebaseReport{}, errors.Wrap(err, "get new base image stack")
}
if appStackID == "" {
return RebaseReport{}, errors.New("stack not defined on app image")
}
if newBaseStackID == "" {
return RebaseReport{}, errors.New("stack not defined on new base image")
}
if appStackID != newBaseStackID {
return RebaseReport{}, errors.New(fmt.Sprintf("incompatible stack: '%s' is not compatible with '%s'", newBaseStackID, appStackID))
}
if err := validateMixins(appImage, newBaseImage); err != nil {
return RebaseReport{}, err
}
if err := appImage.Rebase(origMetadata.RunImage.TopLayer, newBaseImage); err != nil {
return RebaseReport{}, errors.Wrap(err, "rebase app image")
}
origMetadata.RunImage.TopLayer, err = newBaseImage.TopLayer()
if err != nil {
return RebaseReport{}, errors.Wrap(err, "get rebase run image top layer SHA")
}
identifier, err := newBaseImage.Identifier()
if err != nil {
return RebaseReport{}, errors.Wrap(err, "get run image id or digest")
}
origMetadata.RunImage.Reference = identifier.String()
data, err := json.Marshal(origMetadata)
if err != nil {
return RebaseReport{}, errors.Wrap(err, "marshall metadata")
}
if err := appImage.SetLabel(platform.LayerMetadataLabel, string(data)); err != nil {
return RebaseReport{}, errors.Wrap(err, "set app image metadata label")
}
hasPrefix := func(l string) bool { return strings.HasPrefix(l, "io.buildpacks.stack.") }
if err := syncLabels(newBaseImage, appImage, hasPrefix); err != nil {
return RebaseReport{}, errors.Wrap(err, "set stack labels")
}
report := RebaseReport{}
report.Image, err = saveImage(appImage, additionalNames, r.Logger)
if err != nil {
return RebaseReport{}, err
}
if !r.supportsManifestSize() {
// unset manifest size in report.toml for old platform API versions
report.Image.ManifestSize = 0
}
return report, err
}
func validateMixins(appImg, newBaseImg imgutil.Image) error {
var appImageMixins []string
var newBaseImageMixins []string
if err := DecodeLabel(appImg, platform.MixinsLabel, &appImageMixins); err != nil {
return errors.Wrap(err, "get app image mixins")
}
if err := DecodeLabel(newBaseImg, platform.MixinsLabel, &newBaseImageMixins); err != nil {
return errors.Wrap(err, "get run image mixins")
}
appImageMixins = removeStagePrefixes(appImageMixins)
newBaseImageMixins = removeStagePrefixes(newBaseImageMixins)
_, missing, _ := compare(newBaseImageMixins, appImageMixins)
if len(missing) > 0 {
sort.Strings(missing)
return fmt.Errorf("missing required mixin(s): %s", strings.Join(missing, ", "))
}
return nil
}
func (r *Rebaser) supportsManifestSize() bool {
return r.PlatformAPI.AtLeast("0.6")
}