Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Resolving nested meta ARGs #1260

Merged
merged 2 commits into from
Jun 4, 2020
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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))
tejal29 marked this conversation as resolved.
Show resolved Hide resolved
}

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