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

filter exit code from command output #242

Closed
wants to merge 13 commits into from
Closed

Conversation

balihb
Copy link

@balihb balihb commented Oct 30, 2017

Ok, I won't say this is the nicest solution for this, but I have no better idea to prevent things like the docker client jenkins plugin from failing with kubernetes.

@marvinthepa
Copy link
Contributor

@balihb

Thanks for the pull request.

I think you are right, it is probably a good idea in general to not pass the "EXITCODE" stuff to the client.
And you are right, there "should" be a nicer solution.

Could you please add more information on "the docker client jenkins plugin [...] failing with kubernetes", i.e. an example pipeline that shows it and the error in the build log?
If at all possible, could you also add a test that fails on master but doesn't fail on this branch?

@balihb
Copy link
Author

balihb commented Nov 2, 2017

I'm planning a change on this PR. I hope if it only runs the matching part of the code at the point where the plugin reads out the exitcode it won't need to match on all write.

the docker client part:

19:18:26 java.lang.IllegalArgumentException: Expecting 64-char full image ID, but got 3f8539f89064c27dfb723eb2a7972b78b293243a635dea00cf4089a20a7ea92a
19:18:26 EXITCODE   0
19:18:26 	at org.jenkinsci.plugins.docker.commons.fingerprint.DockerFingerprints.getFingerprintHash(DockerFingerprints.java:71)
19:18:26 	at org.jenkinsci.plugins.docker.commons.fingerprint.DockerFingerprints.forDockerInstance(DockerFingerprints.java:148)
19:18:26 	at org.jenkinsci.plugins.docker.commons.fingerprint.DockerFingerprints.forImage(DockerFingerprints.java:115)
19:18:26 	at org.jenkinsci.plugins.docker.commons.fingerprint.DockerFingerprints.forImage(DockerFingerprints.java:100)
19:18:26 	at org.jenkinsci.plugins.docker.commons.fingerprint.DockerFingerprints.addRunFacet(DockerFingerprints.java:226)
19:18:26 	at org.jenkinsci.plugins.docker.workflow.RunFingerprintStep$Execution.run(RunFingerprintStep.java:77)
19:18:26 	at org.jenkinsci.plugins.docker.workflow.RunFingerprintStep$Execution.run(RunFingerprintStep.java:63)
19:18:26 	at org.jenkinsci.plugins.workflow.steps.AbstractSynchronousNonBlockingStepExecution$1$1.call(AbstractSynchronousNonBlockingStepExecution.java:47)
19:18:26 	at hudson.security.ACL.impersonate(ACL.java:260)
19:18:26 	at org.jenkinsci.plugins.workflow.steps.AbstractSynchronousNonBlockingStepExecution$1.run(AbstractSynchronousNonBlockingStepExecution.java:44)
19:18:26 	at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:511)
19:18:26 	at java.util.concurrent.FutureTask.run(FutureTask.java:266)
19:18:26 	at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1149)
19:18:26 	at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:624)
19:18:26 	at java.lang.Thread.run(Thread.java:748)

The problem here is that the docker plugin expects the ProcStarter to only return the output of the command.

I haven't looked at test code, but I'll add the test (I hope I can :D).

