Skip to content

Commit f03caac

Browse files
committed
fix: wait for pod to exist
1 parent 873417b commit f03caac

File tree

4 files changed

+63
-40
lines changed

4 files changed

+63
-40
lines changed

src/main/java/org/waveywaves/jenkins/plugins/tekton/client/logwatch/PipelineRunLogWatch.java

+10-6
Original file line numberDiff line numberDiff line change
@@ -14,19 +14,23 @@
1414
import java.util.logging.Logger;
1515

1616
public class PipelineRunLogWatch implements Runnable {
17+
1718
private static final Logger LOGGER = Logger.getLogger(PipelineRunLogWatch.class.getName());
1819

20+
private static final String PIPELINE_TASK_LABEL_NAME = "tekton.dev/pipelineTask";
21+
private static final String PIPELINE_RUN_LABEL_NAME = "tekton.dev/pipelineRun";
22+
23+
private final PipelineRun pipelineRun;
24+
1925
private KubernetesClient kubernetesClient;
2026
private TektonClient tektonClient;
21-
private PipelineRun pipelineRun;
2227
private Exception exception;
2328
OutputStream consoleLogger;
2429

25-
ConcurrentHashMap<String, TaskRun> taskRunsOnWatch = new ConcurrentHashMap<String, TaskRun>();
26-
ConcurrentHashMap<String, Boolean> taskRunsWatchDone = new ConcurrentHashMap<String, Boolean>();
30+
//ConcurrentHashMap<String, TaskRun> taskRunsOnWatch = new ConcurrentHashMap<String, TaskRun>();
31+
//ConcurrentHashMap<String, Boolean> taskRunsWatchDone = new ConcurrentHashMap<String, Boolean>();
32+
2733

28-
private final String pipelineTaskLabelName = "tekton.dev/pipelineTask";
29-
private final String pipelineRunLabelName = "tekton.dev/pipelineRun";
3034

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

6064
// the tekton operator may not have created the TasksRuns yet so lets wait a little bit for them to show up

src/main/java/org/waveywaves/jenkins/plugins/tekton/client/logwatch/TaskRunLogWatch.java

+36-9
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
import io.fabric8.kubernetes.api.model.ContainerState;
77
import io.fabric8.kubernetes.api.model.ContainerStateTerminated;
88
import io.fabric8.kubernetes.api.model.ContainerStatus;
9+
import io.fabric8.kubernetes.api.model.ListOptions;
910
import io.fabric8.kubernetes.api.model.OwnerReference;
1011
import io.fabric8.kubernetes.api.model.Pod;
1112
import io.fabric8.kubernetes.api.model.PodStatus;
@@ -28,9 +29,14 @@
2829
public class TaskRunLogWatch implements Runnable{
2930
private static final Logger LOGGER = Logger.getLogger(TaskRunLogWatch.class.getName());
3031

32+
private static final String TASK_RUN_LABEL_NAME = "tekton.dev/taskRun";
33+
34+
// TODO should be final
35+
private TaskRun taskRun;
36+
3137
private KubernetesClient kubernetesClient;
3238
private TektonClient tektonClient;
33-
private TaskRun taskRun;
39+
3440
private Exception exception;
3541
OutputStream consoleLogger;
3642

@@ -52,7 +58,24 @@ public Exception getException() {
5258
public void run() {
5359
HashSet<String> runningPhases = Sets.newHashSet("Running", "Succeeded", "Failed");
5460
String ns = taskRun.getMetadata().getNamespace();
55-
List<Pod> pods = kubernetesClient.pods().inNamespace(ns).list().getItems();
61+
ListOptions lo = new ListOptions();
62+
String selector = String.format("%s=%s", TASK_RUN_LABEL_NAME, taskRun.getMetadata().getName());
63+
lo.setLabelSelector(selector);
64+
List<Pod> pods = null;
65+
for (int i = 0; i < 60; i++) {
66+
pods = kubernetesClient.pods().inNamespace(ns).list(lo).getItems();
67+
LOGGER.info("Found " + pods.size() + " pod(s) for taskRun " + taskRun.getMetadata().getName());
68+
if (pods.size() > 0) {
69+
break;
70+
}
71+
try {
72+
Thread.sleep(1000);
73+
} catch (InterruptedException e) {
74+
e.printStackTrace();
75+
}
76+
}
77+
78+
5679
Pod taskRunPod = null;
5780
String podName = "";
5881
for (Pod pod : pods) {
@@ -144,14 +167,18 @@ public void run() {
144167
*/
145168
protected void logTaskRunFailure(TaskRun taskRun) {
146169
String name = taskRun.getMetadata().getName();
147-
List<Condition> conditions = taskRun.getStatus().getConditions();
148-
if (conditions == null || conditions.size() == 0) {
149-
logMessage("[Tekton] TaskRun " + name + " has no status conditions");
150-
return;
151-
}
170+
if (taskRun.getStatus() != null) {
171+
List<Condition> conditions = taskRun.getStatus().getConditions();
172+
if (conditions == null || conditions.size() == 0) {
173+
logMessage("[Tekton] TaskRun " + name + " has no status conditions");
174+
return;
175+
}
152176

153-
for (Condition condition : conditions) {
154-
logMessage("[Tekton] TaskRun " + name + " " + condition.getType() + "/" + condition.getReason() + ": " + condition.getMessage());
177+
for (Condition condition : conditions) {
178+
logMessage("[Tekton] TaskRun " + name + " " + condition.getType() + "/" + condition.getReason() + ": " + condition.getMessage());
179+
}
180+
} else {
181+
logMessage("[Tekton] TaskRun " + name + " has no status");
155182
}
156183
}
157184

src/test/java/org/waveywaves/jenkins/plugins/tekton/client/build/create/JenkinsFreestyleTest.java

+6-6
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,7 @@ public void testFreestyleJobWithFileInput() throws Exception {
8181

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

84-
String log = jenkinsRule.getLog(b);
84+
String log = JenkinsRule.getLog(b);
8585
System.out.println(log);
8686

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

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

116-
String log = jenkinsRule.getLog(b);
116+
String log = JenkinsRule.getLog(b);
117117
System.out.println(log);
118118

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

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

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

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

222-
String log = jenkinsRule.getLog(b);
222+
String log = JenkinsRule.getLog(b);
223223
System.out.println(log);
224224

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

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

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

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

331-
String log = jenkinsRule.getLog(b);
331+
String log = JenkinsRule.getLog(b);
332332
System.out.println(log);
333333

334334
assertThat(log, containsString("Legacy code started this job"));

src/test/java/org/waveywaves/jenkins/plugins/tekton/client/build/create/JenkinsPipelineTest.java

+11-19
Original file line numberDiff line numberDiff line change
@@ -19,11 +19,8 @@
1919
import io.fabric8.tekton.pipeline.v1beta1.TaskRunBuilder;
2020
import io.fabric8.tekton.pipeline.v1beta1.TaskRunList;
2121
import io.fabric8.tekton.pipeline.v1beta1.TaskRunListBuilder;
22-
import java.io.IOException;
2322
import java.net.HttpURLConnection;
2423
import java.net.URL;
25-
import java.nio.charset.StandardCharsets;
26-
import org.apache.commons.io.IOUtils;
2724
import org.jenkinsci.plugins.workflow.cps.CpsFlowDefinition;
2825
import org.jenkinsci.plugins.workflow.job.WorkflowJob;
2926
import org.jenkinsci.plugins.workflow.job.WorkflowRun;
@@ -32,7 +29,6 @@
3229
import org.junit.Test;
3330
import org.junit.rules.RuleChain;
3431
import org.junit.rules.TestRule;
35-
import org.jvnet.hudson.test.ExtractResourceSCM;
3632
import org.jvnet.hudson.test.JenkinsRule;
3733
import org.waveywaves.jenkins.plugins.tekton.client.TektonUtils;
3834
import org.waveywaves.jenkins.plugins.tekton.client.ToolUtils;
@@ -85,7 +81,7 @@ public void testScriptedPipelineWithFileInput_Task() throws Exception {
8581

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

88-
String log = jenkinsRule.getLog(b);
84+
String log = JenkinsRule.getLog(b);
8985
System.out.println(log);
9086

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

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

128-
String log = jenkinsRule.getLog(b);
124+
String log = JenkinsRule.getLog(b);
129125
System.out.println(log);
130126

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

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

172-
String log = jenkinsRule.getLog(b);
168+
String log = JenkinsRule.getLog(b);
173169
System.out.println(log);
174170

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

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

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

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

294-
String log = jenkinsRule.getLog(b);
290+
String log = JenkinsRule.getLog(b);
295291
System.out.println(log);
296292

297293
assertThat(log, containsString("[Pipeline] tektonCreateRaw"));
@@ -384,7 +380,7 @@ public void testDeclarativePipelineWithYamlInput_PipelineRun_DifferentNamespace(
384380
.addToItems(pod)
385381
.build();
386382

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

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

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

420-
String log = jenkinsRule.getLog(b);
416+
String log = JenkinsRule.getLog(b);
421417
System.out.println(log);
422418

423419
assertThat(log, containsString("[Pipeline] tektonCreateRaw"));
@@ -516,7 +512,7 @@ public void testDeclarativePipelineWithYamlInput_PipelineRun_FailingContainer()
516512
.addToItems(pod)
517513
.build();
518514

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

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

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

552-
String log = jenkinsRule.getLog(b);
548+
String log = JenkinsRule.getLog(b);
553549
System.out.println(log);
554550

555551
assertThat(log, containsString("[Pipeline] tektonCreateRaw"));
@@ -643,7 +639,7 @@ public void testDeclarativePipelineWithYamlInput_PipelineRun_FailingPipelineRun(
643639
.addToItems(pod)
644640
.build();
645641

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

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

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

679-
String log = jenkinsRule.getLog(b);
675+
String log = JenkinsRule.getLog(b);
680676
System.out.println(log);
681677

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

692-
private String contents(String filename) throws IOException {
693-
return IOUtils.toString(this.getClass().getResourceAsStream(filename), StandardCharsets.UTF_8.name());
694-
}
695-
696688
private OwnerReference ownerReference(String uid) {
697689
return new OwnerReference("", false, false, "", "", uid);
698690
}

0 commit comments

Comments
 (0)