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

Restorer changes for run image extension #1014

Merged
merged 37 commits into from
Mar 28, 2023
Merged
Show file tree
Hide file tree
Changes from 13 commits
Commits
Show all changes
37 commits
Select commit Hold shift + click to select a range
f22c4f9
Make a single constructor for lifecycle inputs
natalieparellano Feb 13, 2023
433bf3c
Read values from environment
natalieparellano Feb 14, 2023
f40616e
Buildpack API: run.Dockerfiles are allowed instructions on versions >…
natalieparellano Feb 14, 2023
29a94a6
Platform API: the detector accepts a new -run flag
natalieparellano Feb 14, 2023
569f985
Move responsibility for validating Dockerfiles into the buildpack pac…
natalieparellano Feb 15, 2023
c72a10e
When verifying Dockerfiles, return the new base image name if necessary
natalieparellano Feb 15, 2023
4c5a564
When determining the new runtime base image, use criteria outlined in…
natalieparellano Feb 15, 2023
01ab4b6
Platform API: the schema of analyzed.toml is updated to include run-i…
natalieparellano Feb 15, 2023
9ba40e3
TESTME: Update analyzed.toml with new run image if needed
natalieparellano Feb 15, 2023
eea3182
If extensions are used to switch the runtime base image, the detector…
natalieparellano Feb 15, 2023
647a3db
Add fixture to test re-writing of analyzed.toml
natalieparellano Feb 15, 2023
9157796
Move updating analyzed.toml into lifecycle package for easier testing
natalieparellano Feb 15, 2023
62411e8
Platform API: the restorer will update analyzed.toml with:
natalieparellano Feb 17, 2023
362e755
Update acceptance/extender_test.go
natalieparellano Feb 17, 2023
15c4c06
Merge branch 'main' into runext/restore-997
natalieparellano Feb 28, 2023
8917ca0
Fix merge and restore selective package
natalieparellano Feb 28, 2023
ea13f10
Update analyzed.toml with digest reference or target data if needed
natalieparellano Feb 28, 2023
d0ce558
Merge branch 'main' into runext/detect-996
natalieparellano Mar 1, 2023
9ba92d1
Fix acceptance
natalieparellano Mar 1, 2023
6487081
Merge branch 'main' into runext/restore-997
natalieparellano Mar 1, 2023
76c7284
Merge branch 'runext/detect-996' into runext/restore-997
natalieparellano Mar 1, 2023
b8f029f
Don't redefine -layers
natalieparellano Mar 1, 2023
2d78993
Merge branch 'runext/detect-996' into runext/restore-997
natalieparellano Mar 1, 2023
44663c5
Merge branch 'main' into runext/restore-997
natalieparellano Mar 8, 2023
8a70c55
Test organization and remove the requirement that we're exporting to …
natalieparellano Mar 8, 2023
5cce13c
Fix acceptance
natalieparellano Mar 8, 2023
c630e7d
Fix acceptance
natalieparellano Mar 8, 2023
0f2305a
Test that we don't update target data for older platforms
natalieparellano Mar 9, 2023
5adcca5
Remove target partial and use helper function
natalieparellano Mar 9, 2023
2c17e05
Fix acceptance
natalieparellano Mar 9, 2023
4054ebd
Bump imgutil
natalieparellano Mar 9, 2023
95dbc52
Fix acceptance
natalieparellano Mar 9, 2023
cc1a045
Merge branch 'main' into runext/restore-997
natalieparellano Mar 24, 2023
c8398c3
Use imgutil/sparse package instead of internal/selective package
natalieparellano Mar 24, 2023
ac6ad80
Don't try to pull a builder image if it wasn't specified
natalieparellano Mar 24, 2023
da9e2ff
Fix lint
natalieparellano Mar 24, 2023
f2a068e
Add and update comment
natalieparellano Mar 27, 2023
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -8,5 +8,6 @@
.vscode
acceptance/testdata/*/**/container/cnb/lifecycle/*
acceptance/testdata/*/**/container/docker-config/*
acceptance/testdata/restorer/container/layers/*analyzed.toml
acceptance/testdata/exporter/container/layers/*analyzed.toml
acceptance/testdata/exporter/container/other_layers/*analyzed.toml
2 changes: 2 additions & 0 deletions acceptance/detector_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -362,11 +362,13 @@ fail: fail_detect_buildpack@some_version
"-extensions=/cnb/extensions",
"-generated=/layers/generated",
"-log-level=debug",
"-run=/layers/run.toml", // /cnb/run.toml is the default location of run.toml
),
)

t.Log("runs /bin/detect for buildpacks and extensions")
h.AssertStringContains(t, output, "Platform requested experimental feature 'Dockerfiles'")
h.AssertStringContains(t, output, "FOO=val-from-build-config")
h.AssertStringContains(t, output, "simple_extension: output from /bin/detect")
t.Log("writes group.toml")
foundGroupTOML := filepath.Join(copyDir, "layers", "group.toml")
Expand Down
32 changes: 16 additions & 16 deletions acceptance/exporter_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@ import (
"github.com/buildpacks/lifecycle/cache"
"github.com/buildpacks/lifecycle/internal/encoding"
"github.com/buildpacks/lifecycle/internal/path"
"github.com/buildpacks/lifecycle/platform"
h "github.com/buildpacks/lifecycle/testhelpers"
)

Expand Down Expand Up @@ -287,21 +286,22 @@ func assertImageOSAndArchAndCreatedAt(t *testing.T, imageName string, phaseTest
}

func updateAnalyzedTOMLFixturesWithRegRepoName(t *testing.T, phaseTest *PhaseTest) {
placeHolderPath := filepath.Join("testdata", "exporter", "container", "layers", "analyzed.toml.placeholder")
analyzedMD := assertAnalyzedMetadata(t, placeHolderPath)
analyzedMD.RunImage = &platform.ImageIdentifier{Reference: phaseTest.targetRegistry.fixtures.ReadOnlyRunImage}
encoding.WriteTOML(strings.TrimSuffix(placeHolderPath, ".placeholder"), analyzedMD)

placeHolderPath = filepath.Join("testdata", "exporter", "container", "layers", "some-analyzed.toml.placeholder")
analyzedMD = assertAnalyzedMetadata(t, placeHolderPath)
analyzedMD.PreviousImage = &platform.ImageIdentifier{Reference: phaseTest.targetRegistry.fixtures.SomeAppImage}
analyzedMD.RunImage = &platform.ImageIdentifier{Reference: phaseTest.targetRegistry.fixtures.ReadOnlyRunImage}
encoding.WriteTOML(strings.TrimSuffix(placeHolderPath, ".placeholder"), analyzedMD)

placeHolderPath = filepath.Join("testdata", "exporter", "container", "other_layers", "analyzed.toml.placeholder")
analyzedMD = assertAnalyzedMetadata(t, placeHolderPath)
analyzedMD.RunImage = &platform.ImageIdentifier{Reference: phaseTest.targetRegistry.fixtures.ReadOnlyRunImage}
encoding.WriteTOML(strings.TrimSuffix(placeHolderPath, ".placeholder"), analyzedMD)
placeholderPaths := []string{
filepath.Join(phaseTest.testImageDockerContext, "container", "layers", "analyzed.toml.placeholder"),
filepath.Join(phaseTest.testImageDockerContext, "container", "layers", "some-analyzed.toml.placeholder"),
filepath.Join(phaseTest.testImageDockerContext, "container", "other_layers", "analyzed.toml.placeholder"),
}

for _, placeholderPath := range placeholderPaths {
if _, err := os.Stat(placeholderPath); os.IsNotExist(err) {
continue
}
analyzedMD := assertAnalyzedMetadata(t, placeholderPath)
if analyzedMD.RunImage != nil {
analyzedMD.RunImage.Reference = phaseTest.targetRegistry.fixtures.ReadOnlyRunImage
}
encoding.WriteTOML(strings.TrimSuffix(placeholderPath, ".placeholder"), analyzedMD)
}
}

func calculateEmptyLayerSha(t *testing.T) string {
Expand Down
9 changes: 5 additions & 4 deletions acceptance/extender_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,16 +13,16 @@ import (
"testing"
"time"

"github.com/buildpacks/imgutil"
"github.com/buildpacks/imgutil/layout/sparse"
"github.com/google/go-containerregistry/pkg/authn"
"github.com/google/go-containerregistry/pkg/v1/empty"
"github.com/google/go-containerregistry/pkg/v1/remote"
"github.com/sclevine/spec"
"github.com/sclevine/spec/report"

"github.com/buildpacks/lifecycle/api"
"github.com/buildpacks/lifecycle/auth"
"github.com/buildpacks/lifecycle/internal/encoding"
"github.com/buildpacks/lifecycle/internal/selective"
"github.com/buildpacks/lifecycle/platform"
h "github.com/buildpacks/lifecycle/testhelpers"
)
Expand Down Expand Up @@ -90,9 +90,10 @@ func testExtenderFunc(platformAPI string) func(t *testing.T, when spec.G, it spe
buildImageDigest := buildImageHash.String()
baseCacheDir := filepath.Join(kanikoDir, "cache", "base")
h.AssertNil(t, os.MkdirAll(baseCacheDir, 0755))
layoutPath, err := selective.Write(filepath.Join(baseCacheDir, buildImageDigest), empty.Index)
var sparseImage imgutil.Image
sparseImage, err = sparse.NewImage(filepath.Join(baseCacheDir, digest.DigestStr()), remoteImage)
natalieparellano marked this conversation as resolved.
Show resolved Hide resolved
h.AssertNil(t, err)
h.AssertNil(t, layoutPath.AppendImage(remoteImage))
h.AssertNil(t, sparseImage.Save())

// write build image reference in analyzed.toml
analyzedMD := platform.AnalyzedMetadata{BuildImage: &platform.ImageIdentifier{Reference: fmt.Sprintf("%s@%s", extendTest.RegRepoName(extendImage), buildImageDigest)}}
Expand Down
50 changes: 43 additions & 7 deletions acceptance/restorer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import (
"testing"
"time"

"github.com/google/go-containerregistry/pkg/name"
"github.com/sclevine/spec"
"github.com/sclevine/spec/report"

Expand All @@ -38,7 +39,7 @@ func TestRestorer(t *testing.T) {

testImageDockerContext := filepath.Join("testdata", "restorer")
restoreTest = NewPhaseTest(t, "restorer", testImageDockerContext)
restoreTest.Start(t)
restoreTest.Start(t, updateAnalyzedTOMLFixturesWithRegRepoName)
defer restoreTest.Stop(t)

restoreImage = restoreTest.testImageRef
Expand Down Expand Up @@ -80,7 +81,7 @@ func testRestorerFunc(platformAPI string) func(t *testing.T, when spec.G, it spe
})
})

when("called with -analyzed", func() {
when("called with -analyzed (on older platforms)", func() {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

A+ update to test descriptions

it("errors", func() {
h.SkipIf(t, api.MustParse(platformAPI).AtLeast("0.7"), "Platform API >= 0.7 supports -analyzed flag")
command := exec.Command("docker", "run", "--rm", restoreImage, "-analyzed some-file-location")
Expand All @@ -91,7 +92,7 @@ func testRestorerFunc(platformAPI string) func(t *testing.T, when spec.G, it spe
})
})

when("called with -skip-layers", func() {
when("called with -skip-layers (on older platforms)", func() {
it("errors", func() {
h.SkipIf(t, api.MustParse(platformAPI).AtLeast("0.7"), "Platform API >= 0.7 supports -skip-layers flag")
command := exec.Command("docker", "run", "--rm", restoreImage, "-skip-layers true")
Expand Down Expand Up @@ -188,9 +189,9 @@ func testRestorerFunc(platformAPI string) func(t *testing.T, when spec.G, it spe
})
})

when("using kaniko cache", func() {
it("accepts -build-image", func() {
h.SkipIf(t, api.MustParse(platformAPI).LessThan("0.10"), "Platform API < 0.10 does not use kaniko")
when("restoring builder image metadata", func() {
it("accepts -build-image and saves the metadata to /kaniko/cache", func() {
h.SkipIf(t, api.MustParse(platformAPI).LessThan("0.10"), "Platform API < 0.10 does not restore builder image metadata")
h.DockerRunAndCopy(t,
containerName,
copyDir,
Expand All @@ -208,10 +209,45 @@ func testRestorerFunc(platformAPI string) func(t *testing.T, when spec.G, it spe
h.AssertNil(t, err)
h.AssertStringContains(t, analyzedMD.BuildImage.Reference, restoreRegFixtures.SomeCacheImage+"@sha256:")
t.Log("writes builder manifest and config to the kaniko cache")
ref, err := name.ParseReference(analyzedMD.BuildImage.Reference)
h.AssertNil(t, err)
fis, err := os.ReadDir(filepath.Join(copyDir, "kaniko", "cache", "base"))
h.AssertNil(t, err)
h.AssertEq(t, len(fis), 1)
h.AssertPathExists(t, filepath.Join(copyDir, "kaniko", "cache", "base", ref.Identifier(), "oci-layout"))
})
})

when("restoring run image metadata", func() {
it("saves metadata to /kaniko/cache", func() {
h.SkipIf(t, api.MustParse(platformAPI).LessThan("0.12"), "Platform API < 0.12 does not restore run image metadata")
h.DockerRunAndCopy(t,
containerName,
copyDir,
"/",
restoreImage,
h.WithFlags(
"--env", "CNB_PLATFORM_API="+platformAPI,
"--env", "DOCKER_CONFIG=/docker-config",
"--network", restoreRegNetwork,
),
h.WithArgs(
"-analyzed", "/layers/some-analyzed.toml",
"-log-level", "debug",
),
)
t.Log("updates run image reference in analyzed.toml to include digest")
analyzedMD, err := lifecycle.Config.ReadAnalyzed(filepath.Join(copyDir, "layers", "some-analyzed.toml"))
h.AssertNil(t, err)
h.AssertStringContains(t, analyzedMD.RunImage.Reference, restoreRegFixtures.ReadOnlyRunImage+"@sha256:")
// TODO: assert on target data
t.Log("writes run image manifest and config to the kaniko cache")
ref, err := name.ParseReference(analyzedMD.RunImage.Reference)
h.AssertNil(t, err)
fis, err := os.ReadDir(filepath.Join(copyDir, "kaniko", "cache", "base"))
h.AssertNil(t, err)
h.AssertEq(t, len(fis), 1)
h.AssertPathExists(t, filepath.Join(copyDir, "kaniko", "cache", "base", fis[0].Name(), "oci-layout"))
h.AssertPathExists(t, filepath.Join(copyDir, "kaniko", "cache", "base", ref.Identifier(), "oci-layout"))
})
})
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
val-from-build-config
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
#!/usr/bin/env bash

echo "ENV"
env

plan_path=$2

cat >> "${plan_path}" <<EOL
Expand Down
5 changes: 5 additions & 0 deletions acceptance/testdata/detector/container/cnb/run.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
[[image]]
image = "some-run-image-from-extension"

[[image]]
image = "some-other-run-image"
2 changes: 2 additions & 0 deletions acceptance/testdata/detector/container/layers/analyzed.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
[run-image]
reference = "some-old-run-image"
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
[run-image]
reference = "REPLACE"
extend = true
9 changes: 6 additions & 3 deletions analyzer.go
Original file line number Diff line number Diff line change
Expand Up @@ -191,7 +191,7 @@ func (a *Analyzer) Analyze() (platform.AnalyzedMetadata, error) {
appMeta platform.LayersMetadata
cacheMeta platform.CacheMetadata
previousImageID *platform.ImageIdentifier
runImageID *platform.ImageIdentifier
runImage *platform.RunImage
)

if a.PreviousImage != nil { // Previous image is optional in Platform API >= 0.7
Expand All @@ -212,10 +212,13 @@ func (a *Analyzer) Analyze() (platform.AnalyzedMetadata, error) {
}

if a.RunImage != nil {
runImageID, err = a.getImageIdentifier(a.RunImage)
runImageID, err := a.getImageIdentifier(a.RunImage)
if err != nil {
return platform.AnalyzedMetadata{}, errors.Wrap(err, "identifying run image")
}
if runImageID != nil {
runImage = &platform.RunImage{Reference: runImageID.Reference}
}
}

if a.RestoresLayerMetadata {
Expand All @@ -232,7 +235,7 @@ func (a *Analyzer) Analyze() (platform.AnalyzedMetadata, error) {

return platform.AnalyzedMetadata{
PreviousImage: previousImageID,
RunImage: runImageID,
RunImage: runImage,
Metadata: appMeta,
}, nil
}
Expand Down
2 changes: 1 addition & 1 deletion api/apis.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import (

var (
Platform = newApisMustParse([]string{"0.3", "0.4", "0.5", "0.6", "0.7", "0.8", "0.9", "0.10", "0.11", "0.12"}, []string{"0.3", "0.4", "0.5", "0.6"})
Buildpack = newApisMustParse([]string{"0.2", "0.3", "0.4", "0.5", "0.6", "0.7", "0.8", "0.9"}, []string{"0.2", "0.3", "0.4", "0.5", "0.6"})
Buildpack = newApisMustParse([]string{"0.2", "0.3", "0.4", "0.5", "0.6", "0.7", "0.8", "0.9", "0.10"}, []string{"0.2", "0.3", "0.4", "0.5", "0.6"})
)

type APIs struct {
Expand Down
Loading