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 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
84 changes: 84 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 @@ -264,6 +268,11 @@ 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
uri, err = c.uriFromLifecycleImage(ctx, relativeBaseDir, config)
if err != nil {
return nil, errors.Wrap(err, "Could not parse uri from lifecycle image")
}
case config.Version != "":
v, err := semver.NewVersion(config.Version)
if err != nil {
Expand Down Expand Up @@ -436,3 +445,78 @@ func (c *Client) uriFromLifecycleVersion(version semver.Version, os string, arch

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
}

func (c *Client) uriFromLifecycleImage(ctx context.Context, basePath string, config pubbldr.LifecycleConfig) (uri string, err error) {
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})
if err != nil {
return "", nil
}

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

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

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

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

defer file.Close()

uri, err = paths.FilePathToURI(lifecyclePath, "")
if err != nil {
return "", err
}
return uri, err
}
Loading