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

Fix #859: ARG not support in FROM #1299

Merged
merged 2 commits into from
Aug 29, 2020
Merged

Conversation

rohanKanojia
Copy link
Member

Fix #859

Copy link
Collaborator

@rhuss rhuss left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks a lot for the PR, but I think its not complete. Its not only that you can use args in a tag but as anything behind FROM (also the image name).

Not sure though how to actually tackle this.

doc/changelog.md Outdated
@@ -8,6 +8,7 @@
- Add docker:build support for 'network' option #1030
- Failure referencing a previous staged image in FROM clause #1264
- Treat bridged and default network mode the same (#1234)
- ARG not support in FROM (#859)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
- ARG not support in FROM (#859)
- Support `ARG ` in `FROM` (#859)

for (String[] argLine : argLines) {
if (argLine.length > 1) {
if (argLine[1].contains(":")) {
result.put(argLine[1].split(":")[0], argLine[1].split(":")[1]);
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

  • You should only split once and reuse the result
  • You should split into exactly two parts, with the second parts potentially having :, too. I.e. use the split function which takes the max. nr of resulting split elements as argument.

if (argLine[1].contains(":")) {
result.put(argLine[1].split(":")[0], argLine[1].split(":")[1]);
} else if (argLine[1].contains("=")) {
result.put(argLine[1].split("=")[0], argLine[1].split("=")[1]);
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You can put the split on ':' and '=' into a function for reuse, e.g. addArgWitValue(result, "=", argLine[1])

@rohanKanojia
Copy link
Member Author

I was trying to do the changes but I noticed one thing, I'm testing using this Dockerfile(to test FROM $IMAGENAME scenario):

FROM openjdk:jre

ARG version=0.31-SNAPSHOT
ARG jar_file=target/demp-sample-zero-config-$version.jar
ARG FULLIMAGENAME=busybox

ADD $jar_file /tmp/zero-config.jar
CMD java -cp /tmp/zero-config.jar HelloWorld
FROM $FULLIMAGENAME

But docker doesn't seem to be able to process this:

~/work/repos/docker-maven-plugin/samples/zero-config : $ docker build .
Sending build context to Docker daemon  45.06kB
Step 1/7 : FROM openjdk:jre
 ---> 6b23d41384f9
Step 2/7 : ARG version=0.31-SNAPSHOT
 ---> Using cache
 ---> de78be110994
Step 3/7 : ARG jar_file=target/demp-sample-zero-config-$version.jar
 ---> Using cache
 ---> a8268726a072
Step 4/7 : ARG FULLIMAGENAME=busybox
 ---> Using cache
 ---> f802a9b7666c
Step 5/7 : ADD $jar_file /tmp/zero-config.jar
 ---> Using cache
 ---> 15a65328a8aa
Step 6/7 : CMD java -cp /tmp/zero-config.jar HelloWorld
 ---> Using cache
 ---> dd5d7d159fea
Step 7/7 : FROM $FULLIMAGENAME
base name ($FULLIMAGENAME) should not be blank

Were you talking about this case? I'm not sure whether I've understood your comment correctly or not. I'm on 1.40 API version at the moment.

~/work/repos/docker-maven-plugin/samples/zero-config : $ docker version
Client: Docker Engine - Community
 Version:           19.03.4
 API version:       1.40
 Go version:        go1.12.10
 Git commit:        9013bf583a
 Built:             Fri Oct 18 15:52:46 2019
 OS/Arch:           linux/amd64
 Experimental:      false

Server: Docker Engine - Community
 Engine:
  Version:          19.03.4
  API version:      1.40 (minimum version 1.12)
  Go version:       go1.12.10
  Git commit:       9013bf583a
  Built:            Fri Oct 18 15:51:19 2019
  OS/Arch:          linux/amd64
  Experimental:     false
 containerd:
  Version:          1.2.10
  GitCommit:        b34a5c8af56e510852c35414db4c1f4fa6172339
 runc:
  Version:          1.0.0-rc8+dev
  GitCommit:        3e425f80a8c931f88e6d94a8c831b9d5aa481657
 docker-init:
  Version:          0.18.0

@rohanKanojia
Copy link
Member Author

Sorry, it seems to be working in this case:

ARG FULLIMAGENAME=busybox:latest
FROM $FULLIMAGENAME

@rohanKanojia rohanKanojia force-pushed the pr/issue859 branch 2 times, most recently from fc6fde3 to d19be15 Compare November 22, 2019 18:01
private static AbstractMap.SimpleEntry<String, String> resolveArgsFromArgString(String argString) {
for (int index = 0; index < argString.length(); index++) {
if (argString.charAt(index) == ':' || argString.charAt(index) == '=') {
return new AbstractMap.SimpleEntry<>(argString.substring(0, index), argString.substring(index+1));
Copy link
Collaborator

@rhuss rhuss Dec 19, 2019

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could we add a trim() to allow spaces around : or = ?

well thats not possible as you send only the first argument of the line (and arguments are delmited by spaces).

I'm really not sure that simplistic parsing of values is correct. What happens with arg values that carry spaces ?

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What if index is the lasst char in the string ? Wouldn't it be better to use String.split() with a regexp like :|= ?

Copy link
Collaborator

@rhuss rhuss left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks a lot for the PR but I'm afraid the parsing is not robust enough and only works for simple cases. That's just my feeling, but we need a better test coverage for the corner case and error cases, too.

private static AbstractMap.SimpleEntry<String, String> resolveArgsFromArgString(String argString) {
for (int index = 0; index < argString.length(); index++) {
if (argString.charAt(index) == ':' || argString.charAt(index) == '=') {
return new AbstractMap.SimpleEntry<>(argString.substring(0, index), argString.substring(index+1));
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What if index is the lasst char in the string ? Wouldn't it be better to use String.split() with a regexp like :|= ?

Map<String, String> result = new HashMap<>();
for (String[] argLine : argLines) {
if (argLine.length > 1) {
AbstractMap.SimpleEntry<String, String> resolvedArg = resolveArgsFromArgString(argLine[1]);
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would call the method updateMapWithArgValue() and feed in the map as argument. That allows you to avoid to generate temporary, short-lived obkects like resolvedArg

assertEquals("busybox:latest", fromClauses.next());
assertEquals(false, fromClauses.hasNext());
}

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would love to see more tests directly for the parsing methods with many corner cases. No need to create test Dockerfiles, but calling extractArgs() and resolveImageTagFromArgs() in a loop with different inputs (you could make the methods package private). But we need much more tests about different cases (like with ARG values, containing spaces, if this is allowed. Also check the Docker spec for the possible ARG variations).

@rohanKanojia
Copy link
Member Author

@rhuss : Hi, Sorry I forgot to update this. I will update this around coming weekend

Copy link
Collaborator

@rhuss rhuss left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good to me with one minor suggestion.

But we should merge this sonnish ...

* @param argLines list of lines containing arguments
* @return HashMap of arguments or empty collection if none is found
*/
public static Map<String, String> extractArgs(List<String[]> argLines) {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe you could inline the usage of extractLines ? (e..g change the signature to extractArgs(String dockerfile, FIxedStringSearchInterpolator interpolator) and call extract Lines internally)

}

@Test
public void testExtractArgsFromDockerfile() {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍

@dsmiley
Copy link

dsmiley commented Aug 12, 2020

This issue that this PR fixes is important; I hope the PR can be merged and released.

@rhuss
Copy link
Collaborator

rhuss commented Aug 29, 2020

Thanks, let's merge that now.

@rhuss rhuss merged commit 02822a7 into fabric8io:master Aug 29, 2020
rohanKanojia added a commit to rohanKanojia/jkube that referenced this pull request Sep 11, 2020
rohanKanojia added a commit to rohanKanojia/jkube that referenced this pull request Oct 27, 2020
rohanKanojia added a commit to rohanKanojia/jkube that referenced this pull request Oct 30, 2020
rohanKanojia added a commit to rohanKanojia/jkube that referenced this pull request Oct 30, 2020
rohanKanojia added a commit to rohanKanojia/jkube that referenced this pull request Oct 30, 2020
rohanKanojia added a commit to rohanKanojia/jkube that referenced this pull request Oct 30, 2020
manusa pushed a commit to eclipse-jkube/jkube that referenced this pull request Oct 30, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

ARG not support in FROM
3 participants