Skip to content

Commit

Permalink
Move lifecycle package to sub-directory (#1205)
Browse files Browse the repository at this point in the history
* Move files

Signed-off-by: Natalie Arellano <[email protected]>

* Fix imports

Signed-off-by: Natalie Arellano <[email protected]>

* Update mockgen directives

Signed-off-by: Natalie Arellano <[email protected]>

* WIP: add TODOs

Signed-off-by: Natalie Arellano <[email protected]>

* Rename lifecycle -> phase

Signed-off-by: Natalie Arellano <[email protected]>

* Address TODOs by adding comments

Signed-off-by: Natalie Arellano <[email protected]>

* Update phase/handlers.go

Signed-off-by: Natalie Arellano <[email protected]>

---------

Signed-off-by: Natalie Arellano <[email protected]>
  • Loading branch information
natalieparellano authored Sep 26, 2023
1 parent 7ffcd58 commit 6c7d16a
Show file tree
Hide file tree
Showing 168 changed files with 322 additions and 239 deletions.
12 changes: 6 additions & 6 deletions acceptance/restorer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,9 @@ import (
"github.com/sclevine/spec"
"github.com/sclevine/spec/report"

"github.com/buildpacks/lifecycle"
"github.com/buildpacks/lifecycle/api"
"github.com/buildpacks/lifecycle/cmd"
"github.com/buildpacks/lifecycle/phase"
h "github.com/buildpacks/lifecycle/testhelpers"
)

Expand Down Expand Up @@ -198,7 +198,7 @@ func testRestorerFunc(platformAPI string) func(t *testing.T, when spec.G, it spe
h.WithArgs("-build-image", restoreRegFixtures.SomeCacheImage), // some-cache-image simulates a builder image in a registry
)
t.Log("records builder image digest in analyzed.toml")
analyzedMD, err := lifecycle.Config.ReadAnalyzed(filepath.Join(copyDir, "layers", "analyzed.toml"), cmd.DefaultLogger)
analyzedMD, err := phase.Config.ReadAnalyzed(filepath.Join(copyDir, "layers", "analyzed.toml"), cmd.DefaultLogger)
h.AssertNil(t, err)
h.AssertStringContains(t, analyzedMD.BuildImage.Reference, restoreRegFixtures.SomeCacheImage+"@sha256:")
t.Log("writes builder manifest and config to the kaniko cache")
Expand Down Expand Up @@ -230,7 +230,7 @@ func testRestorerFunc(platformAPI string) func(t *testing.T, when spec.G, it spe
),
)
t.Log("updates run image reference in analyzed.toml to include digest and target data")
analyzedMD, err := lifecycle.Config.ReadAnalyzed(filepath.Join(copyDir, "layers", "some-extend-true-analyzed.toml"), cmd.DefaultLogger)
analyzedMD, err := phase.Config.ReadAnalyzed(filepath.Join(copyDir, "layers", "some-extend-true-analyzed.toml"), cmd.DefaultLogger)
h.AssertNil(t, err)
h.AssertStringContains(t, analyzedMD.RunImage.Reference, restoreRegFixtures.ReadOnlyRunImage+"@sha256:")
h.AssertEq(t, analyzedMD.RunImage.Image, restoreRegFixtures.ReadOnlyRunImage)
Expand Down Expand Up @@ -266,7 +266,7 @@ func testRestorerFunc(platformAPI string) func(t *testing.T, when spec.G, it spe
)
if api.MustParse(platformAPI).AtLeast("0.12") {
t.Log("updates run image reference in analyzed.toml to include digest and target data")
analyzedMD, err := lifecycle.Config.ReadAnalyzed(filepath.Join(copyDir, "layers", "some-extend-false-analyzed.toml"), cmd.DefaultLogger)
analyzedMD, err := phase.Config.ReadAnalyzed(filepath.Join(copyDir, "layers", "some-extend-false-analyzed.toml"), cmd.DefaultLogger)
h.AssertNil(t, err)
h.AssertStringContains(t, analyzedMD.RunImage.Reference, restoreRegFixtures.ReadOnlyRunImage+"@sha256:")
h.AssertEq(t, analyzedMD.RunImage.Image, restoreRegFixtures.ReadOnlyRunImage)
Expand All @@ -279,7 +279,7 @@ func testRestorerFunc(platformAPI string) func(t *testing.T, when spec.G, it spe
h.AssertEq(t, len(fis), 1) // .gitkeep
} else {
t.Log("doesn't update analyzed.toml")
analyzedMD, err := lifecycle.Config.ReadAnalyzed(filepath.Join(copyDir, "layers", "some-extend-false-analyzed.toml"), cmd.DefaultLogger)
analyzedMD, err := phase.Config.ReadAnalyzed(filepath.Join(copyDir, "layers", "some-extend-false-analyzed.toml"), cmd.DefaultLogger)
h.AssertNil(t, err)
h.AssertNil(t, analyzedMD.RunImage.TargetMetadata)
}
Expand All @@ -306,7 +306,7 @@ func testRestorerFunc(platformAPI string) func(t *testing.T, when spec.G, it spe
),
)
t.Log("updates run image reference in analyzed.toml to include digest and target data")
analyzedMD, err := lifecycle.Config.ReadAnalyzed(filepath.Join(copyDir, "layers", "some-extend-false-analyzed.toml"), cmd.DefaultLogger)
analyzedMD, err := phase.Config.ReadAnalyzed(filepath.Join(copyDir, "layers", "some-extend-false-analyzed.toml"), cmd.DefaultLogger)
h.AssertNil(t, err)
h.AssertStringDoesNotContain(t, analyzedMD.RunImage.Reference, "@sha256:") // daemon image ID
h.AssertEq(t, analyzedMD.RunImage.Image, restoreRegFixtures.ReadOnlyRunImage)
Expand Down
6 changes: 5 additions & 1 deletion buildpack/build.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,11 @@ type BuildOutputs struct {
Slices []layers.Slice
}

