Skip to content

Commit

Permalink
add helper functions for extend using daemon
Browse files Browse the repository at this point in the history
Signed-off-by: Darshan Kumar <[email protected]>
  • Loading branch information
itsdarshankumar committed Jun 2, 2023
1 parent 52902b0 commit 12db41c
Show file tree
Hide file tree
Showing 4 changed files with 126 additions and 5 deletions.
1 change: 1 addition & 0 deletions internal/build/docker.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,4 +21,5 @@ type DockerClient interface {
ContainerInspect(ctx context.Context, container string) (types.ContainerJSON, error)
ContainerRemove(ctx context.Context, container string, options types.ContainerRemoveOptions) error
CopyToContainer(ctx context.Context, container, path string, content io.Reader, options types.CopyToContainerOptions) error
ImageBuild(ctx context.Context, buildContext io.Reader, options types.ImageBuildOptions) (types.ImageBuildResponse, error)
}
89 changes: 89 additions & 0 deletions internal/build/helper.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
package build

import (
"fmt"
"os"
"path/filepath"
"strings"

"github.com/BurntSushi/toml"
"github.com/buildpacks/lifecycle/buildpack"
"github.com/buildpacks/lifecycle/cmd"
"github.com/buildpacks/pack/pkg/logging"
)

const (
DockerfileKindBuild = "build"
DockerfileKindRun = "run"
)

type Extensions struct {
Extensions []buildpack.GroupElement
}

func (extensions *Extensions) DockerFiles(kind string, path string, logger logging.Logger) ([]buildpack.DockerfileInfo, error) {
var dockerfiles []buildpack.DockerfileInfo
for _, ext := range extensions.Extensions {
dockerfile, err := extensions.ReadDockerFile(path, kind, ext.ID)
if err != nil {
return nil, err
}
if dockerfile != nil {
logger.Debugf("Found %s Dockerfile for extension '%s'", kind, ext.ID)
switch kind {
case DockerfileKindBuild:
// will implement later
case DockerfileKindRun:
buildpack.ValidateRunDockerfile(dockerfile, logger)
default:
return nil, fmt.Errorf("unknown dockerfile kind: %s", kind)
}
dockerfiles = append(dockerfiles, *dockerfile)
}
}
return dockerfiles, nil
}

func (extensions *Extensions) ReadDockerFile(path string, kind string, extID string) (*buildpack.DockerfileInfo, error) {
dockerfilePath := filepath.Join(path, kind, escapeID(extID), "Dockerfile")
if _, err := os.Stat(dockerfilePath); err != nil {
return nil, nil
}
return &buildpack.DockerfileInfo{
ExtensionID: extID,
Kind: kind,
Path: dockerfilePath,
}, nil
}

func (extensions *Extensions) SetExtensions(path string, logger logging.Logger) error {
groupExt, err := readExtensionsGroup(path)
if err != nil {
return fmt.Errorf("reading group: %w", err)
}
for i := range groupExt {
groupExt[i].Extension = true
}
for _, groupEl := range groupExt {
if err = cmd.VerifyBuildpackAPI(groupEl.Kind(), groupEl.String(), groupEl.API, logger); err != nil {
return err
}
}
extensions.Extensions = groupExt
fmt.Println("extensions.Extensions", extensions.Extensions)
return nil
}

func readExtensionsGroup(path string) ([]buildpack.GroupElement, error) {
var group buildpack.Group
_, err := toml.DecodeFile(filepath.Join(path, "group.toml"), &group)
for e := range group.GroupExtensions {
group.GroupExtensions[e].Extension = true
group.GroupExtensions[e].Optional = true
}
return group.GroupExtensions, err
}

