-
Notifications
You must be signed in to change notification settings - Fork 1.4k
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
parse arg commands at the top of dockerfiles #404
Changes from 7 commits
4756f53
5c14df5
aa2c6d0
a37fbfb
6d808ca
2232fa0
5c91a4b
7dd1e3a
20eaf12
f086581
d18bf34
bbdb139
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
ARG REGISTRY=gcr.io | ||
ARG REPO=google-appengine | ||
ARG WORD=hello | ||
ARG W0RD2=hey | ||
|
||
FROM ${REGISTRY}/${REPO}/debian9 as stage1 | ||
|
||
# Should evaluate WORD and create /tmp/hello | ||
ARG WORD | ||
RUN touch /${WORD} | ||
|
||
FROM ${REGISTRY}/${REPO}/debian9 | ||
|
||
COPY --from=stage1 /hello /tmp | ||
|
||
# /tmp/hey should not get created without the ARG statement | ||
RUN touch /tmp/${WORD2} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -50,11 +50,12 @@ type stageBuilder struct { | |
snapshotter *snapshot.Snapshotter | ||
baseImageDigest string | ||
opts *config.KanikoOptions | ||
metaArgs map[string]string | ||
} | ||
|
||
// newStageBuilder returns a new type stageBuilder which contains all the information required to build the stage | ||
func newStageBuilder(opts *config.KanikoOptions, stage config.KanikoStage) (*stageBuilder, error) { | ||
sourceImage, err := util.RetrieveSourceImage(stage, opts.BuildArgs, opts) | ||
func newStageBuilder(opts *config.KanikoOptions, stage config.KanikoStage, metaArgs map[string]string) (*stageBuilder, error) { | ||
sourceImage, err := util.RetrieveSourceImage(stage, opts, metaArgs) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
@@ -83,6 +84,7 @@ func newStageBuilder(opts *config.KanikoOptions, stage config.KanikoStage) (*sta | |
snapshotter: snapshotter, | ||
baseImageDigest: digest.String(), | ||
opts: opts, | ||
metaArgs: metaArgs, | ||
}, nil | ||
} | ||
|
||
|
@@ -136,6 +138,7 @@ func (s *stageBuilder) build() error { | |
} | ||
|
||
args := dockerfile.NewBuildArgs(s.opts.BuildArgs) | ||
args.AddMetaArgs(s.metaArgs) | ||
for index, command := range cmds { | ||
if command == nil { | ||
continue | ||
|
@@ -250,12 +253,21 @@ func (s *stageBuilder) saveSnapshot(createdBy string, ck string, contents []byte | |
// DoBuild executes building the Dockerfile | ||
func DoBuild(opts *config.KanikoOptions) (v1.Image, error) { | ||
// Parse dockerfile and unpack base image to root | ||
stages, err := dockerfile.Stages(opts) | ||
stages, metaArgs, err := dockerfile.Stages(opts) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. WDYT of storing the metaArgs for each stage in the KanikoStage type? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. yeah, that simplified things considerably. done. |
||
if err != nil { | ||
return nil, err | ||
} | ||
// Parse and apply args declared before any stage. | ||
kanikoMetaArgs := map[string]string{} | ||
for _, arg := range metaArgs { | ||
var v string | ||
if arg.Value != nil { | ||
v = *arg.Value | ||
} | ||
kanikoMetaArgs[arg.Key] = v | ||
} | ||
for index, stage := range stages { | ||
sb, err := newStageBuilder(opts, stage) | ||
sb, err := newStageBuilder(opts, stage, kanikoMetaArgs) | ||
if err != nil { | ||
return nil, errors.Wrap(err, fmt.Sprintf("getting stage builder for stage %d", index)) | ||
} | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -190,6 +190,7 @@ func (s *Snapshotter) snapShotFS(f io.Writer) (bool, error) { | |
} | ||
} | ||
|
||
addedPaths := []string{} | ||
// Now create the tar. | ||
for path := range memFs { | ||
whitelisted, err := util.CheckWhitelist(path) | ||
|
@@ -208,12 +209,18 @@ func (s *Snapshotter) snapShotFS(f io.Writer) (bool, error) { | |
} | ||
if maybeAdd { | ||
logrus.Debugf("Adding %s to layer, because it was changed.", path) | ||
addedPaths = append(addedPaths, path) | ||
filesAdded = true | ||
if err := t.AddFileToTar(path); err != nil { | ||
return false, err | ||
} | ||
} | ||
} | ||
|
||
if filesAdded && len(addedPaths) == 1 && addedPaths[0] == "/" { | ||
logrus.Infof("Only added /, not taking snapshot.") | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Why is this change needed? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. so for a command like There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Hrmm, what if it's like "/foo/${FOO}"? Maybe the check is if the only added path is a directory, rather than root? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. yep, done. |
||
return false, nil | ||
} | ||
|
||
return filesAdded, nil | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks for catching this!! Must have missed it...many times... 😅