forked from buildpacks/lifecycle
-
Notifications
You must be signed in to change notification settings - Fork 0
/
generator.go
261 lines (236 loc) · 8.1 KB
/
generator.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
package lifecycle
import (
"fmt"
"io"
"os"
"path/filepath"
"github.com/buildpacks/lifecycle/api"
"github.com/buildpacks/lifecycle/platform"
"github.com/buildpacks/lifecycle/buildpack"
"github.com/buildpacks/lifecycle/env"
"github.com/buildpacks/lifecycle/internal/fsutil"
"github.com/buildpacks/lifecycle/launch"
"github.com/buildpacks/lifecycle/log"
"github.com/buildpacks/lifecycle/platform/files"
)
type Generator struct {
AppDir string
BuildConfigDir string
GeneratedDir string // e.g., <layers>/generated
PlatformAPI *api.Version
PlatformDir string
AnalyzedMD files.Analyzed
DirStore DirStore
Executor buildpack.GenerateExecutor
Extensions []buildpack.GroupElement
Logger log.Logger
Out, Err io.Writer
Plan files.Plan
RunMetadata files.Run
}
type GeneratorFactory struct {
apiVerifier BuildpackAPIVerifier
configHandler ConfigHandler
dirStore DirStore
}
func NewGeneratorFactory(
apiVerifier BuildpackAPIVerifier,
configHandler ConfigHandler,
dirStore DirStore,
) *GeneratorFactory {
return &GeneratorFactory{
apiVerifier: apiVerifier,
configHandler: configHandler,
dirStore: dirStore,
}
}
func (f *GeneratorFactory) NewGenerator(
analyzedPath string,
appDir string,
buildConfigDir string,
extensions []buildpack.GroupElement,
generatedDir string,
plan files.Plan,
platformAPI *api.Version,
platformDir string,
runPath string,
stdout, stderr io.Writer,
logger log.Logger,
) (*Generator, error) {
generator := &Generator{
AppDir: appDir,
BuildConfigDir: buildConfigDir,
GeneratedDir: generatedDir,
PlatformAPI: platformAPI,
PlatformDir: platformDir,
DirStore: f.dirStore,
Executor: &buildpack.DefaultGenerateExecutor{},
Logger: logger,
Plan: plan,
Out: stdout,
Err: stderr,
}
if err := f.setExtensions(generator, extensions, logger); err != nil {
return nil, err
}
if err := f.setAnalyzedMD(generator, analyzedPath, logger); err != nil {
return nil, err
}
if err := f.setRunMD(generator, runPath, logger); err != nil {
return nil, err
}
return generator, nil
}
func (f *GeneratorFactory) setExtensions(generator *Generator, extensions []buildpack.GroupElement, logger log.Logger) error {
generator.Extensions = extensions
for _, el := range generator.Extensions {
if err := f.apiVerifier.VerifyBuildpackAPI(buildpack.KindExtension, el.String(), el.API, logger); err != nil {
return err
}
}
return nil
}
func (f *GeneratorFactory) setAnalyzedMD(generator *Generator, analyzedPath string, logger log.Logger) error {
var err error
generator.AnalyzedMD, err = f.configHandler.ReadAnalyzed(analyzedPath, logger)
return err
}
func (f *GeneratorFactory) setRunMD(generator *Generator, runPath string, logger log.Logger) error {
var err error
generator.RunMetadata, err = f.configHandler.ReadRun(runPath, logger)
return err
}
type GenerateResult struct {
AnalyzedMD files.Analyzed
Plan files.Plan
UsePlan bool
}
func (g *Generator) Generate() (GenerateResult, error) {
defer log.NewMeasurement("Generator", g.Logger)()
inputs := g.getGenerateInputs()
extensionOutputParentDir, err := os.MkdirTemp("", "cnb-extensions-generated.")
if err != nil {
return GenerateResult{}, err
}
defer os.RemoveAll(extensionOutputParentDir)
inputs.OutputDir = extensionOutputParentDir
var dockerfiles []buildpack.DockerfileInfo
filteredPlan := g.Plan
for _, ext := range g.Extensions {
g.Logger.Debugf("Running generate for extension %s", ext)
g.Logger.Debug("Looking up extension")
descriptor, err := g.DirStore.LookupExt(ext.ID, ext.Version)
if err != nil {
return GenerateResult{}, err
}
g.Logger.Debug("Finding plan")
inputs.Plan = filteredPlan.Find(buildpack.KindExtension, ext.ID)
if g.AnalyzedMD.RunImage != nil && g.AnalyzedMD.RunImage.TargetMetadata != nil && g.PlatformAPI.AtLeast("0.12") {
inputs.Env = env.NewBuildEnv(append(inputs.Env.List(), platform.EnvVarsFor(*g.AnalyzedMD.RunImage.TargetMetadata)...))
}
g.Logger.Debug("Invoking command")
result, err := g.Executor.Generate(*descriptor, inputs, g.Logger)
if err != nil {
return GenerateResult{}, err
}
// aggregate build results
dockerfiles = append(dockerfiles, result.Dockerfiles...)
filteredPlan = filteredPlan.Filter(result.MetRequires)
g.Logger.Debugf("Finished running generate for extension %s", ext)
}
g.Logger.Debug("Checking run image")
finalAnalyzedMD := g.AnalyzedMD
generatedRunImageRef, extend := g.runImageFrom(dockerfiles)
if generatedRunImageRef != "" && g.isNew(generatedRunImageRef) {
if !g.RunMetadata.Contains(generatedRunImageRef) {
g.Logger.Warnf("new runtime base image '%s' not found in run metadata", generatedRunImageRef)
}
g.Logger.Debugf("Updating analyzed metadata with new run image '%s'", generatedRunImageRef)
finalAnalyzedMD.RunImage = &files.RunImage{ // reference and target data are cleared
Extend: extend,
Image: generatedRunImageRef,
}
}
if extend {
if finalAnalyzedMD.RunImage != nil { // sanity check to prevent panic
g.Logger.Debug("Updating analyzed metadata to indicate run image extension")
finalAnalyzedMD.RunImage.Extend = true
}
}
g.Logger.Debug("Copying Dockerfiles")
if err = g.copyDockerfiles(dockerfiles); err != nil {
return GenerateResult{}, err
}
return GenerateResult{
AnalyzedMD: finalAnalyzedMD,
Plan: filteredPlan,
UsePlan: true,
}, nil
}
func (g *Generator) getGenerateInputs() buildpack.GenerateInputs {
return buildpack.GenerateInputs{
AppDir: g.AppDir,
BuildConfigDir: g.BuildConfigDir,
PlatformDir: g.PlatformDir,
Env: env.NewBuildEnv(os.Environ()),
Out: g.Out,
Err: g.Err,
}
}
func (g *Generator) copyDockerfiles(dockerfiles []buildpack.DockerfileInfo) error {
for _, dockerfile := range dockerfiles {
targetDir := filepath.Join(g.GeneratedDir, dockerfile.Kind, launch.EscapeID(dockerfile.ExtensionID))
var targetPath = filepath.Join(targetDir, "Dockerfile")
if dockerfile.Kind == buildpack.DockerfileKindRun && dockerfile.Ignore {
targetPath += ".ignore"
}
if err := os.MkdirAll(targetDir, os.ModePerm); err != nil {
return err
}
g.Logger.Debugf("Copying %s to %s", dockerfile.Path, targetPath)
if err := fsutil.Copy(dockerfile.Path, targetPath); err != nil {
return fmt.Errorf("failed to copy Dockerfile at %s: %w", dockerfile.Path, err)
}
// check for extend-config.toml and if found, copy
extendConfigPath := filepath.Join(filepath.Dir(dockerfile.Path), "extend-config.toml")
if err := fsutil.Copy(extendConfigPath, filepath.Join(targetDir, "extend-config.toml")); err != nil {
if !os.IsNotExist(err) {
return fmt.Errorf("failed to copy extend config at %s: %w", extendConfigPath, err)
}
}
}
return nil
}
func (g *Generator) runImageFrom(dockerfiles []buildpack.DockerfileInfo) (newBase string, extend bool) {
var ignoreNext bool
for i := len(dockerfiles) - 1; i >= 0; i-- {
// There may be extensions that contribute only a build.Dockerfile;
// work backward through extensions until we find a run.Dockerfile.
if dockerfiles[i].Kind != buildpack.DockerfileKindRun {
continue
}
if ignoreNext {
// If a run.Dockerfile following this one (in the build, not in the loop) switches the run image,
// we can ignore this run.Dockerfile as it has no effect.
// We set Ignore to true so that when the Dockerfiles are copied to the "generated" directory,
// we'll add the suffix `.ignore` so that the extender won't try to apply them.
dockerfiles[i].Ignore = true
continue
}
if dockerfiles[i].Extend {
extend = true
}
if dockerfiles[i].WithBase != "" {
newBase = dockerfiles[i].WithBase
g.Logger.Debugf("Found a run.Dockerfile from extension '%s' setting run image to '%s' ", dockerfiles[i].ExtensionID, newBase)
ignoreNext = true
}
}
return newBase, extend
}
func (g *Generator) isNew(ref string) bool {
if g.PlatformAPI.AtLeast("0.12") {
return ref != g.AnalyzedMD.RunImageImage() // don't use `name.ParseMaybe` as this will strip the digest, and we want to use exactly what the extension author wrote
}
return ref != ""
}