Skip to content

Commit

Permalink
WIP - I removed the layout-repo volume and now every input (run-image…
Browse files Browse the repository at this point in the history
…, app image destination and previous image) is mounting independently

Signed-off-by: Juan Bustamante <[email protected]>
  • Loading branch information
jjbustamante committed Feb 6, 2023
1 parent a8a4c79 commit 7146ec9
Show file tree
Hide file tree
Showing 9 changed files with 231 additions and 166 deletions.
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,7 @@ go 1.19
// Ensure compatibility with lifecycle/kaniko; match dependencies configured in:
// https://github.com/GoogleContainerTools/kaniko/blob/f9aaa9fca7bf4077778ed527c1a8a6e09e60c53c/go.mod (v1.9.1)
replace (
github.com/buildpacks/imgutil => ../../buildpacks/imgutil
github.com/docker/docker => github.com/docker/docker v20.10.21+incompatible
github.com/moby/buildkit => github.com/moby/buildkit v0.8.3
github.com/opencontainers/runc => github.com/opencontainers/runc v1.0.0-rc95
Expand Down
8 changes: 0 additions & 8 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -269,14 +269,6 @@ github.com/bugsnag/osext v0.0.0-20130617224835-0dd3f918b21b/go.mod h1:obH5gd0Bsq
github.com/bugsnag/panicwrap v0.0.0-20151223152923-e2c28503fcd0/go.mod h1:D/8v3kj0zr8ZAKg1AQ6crr+5VwKN5eIywRkfhyM/+dE=
github.com/buildpacks/lifecycle v0.15.3 h1:jjYc0sclsxyCpBQoMkAQKynRrhuRbXXCj9eH7ApUOYQ=
github.com/buildpacks/lifecycle v0.15.3/go.mod h1:vNhpm82BD9JlzZbFP9JXYt2tMBlIf9EA20VtZ0PkyRE=
github.com/buildpacks/imgutil v0.0.0-20221109161319-3252b1f13dfa h1:g72s4SWu3zI2FY/0Vu4IAoc9UPbGmy5/IR+pJT3SRB4=
github.com/buildpacks/imgutil v0.0.0-20221109161319-3252b1f13dfa/go.mod h1:/aefwfzb14yQfd4KauUS5B0UVJpi+Fno25mh/Us81Ak=
github.com/buildpacks/imgutil v0.0.0-20230111234127-22d7d62c19c1 h1:FLpmSOrnV8IpUaxWx8auEuth3y32NlrZXYlVMYgBjAI=
github.com/buildpacks/imgutil v0.0.0-20230111234127-22d7d62c19c1/go.mod h1:7XJyKf8MwQfHcsjjA9et24BWN3gE1FztxnN8de54mL0=
github.com/buildpacks/imgutil v0.0.0-20230117181026-e48cccced975 h1:g3rQ/EvRGsdP1NJ84Cu3gZuscDbMGXIpa7q1VEdSIOk=
github.com/buildpacks/imgutil v0.0.0-20230117181026-e48cccced975/go.mod h1:7XJyKf8MwQfHcsjjA9et24BWN3gE1FztxnN8de54mL0=
github.com/buildpacks/imgutil v0.0.0-20230117193450-b75b1d22c377 h1:8vefwU3GGCtD/B87ybEF03BwjNq6ZfuMe7qIBjXT5Fk=
github.com/buildpacks/imgutil v0.0.0-20230117193450-b75b1d22c377/go.mod h1:7XJyKf8MwQfHcsjjA9et24BWN3gE1FztxnN8de54mL0=
github.com/bwesterb/go-ristretto v1.2.0/go.mod h1:fUIoIZaG73pV5biE2Blr2xEzDoMj7NFEuV9ekS419A0=
github.com/caarlos0/ctrlc v1.0.0/go.mod h1:CdXpj4rmq0q/1Eb44M9zi2nKB0QraNKuRGYGrrHhcQw=
github.com/campoy/unique v0.0.0-20180121183637-88950e537e7e/go.mod h1:9IOqJGCPMSc6E5ydlp5NIonxObaeu/Iub/X03EKPVYo=
Expand Down
52 changes: 3 additions & 49 deletions internal/build/lifecycle_execution.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,8 @@ import (
"context"
"fmt"
"math/rand"
"os"
"path/filepath"
"strconv"

"github.com/buildpacks/imgutil/layout"

"github.com/buildpacks/lifecycle/api"
"github.com/buildpacks/lifecycle/auth"
"github.com/docker/docker/client"
Expand All @@ -35,7 +31,6 @@ type LifecycleExecution struct {
docker client.CommonAPIClient
platformAPI *api.Version
layersVolume string
layoutVolume string
appVolume string
os string
mountPaths mountPaths
Expand All @@ -61,7 +56,6 @@ func NewLifecycleExecution(logger logging.Logger, docker client.CommonAPIClient,
docker: docker,
layersVolume: paths.FilterReservedNames("pack-layers-" + randString(10)),
appVolume: paths.FilterReservedNames("pack-app-" + randString(10)),
layoutVolume: paths.FilterReservedNames("pack-layout-" + randString(10)),
platformAPI: latestSupportedPlatformAPI,
opts: opts,
os: osType,
Expand Down Expand Up @@ -259,11 +253,6 @@ func (l *LifecycleExecution) Cleanup() error {
if err := l.docker.VolumeRemove(context.Background(), l.appVolume, true); err != nil {
reterr = errors.Wrapf(err, "failed to clean up app volume %s", l.appVolume)
}
if l.opts.ImageDestinationDir != "" {
if err := l.docker.VolumeRemove(context.Background(), l.layoutVolume, true); err != nil {
reterr = errors.Wrapf(err, "failed to clean up app volume %s", l.layoutVolume)
}
}
return reterr
}

Expand Down Expand Up @@ -345,15 +334,15 @@ func (l *LifecycleExecution) Create(ctx context.Context, buildCache, launchCache
withEnv,
}

if l.opts.ImageDestinationDir != "" {
if l.opts.Layout {
var err error
opts, err = l.appendLayoutOperations(opts)
if err != nil {
return err
}
}

if l.opts.Publish || l.opts.ImageDestinationDir != "" {
if l.opts.Publish || l.opts.Layout {
authConfig, err := auth.BuildEnvVar(authn.DefaultKeychain, l.opts.Image.String(), l.opts.RunImage, l.opts.CacheImage, l.opts.PreviousImage)
if err != nil {
return err
Expand Down Expand Up @@ -758,37 +747,7 @@ func (l *LifecycleExecution) hasExtensions() bool {
}

func (l *LifecycleExecution) appendLayoutOperations(opts []PhaseConfigProviderOperation) ([]PhaseConfigProviderOperation, error) {
runImageRef, err := layout.ParseRefToPath(l.opts.RunImage)
if err != nil {
return nil, fmt.Errorf("invalid run image name: %s", err)
}

outputImageRef, err := layout.ParseRefToPath(l.opts.Image.Name())
if err != nil {
return nil, fmt.Errorf("invalid image name: %s", err)
}

prevImage := l.opts.Image.Name()
if l.opts.PreviousImage != "" {
prevImage = l.opts.PreviousImage
}

prevImageRef, err := layout.ParseRefToPath(prevImage)
if err != nil {
return nil, fmt.Errorf("invalid previous name: %s", err)
}

containerOutputImagePath := filepath.Join(l.mountPaths.LayoutRepoDir(), outputImageRef)
localRunImagePath := filepath.Join(l.opts.LayoutRepoDir, runImageRef)
containerRunImagePath := filepath.Join(l.mountPaths.LayoutRepoDir(), runImageRef)
containerPreviousImagePath := filepath.Join(l.mountPaths.LayoutRepoDir(), prevImageRef)

opts = append(opts, WithEnv("CNB_USE_LAYOUT=true", "CNB_EXPERIMENTAL_MODE=warn"),
WithContainerOperations(CopyDir(localRunImagePath, containerRunImagePath, l.opts.Builder.UID(), l.opts.Builder.GID(), l.os, true, nil)),
If(exists(l.opts.PreviousImageDir),
WithContainerOperations(CopyDir(l.opts.PreviousImageDir, containerPreviousImagePath, l.opts.Builder.UID(), l.opts.Builder.GID(), l.os, true, nil))),
WithPostContainerRunOperations(CopyOutTo(containerOutputImagePath, l.opts.ImageDestinationDir)))

opts = append(opts, WithEnv("CNB_USE_LAYOUT=true", "CNB_EXPERIMENTAL_MODE=warn"))
return opts, nil
}

Expand All @@ -802,8 +761,3 @@ func addTags(flags, additionalTags []string) []string {
}
return flags
}

func exists(path string) bool {
_, err := os.Stat(path)
return !os.IsNotExist(err)
}
5 changes: 1 addition & 4 deletions internal/build/lifecycle_executor.go
Original file line number Diff line number Diff line change
Expand Up @@ -82,13 +82,13 @@ type LifecycleOptions struct {
TrustBuilder bool
UseCreator bool
Interactive bool
Layout bool
Termui Termui
DockerHost string
Cache cache.CacheOpts
CacheImage string
HTTPProxy string
HTTPSProxy string
LayoutPath string
NoProxy string
Network string
AdditionalTags []string
Expand All @@ -98,12 +98,9 @@ type LifecycleOptions struct {
Workspace string
GID int
PreviousImage string
PreviousImageDir string
ReportDestinationDir string
SBOMDestinationDir string
CreationTime *time.Time
ImageDestinationDir string
LayoutRepoDir string
}

func NewLifecycleExecutor(logger logging.Logger, docker client.CommonAPIClient) *LifecycleExecutor {
Expand Down
4 changes: 0 additions & 4 deletions internal/build/mount_paths.go
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,3 @@ func (m mountPaths) launchCacheDir() string {
func (m mountPaths) sbomDir() string {
return m.join(m.volume, "layers", "sbom")
}

func (m mountPaths) LayoutRepoDir() string {
return m.join(m.volume, "layout-repo")
}
6 changes: 0 additions & 6 deletions internal/build/phase_config_provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,12 +59,6 @@ func NewPhaseConfigProvider(name string, lifecycleExec *LifecycleExecution, ops
}...),
)

if lifecycleExec.opts.ImageDestinationDir != "" {
ops = append(ops, WithBinds([]string{
fmt.Sprintf("%s:%s", lifecycleExec.layoutVolume, lifecycleExec.mountPaths.LayoutRepoDir()),
}...))
}

for _, op := range ops {
op(provider)
}
Expand Down
42 changes: 15 additions & 27 deletions internal/commands/build.go
Original file line number Diff line number Diff line change
Expand Up @@ -69,19 +69,12 @@ func Build(logger logging.Logger, cfg config.Config, packClient PackClient) *cob
"be provided directly to build using `--builder`, or can be set using the `set-default-builder` command. For more " +
"on how to use `pack build`, see: https://buildpacks.io/docs/app-developer-guide/build-an-app/.",
RunE: logError(logger, func(cmd *cobra.Command, args []string) error {
imageName := args[0]
previousImage := flags.PreviousImage
userProvidedLayoutPath := parseLayoutDestinationPath(imageName)
if err := validateBuildFlags(&flags, cfg, packClient, userProvidedLayoutPath, logger); err != nil {
inputImageName := client.ParseInputImageReference(args[0])
if err := validateBuildFlags(&flags, cfg, inputImageName, logger); err != nil {
return err
}
if userProvidedLayoutPath != "" {
imageName = userProvidedLayoutPath
logger.Debugf("Using layout repository directory at %s", cfg.LayoutRepositoryDir)
if previousImage != "" {
previousImage = parseLayoutDestinationPath(previousImage)
}
}

inputPreviousImage := client.ParseInputImageReference(flags.PreviousImage)

descriptor, actualDescriptorPath, err := parseProjectToml(flags.AppPath, flags.DescriptorPath)
if err != nil {
Expand Down Expand Up @@ -159,7 +152,7 @@ func Build(logger logging.Logger, cfg config.Config, packClient PackClient) *cob
AdditionalTags: flags.AdditionalTags,
RunImage: flags.RunImage,
Env: env,
Image: imageName,
Image: inputImageName.Name(),
Publish: flags.Publish,
DockerHost: flags.DockerHost,
PullPolicy: pullPolicy,
Expand All @@ -180,20 +173,23 @@ func Build(logger logging.Logger, cfg config.Config, packClient PackClient) *cob
Workspace: flags.Workspace,
LifecycleImage: lifecycleImage,
GroupID: gid,
PreviousImage: previousImage,
PreviousImage: inputPreviousImage.Name(),
Interactive: flags.Interactive,
SBOMDestinationDir: flags.SBOMDestinationDir,
ReportDestinationDir: flags.ReportDestinationDir,
CreationTime: dateTime,
PreBuildpacks: flags.PreBuildpacks,
PostBuildpacks: flags.PostBuildpacks,
LayoutAppPath: userProvidedLayoutPath,
LayoutRepoDir: cfg.LayoutRepositoryDir,
Sparse: flags.Sparse,
LayoutConfig: client.LayoutConfig{
Sparse: flags.Sparse,
InputImage: inputImageName,
PreviousInputImage: inputPreviousImage,
LayoutRepoDir: cfg.LayoutRepositoryDir,
},
}); err != nil {
return errors.Wrap(err, "failed to build")
}
logger.Infof("Successfully built image %s", style.Symbol(imageName))
logger.Infof("Successfully built image %s", style.Symbol(inputImageName.Name()))
return nil
}),
}
Expand Down Expand Up @@ -267,7 +263,7 @@ This option may set DOCKER_HOST environment variable for the build container if
}
}

func validateBuildFlags(flags *BuildFlags, cfg config.Config, packClient PackClient, layoutPath string, logger logging.Logger) error {
func validateBuildFlags(flags *BuildFlags, cfg config.Config, inputImageRef client.InputImageReference, logger logging.Logger) error {
if flags.Registry != "" && !cfg.Experimental {
return client.NewExperimentError("Support for buildpack registries is currently experimental.")
}
Expand Down Expand Up @@ -296,7 +292,7 @@ func validateBuildFlags(flags *BuildFlags, cfg config.Config, packClient PackCli
return client.NewExperimentError("Interactive mode is currently experimental.")
}

if layoutPath != "" && !cfg.Experimental {
if inputImageRef.Layout() && !cfg.Experimental {
return client.NewExperimentError("Exporting to OCI layout is currently experimental.")
}

Expand Down Expand Up @@ -366,11 +362,3 @@ func parseProjectToml(appPath, descriptorPath string) (projectTypes.Descriptor,
descriptor, err := project.ReadProjectDescriptor(actualPath)
return descriptor, actualPath, err
}

func parseLayoutDestinationPath(imageName string) string {
if strings.HasPrefix(imageName, "oci:") {
imageNameParsed := strings.SplitN(imageName, ":", 2)
return imageNameParsed[1]
}
return ""
}
Loading

0 comments on commit 7146ec9

Please sign in to comment.