Skip to content

Commit

Permalink
WIP
Browse files Browse the repository at this point in the history
Signed-off-by: Natalie Arellano <narellano@vmware.com>
  • Loading branch information
natalieparellano committed Apr 3, 2023
1 parent cf02f03 commit 4f16428
Show file tree
Hide file tree
Showing 2 changed files with 70 additions and 21 deletions.
87 changes: 68 additions & 19 deletions internal/build/lifecycle_execution.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import (
"github.com/google/go-containerregistry/pkg/authn"
"github.com/google/go-containerregistry/pkg/name"
"github.com/pkg/errors"
"golang.org/x/sync/errgroup"

"github.com/buildpacks/pack/internal/builder"
"github.com/buildpacks/pack/internal/cache"
Expand Down Expand Up @@ -220,26 +221,38 @@ func (l *LifecycleExecution) Run(ctx context.Context, phaseFactoryCreator PhaseF
return err
}

if l.platformAPI.AtLeast("0.10") && l.hasExtensions() {
if l.os == "windows" {
return fmt.Errorf("builder has an order for extensions which is not supported for Windows builds")
}
l.logger.Info(style.Step("EXTENDING"))
if err := l.Extend(ctx, buildCache, phaseFactory); err != nil {
return err
}
group, _ := errgroup.WithContext(context.TODO())
if l.platformAPI.AtLeast("0.10") && l.hasExtensionsForBuild() {
//if l.os == "windows" { // TODO: do this check earlier
// return fmt.Errorf("builder has an order for extensions which is not supported for Windows builds")
//}
group.Go(func() error {
l.logger.Info(style.Step("EXTENDING (BUILD)"))
return l.ExtendBuild(ctx, buildCache, phaseFactory)
})
} else {
l.logger.Info(style.Step("BUILDING"))
if err := l.Build(ctx, phaseFactory); err != nil {
return err
}
group.Go(func() error {
l.logger.Info(style.Step("BUILDING"))
return l.Build(ctx, phaseFactory)
})
}

if l.platformAPI.AtLeast("0.12") && l.hasExtensionsForRun() {
group.Go(func() error {
l.logger.Info(style.Step("EXTENDING (RUN)"))
return l.ExtendRun(ctx, buildCache, phaseFactory)
})
}

if err := group.Wait(); err != nil {
return err
}

l.logger.Info(style.Step("EXPORTING"))
return l.Export(ctx, buildCache, launchCache, phaseFactory)
}

if l.platformAPI.AtLeast("0.10") && l.hasExtensions() {
if l.platformAPI.AtLeast("0.10") && l.hasExtensionsForBuild() {
return errors.New("builder has an order for extensions which is not supported when using the creator")
}
return l.Create(ctx, buildCache, launchCache, phaseFactory)
Expand Down Expand Up @@ -366,7 +379,7 @@ func (l *LifecycleExecution) Detect(ctx context.Context, phaseFactory PhaseFacto
flags := []string{"-app", l.mountPaths.appDir()}

envOp := NullOp()
if l.hasExtensions() && l.platformAPI.AtLeast("0.10") {
if l.platformAPI.AtLeast("0.10") && l.hasExtensionsForBuild() {
envOp = WithEnv("CNB_EXPERIMENTAL_MODE=warn")
}

Expand Down Expand Up @@ -418,7 +431,7 @@ func (l *LifecycleExecution) Restore(ctx context.Context, buildCache Cache, phas

// for kaniko
kanikoCacheBindOp := NullOp()
if l.platformAPI.AtLeast("0.10") && l.hasExtensions() {
if l.platformAPI.AtLeast("0.10") && l.hasExtensionsForBuild() {
flags = append(flags, "-build-image", l.opts.BuilderImage)
registryImages = append(registryImages, l.opts.BuilderImage)

Expand Down Expand Up @@ -611,7 +624,7 @@ func (l *LifecycleExecution) Build(ctx context.Context, phaseFactory PhaseFactor
return build.Run(ctx)
}

func (l *LifecycleExecution) Extend(ctx context.Context, buildCache Cache, phaseFactory PhaseFactory) error {
func (l *LifecycleExecution) ExtendBuild(ctx context.Context, buildCache Cache, phaseFactory PhaseFactory) error {
flags := []string{"-app", l.mountPaths.appDir()}

// set kaniko cache opt
Expand All @@ -626,7 +639,37 @@ func (l *LifecycleExecution) Extend(ctx context.Context, buildCache Cache, phase
configProvider := NewPhaseConfigProvider(
"extender",
l,
WithLogPrefix("extender"),
WithLogPrefix("extender (build)"),
WithArgs(l.withLogLevel()...),
WithBinds(l.opts.Volumes...),
WithEnv("CNB_EXPERIMENTAL_MODE=warn"),
WithFlags(flags...),
WithNetwork(l.opts.Network),
WithRoot(),
kanikoCacheBindOp,
)

extend := phaseFactory.New(configProvider)
defer extend.Cleanup()
return extend.Run(ctx)
}

func (l *LifecycleExecution) ExtendRun(ctx context.Context, buildCache Cache, phaseFactory PhaseFactory) error {
flags := []string{"-app", l.mountPaths.appDir(), "-kind", "run"}

// set kaniko cache opt
var kanikoCacheBindOp PhaseConfigProviderOperation
switch buildCache.Type() {
case cache.Volume:
kanikoCacheBindOp = WithBinds(fmt.Sprintf("%s:%s", buildCache.Name(), l.mountPaths.kanikoCacheDir()))
default:
return fmt.Errorf("build cache must be volume cache when building with extensions")
}

configProvider := NewPhaseConfigProvider(
"extender",
l,
WithLogPrefix("extender (run)"),
WithArgs(l.withLogLevel()...),
WithBinds(l.opts.Volumes...),
WithEnv("CNB_EXPERIMENTAL_MODE=warn"),
Expand Down Expand Up @@ -655,7 +698,7 @@ func (l *LifecycleExecution) Export(ctx context.Context, buildCache, launchCache
flags := []string{
"-app", l.mountPaths.appDir(),
"-cache-dir", l.mountPaths.cacheDir(),
"-stack", l.mountPaths.stackPath(),
//"-stack", l.mountPaths.stackPath(),
}

if l.platformAPI.LessThan("0.7") {
Expand Down Expand Up @@ -747,7 +790,13 @@ func (l *LifecycleExecution) withLogLevel(args ...string) []string {
return args
}

func (l *LifecycleExecution) hasExtensions() bool {
func (l *LifecycleExecution) hasExtensionsForBuild() bool {
// TODO: look for Dockerfiles in <layers>/generated/build/
return len(l.opts.Builder.OrderExtensions()) > 0
}

func (l *LifecycleExecution) hasExtensionsForRun() bool {
// TODO: look for Dockerfiles in <layers>/generated/run/ and extend = true in analyzed.toml
return len(l.opts.Builder.OrderExtensions()) > 0
}

Expand Down
4 changes: 2 additions & 2 deletions internal/build/lifecycle_execution_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1691,9 +1691,9 @@ func testLifecycleExecution(t *testing.T, when spec.G, it spec.S) {
})
})

when("#Extend", func() {
when("#ExtendBuild", func() {
it.Before(func() {
err := lifecycle.Extend(context.Background(), fakeBuildCache, fakePhaseFactory)
err := lifecycle.ExtendBuild(context.Background(), fakeBuildCache, fakePhaseFactory)
h.AssertNil(t, err)

lastCallIndex := len(fakePhaseFactory.NewCalledWithProvider) - 1
Expand Down

0 comments on commit 4f16428

Please sign in to comment.