Skip to content

Commit

Permalink
Resolving nested meta ARGs against themselves and build ARGs
Browse files Browse the repository at this point in the history
  • Loading branch information
ljakimczuk committed May 8, 2020
1 parent cb11a99 commit 3dcac1b
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 2 deletions.
24 changes: 24 additions & 0 deletions pkg/dockerfile/dockerfile.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,11 @@ func ParseStages(opts *config.KanikoOptions) ([]instructions.Stage, []instructio
return nil, nil, errors.Wrap(err, "parsing dockerfile")
}

metaArgs, err = expandNested(metaArgs, opts.BuildArgs)
if err != nil {
return nil, nil, errors.Wrap(err, "expanding meta ARGs")
}

return stages, metaArgs, nil
}

Expand Down Expand Up @@ -97,6 +102,25 @@ func Parse(b []byte) ([]instructions.Stage, []instructions.ArgCommand, error) {
return stages, metaArgs, nil
}

// expandNestedArgs tries to resolve nested ARG value against the previously defined ARGs
func expandNested(metaArgs []instructions.ArgCommand, buildArgs []string) ([]instructions.ArgCommand, error) {
prevArgs := make([]string, 0)
for i := range metaArgs {
arg := metaArgs[i]
v := arg.Value
if v != nil {
val, err := util.ResolveEnvironmentReplacement(*v, append(prevArgs, buildArgs...), false)
if err != nil {
return nil, err
}
prevArgs = append(prevArgs, arg.Key+"="+val)
arg.Value = &val
metaArgs[i] = arg
}
}
return metaArgs, nil
}

// stripEnclosingQuotes removes quotes enclosing the value of each instructions.ArgCommand in a slice
// if the quotes are escaped it leaves them
func stripEnclosingQuotes(metaArgs []instructions.ArgCommand) ([]instructions.ArgCommand, error) {
Expand Down
7 changes: 5 additions & 2 deletions pkg/dockerfile/dockerfile_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,9 @@ func Test_ParseStages_ArgValueWithQuotes(t *testing.T) {
dockerfile := `
ARG IMAGE="ubuntu:16.04"
ARG FOO=bar
ARG HELLO="Hello"
ARG WORLD="World"
ARG NESTED="$HELLO $WORLD"
FROM ${IMAGE}
RUN echo hi > /hi
Expand Down Expand Up @@ -65,11 +68,11 @@ func Test_ParseStages_ArgValueWithQuotes(t *testing.T) {
t.Fatal("length of stages expected to be greater than zero, but was zero")
}

if len(metaArgs) != 2 {
if len(metaArgs) != 5 {
t.Fatalf("length of stage meta args expected to be 2, but was %d", len(metaArgs))
}

for i, expectedVal := range []string{"ubuntu:16.04", "bar"} {
for i, expectedVal := range []string{"ubuntu:16.04", "bar", "Hello", "World", "Hello World"} {
if metaArgs[i].ValueString() != expectedVal {
t.Fatalf("expected metaArg %d val to be %s but was %s", i, expectedVal, metaArgs[i].ValueString())
}
Expand Down

0 comments on commit 3dcac1b

Please sign in to comment.