//go:generate mockgen -package testmock -destination ../testmock/build_executor.go github.com/buildpacks/lifecycle/buildpack BuildExecutor
// BuildExecutor executes a single buildpack's `./bin/build` binary,
// providing inputs as defined in the Buildpack Interface Specification,
// and processing outputs for the platform.
//
//go:generate mockgen -package testmock -destination ../phase/testmock/build_executor.go github.com/buildpacks/lifecycle/buildpack BuildExecutor
type BuildExecutor interface {
Build(d BpDescriptor, inputs BuildInputs, logger log.Logger) (BuildOutputs, error)
}
Expand Down
4 changes: 2 additions & 2 deletions buildpack/build_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,11 +24,11 @@ import (
"github.com/buildpacks/lifecycle/launch"
"github.com/buildpacks/lifecycle/layers"
llog "github.com/buildpacks/lifecycle/log"
"github.com/buildpacks/lifecycle/phase/testmock"
h "github.com/buildpacks/lifecycle/testhelpers"
"github.com/buildpacks/lifecycle/testmock"
)

//go:generate mockgen -package testmock -destination testmock/env.go github.com/buildpacks/lifecycle BuildEnv
//go:generate mockgen -package testmock -destination testmock/env.go github.com/buildpacks/lifecycle/phase BuildEnv

