Skip to content

Commit

Permalink
Merge pull request #3676 from rhatdan/name
Browse files Browse the repository at this point in the history
Allow callers to replace the ContainerPrefix
  • Loading branch information
openshift-merge-robot committed Dec 21, 2021
2 parents c5ef5c0 + a73e108 commit 0bd4dd1
Show file tree
Hide file tree
Showing 7 changed files with 22 additions and 0 deletions.
2 changes: 2 additions & 0 deletions buildah.go
Original file line number Diff line number Diff line change
Expand Up @@ -257,6 +257,8 @@ type BuilderOptions struct {
// or "scratch" to indicate that the container should not be based on
// an image.
FromImage string
// ContainerSuffix is the suffix to add for generated container names
ContainerSuffix string
// Container is a desired name for the build container.
Container string
// PullPolicy decides whether or not we should pull the image that
Expand Down
8 changes: 8 additions & 0 deletions cmd/buildah/from.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,8 @@ type fromReply struct {
*buildahcli.NameSpaceResults
}

var suffix string

func init() {
var (
fromDescription = "\n Creates a new working container, either from scratch or using a specified\n image as a starting point."
Expand Down Expand Up @@ -74,6 +76,11 @@ func init() {
flags.BoolVar(&opts.pullNever, "pull-never", false, "do not pull the image, use the image present in store if available")
flags.BoolVarP(&opts.quiet, "quiet", "q", false, "don't output progress information when pulling images")
flags.StringVar(&opts.signaturePolicy, "signature-policy", "", "`pathname` of signature policy file (not usually used)")
flags.StringVar(&suffix, "suffix", "", "suffix to add to intermediate containers")
if err := flags.MarkHidden("suffix"); err != nil {
panic(fmt.Sprintf("error marking the suffix flag as hidden: %v", err))
}

if err := flags.MarkHidden("signature-policy"); err != nil {
panic(fmt.Sprintf("error marking signature-policy as hidden: %v", err))
}
Expand Down Expand Up @@ -284,6 +291,7 @@ func fromCmd(c *cobra.Command, args []string, iopts fromReply) error {
options := buildah.BuilderOptions{
FromImage: args[0],
Container: iopts.name,
ContainerSuffix: suffix,
PullPolicy: pullPolicy,
SignaturePolicyPath: signaturePolicy,
SystemContext: systemContext,
Expand Down
2 changes: 2 additions & 0 deletions define/build.go
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,8 @@ type CommonBuildOptions struct {

// BuildOptions can be used to alter how an image is built.
type BuildOptions struct {
// ContainerSuffix it the name to suffix containers with
ContainerSuffix string
// ContextDirectory is the default source location for COPY and ADD
// commands.
ContextDirectory string
Expand Down
2 changes: 2 additions & 0 deletions imagebuildah/executor.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ var builtinAllowedBuildArgs = map[string]bool{
// interface. It coordinates the entire build by using one or more
// StageExecutors to handle each stage of the build.
type Executor struct {
containerSuffix string
logger *logrus.Logger
stages map[string]*StageExecutor
store storage.Store
Expand Down Expand Up @@ -205,6 +206,7 @@ func newExecutor(logger *logrus.Logger, logPrefix string, store storage.Store, o
}

exec := Executor{
containerSuffix: options.ContainerSuffix,
logger: logger,
stages: make(map[string]*StageExecutor),
store: store,
Expand Down
1 change: 1 addition & 0 deletions imagebuildah/stage_executor.go
Original file line number Diff line number Diff line change
Expand Up @@ -539,6 +539,7 @@ func (s *StageExecutor) prepare(ctx context.Context, from string, initializeIBCo
Args: ib.Args,
FromImage: from,
PullPolicy: pullPolicy,
ContainerSuffix: s.executor.containerSuffix,
Registry: s.executor.registry,
BlobDirectory: s.executor.blobDirectory,
SignaturePolicyPath: s.executor.signaturePolicyPath,
Expand Down
3 changes: 3 additions & 0 deletions new.go
Original file line number Diff line number Diff line change
Expand Up @@ -197,6 +197,9 @@ func newBuilder(ctx context.Context, store storage.Store, options BuilderOptions
}

name := "working-container"
if options.ContainerSuffix != "" {
name = options.ContainerSuffix
}
if options.Container != "" {
name = options.Container
} else {
Expand Down
4 changes: 4 additions & 0 deletions tests/from.bats
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,10 @@ load helpers
run_buildah from --quiet --signature-policy ${TESTSDIR}/policy.json localhost/alpine2
expect_output "alpine2-working-container"
run_buildah rm $output
tmp=$RANDOM
run_buildah from --suffix $tmp --quiet --signature-policy ${TESTSDIR}/policy.json localhost/alpine2
expect_output "alpine2-$tmp"
run_buildah rm $output
run_buildah rmi alpine alpine2

run_buildah from --quiet --pull=true --signature-policy ${TESTSDIR}/policy.json docker.io/alpine
Expand Down

0 comments on commit 0bd4dd1

Please sign in to comment.