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

Dynamically determine valid lifecycle #2328

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
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
38 changes: 24 additions & 14 deletions pkg/client/create_builder.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@

pubbldr "github.com/buildpacks/pack/builder"
"github.com/buildpacks/pack/internal/builder"
"github.com/buildpacks/pack/internal/config"
"github.com/buildpacks/pack/internal/paths"
"github.com/buildpacks/pack/internal/style"
"github.com/buildpacks/pack/pkg/buildpack"
Expand Down Expand Up @@ -284,14 +285,20 @@
return nil, errors.Wrapf(err, "%s must be a valid semver", style.Symbol("lifecycle.version"))
}

uri = c.uriFromLifecycleVersion(*v, os, architecture)
uri, err = c.uriFromLifecycleVersion(*v, os, architecture)
if err != nil {
return nil, errors.Wrap(err, "determine lifecycle")
}
case config.URI != "":
uri, err = paths.FilePathToURI(config.URI, relativeBaseDir)
if err != nil {
return nil, err
return nil, errors.Wrap(err, "determine lifecycle")
}
default:
uri = c.uriFromLifecycleVersion(*semver.MustParse(builder.DefaultLifecycleVersion), os, architecture)
uri, err = c.uriFromLifecycleVersion(*semver.MustParse(builder.DefaultLifecycleVersion), os, architecture)
if err != nil {
return nil, errors.Wrap(err, "determine lifecycle")
}
}

blob, err := c.downloader.Download(ctx, uri)
Expand Down Expand Up @@ -434,19 +441,22 @@
return nil
}

func (c *Client) uriFromLifecycleVersion(version semver.Version, os string, architecture string) string {
arch := "x86-64"
func (c *Client) uriFromLifecycleVersion(version semver.Version, os string, architecture string) (string, error) {
image, _ := c.indexFactory.FetchIndex(config.DefaultLifecycleImageRepo, imgutil.FromBaseIndex(config.DefaultLifecycleImageRepo))
manifest, _ := image.IndexManifest()

Check failure on line 446 in pkg/client/create_builder.go

View workflow job for this annotation

GitHub Actions / test (linux)

image.IndexManifest undefined (type imgutil.ImageIndex has no field or method IndexManifest)) (typecheck)

Check failure on line 446 in pkg/client/create_builder.go

View workflow job for this annotation

GitHub Actions / test (linux)

image.IndexManifest undefined (type imgutil.ImageIndex has no field or method IndexManifest)) (typecheck)

Check failure on line 446 in pkg/client/create_builder.go

View workflow job for this annotation

GitHub Actions / test (linux)

image.IndexManifest undefined (type imgutil.ImageIndex has no field or method IndexManifest)) (typecheck)

Check failure on line 446 in pkg/client/create_builder.go

View workflow job for this annotation

GitHub Actions / test (linux)

image.IndexManifest undefined (type imgutil.ImageIndex has no field or method IndexManifest) (typecheck)

Check failure on line 446 in pkg/client/create_builder.go

View workflow job for this annotation

GitHub Actions / test (linux)

image.IndexManifest undefined (type imgutil.ImageIndex has no field or method IndexManifest)) (typecheck)

Check failure on line 446 in pkg/client/create_builder.go

View workflow job for this annotation

GitHub Actions / test (macos)

image.IndexManifest undefined (type imgutil.ImageIndex has no field or method IndexManifest)) (typecheck)

Check failure on line 446 in pkg/client/create_builder.go

View workflow job for this annotation

GitHub Actions / test (macos)

image.IndexManifest undefined (type imgutil.ImageIndex has no field or method IndexManifest)) (typecheck)

Check failure on line 446 in pkg/client/create_builder.go

View workflow job for this annotation

GitHub Actions / test (macos)

image.IndexManifest undefined (type imgutil.ImageIndex has no field or method IndexManifest)) (typecheck)

Check failure on line 446 in pkg/client/create_builder.go

View workflow job for this annotation

GitHub Actions / test (macos)

image.IndexManifest undefined (type imgutil.ImageIndex has no field or method IndexManifest) (typecheck)

Check failure on line 446 in pkg/client/create_builder.go

View workflow job for this annotation

GitHub Actions / test (macos)

image.IndexManifest undefined (type imgutil.ImageIndex has no field or method IndexManifest)) (typecheck)