func TestBuild(t *testing.T) {
spec.Run(t, "unit-build", testBuild, spec.Report(report.Terminal{}))
Expand Down
5 changes: 4 additions & 1 deletion buildpack/descriptor.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,10 @@ const (
KindExtension = "Extension"
)

//go:generate mockgen -package testmock -destination ../testmock/component_descriptor.go github.com/buildpacks/lifecycle/buildpack Descriptor
// Descriptor exposes information contained in a buildpack.toml or extension.toml
// that is generic to buildpacks and/or image extensions.
//
//go:generate mockgen -package testmock -destination ../lifecycle/testmock/component_descriptor.go github.com/buildpacks/lifecycle/buildpack Descriptor
type Descriptor interface {
API() string
Homepage() string
Expand Down
7 changes: 6 additions & 1 deletion buildpack/detect.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,12 @@ type DetectOutputs struct {
Err error `toml:"-"`
}

//go:generate mockgen -package testmock -destination ../testmock/detect_executor.go github.com/buildpacks/lifecycle/buildpack DetectExecutor
// DetectExecutor executes a single buildpack or image extension's `./bin/detect` binary,
// providing inputs as defined in the Buildpack Interface Specification,
// and processing outputs for the platform.
// For image extensions (where `./bin/detect` is optional), pre-populated outputs are processed here.
//
//go:generate mockgen -package testmock -destination ../phase/testmock/detect_executor.go github.com/buildpacks/lifecycle/buildpack DetectExecutor
type DetectExecutor interface {
Detect(d Descriptor, inputs DetectInputs, logger log.Logger) DetectOutputs
}
Expand Down
7 changes: 6 additions & 1 deletion buildpack/generate.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,12 @@ type GenerateOutputs struct {
MetRequires []string
}

//go:generate mockgen -package testmock -destination ../testmock/generate_executor.go github.com/buildpacks/lifecycle/buildpack GenerateExecutor
// GenerateExecutor executes a single image extension's `./bin/generate` binary,
// providing inputs as defined in the Buildpack Interface Specification,
// and processing outputs for the platform.
// Pre-populated outputs for image extensions that are missing `./bin/generate` are processed here.
//
//go:generate mockgen -package testmock -destination ../phase/testmock/generate_executor.go github.com/buildpacks/lifecycle/buildpack GenerateExecutor
type GenerateExecutor interface {
Generate(d ExtDescriptor, inputs GenerateInputs, logger log.Logger) (GenerateOutputs, error)
}
Expand Down
2 changes: 1 addition & 1 deletion buildpack/generate_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,8 @@ import (
"github.com/buildpacks/lifecycle/api"
"github.com/buildpacks/lifecycle/buildpack"
llog "github.com/buildpacks/lifecycle/log"
"github.com/buildpacks/lifecycle/phase/testmock"
h "github.com/buildpacks/lifecycle/testhelpers"
"github.com/buildpacks/lifecycle/testmock"
)

func TestGenerate(t *testing.T) {
Expand Down
2 changes: 1 addition & 1 deletion buildpack/testmock/env.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

7 changes: 3 additions & 4 deletions cache/image_cache_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,6 @@ import (
"path/filepath"
"testing"

cacheMock "github.com/buildpacks/lifecycle/testmock/cache"

"github.com/golang/mock/gomock"

"github.com/buildpacks/imgutil/fakes"
Expand All @@ -20,6 +18,7 @@ import (
"github.com/buildpacks/lifecycle/cache"
"github.com/buildpacks/lifecycle/cmd"
"github.com/buildpacks/lifecycle/log"
testmockcache "github.com/buildpacks/lifecycle/phase/testmock/cache"
"github.com/buildpacks/lifecycle/platform"
h "github.com/buildpacks/lifecycle/testhelpers"
)
Expand Down Expand Up @@ -48,8 +47,8 @@ func testImageCache(t *testing.T, when spec.G, it spec.S) {
fakeOriginalImage = fakes.NewImage("fake-image", "", local.IDIdentifier{ImageID: "fakeOriginalImage"})
fakeNewImage = fakes.NewImage("fake-image", "", local.IDIdentifier{ImageID: "fakeImage"})
mockController := gomock.NewController(t)
fakeImageDeleter := cacheMock.NewMockImageDeleter(mockController)
fakeImageComparer := cacheMock.NewMockImageComparer(mockController)
fakeImageDeleter := testmockcache.NewMockImageDeleter(mockController)
fakeImageComparer := testmockcache.NewMockImageComparer(mockController)
fakeImageComparer.EXPECT().ImagesEq(gomock.Any(), gomock.Any()).AnyTimes().Return(false, nil)
fakeImageDeleter.EXPECT().DeleteOrigImageIfDifferentFromNewImage(gomock.Any(), gomock.Any()).AnyTimes()
testLogger = cmd.DefaultLogger
Expand Down
2 changes: 1 addition & 1 deletion cache/image_comparer.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import (
"github.com/pkg/errors"
)

//go:generate mockgen -package testmockcache -destination ../testmock/cache/image_comparer.go github.com/buildpacks/lifecycle/cache ImageComparer
//go:generate mockgen -package testmockcache -destination ../lifecycle/testmock/cache/image_comparer.go github.com/buildpacks/lifecycle/cache ImageComparer

// ImageComparer provides a way to compare images
type ImageComparer interface {
Expand Down
2 changes: 1 addition & 1 deletion cache/image_deleter.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import (
"github.com/buildpacks/lifecycle/log"
)

//go:generate mockgen -package testmockcache -destination ../testmock/cache/image_deleter.go github.com/buildpacks/lifecycle/cache ImageDeleter
//go:generate mockgen -package testmockcache -destination ../phase/testmock/cache/image_deleter.go github.com/buildpacks/lifecycle/cache ImageDeleter

// ImageDeleter defines the methods available to delete and compare cached images
type ImageDeleter interface {
Expand Down
2 changes: 1 addition & 1 deletion cache/image_deleter_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@ import (

"github.com/buildpacks/lifecycle/cmd"
"github.com/buildpacks/lifecycle/log"
cacheMock "github.com/buildpacks/lifecycle/phase/testmock/cache"
h "github.com/buildpacks/lifecycle/testhelpers"
cacheMock "github.com/buildpacks/lifecycle/testmock/cache"
)

func TestImageDeleter(t *testing.T) {
Expand Down
58 changes: 35 additions & 23 deletions cmd/apis.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,37 +24,45 @@ var DeprecationMode = EnvOrDefault(EnvDeprecationMode, DefaultDeprecationMode)

type BuildpackAPIVerifier struct{}

func (v *BuildpackAPIVerifier) VerifyBuildpackAPI(kind, name, requested string, logger log.Logger) error {
return VerifyBuildpackAPI(kind, name, requested, logger)
// VerifyBuildpackAPI given a Buildpack API version and relevant information for logging
// will error if the requested version is unsupported,
// and will log a warning, error, or do nothing if the requested version is deprecated,
// depending on if the configured deprecation mode is "warn", "error", or "silent", respectively.
func (v *BuildpackAPIVerifier) VerifyBuildpackAPI(kind, name, requestedVersion string, logger log.Logger) error {
return VerifyBuildpackAPI(kind, name, requestedVersion, logger)
}

func VerifyBuildpackAPI(kind, name, requested string, logger log.Logger) error {
requestedAPI, err := api.NewVersion(requested)
// VerifyBuildpackAPI given a Buildpack API version and relevant information for logging
// will error if the requested version is unsupported,
// and will log a warning, error, or do nothing if the requested version is deprecated,
// depending on if the configured deprecation mode is "warn", "error", or "silent", respectively.
func VerifyBuildpackAPI(kind, name, requestedVersion string, logger log.Logger) error {
requested, err := api.NewVersion(requestedVersion)
if err != nil {
return FailErrCode(
nil,
CodeForIncompatibleBuildpackAPI,
fmt.Sprintf("parse buildpack API '%s' for %s '%s'", requestedAPI, strings.ToLower(kind), name),
fmt.Sprintf("parse buildpack API '%s' for %s '%s'", requested, strings.ToLower(kind), name),
)
}
if api.Buildpack.IsSupported(requestedAPI) {
if api.Buildpack.IsDeprecated(requestedAPI) {
if api.Buildpack.IsSupported(requested) {
if api.Buildpack.IsDeprecated(requested) {
switch DeprecationMode {
case ModeQuiet:
break
case ModeError:
logger.Errorf("%s '%s' requests deprecated API '%s'", kind, name, requested)
logger.Errorf("%s '%s' requests deprecated API '%s'", kind, name, requestedVersion)
logger.Errorf("Deprecated APIs are disabled by %s=%s", EnvDeprecationMode, ModeError)
return buildpackAPIError(kind, name, requested)
return buildpackAPIError(kind, name, requestedVersion)
case ModeWarn:
logger.Warnf("%s '%s' requests deprecated API '%s'", kind, name, requested)
logger.Warnf("%s '%s' requests deprecated API '%s'", kind, name, requestedVersion)
default:
logger.Warnf("%s '%s' requests deprecated API '%s'", kind, name, requested)
logger.Warnf("%s '%s' requests deprecated API '%s'", kind, name, requestedVersion)
}
}
return nil
}
return buildpackAPIError(kind, name, requested)
return buildpackAPIError(kind, name, requestedVersion)
}

func buildpackAPIError(moduleKind string, name string, requested string) error {
Expand All @@ -65,40 +73,44 @@ func buildpackAPIError(moduleKind string, name string, requested string) error {
)
}

func VerifyPlatformAPI(requested string, logger log.Logger) error {
if strings.TrimSpace(requested) == "" {
// VerifyPlatformAPI given a Platform API version and relevant information for logging
// will error if the requested version is unsupported,
// and will log a warning, error, or do nothing if the requested version is deprecated,
// depending on if the configured deprecation mode is "warn", "error", or "silent", respectively.
func VerifyPlatformAPI(requestedVersion string, logger log.Logger) error {
if strings.TrimSpace(requestedVersion) == "" {
return FailErrCode(
nil,
CodeForIncompatiblePlatformAPI,
fmt.Sprintf("get platform API version; please set '%s' to specify the desired platform API version", EnvPlatformAPI),
)
}
requestedAPI, err := api.NewVersion(requested)
requested, err := api.NewVersion(requestedVersion)
if err != nil {
return FailErrCode(
nil,
CodeForIncompatiblePlatformAPI,
fmt.Sprintf("parse platform API '%s'", requested),
fmt.Sprintf("parse platform API '%s'", requestedVersion),
)
}
if api.Platform.IsSupported(requestedAPI) {
if api.Platform.IsDeprecated(requestedAPI) {
if api.Platform.IsSupported(requested) {
if api.Platform.IsDeprecated(requested) {
switch DeprecationMode {
case ModeQuiet:
break
case ModeError:
logger.Errorf("Platform requested deprecated API '%s'", requested)
logger.Errorf("Platform requested deprecated API '%s'", requestedVersion)
logger.Errorf("Deprecated APIs are disabled by %s=%s", EnvDeprecationMode, ModeError)
return platformAPIError(requested)
return platformAPIError(requestedVersion)
case ModeWarn:
logger.Warnf("Platform requested deprecated API '%s'", requested)
logger.Warnf("Platform requested deprecated API '%s'", requestedVersion)
default:
logger.Warnf("Platform requested deprecated API '%s'", requested)
logger.Warnf("Platform requested deprecated API '%s'", requestedVersion)
}
}
return nil
}
return platformAPIError(requested)
return platformAPIError(requestedVersion)
}

func platformAPIError(requested string) error {
Expand Down
11 changes: 5 additions & 6 deletions cmd/lifecycle/analyzer.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,15 @@ package main
import (
"fmt"

"github.com/buildpacks/lifecycle/image"
"github.com/buildpacks/lifecycle/internal/encoding"

"github.com/docker/docker/client"
"github.com/google/go-containerregistry/pkg/authn"

"github.com/buildpacks/lifecycle"
"github.com/buildpacks/lifecycle/auth"
"github.com/buildpacks/lifecycle/cmd"
"github.com/buildpacks/lifecycle/cmd/lifecycle/cli"
"github.com/buildpacks/lifecycle/image"
"github.com/buildpacks/lifecycle/internal/encoding"
"github.com/buildpacks/lifecycle/phase"
"github.com/buildpacks/lifecycle/platform"
"github.com/buildpacks/lifecycle/priv"
)
Expand Down Expand Up @@ -97,11 +96,11 @@ func (a *analyzeCmd) Privileges() error {

// Exec executes the command.
func (a *analyzeCmd) Exec() error {
factory := lifecycle.NewAnalyzerFactory(
factory := phase.NewAnalyzerFactory(
a.PlatformAPI,
&cmd.BuildpackAPIVerifier{},
NewCacheHandler(a.keychain),
lifecycle.NewConfigHandler(),
phase.NewConfigHandler(),
image.NewHandler(a.docker, a.keychain, a.LayoutDir, a.UseLayout, a.InsecureRegistries),
image.NewRegistryHandler(a.keychain, a.InsecureRegistries),
)
Expand Down
6 changes: 3 additions & 3 deletions cmd/lifecycle/builder.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,12 @@ import (

"github.com/BurntSushi/toml"

"github.com/buildpacks/lifecycle"
"github.com/buildpacks/lifecycle/buildpack"
"github.com/buildpacks/lifecycle/cmd"
"github.com/buildpacks/lifecycle/cmd/lifecycle/cli"
"github.com/buildpacks/lifecycle/internal/encoding"
"github.com/buildpacks/lifecycle/launch"
"github.com/buildpacks/lifecycle/phase"
"github.com/buildpacks/lifecycle/platform"
"github.com/buildpacks/lifecycle/platform/files"
"github.com/buildpacks/lifecycle/priv"
Expand Down Expand Up @@ -74,7 +74,7 @@ func (b *buildCmd) Exec() error {
}

func (b *buildCmd) build(group buildpack.Group, plan files.Plan, analyzedMD files.Analyzed) error {
builder := &lifecycle.Builder{
builder := &phase.Builder{
AppDir: b.AppDir,
BuildConfigDir: b.BuildConfigDir,
LayersDir: b.LayersDir,
Expand Down Expand Up @@ -109,7 +109,7 @@ func (b *buildCmd) unwrapBuildFail(err error) error {
}

func (b *buildCmd) readData() (buildpack.Group, files.Plan, error) {
group, err := lifecycle.ReadGroup(b.GroupPath)
group, err := phase.ReadGroup(b.GroupPath)
if err != nil {
return buildpack.Group{}, files.Plan{}, cmd.FailErr(err, "read buildpack group")
}
Expand Down
Loading

0 comments on commit 6c7d16a

Please sign in to comment.