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

Support docker uri for lifecycle #2112

Open
wants to merge 6 commits into
base: main
Choose a base branch
from
Open
Changes from 2 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
77 changes: 77 additions & 0 deletions pkg/client/create_builder.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,12 @@
package client

import (
"archive/tar"
"context"
"fmt"
"io"
OS "os"
"path/filepath"
"sort"
"strings"

Expand Down Expand Up @@ -212,6 +216,43 @@ func (c *Client) fetchLifecycle(ctx context.Context, config pubbldr.LifecycleCon
var uri string
var err error
switch {
case buildpack.HasDockerLocator(config.URI):
rashadism marked this conversation as resolved.
Show resolved Hide resolved
var lifecycleImage imgutil.Image
imageName := buildpack.ParsePackageLocator(config.URI)
c.logger.Debugf("Downloading lifecycle image: %s", style.Symbol(imageName))

lifecycleImage, err = c.imageFetcher.Fetch(ctx, imageName, image.FetchOptions{Daemon: false})
Copy link
Member

Choose a reason for hiding this comment

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

I think, we need to passthrough some key fetch options: Daemon, PullPolicy, Platform, those values should be available from method caller and we need to configure the Fetcher with them

if err != nil {
return nil, err
}

lifecyclePath := filepath.Join(relativeBaseDir, "lifecycle.tar")
layers, err := lifecycleImage.UnderlyingImage().Layers()
if err != nil {
return nil, err
}

// Assume the last layer has the lifecycle
lifecycleLayer := layers[len(layers)-1]

layerReader, err := lifecycleLayer.Uncompressed()
if err != nil {
return nil, err
}
defer layerReader.Close()

file, err := stripTopDirAndWrite(layerReader, lifecyclePath)
if err != nil {
return nil, err
}

defer file.Close()
// defer OS.Remove(lifecyclePath)
rashadism marked this conversation as resolved.
Show resolved Hide resolved

uri, err = paths.FilePathToURI(lifecyclePath, "")
rashadism marked this conversation as resolved.
Show resolved Hide resolved
if err != nil {
return nil, err
}
case config.Version != "":
v, err := semver.NewVersion(config.Version)
if err != nil {
Expand Down Expand Up @@ -361,3 +402,39 @@ func uriFromLifecycleVersion(version semver.Version, os string, architecture str

return fmt.Sprintf("https://github.com/buildpacks/lifecycle/releases/download/v%s/lifecycle-v%s+linux.%s.tgz", version.String(), version.String(), arch)
}

func stripTopDirAndWrite(layerReader io.ReadCloser, outputPath string) (*OS.File, error) {
file, err := OS.Create(outputPath)
if err != nil {
return nil, err
}

tarWriter := tar.NewWriter(file)
tarReader := tar.NewReader(layerReader)
tarReader.Next()
rashadism marked this conversation as resolved.
Show resolved Hide resolved

for {
header, err := tarReader.Next()
if err == io.EOF {
break
}
if err != nil {
return nil, err
}

pathSep := string(OS.PathSeparator)
cnbPrefix := fmt.Sprintf("%scnb%s", pathSep, pathSep)
newHeader := *header
newHeader.Name = strings.TrimPrefix(header.Name, cnbPrefix)

if err := tarWriter.WriteHeader(&newHeader); err != nil {
return nil, err
}

if _, err := io.Copy(tarWriter, tarReader); err != nil {
return nil, err
}
}

return file, nil
}
Loading