if os == "windows" {
return fmt.Sprintf("https://github.com/buildpacks/lifecycle/releases/download/v%s/lifecycle-v%s+windows.%s.tgz", version.String(), version.String(), arch)
for _, m := range manifest.Manifests {
if m.Platform.OS == os && m.Platform.Architecture == architecture {
return lifecycleDownloadURL(version, os, architecture), nil
}
}

if builder.SupportedLinuxArchitecture(architecture) {
arch = architecture
} else {
// FIXME: this should probably be an error case in the future, see https://github.com/buildpacks/pack/issues/2163
c.logger.Warnf("failed to find a lifecycle binary for requested architecture %s, defaulting to %s", style.Symbol(architecture), style.Symbol(arch))
}
return "", fmt.Errorf("could not determine lifecyle, unsupported os/arch: %s/%s", os, architecture)
}

return fmt.Sprintf("https://github.com/buildpacks/lifecycle/releases/download/v%s/lifecycle-v%s+linux.%s.tgz", version.String(), version.String(), arch)
func lifecycleDownloadURL(version semver.Version, os, architecture string) string {
if architecture == "amd64" {
architecture = "x86-64"
}
return fmt.Sprintf("https://github.com/buildpacks/lifecycle/releases/download/v%s/lifecycle-v%s+%s.%s.tgz", version.String(), version.String(), os, architecture)
}
41 changes: 41 additions & 0 deletions pkg/client/create_builder_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
"github.com/buildpacks/lifecycle/api"
"github.com/docker/docker/api/types/system"
"github.com/golang/mock/gomock"
v1 "github.com/google/go-containerregistry/pkg/v1"
"github.com/heroku/color"
"github.com/pkg/errors"
"github.com/sclevine/spec"
Expand All @@ -27,7 +28,7 @@
"github.com/buildpacks/pack/pkg/archive"
"github.com/buildpacks/pack/pkg/blob"
"github.com/buildpacks/pack/pkg/buildpack"
"github.com/buildpacks/pack/pkg/client"

Check failure on line 31 in pkg/client/create_builder_test.go

View workflow job for this annotation

GitHub Actions / test (linux)

