From 0c730b99979efba71586660dfae30bb905873248 Mon Sep 17 00:00:00 2001 From: Rashad Sirajudeen Date: Fri, 19 Apr 2024 15:43:15 +0530 Subject: [PATCH] dove into the layers, making progress --- pkg/client/create_builder.go | 101 ++++++++++++++++++++++------------- 1 file changed, 63 insertions(+), 38 deletions(-) diff --git a/pkg/client/create_builder.go b/pkg/client/create_builder.go index d8f2c210e..1d28d64f9 100644 --- a/pkg/client/create_builder.go +++ b/pkg/client/create_builder.go @@ -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" ) @@ -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)) @@ -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)