Skip to content

Commit

Permalink
Merge pull request #5690 from nalind/compat-scratch-config
Browse files Browse the repository at this point in the history
imagebuildah: make scratch config handling toggleable
  • Loading branch information
openshift-merge-bot[bot] authored Aug 28, 2024
2 parents ef563e1 + f88579d commit 6925972
Show file tree
Hide file tree
Showing 9 changed files with 217 additions and 103 deletions.
5 changes: 5 additions & 0 deletions buildah.go
Original file line number Diff line number Diff line change
Expand Up @@ -377,6 +377,11 @@ type BuilderOptions struct {
// CDIConfigDir is the location of CDI configuration files, if the files in
// the default configuration locations shouldn't be used.
CDIConfigDir string
// CompatScratchConfig controls whether a "scratch" image is created
// with a truly empty configuration, as would have happened in the past
// (when set to true), or with a minimal initial configuration which
// has a working directory set in it.
CompatScratchConfig types.OptionalBool
}

// ImportOptions are used to initialize a Builder from an existing container
Expand Down
17 changes: 16 additions & 1 deletion config.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ func unmarshalConvertedConfig(ctx context.Context, dest interface{}, img types.I
return nil
}

func (b *Builder) initConfig(ctx context.Context, img types.Image, sys *types.SystemContext) error {
func (b *Builder) initConfig(ctx context.Context, sys *types.SystemContext, img types.Image, options *BuilderOptions) error {
if img != nil { // A pre-existing image, as opposed to a "FROM scratch" new one.
rawManifest, manifestMIMEType, err := img.Manifest(ctx)
if err != nil {
Expand Down Expand Up @@ -101,6 +101,21 @@ func (b *Builder) initConfig(ctx context.Context, img types.Image, sys *types.Sy
}
}
}
} else {
if options == nil || options.CompatScratchConfig != types.OptionalBoolTrue {
b.Docker = docker.V2Image{
V1Image: docker.V1Image{
Config: &docker.Config{
WorkingDir: "/",
},
},
}
b.OCIv1 = ociv1.Image{
Config: ociv1.ImageConfig{
WorkingDir: "/",
},
}
}
}

b.setupLogger()
Expand Down
6 changes: 6 additions & 0 deletions define/build.go
Original file line number Diff line number Diff line change
Expand Up @@ -373,4 +373,10 @@ type BuildOptions struct {
// base images or by a VOLUME instruction to be preserved during RUN
// instructions. Newer BuildKit-based docker build doesn't bother.
CompatVolumes types.OptionalBool
// CompatScratchConfig causes the image, if it does not have a base
// image, to begin with a truly empty default configuration instead of
// a minimal default configuration. Newer BuildKit-based docker build
// provides a minimal initial configuration with a working directory
// set in it.
CompatScratchConfig types.OptionalBool
}
2 changes: 1 addition & 1 deletion docs/buildah-copy.1.md
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,7 @@ Users can specify a series of Unix shell glob patterns in an ignore file to
identify files/directories to exclude.

Buildah supports a special wildcard string `**` which matches any number of
directories (including zero). For example, **/*.go will exclude all files that
directories (including zero). For example, `**/*.go` will exclude all files that
end with .go that are found in all directories.

Example .containerignore/.dockerignore file:
Expand Down
2 changes: 2 additions & 0 deletions imagebuildah/executor.go
Original file line number Diff line number Diff line change
Expand Up @@ -162,6 +162,7 @@ type Executor struct {
cdiConfigDir string
compatSetParent types.OptionalBool
compatVolumes types.OptionalBool
compatScratchConfig types.OptionalBool
}

type imageTypeAndHistoryAndDiffIDs struct {
Expand Down Expand Up @@ -320,6 +321,7 @@ func newExecutor(logger *logrus.Logger, logPrefix string, store storage.Store, o
cdiConfigDir: options.CDIConfigDir,
compatSetParent: options.CompatSetParent,
compatVolumes: options.CompatVolumes,
compatScratchConfig: options.CompatScratchConfig,
}
if exec.err == nil {
exec.err = os.Stderr
Expand Down
1 change: 1 addition & 0 deletions imagebuildah/stage_executor.go
Original file line number Diff line number Diff line change
Expand Up @@ -1007,6 +1007,7 @@ func (s *StageExecutor) prepare(ctx context.Context, from string, initializeIBCo
MountLabel: s.executor.mountLabel,
PreserveBaseImageAnns: preserveBaseImageAnnotations,
CDIConfigDir: s.executor.cdiConfigDir,
CompatScratchConfig: s.executor.compatScratchConfig,
}

builder, err = buildah.NewBuilder(ctx, s.executor.store, builderOptions)
Expand Down
2 changes: 1 addition & 1 deletion import.go
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,7 @@ func importBuilderDataFromImage(ctx context.Context, store storage.Store, system
CommonBuildOpts: &CommonBuildOptions{},
}

if err := builder.initConfig(ctx, image, systemContext); err != nil {
if err := builder.initConfig(ctx, systemContext, image, nil); err != nil {
return nil, fmt.Errorf("preparing image configuration: %w", err)
}

Expand Down
2 changes: 1 addition & 1 deletion new.go
Original file line number Diff line number Diff line change
Expand Up @@ -334,7 +334,7 @@ func newBuilder(ctx context.Context, store storage.Store, options BuilderOptions
}
}

if err := builder.initConfig(ctx, src, systemContext); err != nil {
if err := builder.initConfig(ctx, systemContext, src, &options); err != nil {
return nil, fmt.Errorf("preparing image configuration: %w", err)
}

Expand Down
Loading

0 comments on commit 6925972

Please sign in to comment.