Skip to content

Commit

Permalink
Units pass
Browse files Browse the repository at this point in the history
Signed-off-by: Natalie Arellano <[email protected]>
  • Loading branch information
natalieparellano committed Sep 22, 2023
1 parent db7636c commit 7253994
Show file tree
Hide file tree
Showing 16 changed files with 431 additions and 406 deletions.
4 changes: 2 additions & 2 deletions cmd/lifecycle/analyzer.go
Original file line number Diff line number Diff line change
Expand Up @@ -96,15 +96,15 @@ func (a *analyzeCmd) Privileges() error {

// Exec executes the command.
func (a *analyzeCmd) Exec() error {
factory := lifecycle.NewAnalyzerFactory(
factory := lifecycle.NewConnectedFactory(
a.PlatformAPI,
&cmd.BuildpackAPIVerifier{},
NewCacheHandler(a.keychain),
lifecycle.NewConfigHandler(),
image.NewHandler(a.docker, a.keychain, a.LayoutDir, a.UseLayout, a.InsecureRegistries),
image.NewRegistryHandler(a.keychain, a.InsecureRegistries),
)
analyzer, err := factory.NewAnalyzer(a.AdditionalTags, a.CacheImageRef, a.LaunchCacheDir, a.LayersDir, a.OutputImageRef, a.PreviousImageRef, a.RunImageRef, a.SkipLayers, cmd.DefaultLogger)
analyzer, err := factory.NewAnalyzer(*a.LifecycleInputs, cmd.DefaultLogger)
if err != nil {
return unwrapErrorFailWithMessage(err, "initialize analyzer")
}
Expand Down
8 changes: 4 additions & 4 deletions cmd/lifecycle/creator.go
Original file line number Diff line number Diff line change
Expand Up @@ -123,15 +123,15 @@ func (c *createCmd) Exec() error {
plan files.Plan
)
cmd.DefaultLogger.Phase("ANALYZING")
analyzerFactory := lifecycle.NewAnalyzerFactory(
analyzerFactory := lifecycle.NewConnectedFactory(
c.PlatformAPI,
&cmd.BuildpackAPIVerifier{},
NewCacheHandler(c.keychain),
lifecycle.NewConfigHandler(),
image.NewHandler(c.docker, c.keychain, c.LayoutDir, c.UseLayout, c.InsecureRegistries),
image.NewRegistryHandler(c.keychain, c.InsecureRegistries),
)
analyzer, err := analyzerFactory.NewAnalyzer(c.AdditionalTags, c.CacheImageRef, c.LaunchCacheDir, c.LayersDir, c.OutputImageRef, c.PreviousImageRef, c.RunImageRef, c.SkipLayers, cmd.DefaultLogger)
analyzer, err := analyzerFactory.NewAnalyzer(*c.LifecycleInputs, cmd.DefaultLogger)
if err != nil {
return unwrapErrorFailWithMessage(err, "initialize analyzer")
}
Expand All @@ -142,13 +142,13 @@ func (c *createCmd) Exec() error {

// Detect
cmd.DefaultLogger.Phase("DETECTING")
detectorFactory := lifecycle.NewDetectorFactory(
detectorFactory := lifecycle.NewHermeticFactory(
c.PlatformAPI,
&cmd.BuildpackAPIVerifier{},
lifecycle.NewConfigHandler(),
dirStore,
)
detector, err := detectorFactory.NewDetector(analyzedMD, c.AppDir, c.BuildConfigDir, c.OrderPath, c.PlatformDir, cmd.DefaultLogger)
detector, err := detectorFactory.NewDetector(*c.LifecycleInputs, cmd.DefaultLogger)
if err != nil {
return unwrapErrorFailWithMessage(err, "initialize detector")
}
Expand Down
25 changes: 5 additions & 20 deletions cmd/lifecycle/detector.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,22 +60,14 @@ func (d *detectCmd) Privileges() error {

func (d *detectCmd) Exec() error {
dirStore := platform.NewDirStore(d.BuildpacksDir, d.ExtensionsDir)
detectorFactory := lifecycle.NewDetectorFactory(
detectorFactory := lifecycle.NewHermeticFactory(
d.PlatformAPI,
&cmd.BuildpackAPIVerifier{},
lifecycle.NewConfigHandler(),
dirStore,
)
amd, err := files.ReadAnalyzed(d.AnalyzedPath, cmd.DefaultLogger)
if err != nil {
return unwrapErrorFailWithMessage(err, "reading analyzed.toml")
}
detector, err := detectorFactory.NewDetector(
amd,
d.AppDir,
d.BuildConfigDir,
d.OrderPath,
d.PlatformDir,
*d.LifecycleInputs,
cmd.DefaultLogger,
)
if err != nil {
Expand All @@ -91,22 +83,15 @@ func (d *detectCmd) Exec() error {
return err // pass through error
}
if group.HasExtensions() {
generatorFactory := lifecycle.NewGeneratorFactory(
generatorFactory := lifecycle.NewHermeticFactory(
d.PlatformAPI,
&cmd.BuildpackAPIVerifier{},
lifecycle.Config,
dirStore,
)
var generator *lifecycle.Generator
generator, err = generatorFactory.NewGenerator(
d.AnalyzedPath,
d.AppDir,
d.BuildConfigDir,
group.GroupExtensions,
d.GeneratedDir,
plan,
d.PlatformAPI,
d.PlatformDir,
d.RunPath,
*d.LifecycleInputs,
cmd.Stdout, cmd.Stderr,
cmd.DefaultLogger,
)
Expand Down
12 changes: 2 additions & 10 deletions cmd/lifecycle/extender.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,22 +52,14 @@ func (e *extendCmd) Privileges() error {
}

func (e *extendCmd) Exec() error {
extenderFactory := lifecycle.NewExtenderFactory(&cmd.BuildpackAPIVerifier{}, lifecycle.NewConfigHandler())
extenderFactory := lifecycle.NewHermeticFactory(e.PlatformAPI, &cmd.BuildpackAPIVerifier{}, lifecycle.NewConfigHandler(), nil) // TODO: fix
applier, err := kaniko.NewDockerfileApplier()
if err != nil {
return err
}
extender, err := extenderFactory.NewExtender(
e.AnalyzedPath,
e.AppDir,
e.ExtendedDir,
e.GeneratedDir,
e.GroupPath,
e.LayersDir,
e.PlatformDir,
e.KanikoCacheTTL,
*e.LifecycleInputs,
applier,
e.ExtendKind,
cmd.DefaultLogger,
)
if err != nil {
Expand Down
102 changes: 10 additions & 92 deletions lifecycle/analyzer.go
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
// Package lifecycle TODO
package lifecycle

import (
"github.com/buildpacks/imgutil"
"github.com/pkg/errors"

"github.com/buildpacks/lifecycle/api"
"github.com/buildpacks/lifecycle/cache"
"github.com/buildpacks/lifecycle/image"
"github.com/buildpacks/lifecycle/internal/fsutil"
"github.com/buildpacks/lifecycle/internal/layer"
Expand All @@ -14,33 +14,7 @@ import (
"github.com/buildpacks/lifecycle/platform/files"
)

type AnalyzerFactory struct {
platformAPI *api.Version
apiVerifier BuildpackAPIVerifier
cacheHandler CacheHandler
configHandler ConfigHandler
imageHandler image.Handler
registryHandler image.RegistryHandler
}

func NewAnalyzerFactory(
platformAPI *api.Version,
apiVerifier BuildpackAPIVerifier,
cacheHandler CacheHandler,
configHandler ConfigHandler,
imageHandler image.Handler,
registryHandler image.RegistryHandler,
) *AnalyzerFactory {
return &AnalyzerFactory{
platformAPI: platformAPI,
apiVerifier: apiVerifier,
cacheHandler: cacheHandler,
configHandler: configHandler,
imageHandler: imageHandler,
registryHandler: registryHandler,
}
}

// Analyzer TODO
type Analyzer struct {
PreviousImage imgutil.Image
RunImage imgutil.Image
Expand All @@ -50,90 +24,34 @@ type Analyzer struct {
}

// NewAnalyzer configures a new Analyzer according to the provided Platform API version.
func (f *AnalyzerFactory) NewAnalyzer(additionalTags []string, cacheImageRef string, launchCacheDir string, layersDir string, outputImageRef string, previousImageRef string, runImageRef string, skipLayers bool, logger log.Logger) (*Analyzer, error) {
func (f *ConnectedFactory) NewAnalyzer(inputs platform.LifecycleInputs, logger log.Logger) (*Analyzer, error) {
analyzer := &Analyzer{
Logger: logger,
SBOMRestorer: &layer.NopSBOMRestorer{},
PlatformAPI: f.platformAPI,
}

if err := f.ensureRegistryAccess(additionalTags, cacheImageRef, outputImageRef, runImageRef, previousImageRef); err != nil {
if err := f.ensureRegistryAccess(inputs); err != nil {
return nil, err
}

if f.platformAPI.AtLeast("0.8") && !skipLayers {
analyzer.SBOMRestorer = &layer.DefaultSBOMRestorer{ // FIXME: eventually layer.NewSBOMRestorer should always return the default one, and then we can use the constructor
LayersDir: layersDir,
if f.platformAPI.AtLeast("0.8") && !inputs.SkipLayers {
analyzer.SBOMRestorer = &layer.DefaultSBOMRestorer{
LayersDir: inputs.LayersDir,
Logger: logger,
}
}

if err := f.setPrevious(analyzer, previousImageRef, launchCacheDir); err != nil {
var err error
if analyzer.PreviousImage, err = f.getPreviousImage(inputs.PreviousImageRef, inputs.LaunchCacheDir); err != nil {
return nil, err
}
if err := f.setRun(analyzer, runImageRef); err != nil {
if analyzer.RunImage, err = f.getRunImage(inputs.RunImageRef); err != nil {
return nil, err
}
return analyzer, nil
}

func (f *AnalyzerFactory) ensureRegistryAccess(
additionalTags []string,
cacheImageRef string,
outputImageRef string,
runImageRef string,
previousImageRef string,
) error {
var readImages, writeImages []string
writeImages = append(writeImages, cacheImageRef)
if f.imageHandler.Kind() == image.RemoteKind {
readImages = append(readImages, previousImageRef, runImageRef)
writeImages = append(writeImages, outputImageRef)
writeImages = append(writeImages, additionalTags...)
}

if err := f.registryHandler.EnsureReadAccess(readImages...); err != nil {
return errors.Wrap(err, "validating registry read access")
}
if err := f.registryHandler.EnsureWriteAccess(writeImages...); err != nil {
return errors.Wrap(err, "validating registry write access")
}
return nil
}

func (f *AnalyzerFactory) setPrevious(analyzer *Analyzer, imageRef string, launchCacheDir string) error {
if imageRef == "" {
return nil
}
var err error
analyzer.PreviousImage, err = f.imageHandler.InitImage(imageRef)
if err != nil {
return errors.Wrap(err, "getting previous image")
}
if launchCacheDir == "" || f.imageHandler.Kind() != image.LocalKind {
return nil
}

volumeCache, err := cache.NewVolumeCache(launchCacheDir)
if err != nil {
return errors.Wrap(err, "creating launch cache")
}
analyzer.PreviousImage = cache.NewCachingImage(analyzer.PreviousImage, volumeCache)
return nil
}

func (f *AnalyzerFactory) setRun(analyzer *Analyzer, imageRef string) error {
if imageRef == "" {
return nil
}
var err error
analyzer.RunImage, err = f.imageHandler.InitImage(imageRef)
if err != nil {
return errors.Wrap(err, "getting run image")
}
return nil
}

// Analyze fetches the layers metadata from the previous image and writes analyzed.toml.
func (a *Analyzer) Analyze() (files.Analyzed, error) {
defer log.NewMeasurement("Analyzer", a.Logger)()
Expand Down
Loading

0 comments on commit 7253994

Please sign in to comment.