Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add -daemon to restorer for newer platform #1852

Merged
merged 11 commits into from
Aug 4, 2023
Merged
67 changes: 44 additions & 23 deletions acceptance/acceptance_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -797,12 +797,12 @@ func testAcceptance(
it("creates builder", func() {
// Linux containers (including Linux containers on Windows)
extSimpleLayersDiffID := "sha256:b9e4a0ddfb650c7aa71d1e6aceea1665365e409b3078bfdc1e51c2b07ab2b423"
extReadEnvDiffID := "sha256:6801a0398d023ff06a43c4fd03ef325a45daaa4540fc3ee140e2fb22bf5143a7"
extReadEnvDiffID := "sha256:4490d78f2b056cdb99ad9cd3892f3c0617c5a485fb300dd90c572ce375ee45b2"
bpSimpleLayersDiffID := "sha256:285ff6683c99e5ae19805f6a62168fb40dca64d813c53b782604c9652d745c70"
bpReadEnvDiffID := "sha256:dd1e0efcbf3f08b014ef6eff9cfe7a9eac1cf20bd9b6a71a946f0a74575aa56f"
if imageManager.HostOS() == "windows" { // Windows containers on Windows
extSimpleLayersDiffID = "sha256:a063cf949b9c267133e451ac8cd95b4e77571bb7c629dd817461dca769170810"
extReadEnvDiffID = "sha256:4c37e22595762315d28805f32e1f5fd48b4ddfc293ef7b41e0e609a4241b8479"
extReadEnvDiffID = "sha256:ae9520eef7d84f69da6adf2597266660ce3fa5fd8ddac716cbfbecb67ded50e5"
bpSimpleLayersDiffID = "sha256:ccd1234cc5685e8a412b70c5f9a8e7b584b8e4f2a20c987ec242c9055de3e45e"
bpReadEnvDiffID = "sha256:8b22a7742ffdfbdd978787c6937456b68afb27c3585a3903048be7434d251e3f"
}
Expand Down Expand Up @@ -865,33 +865,54 @@ func testAcceptance(
})

when("there are run image extensions", func() {
it.Before(func() {
h.SkipIf(t, !pack.SupportsFeature(invoke.RunImageExtensions), "")
h.SkipIf(t, !lifecycle.SupportsFeature(config.RunImageExtensions), "")
when("switching the run image", func() {
it.Before(func() {
h.SkipIf(t, !pack.SupportsFeature(invoke.RunImageExtensions), "")
h.SkipIf(t, !lifecycle.SupportsFeature(config.RunImageExtensions), "")
})

it("uses the 5 phases, and tries to pull the new run image before restore", func() {
output, _ := pack.Run(
"build", repoName,
"-p", filepath.Join("testdata", "mock_app"),
"--network", "host",
"-B", builderName,
"--env", "EXT_RUN_SWITCH=1",
)
h.AssertContains(t, output, "ERROR: failed to build: executing lifecycle: resolve auth for ref some-not-exist-run-image!")
h.AssertNotContains(t, output, "RESTORING")
})
})

it("uses the 5 phases, and runs the extender (run)", func() {
output := pack.RunSuccessfully(
"build", repoName,
"-p", filepath.Join("testdata", "mock_app"),
"--network", "host", // export target is the daemon, but we need to be able to reach the registry where the builder image and run image are saved
"-B", builderName,
"--env", "EXT_RUN=1",
)
when("extending the run image", func() {
it.Before(func() {
h.SkipIf(t, !pack.SupportsFeature(invoke.RunImageExtensions), "")
h.SkipIf(t, !lifecycle.SupportsFeature(config.RunImageExtensions), "")
})

assertions.NewOutputAssertionManager(t, output).ReportsSuccessfulImageBuild(repoName)
it("uses the 5 phases, and runs the extender (run)", func() {
output := pack.RunSuccessfully(
"build", repoName,
"-p", filepath.Join("testdata", "mock_app"),
"--network", "host", // export target is the daemon, but we need to be able to reach the registry where the builder image and run image are saved
"-B", builderName,
"--env", "EXT_RUN=1",
)

assertOutput := assertions.NewLifecycleOutputAssertionManager(t, output)
assertOutput.IncludesLifecycleImageTag(lifecycle.Image())
assertOutput.IncludesSeparatePhasesWithRunExtension()
assertions.NewOutputAssertionManager(t, output).ReportsSuccessfulImageBuild(repoName)

t.Log("inspecting image")
inspectCmd := "inspect"
if !pack.Supports("inspect") {
inspectCmd = "inspect-image"
}
assertOutput := assertions.NewLifecycleOutputAssertionManager(t, output)
assertOutput.IncludesLifecycleImageTag(lifecycle.Image())
assertOutput.IncludesSeparatePhasesWithRunExtension()

output = pack.RunSuccessfully(inspectCmd, repoName)
t.Log("inspecting image")
inspectCmd := "inspect"
if !pack.Supports("inspect") {
inspectCmd = "inspect-image"
}

output = pack.RunSuccessfully(inspectCmd, repoName)
})
})
})
})
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,3 +29,12 @@ ARG user_id
USER \${user_id}
EOL
fi

if [[ -z "$EXT_RUN_SWITCH" ]]; then
echo "Skipping run image switch, not requested..."
else
echo "Generating run.Dockerfile for run image switch..."
cat >>"${output_dir}/run.Dockerfile" <<EOL
FROM some-not-exist-run-image!
EOL
fi
2 changes: 1 addition & 1 deletion acceptance/testdata/pack_fixtures/report_output.txt
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ Pack:
Version: {{ .Version }}
OS/Arch: {{ .OS }}/{{ .Arch }}

Default Lifecycle Version: 0.17.0-rc.3
Default Lifecycle Version: 0.17.0-rc.4

Supported Platform APIs: 0.3, 0.4, 0.5, 0.6, 0.7, 0.8, 0.9, 0.10, 0.11, 0.12

Expand Down
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ require (
github.com/Microsoft/go-winio v0.6.1
github.com/apex/log v1.9.0
github.com/buildpacks/imgutil v0.0.0-20230626185301-726f02e4225c
github.com/buildpacks/lifecycle v0.17.0-rc.3
github.com/buildpacks/lifecycle v0.17.0-rc.4
github.com/docker/cli v24.0.5+incompatible
github.com/docker/docker v24.0.5+incompatible
github.com/docker/go-connections v0.4.0
Expand Down
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -97,8 +97,8 @@ github.com/awslabs/amazon-ecr-credential-helper/ecr-login v0.0.0-20230522190001-
github.com/aybabtme/rgbterm v0.0.0-20170906152045-cc83f3b3ce59/go.mod h1:q/89r3U2H7sSsE2t6Kca0lfwTK8JdoNGS/yzM/4iH5I=
github.com/buildpacks/imgutil v0.0.0-20230626185301-726f02e4225c h1:HlRuSz+JGAzudNtNCfHIzXe0AEuHX6Vx8uZgmjvX02o=
github.com/buildpacks/imgutil v0.0.0-20230626185301-726f02e4225c/go.mod h1:mBG5M3GJW5nknCEOOqtmMHyPYnSpw/5GEiciuYU/COw=
github.com/buildpacks/lifecycle v0.17.0-rc.3 h1:GAo6Gv2hFbhjf0JX7M2khS8jd6vU8I9oCkheIhHQwxg=
github.com/buildpacks/lifecycle v0.17.0-rc.3/go.mod h1:WFzcNp1WG4bwgHuXtKxMg4tdU3AguL44ZlP3knANeVs=
github.com/buildpacks/lifecycle v0.17.0-rc.4 h1:0mCWV9RAWLYRj8BtdEJdAX5uk8EFE8YOvdfjYIHu0Tw=
github.com/buildpacks/lifecycle v0.17.0-rc.4/go.mod h1:WFzcNp1WG4bwgHuXtKxMg4tdU3AguL44ZlP3knANeVs=
github.com/bwesterb/go-ristretto v1.2.3/go.mod h1:fUIoIZaG73pV5biE2Blr2xEzDoMj7NFEuV9ekS419A0=
github.com/chrismellard/docker-credential-acr-env v0.0.0-20230304212654-82a0ddb27589 h1:krfRl01rzPzxSxyLyrChD+U+MzsBXbm0OwYYB67uF+4=
github.com/chrismellard/docker-credential-acr-env v0.0.0-20230304212654-82a0ddb27589/go.mod h1:OuDyvmLnMCwa2ep4Jkm6nyA0ocJuZlGyk2gGseVzERM=
Expand Down
34 changes: 25 additions & 9 deletions internal/build/lifecycle_execution.go
Original file line number Diff line number Diff line change
Expand Up @@ -240,6 +240,13 @@
}
}

currentRunImage := l.runImageAfterExtensions()
if currentRunImage != "" && currentRunImage != l.opts.RunImage {
if err := l.opts.FetchRunImage(currentRunImage); err != nil {
return err
}

Check warning on line 247 in internal/build/lifecycle_execution.go

View check run for this annotation

Codecov / codecov/patch

internal/build/lifecycle_execution.go#L246-L247

Added lines #L246 - L247 were not covered by tests
}

l.logger.Info(style.Step("RESTORING"))
if l.opts.ClearCache && l.PlatformAPI().LessThan("0.10") {
l.logger.Info("Skipping 'restore' due to clearing cache")
Expand All @@ -260,13 +267,6 @@
})
}

currentRunImage := l.runImageAfterExtensions()
if currentRunImage != "" && currentRunImage != l.opts.RunImage {
if err := l.opts.FetchRunImage(currentRunImage); err != nil {
return err
}
}

if l.platformAPI.AtLeast("0.12") && l.hasExtensionsForRun() {
group.Go(func() error {
l.logger.Info(style.Step("EXTENDING (RUN)"))
Expand Down Expand Up @@ -492,6 +492,12 @@
registryOp = WithRegistryAccess(authConfig)
}

dockerOp := NullOp()
if !l.opts.Publish && l.platformAPI.AtLeast("0.12") {
dockerOp = WithDaemonAccess(l.opts.DockerHost)
flags = append(flags, "-daemon")
}

flagsOp := WithFlags(flags...)

configProvider := NewPhaseConfigProvider(
Expand All @@ -507,10 +513,11 @@
WithNetwork(l.opts.Network),
If(l.hasExtensionsForRun(), WithPostContainerRunOperations(
CopyOutToMaybe(l.mountPaths.cnbDir(), l.tmpDir))), // FIXME: this is hacky; we should get the lifecycle binaries from the lifecycle image
flagsOp,
cacheBindOp,
registryOp,
dockerOp,
flagsOp,
kanikoCacheBindOp,
registryOp,
)

restore := phaseFactory.New(configProvider)
Expand Down Expand Up @@ -835,6 +842,9 @@
}

func (l *LifecycleExecution) hasExtensionsForBuild() bool {
if !l.hasExtensions() {
return false
}
// the directory is <layers>/generated/build inside the build container, but `CopyOutTo` only copies the directory
fis, err := os.ReadDir(filepath.Join(l.tmpDir, "build"))
if err != nil {
Expand All @@ -844,6 +854,9 @@
}

func (l *LifecycleExecution) hasExtensionsForRun() bool {
if !l.hasExtensions() {
return false
}
var amd files.Analyzed
if _, err := toml.DecodeFile(filepath.Join(l.tmpDir, "analyzed.toml"), &amd); err != nil {
l.logger.Warnf("failed to parse analyzed.toml file, assuming no run image extensions: %s", err)
Expand All @@ -858,6 +871,9 @@
}

func (l *LifecycleExecution) runImageAfterExtensions() string {
if !l.hasExtensions() {
return l.opts.RunImage
}
var amd files.Analyzed
if _, err := toml.DecodeFile(filepath.Join(l.tmpDir, "analyzed.toml"), &amd); err != nil {
l.logger.Warnf("failed to parse analyzed.toml file, assuming run image did not change: %s", err)
Expand Down
25 changes: 25 additions & 0 deletions internal/build/lifecycle_execution_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -553,6 +553,7 @@ func testLifecycleExecution(t *testing.T, when spec.G, it spec.S) {

when("extensions", func() {
providedUseCreator = false
providedOrderExt = dist.Order{dist.OrderEntry{Group: []dist.ModuleRef{ /* don't care */ }}}

when("for build", func() {
when("present <layers>/generated/build", func() {
Expand Down Expand Up @@ -1703,6 +1704,7 @@ func testLifecycleExecution(t *testing.T, when spec.G, it spec.S) {

when("there are extensions", func() {
platformAPI = api.MustParse("0.12")
providedOrderExt = dist.Order{dist.OrderEntry{Group: []dist.ModuleRef{ /* don't care */ }}}

when("for build", func() {
extensionsForBuild = true
Expand Down Expand Up @@ -1772,6 +1774,8 @@ func testLifecycleExecution(t *testing.T, when spec.G, it spec.S) {
})

when("extensions", func() {
providedOrderExt = dist.Order{dist.OrderEntry{Group: []dist.ModuleRef{ /* don't care */ }}}

when("for build", func() {
when("present in <layers>/generated/build", func() {
extensionsForBuild = true
Expand Down Expand Up @@ -1838,6 +1842,24 @@ func testLifecycleExecution(t *testing.T, when spec.G, it spec.S) {
})
})
})

when("publish is false", func() {
when("platform >= 0.12", func() {
platformAPI = api.MustParse("0.12")

it("configures the phase with daemon access", func() {
h.AssertEq(t, configProvider.ContainerConfig().User, "root")
h.AssertSliceContains(t, configProvider.HostConfig().Binds, "/var/run/docker.sock:/var/run/docker.sock")
})

it("configures the phase with the expected arguments", func() {
h.AssertIncludeAllExpectedPatterns(t,
configProvider.ContainerConfig().Cmd,
[]string{"-daemon"},
)
})
})
})
})

when("#Build", func() {
Expand Down Expand Up @@ -1938,6 +1960,7 @@ func testLifecycleExecution(t *testing.T, when spec.G, it spec.S) {

when("extensions change the run image", func() {
extensionsRunImage = "some-new-run-image"
providedOrderExt = dist.Order{dist.OrderEntry{Group: []dist.ModuleRef{ /* don't care */ }}}

it("runs the phase with the new run image", func() {
h.AssertEq(t, configProvider.ContainerConfig().Image, "some-new-run-image")
Expand Down Expand Up @@ -2012,6 +2035,8 @@ func testLifecycleExecution(t *testing.T, when spec.G, it spec.S) {
})

when("there are extensions", func() {
providedOrderExt = dist.Order{dist.OrderEntry{Group: []dist.ModuleRef{ /* don't care */ }}}

when("for run", func() {
extensionsForRun = true

Expand Down
4 changes: 2 additions & 2 deletions internal/builder/builder.go
Original file line number Diff line number Diff line change
Expand Up @@ -783,11 +783,11 @@ func (b *Builder) validateBuildpacks() error {
if err != nil {
return err
}
buildDistroName, err := b.Image().Label(lifecycleplatform.OSDistributionNameLabel)
buildDistroName, err := b.Image().Label(lifecycleplatform.OSDistroNameLabel)
if err != nil {
return err
}
buildDistroVersion, err := b.Image().Label(lifecycleplatform.OSDistributionVersionLabel)
buildDistroVersion, err := b.Image().Label(lifecycleplatform.OSDistroVersionLabel)
if err != nil {
return err
}
Expand Down
2 changes: 1 addition & 1 deletion internal/builder/lifecycle.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ import (

// A snapshot of the latest tested lifecycle version values
const (
DefaultLifecycleVersion = "0.17.0-rc.3"
DefaultLifecycleVersion = "0.17.0-rc.4"
DefaultBuildpackAPIVersion = "0.2"
)

Expand Down
2 changes: 1 addition & 1 deletion pkg/client/build.go
Original file line number Diff line number Diff line change
Expand Up @@ -451,7 +451,7 @@ func (c *Client) Build(ctx context.Context, opts BuildOptions) error {
}
defer c.docker.ImageRemove(context.Background(), ephemeralBuilder.Name(), types.ImageRemoveOptions{Force: true})

if len(bldr.OrderExtensions()) > 0 {
if len(bldr.OrderExtensions()) > 0 || len(ephemeralBuilder.OrderExtensions()) > 0 {
if !c.experimental {
return fmt.Errorf("experimental features must be enabled when builder contains image extensions")
}
Expand Down
Loading