could not import github.com/buildpacks/pack/pkg/client (-: # github.com/buildpacks/pack/pkg/client [github.com/buildpacks/pack/pkg/client.test]

Check failure on line 31 in pkg/client/create_builder_test.go

View workflow job for this annotation

GitHub Actions / test (macos)

could not import github.com/buildpacks/pack/pkg/client (-: # github.com/buildpacks/pack/pkg/client [github.com/buildpacks/pack/pkg/client.test]
"github.com/buildpacks/pack/pkg/dist"
"github.com/buildpacks/pack/pkg/image"
"github.com/buildpacks/pack/pkg/logging"
Expand All @@ -49,10 +50,12 @@
mockBuildpackDownloader *testmocks.MockBuildpackDownloader
mockImageFactory *testmocks.MockImageFactory
mockImageFetcher *testmocks.MockImageFetcher
mockIndexFactory *testmocks.MockIndexFactory
mockDockerClient *testmocks.MockCommonAPIClient
fakeBuildImage *fakes.Image
fakeRunImage *fakes.Image
fakeRunImageMirror *fakes.Image
fakeLifecycleImage *fakes.ImageIndex

Check failure on line 58 in pkg/client/create_builder_test.go

View workflow job for this annotation

GitHub Actions / test (linux)

undefined: fakes.ImageIndex (typecheck)

Check failure on line 58 in pkg/client/create_builder_test.go

View workflow job for this annotation

GitHub Actions / test (macos)

undefined: fakes.ImageIndex (typecheck)
opts client.CreateBuilderOptions
subject *client.Client
logger logging.Logger
Expand All @@ -68,6 +71,10 @@
mockImageFetcher.EXPECT().Fetch(gomock.Any(), "some/build-image", gomock.Any()).Return(fakeBuildImage, nil)
}

var prepareIndexFetcherWithLifecycleImage = func() {
mockIndexFactory.EXPECT().FetchIndex(gomock.Any(), gomock.Any()).Return(fakeLifecycleImage, nil)
}

var createBuildpack = func(descriptor dist.BuildpackDescriptor) buildpack.BuildModule {
buildpack, err := ifakes.NewFakeBuildpack(descriptor, 0644)
h.AssertNil(t, err)
Expand All @@ -89,6 +96,7 @@
mockDownloader = testmocks.NewMockBlobDownloader(mockController)
mockImageFetcher = testmocks.NewMockImageFetcher(mockController)
mockImageFactory = testmocks.NewMockImageFactory(mockController)
mockIndexFactory = testmocks.NewMockIndexFactory(mockController)
mockDockerClient = testmocks.NewMockCommonAPIClient(mockController)
mockBuildpackDownloader = testmocks.NewMockBuildpackDownloader(mockController)

Expand All @@ -101,6 +109,29 @@
fakeRunImage = fakes.NewImage("some/run-image", "", nil)
h.AssertNil(t, fakeRunImage.SetLabel("io.buildpacks.stack.id", "some.stack.id"))

fakeLifecycleImage = &fakes.ImageIndex{
Manifests: []v1.Descriptor{
{
Platform: &v1.Platform{
OS: "linux",
Architecture: "amd64",
},
},
{
Platform: &v1.Platform{
OS: "linux",
Architecture: "arm64",
},
},
{
Platform: &v1.Platform{
OS: "windows",
Architecture: "amd64",
},
},
},
}

fakeRunImageMirror = fakes.NewImage("localhost:5000/some/run-image", "", nil)
h.AssertNil(t, fakeRunImageMirror.SetLabel("io.buildpacks.stack.id", "some.stack.id"))

Expand All @@ -124,6 +155,7 @@
client.WithDownloader(mockDownloader),
client.WithImageFactory(mockImageFactory),
client.WithFetcher(mockImageFetcher),
client.WithIndexFactory(mockIndexFactory),
client.WithDockerClient(mockDockerClient),
client.WithBuildpackDownloader(mockBuildpackDownloader),
)
Expand Down Expand Up @@ -452,6 +484,7 @@
client.WithDownloader(mockDownloader),
client.WithImageFactory(mockImageFactory),
client.WithFetcher(mockImageFetcher),
client.WithIndexFactory(mockIndexFactory),
client.WithExperimental(true),
)
h.AssertNil(t, err)
Expand Down Expand Up @@ -516,6 +549,7 @@
it("should download from predetermined uri", func() {
prepareFetcherWithBuildImage()
prepareFetcherWithRunImages()
prepareIndexFetcherWithLifecycleImage()
opts.Config.Lifecycle.URI = ""
opts.Config.Lifecycle.Version = "3.4.5"

Expand All @@ -533,6 +567,7 @@
it("should download from predetermined uri for arm64", func() {
prepareFetcherWithBuildImage()
prepareFetcherWithRunImages()
prepareIndexFetcherWithLifecycleImage()
opts.Config.Lifecycle.URI = ""
opts.Config.Lifecycle.Version = "3.4.5"
h.AssertNil(t, fakeBuildImage.SetArchitecture("arm64"))
Expand All @@ -557,12 +592,14 @@
client.WithDownloader(mockDownloader),
client.WithImageFactory(mockImageFactory),
client.WithFetcher(mockImageFetcher),
client.WithIndexFactory(mockIndexFactory),
client.WithExperimental(true),
)
h.AssertNil(t, err)

prepareFetcherWithBuildImage()
prepareFetcherWithRunImages()
prepareIndexFetcherWithLifecycleImage()
opts.Config.Lifecycle.URI = ""
opts.Config.Lifecycle.Version = "3.4.5"
h.AssertNil(t, fakeBuildImage.SetOS("windows"))
Expand All @@ -584,6 +621,7 @@
it("should download default lifecycle", func() {
prepareFetcherWithBuildImage()
prepareFetcherWithRunImages()
prepareIndexFetcherWithLifecycleImage()
opts.Config.Lifecycle.URI = ""
opts.Config.Lifecycle.Version = ""

Expand All @@ -605,6 +643,7 @@
it("should download default lifecycle on arm64", func() {
prepareFetcherWithBuildImage()
prepareFetcherWithRunImages()
prepareIndexFetcherWithLifecycleImage()
opts.Config.Lifecycle.URI = ""
opts.Config.Lifecycle.Version = ""
h.AssertNil(t, fakeBuildImage.SetArchitecture("arm64"))
Expand Down Expand Up @@ -633,12 +672,14 @@
client.WithDownloader(mockDownloader),
client.WithImageFactory(mockImageFactory),
client.WithFetcher(mockImageFetcher),
client.WithIndexFactory(mockIndexFactory),
client.WithExperimental(true),
)
h.AssertNil(t, err)

prepareFetcherWithBuildImage()
prepareFetcherWithRunImages()
prepareIndexFetcherWithLifecycleImage()
opts.Config.Lifecycle.URI = ""
opts.Config.Lifecycle.Version = ""
h.AssertNil(t, fakeBuildImage.SetOS("windows"))
Expand Down
Loading