Skip to content

Commit

Permalink
Adjust argument slice size in builder
Browse files Browse the repository at this point in the history
A recent update to the argument handling didn't increase
the argument slice to take into the account the values
from b.Args() that were now being added.  The created
unpredicatble results.

After addressing a comment from @nalind, I realized that
the value from b.Arguments should be used in the steps
as it keeps the ordering of when the ARGS and ENV were
defined in the Dockerfile.  Changed makeUserArgs
in internals.go to take two variables rather than just
a builder.

Adddresses the Buildah bug: containers/buildah#2345

Signed-off-by: TomSweeneyRedHat <[email protected]>
  • Loading branch information
TomSweeneyRedHat committed Jun 3, 2020
1 parent de01fac commit 3ccf1b9
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 8 deletions.
14 changes: 12 additions & 2 deletions builder.go
Original file line number Diff line number Diff line change
Expand Up @@ -332,9 +332,19 @@ func ParseFile(path string) (*parser.Node, error) {

// Step creates a new step from the current state.
func (b *Builder) Step() *Step {
dst := make([]string, len(b.Env)+len(b.RunConfig.Env))
copy(dst, makeUserArgs(b))
argsMap := make(map[string]string)
for _, argsVal := range b.Arguments() {
val := strings.Split(argsVal, "=")
if len(val) > 1 {
argsMap[val[0]] = val[1]
}
}

userArgs := makeUserArgs(b.Env, argsMap)
dst := make([]string, len(userArgs)+len(b.RunConfig.Env))
copy(dst, userArgs)
dst = append(dst, b.RunConfig.Env...)

return &Step{Env: dst}
}

Expand Down
4 changes: 2 additions & 2 deletions dispatchers.go
Original file line number Diff line number Diff line change
Expand Up @@ -153,7 +153,7 @@ func add(b *Builder, args []string, attributes map[string]bool, flagArgs []strin
var chown string
last := len(args) - 1
dest := makeAbsolute(args[last], b.RunConfig.WorkingDir)
userArgs := makeUserArgs(b)
userArgs := makeUserArgs(b.Env, b.Args)
for _, a := range flagArgs {
arg, err := ProcessWord(a, userArgs)
if err != nil {
Expand Down Expand Up @@ -182,7 +182,7 @@ func dispatchCopy(b *Builder, args []string, attributes map[string]bool, flagArg
dest := makeAbsolute(args[last], b.RunConfig.WorkingDir)
var chown string
var from string
userArgs := makeUserArgs(b)
userArgs := makeUserArgs(b.Env, b.Args)
for _, a := range flagArgs {
arg, err := ProcessWord(a, userArgs)
if err != nil {
Expand Down
8 changes: 4 additions & 4 deletions internals.go
Original file line number Diff line number Diff line change
Expand Up @@ -98,18 +98,18 @@ func parseOptInterval(f *flag.Flag) (time.Duration, error) {
// defined by both can later be evaluated when resolving variables
// such as ${MY_USER}. If the variable is defined by both ARG and ENV
// don't include the definition of the ARG variable.
func makeUserArgs(b *Builder) (userArgs []string) {
func makeUserArgs(bEnv []string, bArgs map[string]string) (userArgs []string) {

userArgs = b.Env
userArgs = bEnv
envMap := make(map[string]string)
for _, envVal := range b.Env {
for _, envVal := range bEnv {
val := strings.Split(envVal, "=")
if len(val) > 1 {
envMap[val[0]] = val[1]
}
}

for key, value := range b.Args {
for key, value := range bArgs {
if _, ok := envMap[key]; ok {
continue
}
Expand Down

0 comments on commit 3ccf1b9

Please sign in to comment.