Skip to content

Commit

Permalink
dove into the layers, making progress
Browse files Browse the repository at this point in the history
  • Loading branch information
rashadism committed Apr 19, 2024
1 parent 80af078 commit 0c730b9
Showing 1 changed file with 63 additions and 38 deletions.
101 changes: 63 additions & 38 deletions pkg/client/create_builder.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@ import (
"github.com/buildpacks/pack/internal/builder"
"github.com/buildpacks/pack/internal/paths"
"github.com/buildpacks/pack/internal/style"
"github.com/buildpacks/pack/pkg/blob"
"github.com/buildpacks/pack/pkg/buildpack"
"github.com/buildpacks/pack/pkg/image"
)
Expand Down Expand Up @@ -223,7 +222,7 @@ func (c *Client) fetchLifecycle(ctx context.Context, config pubbldr.LifecycleCon
return nil, err
}
var lifecycleImage imgutil.Image
var blob blob.Blob
// var blob blob.Blob
imageName := buildpack.ParsePackageLocator(config.URI)
c.logger.Debugf("Downloading lifecycle image: %s", style.Symbol(imageName))

Expand All @@ -234,50 +233,76 @@ func (c *Client) fetchLifecycle(ctx context.Context, config pubbldr.LifecycleCon
}

// Good till this point

lifecycleImageTar := filepath.Join(relativeBaseDir, "lifecycle-image.tar")
lifecycleImageReader, err := c.docker.ImageSave(context.Background(), []string{lifecycleImage.Name()})
if err != nil {
return nil, err
}
defer lifecycleImageReader.Close()
lifecycleImageWriter, err := os.Create(lifecycleImageTar)
layers, err := lifecycleImage.UnderlyingImage().Layers()
if err != nil {
return nil, err
}
defer lifecycleImageWriter.Close()
if _, err = io.Copy(lifecycleImageWriter, lifecycleImageReader); err != nil {
return nil, err
}
lifecycleLayerName, err := LifecycleLayerName(lifecycleImageTar)
if err != nil {
return nil, err
}
if lifecycleLayerName == "" {
return nil, errors.New("failed to find lifecycle layer")
}

lifecycleLayerTar, err := LifecycleLayerTar(lifecycleImageTar, lifecycleLayerName)
if err != nil {
return nil, err
}

strippedTar, err := stripTopLevelDir(lifecycleLayerTar)
if err != nil {
return nil, err
}
for _, layer := range layers {
diffID, err := layer.DiffID()
if err != nil {
return nil, err
}
layerContent, err := lifecycleImage.GetLayer(diffID.String())
if err != nil {
panic(err)
}
defer layerContent.Close()

// Extract the layer tarball
tarReader := tar.NewReader(layerContent)
fmt.Println("--------------------------")
fmt.Print(diffID)
fmt.Println("--------------------------")

for {
header, err := tarReader.Next()
if err == io.EOF {
break // End of archive
}
if err != nil {
panic(err)
}

blob, err = c.downloader.Download(ctx, strippedTar)
if err != nil {
return nil, errors.Wrap(err, "downloading lifecycle")
}
if header.Name == "cnb" && header.Typeflag == tar.TypeDir {
fmt.Println(header.Name)
}
}
fmt.Println("--------------------------")

lifecycle, err := builder.NewLifecycle(blob)
if err != nil {
return nil, errors.Wrap(err, "invalid lifecycle")
}

return lifecycle, nil
// lifecycleImageTar := filepath.Join(relativeBaseDir, "lifecycle-image.tar")
// lifecycleImageReader, err := c.docker.ImageSave(context.Background(), []string{lifecycleImage.Name()})
// if err != nil {
// return nil, err
// }
// defer lifecycleImageReader.Close()
// lifecycleImageWriter, err := os.Create(lifecycleImageTar)
// if err != nil {
// return nil, err
// }
// defer lifecycleImageWriter.Close()
// if _, err = io.Copy(lifecycleImageWriter, lifecycleImageReader); err != nil {
// return nil, err
// }
// lifecycleLayerName, err := LifecycleLayerName(lifecycleImageTar)
// if err != nil {
// return nil, err
// }
// if lifecycleLayerName == "" {
// return nil, errors.New("failed to find lifecycle layer")
// }

// lifecycleLayerTar, err := LifecycleLayerTar(lifecycleImageTar, lifecycleLayerName)
// if err != nil {
// return nil, err
// }

// uri, err = stripTopLevelDir(lifecycleLayerTar)
// if err != nil {
// return nil, err
// }

case config.Version != "":
v, err := semver.NewVersion(config.Version)
Expand Down

0 comments on commit 0c730b9

Please sign in to comment.