One question:
https://github.com/jenkinsci/kubernetes-plugin/blob/master/src/main/java/org/csanchez/jenkins/plugins/kubernetes/pipeline/ContainerExecDecorator.java#L327
Can these debug messages be passed to the logger and not the output? I can make the change (tried it already, but haven't added to the PR).

@marvinthepa marvinthepa changed the title filter exit code from command output [WIPfilter exit code from command output Nov 2, 2017
@marvinthepa marvinthepa changed the title [WIPfilter exit code from command output [WIP] filter exit code from command output Nov 2, 2017
@marvinthepa
Copy link
Contributor

I'm planning a change on this PR.

Cool. I added "[WIP]" to the title of this PR, please remove once it is ready to review again.

@balihb
Copy link
Author

balihb commented Nov 6, 2017

I've been testing out the ssh-agent bug and it looks like I found a solution:
in doExec:

                 watch.getInput().write(s.getBytes(StandardCharsets.UTF_8));
             }
+            sb.append(" 2> /dev/null");
             sb.append(NEWLINE);

@marvinthepa
Copy link
Contributor

@balihb:

while this might get rid of the error, I guess it has to be considered a change of functionality to just throw away stderr...

@balihb
Copy link
Author

balihb commented Nov 7, 2017

there is no stderr anyway. at least not from the command.
An example command:

INFO: Executing command: "nohup" "sh" "-c" "echo \$\$ > '/home/jenkins/workspace/boot-react@tmp/durable-4f232592/pid'; jsc=durable-9c1ff7314119e604e5e6da6a39f23b37; JENKINS_SERVER_COOKIE=\$jsc '/home/jenkins/workspace/boot-react@tmp/durable-4f232592/script.sh' > '/home/jenkins/workspace/boot-react@tmp/durable-4f232592/jenkins-log.txt' 2>&1; echo \$? > '/home/jenkins/workspace/boot-react@tmp/durable-4f232592/jenkins-result.txt'"  2> /dev/null
printf "EXITCODE %3d" $?; exit

The output of the command is piped to stdout anyway, there is something else that sneaks something funny on the output and stderr is not collected separately:

                Execable<String, ExecWatch> execable = client.pods().inNamespace(namespace).withName(podName).inContainer(containerName)
                        .redirectingInput().writingOutput(stream).writingError(stream)

IMHO it shouldn't be collected anyway. It can cause problems if it is pushed on stdout (which is happening now) and if another plugin expects it the current solution can't give it anything.

@balihb balihb force-pushed the exitcode2 branch 2 times, most recently from 19f408b to 158774b Compare November 13, 2017 15:17
@balihb
Copy link
Author

balihb commented Nov 13, 2017

this should work now IMHO.
@marvinthepa I've changed a few of the writes to the output to be just log. I hope it is ok.

@balihb balihb changed the title [WIP] filter exit code from command output filter exit code from command output Nov 13, 2017
@balihb
Copy link
Author

balihb commented Nov 14, 2017

this latest change should also fix the ssh-agent problem.
if I can do anything else to get this PR accepted, please let me know.

@balihb
Copy link
Author

balihb commented Nov 21, 2017

@marvinthepa or maybe @carlossg ?

@balihb balihb force-pushed the exitcode2 branch 4 times, most recently from 7943f8c to fdbd931 Compare November 22, 2017 16:38
@MattLud
Copy link
Contributor

MattLud commented Dec 29, 2017

Tested locally a few dozen times with a particular smoke test 👍

@balihb
Copy link
Author

balihb commented Jan 7, 2018

@carlossg
I am contacting you because the merging of my PR would be crucial for my company (and for others, too), which is why I have been wondering if there is any error or problem you have found within the code? I certainly hope you did not find any of my earlier comments rude, since it was not my intention. Should you have any suggestions or requirements concerning the PR, please let me know.

@carlossg
Copy link
Contributor

sorry for the delay, I am trying to figure out if there is a better way to fetch the exit code because this is adding a lot of complexity to the code

Copy link
Contributor

@carlossg carlossg left a comment

Choose a reason for hiding this comment

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

Can you add a summary on how the stream flow works across all classes?

Thanks!


//check if the cmd is sourced from Jenkins, rather than another plugin; if so, skip cmdEnvs as we are getting other environment variables
for (String cmd : cmdEnvs) {
if (cmd.startsWith(JENKINS_HOME)) {
Copy link
Contributor

Choose a reason for hiding this comment

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

@@ -431,4 +437,37 @@ public int getExitCode() {
return i;
}
}

static class FilterOutExitCodeOutputStream extends OutputStream {
Copy link
Contributor

Choose a reason for hiding this comment

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

can we move it to a new class org.csanchez.jenkins.plugins.kubernetes.pipeline.exec. FilterOutExitCodeOutputStream with

@Restricted(NoExternalUse.class)

@@ -0,0 +1,69 @@
package org.csanchez.jenkins.plugins.kubernetes.pipeline;
Copy link
Contributor

Choose a reason for hiding this comment

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

can we put it under org.csanchez.jenkins.plugins.kubernetes.pipeline.exec


import org.apache.commons.lang.ArrayUtils;

public class ContainerExecCutExitCodeUtil {
Copy link
Contributor

Choose a reason for hiding this comment

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

@Restricted(NoExternalUse.class)

@balihb
Copy link
Author

balihb commented Mar 9, 2018

I've made a graph about the output stream flow
https://pasteboard.co/Hb6dUnf.png

@balihb
Copy link
Author

balihb commented Mar 20, 2018

@carlossg
is there any chance to get this pulled?

@giannello
Copy link

@balihb for how much I'd love to have this merged, there is a better way than filtering streams this way.

This fabric8io/kubernetes-client#1045 provides the base support for keeping the signalling stream separate. Would you mind giving it a look?

@carlossg
Copy link
Contributor

Yes, this whole section needs to be rewritten with the improvements in fabric8io/kubernetes-client#1045 JENKINS-50392

Copy link
Contributor

@carlossg carlossg left a comment

Choose a reason for hiding this comment

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

To be rewritten with the improvements in fabric8io/kubernetes-client#1045

Thanks for the effort though!

@carlossg
Copy link
Contributor

I think #300 will sort this out

@carlossg carlossg closed this Mar 26, 2018
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.

6 participants