Skip to content

Commit

Permalink
Merge pull request #168 from garethjevans/wait-for-pod-start
Browse files Browse the repository at this point in the history
fix: wait for pod to exist
  • Loading branch information
garethjevans authored May 6, 2021
2 parents c389cef + f03caac commit 8c5e3af
Show file tree
Hide file tree
Showing 4 changed files with 63 additions and 40 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -14,19 +14,23 @@
import java.util.logging.Logger;

public class PipelineRunLogWatch implements Runnable {

private static final Logger LOGGER = Logger.getLogger(PipelineRunLogWatch.class.getName());

private static final String PIPELINE_TASK_LABEL_NAME = "tekton.dev/pipelineTask";
private static final String PIPELINE_RUN_LABEL_NAME = "tekton.dev/pipelineRun";

private final PipelineRun pipelineRun;

private KubernetesClient kubernetesClient;
private TektonClient tektonClient;
private PipelineRun pipelineRun;
private Exception exception;
OutputStream consoleLogger;

ConcurrentHashMap<String, TaskRun> taskRunsOnWatch = new ConcurrentHashMap<String, TaskRun>();
ConcurrentHashMap<String, Boolean> taskRunsWatchDone = new ConcurrentHashMap<String, Boolean>();
//ConcurrentHashMap<String, TaskRun> taskRunsOnWatch = new ConcurrentHashMap<String, TaskRun>();
//ConcurrentHashMap<String, Boolean> taskRunsWatchDone = new ConcurrentHashMap<String, Boolean>();


private final String pipelineTaskLabelName = "tekton.dev/pipelineTask";
private final String pipelineRunLabelName = "tekton.dev/pipelineRun";

public PipelineRunLogWatch(KubernetesClient kubernetesClient, TektonClient tektonClient, PipelineRun pipelineRun, OutputStream consoleLogger) {
this.kubernetesClient = kubernetesClient;
Expand Down Expand Up @@ -54,7 +58,7 @@ public void run() {
String pipelineTaskName = pt.getName();
LOGGER.info("Streaming logs for PipelineTask namespace=" + ns + ", runName=" + pipelineRunName + ", taskName=" + pipelineTaskName);
ListOptions lo = new ListOptions();
String selector = String.format("%s=%s,%s=%s", pipelineTaskLabelName, pipelineTaskName, pipelineRunLabelName, pipelineRunName);
String selector = String.format("%s=%s,%s=%s", PIPELINE_TASK_LABEL_NAME, pipelineTaskName, PIPELINE_RUN_LABEL_NAME, pipelineRunName);
lo.setLabelSelector(selector);

// the tekton operator may not have created the TasksRuns yet so lets wait a little bit for them to show up
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import io.fabric8.kubernetes.api.model.ContainerState;
import io.fabric8.kubernetes.api.model.ContainerStateTerminated;
import io.fabric8.kubernetes.api.model.ContainerStatus;
import io.fabric8.kubernetes.api.model.ListOptions;
import io.fabric8.kubernetes.api.model.OwnerReference;
import io.fabric8.kubernetes.api.model.Pod;
import io.fabric8.kubernetes.api.model.PodStatus;
Expand All @@ -28,9 +29,14 @@
public class TaskRunLogWatch implements Runnable{
private static final Logger LOGGER = Logger.getLogger(TaskRunLogWatch.class.getName());

private static final String TASK_RUN_LABEL_NAME = "tekton.dev/taskRun";

// TODO should be final
private TaskRun taskRun;

private KubernetesClient kubernetesClient;
private TektonClient tektonClient;
private TaskRun taskRun;

private Exception exception;
OutputStream consoleLogger;

Expand All @@ -52,7 +58,24 @@ public Exception getException() {
public void run() {
HashSet<String> runningPhases = Sets.newHashSet("Running", "Succeeded", "Failed");
String ns = taskRun.getMetadata().getNamespace();
List<Pod> pods = kubernetesClient.pods().inNamespace(ns).list().getItems();
ListOptions lo = new ListOptions();
String selector = String.format("%s=%s", TASK_RUN_LABEL_NAME, taskRun.getMetadata().getName());
lo.setLabelSelector(selector);
List<Pod> pods = null;
for (int i = 0; i < 60; i++) {
pods = kubernetesClient.pods().inNamespace(ns).list(lo).getItems();
LOGGER.info("Found " + pods.size() + " pod(s) for taskRun " + taskRun.getMetadata().getName());
if (pods.size() > 0) {
break;
}
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}


Pod taskRunPod = null;
String podName = "";
for (Pod pod : pods) {
Expand Down Expand Up @@ -144,14 +167,18 @@ public void run() {
*/
protected void logTaskRunFailure(TaskRun taskRun) {
String name = taskRun.getMetadata().getName();
List<Condition> conditions = taskRun.getStatus().getConditions();
if (conditions == null || conditions.size() == 0) {
logMessage("[Tekton] TaskRun " + name + " has no status conditions");
return;
}
if (taskRun.getStatus() != null) {
List<Condition> conditions = taskRun.getStatus().getConditions();
if (conditions == null || conditions.size() == 0) {
logMessage("[Tekton] TaskRun " + name + " has no status conditions");
return;
}

for (Condition condition : conditions) {
logMessage("[Tekton] TaskRun " + name + " " + condition.getType() + "/" + condition.getReason() + ": " + condition.getMessage());
for (Condition condition : conditions) {
logMessage("[Tekton] TaskRun " + name + " " + condition.getType() + "/" + condition.getReason() + ": " + condition.getMessage());
}
} else {
logMessage("[Tekton] TaskRun " + name + " has no status");
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ public void testFreestyleJobWithFileInput() throws Exception {

assertThat(kubernetesRule.getMockServer().getRequestCount(), is(1));

String log = jenkinsRule.getLog(b);
String log = JenkinsRule.getLog(b);
System.out.println(log);

assertThat(log, containsString("Legacy code started this job"));
Expand Down Expand Up @@ -113,7 +113,7 @@ public void testFreestyleJobWithYamlInput() throws Exception {

assertThat(kubernetesRule.getMockServer().getRequestCount(), is(1));

String log = jenkinsRule.getLog(b);
String log = JenkinsRule.getLog(b);
System.out.println(log);

assertThat(log, containsString("Legacy code started this job"));
Expand Down Expand Up @@ -199,7 +199,7 @@ public void testFreestyleJobWithComplexYamlInput() throws Exception {
.addToItems(pod)
.build();

kubernetesRule.expect().get().withPath("/api/v1/namespaces/test/pods")
kubernetesRule.expect().get().withPath("/api/v1/namespaces/test/pods?labelSelector=tekton.dev%2FtaskRun%3DtestTaskRun")
.andReturn(HttpURLConnection.HTTP_OK, podList).once();

kubernetesRule.expect().get().withPath("/api/v1/namespaces/test/pods/hello-world-pod")
Expand All @@ -219,7 +219,7 @@ public void testFreestyleJobWithComplexYamlInput() throws Exception {

FreeStyleBuild b = jenkinsRule.assertBuildStatus(Result.SUCCESS, p.scheduleBuild2(0).get());

String log = jenkinsRule.getLog(b);
String log = JenkinsRule.getLog(b);
System.out.println(log);

assertThat(log, containsString("Legacy code started this job"));
Expand Down Expand Up @@ -308,7 +308,7 @@ public void testFreestyleJobWithExpandedYamlInput() throws Exception {
.addToItems(pod)
.build();

kubernetesRule.expect().get().withPath("/api/v1/namespaces/test/pods")
kubernetesRule.expect().get().withPath("/api/v1/namespaces/test/pods?labelSelector=tekton.dev%2FtaskRun%3DtestTaskRun")
.andReturn(HttpURLConnection.HTTP_OK, podList).once();

kubernetesRule.expect().get().withPath("/api/v1/namespaces/test/pods/hello-world-pod")
Expand All @@ -328,7 +328,7 @@ public void testFreestyleJobWithExpandedYamlInput() throws Exception {

FreeStyleBuild b = jenkinsRule.assertBuildStatus(Result.SUCCESS, p.scheduleBuild2(0).get());

String log = jenkinsRule.getLog(b);
String log = JenkinsRule.getLog(b);
System.out.println(log);

assertThat(log, containsString("Legacy code started this job"));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,8 @@
import io.fabric8.tekton.pipeline.v1beta1.TaskRunBuilder;
import io.fabric8.tekton.pipeline.v1beta1.TaskRunList;
import io.fabric8.tekton.pipeline.v1beta1.TaskRunListBuilder;
import java.io.IOException;
import java.net.HttpURLConnection;
import java.net.URL;
import java.nio.charset.StandardCharsets;
import org.apache.commons.io.IOUtils;
import org.jenkinsci.plugins.workflow.cps.CpsFlowDefinition;
import org.jenkinsci.plugins.workflow.job.WorkflowJob;
import org.jenkinsci.plugins.workflow.job.WorkflowRun;
Expand All @@ -32,7 +29,6 @@
import org.junit.Test;
import org.junit.rules.RuleChain;
import org.junit.rules.TestRule;
import org.jvnet.hudson.test.ExtractResourceSCM;
import org.jvnet.hudson.test.JenkinsRule;
import org.waveywaves.jenkins.plugins.tekton.client.TektonUtils;
import org.waveywaves.jenkins.plugins.tekton.client.ToolUtils;
Expand Down Expand Up @@ -85,7 +81,7 @@ public void testScriptedPipelineWithFileInput_Task() throws Exception {

assertThat(kubernetesRule.getMockServer().getRequestCount(), is(1));

String log = jenkinsRule.getLog(b);
String log = JenkinsRule.getLog(b);
System.out.println(log);

assertThat(log, containsString("Extracting: .tekton/task.yaml"));
Expand Down Expand Up @@ -125,7 +121,7 @@ public void testDeclarativePipelineWithFileInput_Task() throws Exception {

assertThat(kubernetesRule.getMockServer().getRequestCount(), is(1));

String log = jenkinsRule.getLog(b);
String log = JenkinsRule.getLog(b);
System.out.println(log);

assertThat(log, containsString("Extracting: .tekton/task.yaml"));
Expand Down Expand Up @@ -169,7 +165,7 @@ public void testDeclarativePipelineWithYamlInput_Task() throws Exception {

assertThat(kubernetesRule.getMockServer().getRequestCount(), is(1));

String log = jenkinsRule.getLog(b);
String log = JenkinsRule.getLog(b);
System.out.println(log);

assertThat(log, containsString("Extracting: .tekton/task.yaml"));
Expand Down Expand Up @@ -258,7 +254,7 @@ public void testDeclarativePipelineWithYamlInput_PipelineRun() throws Exception
.addToItems(pod)
.build();

kubernetesRule.expect().get().withPath("/api/v1/namespaces/test/pods")
kubernetesRule.expect().get().withPath("/api/v1/namespaces/test/pods?labelSelector=tekton.dev%2FtaskRun%3DtestTaskRun")
.andReturn(HttpURLConnection.HTTP_OK, podList).once();

kubernetesRule.expect().get().withPath("/api/v1/namespaces/test/pods/hello-world-pod")
Expand Down Expand Up @@ -291,7 +287,7 @@ public void testDeclarativePipelineWithYamlInput_PipelineRun() throws Exception

WorkflowRun b = jenkinsRule.assertBuildStatus(Result.SUCCESS, p.scheduleBuild2(0).get());

String log = jenkinsRule.getLog(b);
String log = JenkinsRule.getLog(b);
System.out.println(log);

assertThat(log, containsString("[Pipeline] tektonCreateRaw"));
Expand Down Expand Up @@ -384,7 +380,7 @@ public void testDeclarativePipelineWithYamlInput_PipelineRun_DifferentNamespace(
.addToItems(pod)
.build();

kubernetesRule.expect().get().withPath("/api/v1/namespaces/tekton-pipelines/pods")
kubernetesRule.expect().get().withPath("/api/v1/namespaces/tekton-pipelines/pods?labelSelector=tekton.dev%2FtaskRun%3DtestTaskRun")
.andReturn(HttpURLConnection.HTTP_OK, podList).once();

kubernetesRule.expect().get().withPath("/api/v1/namespaces/tekton-pipelines/pods/hello-world-pod")
Expand Down Expand Up @@ -417,7 +413,7 @@ public void testDeclarativePipelineWithYamlInput_PipelineRun_DifferentNamespace(

WorkflowRun b = jenkinsRule.assertBuildStatus(Result.SUCCESS, p.scheduleBuild2(0).get());

String log = jenkinsRule.getLog(b);
String log = JenkinsRule.getLog(b);
System.out.println(log);

assertThat(log, containsString("[Pipeline] tektonCreateRaw"));
Expand Down Expand Up @@ -516,7 +512,7 @@ public void testDeclarativePipelineWithYamlInput_PipelineRun_FailingContainer()
.addToItems(pod)
.build();

kubernetesRule.expect().get().withPath("/api/v1/namespaces/tekton-pipelines/pods")
kubernetesRule.expect().get().withPath("/api/v1/namespaces/tekton-pipelines/pods?labelSelector=tekton.dev%2FtaskRun%3DtestTaskRun")
.andReturn(HttpURLConnection.HTTP_OK, podList).once();

kubernetesRule.expect().get().withPath("/api/v1/namespaces/tekton-pipelines/pods/hello-world-pod")
Expand Down Expand Up @@ -549,7 +545,7 @@ public void testDeclarativePipelineWithYamlInput_PipelineRun_FailingContainer()

WorkflowRun b = jenkinsRule.assertBuildStatus(Result.FAILURE, p.scheduleBuild2(0).get());

String log = jenkinsRule.getLog(b);
String log = JenkinsRule.getLog(b);
System.out.println(log);

assertThat(log, containsString("[Pipeline] tektonCreateRaw"));
Expand Down Expand Up @@ -643,7 +639,7 @@ public void testDeclarativePipelineWithYamlInput_PipelineRun_FailingPipelineRun(
.addToItems(pod)
.build();

kubernetesRule.expect().get().withPath("/api/v1/namespaces/tekton-pipelines/pods")
kubernetesRule.expect().get().withPath("/api/v1/namespaces/tekton-pipelines/pods?labelSelector=tekton.dev%2FtaskRun%3DtestTaskRun")
.andReturn(HttpURLConnection.HTTP_OK, podList).once();

kubernetesRule.expect().get().withPath("/api/v1/namespaces/tekton-pipelines/pods/hello-world-pod")
Expand Down Expand Up @@ -676,7 +672,7 @@ public void testDeclarativePipelineWithYamlInput_PipelineRun_FailingPipelineRun(

WorkflowRun b = jenkinsRule.assertBuildStatus(Result.FAILURE, p.scheduleBuild2(0).get());

String log = jenkinsRule.getLog(b);
String log = JenkinsRule.getLog(b);
System.out.println(log);

assertThat(log, containsString("[Pipeline] tektonCreateRaw"));
Expand All @@ -689,10 +685,6 @@ public void testDeclarativePipelineWithYamlInput_PipelineRun_FailingPipelineRun(
assertThat(kubernetesRule.getMockServer().getRequestCount(), is(9));
}

private String contents(String filename) throws IOException {
return IOUtils.toString(this.getClass().getResourceAsStream(filename), StandardCharsets.UTF_8.name());
}

private OwnerReference ownerReference(String uid) {
return new OwnerReference("", false, false, "", "", uid);
}
Expand Down

0 comments on commit 8c5e3af

Please sign in to comment.