Skip to content

Commit

Permalink
Extracted out the common code for Build and Detect
Browse files Browse the repository at this point in the history
Signed-off-by: Rashad Sirajudeen <[email protected]>
  • Loading branch information
rashadism committed Apr 22, 2024
1 parent d669eb1 commit 91d3eda
Showing 1 changed file with 31 additions and 19 deletions.
50 changes: 31 additions & 19 deletions pkg/client/build.go
Original file line number Diff line number Diff line change
Expand Up @@ -296,6 +296,7 @@ func (c *Client) Build(ctx context.Context, opts BuildOptions) error {
if err != nil {
return err
}
defer c.docker.ImageRemove(context.Background(), lifecycleOpts.Builder.Name(), types.ImageRemoveOptions{Force: true})

lifecycleOpts.FetchRunImageWithLifecycleLayer = func(runImageName string) (string, error) {
ephemeralRunImageName := fmt.Sprintf("pack.local/run-image/%x:latest", randString(10))
Expand Down Expand Up @@ -1288,24 +1289,34 @@ func readOnlyVolume(hostPath, targetPath string) string {
}

func (c *Client) ResolveLifecycleOptions(ctx context.Context, opts BuildOptions) (*build.LifecycleOptions, error) {
// detectOnly := opts.DetectOnly
var pathsConfig layoutPathConfig

imageRef, err := c.parseReference(opts)
if err != nil {
return nil, errors.Wrapf(err, "invalid image name '%s'", opts.Image)
type BuildVars struct {
imageRef name.Reference
imgRegistry string
imageName string
}
imgRegistry := imageRef.Context().RegistryStr()
imageName := imageRef.Name()
buildVars := BuildVars{}

if opts.Layout() {
pathsConfig, err = c.processLayoutPath(opts.LayoutConfig.InputImage, opts.LayoutConfig.PreviousInputImage)
detectOnly := opts.DetectOnly

if !detectOnly {
imageRef, err := c.parseReference(opts)
if err != nil {
if opts.LayoutConfig.PreviousInputImage != nil {
return nil, errors.Wrapf(err, "invalid layout paths image name '%s' or previous-image name '%s'", opts.LayoutConfig.InputImage.Name(),
opts.LayoutConfig.PreviousInputImage.Name())
return nil, errors.Wrapf(err, "invalid image name '%s'", opts.Image)
}
buildVars.imgRegistry = imageRef.Context().RegistryStr()
buildVars.imageName = imageRef.Name()
buildVars.imageRef = imageRef

if opts.Layout() {
pathsConfig, err = c.processLayoutPath(opts.LayoutConfig.InputImage, opts.LayoutConfig.PreviousInputImage)
if err != nil {
if opts.LayoutConfig.PreviousInputImage != nil {
return nil, errors.Wrapf(err, "invalid layout paths image name '%s' or previous-image name '%s'", opts.LayoutConfig.InputImage.Name(),
opts.LayoutConfig.PreviousInputImage.Name())
}
return nil, errors.Wrapf(err, "invalid layout paths image name '%s'", opts.LayoutConfig.InputImage.Name())
}
return nil, errors.Wrapf(err, "invalid layout paths image name '%s'", opts.LayoutConfig.InputImage.Name())
}
}

Expand Down Expand Up @@ -1341,7 +1352,7 @@ func (c *Client) ResolveLifecycleOptions(ctx context.Context, opts BuildOptions)
return nil, errors.Wrapf(err, "invalid builder %s", style.Symbol(opts.Builder))
}

runImageName := c.resolveRunImage(opts.RunImage, imgRegistry, builderRef.Context().RegistryStr(), bldr.DefaultRunImage(), opts.AdditionalMirrors, opts.Publish, c.accessChecker)
runImageName := c.resolveRunImage(opts.RunImage, buildVars.imgRegistry, builderRef.Context().RegistryStr(), bldr.DefaultRunImage(), opts.AdditionalMirrors, opts.Publish, c.accessChecker)

fetchOptions := image.FetchOptions{
Daemon: !opts.Publish,
Expand Down Expand Up @@ -1465,7 +1476,6 @@ func (c *Client) ResolveLifecycleOptions(ctx context.Context, opts BuildOptions)
if err != nil {
return nil, err
}
defer c.docker.ImageRemove(context.Background(), ephemeralBuilder.Name(), types.ImageRemoveOptions{Force: true})

if len(bldr.OrderExtensions()) > 0 || len(ephemeralBuilder.OrderExtensions()) > 0 {
if !c.experimental {
Expand Down Expand Up @@ -1517,9 +1527,9 @@ func (c *Client) ResolveLifecycleOptions(ctx context.Context, opts BuildOptions)
}
}

lifecycleOpts := build.LifecycleOptions{
lifecycleOpts := &build.LifecycleOptions{
AppPath: appPath,
Image: imageRef,
Image: buildVars.imageRef,
Builder: ephemeralBuilder,
BuilderImage: builderRef.Name(),
LifecycleImage: ephemeralBuilder.Name(),
Expand All @@ -1546,12 +1556,13 @@ func (c *Client) ResolveLifecycleOptions(ctx context.Context, opts BuildOptions)
UID: opts.UserID,
PreviousImage: opts.PreviousImage,
Interactive: opts.Interactive,
Termui: termui.NewTermui(imageName, ephemeralBuilder, runImageName),
Termui: termui.NewTermui(buildVars.imageName, ephemeralBuilder, runImageName),
ReportDestinationDir: opts.ReportDestinationDir,
SBOMDestinationDir: opts.SBOMDestinationDir,
CreationTime: opts.CreationTime,
Layout: opts.Layout(),
Keychain: c.keychain,
DetectOnly: detectOnly,
}

switch {
Expand All @@ -1564,5 +1575,6 @@ func (c *Client) ResolveLifecycleOptions(ctx context.Context, opts BuildOptions)
return nil, errors.Errorf("Lifecycle %s does not have an associated lifecycle image. Builder must be trusted.", lifecycleVersion.String())
}

return &lifecycleOpts, nil
return lifecycleOpts, nil

}

0 comments on commit 91d3eda

Please sign in to comment.