func escapeID(id string) string {
return strings.ReplaceAll(id, "/", "_")
}
40 changes: 35 additions & 5 deletions internal/build/lifecycle_execution.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,12 @@ import (
"os"
"path/filepath"
"strconv"
"time"

"github.com/BurntSushi/toml"
"github.com/buildpacks/pack/pkg/cache"

"github.com/BurntSushi/toml"
// "github.com/buildpacks/pack/pkg/archive"
"github.com/buildpacks/lifecycle/api"
"github.com/buildpacks/lifecycle/auth"
"github.com/google/go-containerregistry/pkg/authn"
Expand Down Expand Up @@ -247,10 +249,17 @@ func (l *LifecycleExecution) Run(ctx context.Context, phaseFactoryCreator PhaseF
}

if l.platformAPI.AtLeast("0.12") && l.hasExtensionsForRun() {
group.Go(func() error {
l.logger.Info(style.Step("EXTENDING (RUN)"))
return l.ExtendRun(ctx, buildCache, phaseFactory)
})
if l.opts.Publish {
group.Go(func() error {
l.logger.Info(style.Step("EXTENDING (RUN)"))
return l.ExtendRun(ctx, buildCache, phaseFactory)
})
} else {
group.Go(func() error {
l.logger.Info(style.Step("EXTENDING (RUN) BY DAEMON"))
return l.ExtendRunByDaemon(ctx)
})
}
}

if err := group.Wait(); err != nil {
Expand Down Expand Up @@ -413,6 +422,10 @@ func (l *LifecycleExecution) Detect(ctx context.Context, phaseFactory PhaseFacto
CopyOutToMaybe(filepath.Join(l.mountPaths.layersDir(), "analyzed.toml"), l.tmpDir))),
If(l.hasExtensions(), WithPostContainerRunOperations(
CopyOutToMaybe(filepath.Join(l.mountPaths.layersDir(), "generated", "build"), l.tmpDir))),
If(l.hasExtensions(), WithPostContainerRunOperations(
CopyOutToMaybe(filepath.Join(l.mountPaths.layersDir(), "generated", "run"), l.tmpDir))),
If(l.hasExtensions(), WithPostContainerRunOperations(
CopyOutToMaybe(filepath.Join(l.mountPaths.layersDir(), "group.toml"), l.tmpDir))),
envOp,
)

Expand Down Expand Up @@ -707,6 +720,23 @@ func (l *LifecycleExecution) ExtendRun(ctx context.Context, buildCache Cache, ph
return extend.Run(ctx)
}

func (l *LifecycleExecution) ExtendRunByDaemon(ctx context.Context) error {
var extensions Extensions
l.logger.Debugf("extending run image %s", l.opts.RunImage)
fmt.Println("tmpDir: ",l.tmpDir)
extensions.SetExtensions(l.tmpDir,l.logger)
dockerfiles, err := extensions.DockerFiles(DockerfileKindRun,l.tmpDir,l.logger)
if err != nil {
return fmt.Errorf("getting %s.Dockerfiles: %w", DockerfileKindRun, err)
}
fmt.Println("Dockerfiles: ",dockerfiles)
fmt.Println("extend: ",dockerfiles[1].Extend)
time.Sleep(10 * time.Minute)
return nil;
// buildContest := archive.ReadDirAsTar(filepath.Join(l.tmpDir,"generated","run"),"/",0,0,-1,true,false)
}


func determineDefaultProcessType(platformAPI *api.Version, providedValue string) string {
shouldSetForceDefault := platformAPI.Compare(api.MustParse("0.4")) >= 0 &&
platformAPI.Compare(api.MustParse("0.6")) < 0
Expand Down
1 change: 1 addition & 0 deletions pkg/client/docker.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,4 +28,5 @@ type DockerClient interface {
ContainerWait(ctx context.Context, container string, condition containertypes.WaitCondition) (<-chan containertypes.WaitResponse, <-chan error)
ContainerAttach(ctx context.Context, container string, options types.ContainerAttachOptions) (types.HijackedResponse, error)
ContainerStart(ctx context.Context, container string, options types.ContainerStartOptions) error
ImageBuild(ctx context.Context, buildContext io.Reader, options types.ImageBuildOptions) (types.ImageBuildResponse, error)
}

0 comments on commit 12db41c

Please sign in to comment.