forked from jenkinsci/kubernetes-plugin
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathKubernetesLauncher.java
376 lines (342 loc) · 17.3 KB
/
KubernetesLauncher.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
/*
* The MIT License
*
* Copyright (c) 2017, CloudBees, Inc.
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
package org.csanchez.jenkins.plugins.kubernetes;
import static java.util.logging.Level.FINE;
import static java.util.logging.Level.INFO;
import static java.util.logging.Level.WARNING;
import edu.umd.cs.findbugs.annotations.CheckForNull;
import edu.umd.cs.findbugs.annotations.SuppressFBWarnings;
import hudson.Functions;
import hudson.model.TaskListener;
import hudson.slaves.JNLPLauncher;
import hudson.slaves.SlaveComputer;
import io.fabric8.kubernetes.api.model.ContainerStatus;
import io.fabric8.kubernetes.api.model.ObjectMeta;
import io.fabric8.kubernetes.api.model.Pod;
import io.fabric8.kubernetes.client.KubernetesClient;
import io.fabric8.kubernetes.client.KubernetesClientException;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.concurrent.TimeUnit;
import java.util.logging.Level;
import java.util.logging.Logger;
import java.util.stream.Collectors;
import jenkins.metrics.api.Metrics;
import org.apache.commons.lang.StringUtils;
import org.csanchez.jenkins.plugins.kubernetes.pod.retention.Reaper;
import org.kohsuke.stapler.DataBoundConstructor;
/**
* Launches on Kubernetes the specified {@link KubernetesComputer} instance.
*/
public class KubernetesLauncher extends JNLPLauncher {
// Report progress every 30 seconds
private static final long REPORT_INTERVAL = TimeUnit.SECONDS.toMillis(30L);
private static final Collection<String> POD_TERMINATED_STATES =
Collections.unmodifiableCollection(Arrays.asList("Succeeded", "Failed"));
private static final Logger LOGGER = Logger.getLogger(KubernetesLauncher.class.getName());
private volatile boolean launched = false;
/**
* Provisioning exception if any.
*/
@CheckForNull
private transient Throwable problem;
@DataBoundConstructor
public KubernetesLauncher(String tunnel, String vmargs) {
super(tunnel, vmargs);
}
public KubernetesLauncher() {
super();
}
@Override
public boolean isLaunchSupported() {
return !launched;
}
@Override
@SuppressFBWarnings(value = "SWL_SLEEP_WITH_LOCK_HELD", justification = "This is fine")
public synchronized void launch(SlaveComputer computer, TaskListener listener) {
if (!(computer instanceof KubernetesComputer)) {
throw new IllegalArgumentException("This Launcher can be used only with KubernetesComputer");
}
// Activate reaper if it never got activated.
Reaper.getInstance().maybeActivate();
KubernetesComputer kubernetesComputer = (KubernetesComputer) computer;
computer.setAcceptingTasks(false);
KubernetesSlave node = kubernetesComputer.getNode();
if (node == null) {
throw new IllegalStateException("Node has been removed, cannot launch " + computer.getName());
}
if (launched) {
LOGGER.log(INFO, "Agent has already been launched, activating: {0}", node.getNodeName());
computer.setAcceptingTasks(true);
return;
}
String cloudName = node.getCloudName();
try {
PodTemplate template = node.getTemplate();
KubernetesCloud cloud = node.getKubernetesCloud();
KubernetesClient client = cloud.connect();
Pod pod = template.build(node);
node.assignPod(pod);
String podName = pod.getMetadata().getName();
String namespace = Arrays.asList( //
pod.getMetadata().getNamespace(), template.getNamespace(), client.getNamespace()) //
.stream()
.filter(s -> StringUtils.isNotBlank(s))
.findFirst()
.orElse(null);
node.setNamespace(namespace);
// if the controller was interrupted after creating the pod but before it connected back, then
// the pod might already exist and the creating logic must be skipped.
Pod existingPod =
client.pods().inNamespace(namespace).withName(podName).get();
if (existingPod == null) {
LOGGER.log(FINE, () -> "Creating Pod: " + cloudName + " " + namespace + "/" + podName);
try {
pod = client.pods().inNamespace(namespace).create(pod);
} catch (KubernetesClientException e) {
Metrics.metricRegistry()
.counter(MetricNames.CREATION_FAILED)
.inc();
int httpCode = e.getCode();
if (400 <= httpCode && httpCode < 500) { // 4xx
if (httpCode == 403 && e.getMessage().contains("is forbidden: exceeded quota")) {
node.getRunListener()
.getLogger()
.printf(
"WARNING: Unable to create pod: %s %s/%s because kubernetes resource quota exceeded. %n%s%nRetrying...%n%n",
cloudName,
namespace,
pod.getMetadata().getName(),
e.getMessage());
} else if (httpCode == 409
&& e.getMessage().contains("Operation cannot be fulfilled on resourcequotas")) {
// See: https://github.com/kubernetes/kubernetes/issues/67761 ; A retry usually works.
node.getRunListener()
.getLogger()
.printf(
"WARNING: Unable to create pod: %s %s/%s because kubernetes resource quota update conflict. %n%s%nRetrying...%n%n",
cloudName,
namespace,
pod.getMetadata().getName(),
e.getMessage());
} else {
node.getRunListener()
.getLogger()
.printf(
"ERROR: Unable to create pod %s %s/%s.%n%s%n",
cloudName,
namespace,
pod.getMetadata().getName(),
e.getMessage());
PodUtils.cancelQueueItemFor(pod, e.getMessage());
}
} else if (500 <= httpCode && httpCode < 600) { // 5xx
LOGGER.log(FINE, "Kubernetes returned HTTP code {0} {1}. Retrying...", new Object[] {
e.getCode(), e.getStatus()
});
} else {
LOGGER.log(WARNING, "Kubernetes returned unhandled HTTP code {0} {1}", new Object[] {
e.getCode(), e.getStatus()
});
}
throw e;
}
LOGGER.log(INFO, () -> "Created Pod: " + cloudName + " " + namespace + "/" + podName);
listener.getLogger().printf("Created Pod: %s %s/%s%n", cloudName, namespace, podName);
Metrics.metricRegistry().counter(MetricNames.PODS_CREATED).inc();
node.getRunListener().getLogger().printf("Created Pod: %s %s/%s%n", cloudName, namespace, podName);
} else {
LOGGER.log(INFO, () -> "Pod already exists: " + cloudName + " " + namespace + "/" + podName);
listener.getLogger().printf("Pod already exists: %s %s/%s%n", cloudName, namespace, podName);
}
kubernetesComputer.setLaunching(true);
ObjectMeta podMetadata = pod.getMetadata();
template.getWorkspaceVolume().createVolume(client, podMetadata);
template.getVolumes().forEach(volume -> volume.createVolume(client, podMetadata));
client.pods()
.inNamespace(namespace)
.withName(podName)
.waitUntilReady(template.getSlaveConnectTimeout(), TimeUnit.SECONDS);
LOGGER.log(INFO, () -> "Pod is running: " + cloudName + " " + namespace + "/" + podName);
// We need the pod to be running and connected before returning
// otherwise this method keeps being called multiple times
// so wait for agent to be online
int waitForSlaveToConnect = template.getSlaveConnectTimeout();
int waitedForSlave;
SlaveComputer slaveComputer = null;
String status = null;
List<ContainerStatus> containerStatuses = null;
long lastReportTimestamp = System.currentTimeMillis();
for (waitedForSlave = 0; waitedForSlave < waitForSlaveToConnect; waitedForSlave++) {
slaveComputer = node.getComputer();
if (slaveComputer == null) {
Metrics.metricRegistry().counter(MetricNames.LAUNCH_FAILED).inc();
throw new IllegalStateException("Node was deleted, computer is null");
}
if (slaveComputer.isOnline()) {
break;
}
// Check that the pod hasn't failed already
pod = client.pods().inNamespace(namespace).withName(podName).get();
if (pod == null) {
Metrics.metricRegistry().counter(MetricNames.LAUNCH_FAILED).inc();
throw new IllegalStateException("Pod no longer exists: " + podName);
}
status = pod.getStatus().getPhase();
if (POD_TERMINATED_STATES.contains(status)) {
Metrics.metricRegistry().counter(MetricNames.LAUNCH_FAILED).inc();
Metrics.metricRegistry()
.counter(MetricNames.metricNameForPodStatus(status))
.inc();
logLastLines(containerStatuses, podName, namespace, node, null, client);
throw new IllegalStateException("Pod '" + podName + "' is terminated. Status: " + status);
}
containerStatuses = pod.getStatus().getContainerStatuses();
List<ContainerStatus> terminatedContainers = new ArrayList<>();
for (ContainerStatus info : containerStatuses) {
if (info != null) {
if (info.getState().getTerminated() != null) {
// Container has errored
LOGGER.log(INFO, "Container is terminated {0} [{2}]: {1}", new Object[] {
podName, info.getState().getTerminated(), info.getName()
});
listener.getLogger()
.printf(
"Container is terminated %1$s [%3$s]: %2$s%n",
podName, info.getState().getTerminated(), info.getName());
Metrics.metricRegistry()
.counter(MetricNames.LAUNCH_FAILED)
.inc();
terminatedContainers.add(info);
}
}
}
checkTerminatedContainers(terminatedContainers, podName, namespace, node, client);
if (lastReportTimestamp + REPORT_INTERVAL < System.currentTimeMillis()) {
LOGGER.log(INFO, "Waiting for agent to connect ({1}/{2}): {0}", new Object[] {
podName, waitedForSlave, waitForSlaveToConnect
});
listener.getLogger()
.printf(
"Waiting for agent to connect (%2$s/%3$s): %1$s%n",
podName, waitedForSlave, waitForSlaveToConnect);
lastReportTimestamp = System.currentTimeMillis();
}
Thread.sleep(1000);
}
if (slaveComputer == null || slaveComputer.isOffline()) {
Metrics.metricRegistry().counter(MetricNames.LAUNCH_FAILED).inc();
Metrics.metricRegistry().counter(MetricNames.FAILED_TIMEOUT).inc();
logLastLines(containerStatuses, podName, namespace, node, null, client);
throw new IllegalStateException(
"Agent is not connected after " + waitedForSlave + " seconds, status: " + status);
}
computer.setAcceptingTasks(true);
launched = true;
try {
// We need to persist the "launched" setting...
node.save();
} catch (IOException e) {
LOGGER.log(Level.WARNING, "Could not save() agent: " + e.getMessage(), e);
}
Metrics.metricRegistry().counter(MetricNames.PODS_LAUNCHED).inc();
} catch (Throwable ex) {
setProblem(ex);
Functions.printStackTrace(ex, node.getRunListener().error("Failed to launch " + node.getPodName()));
LOGGER.log(
Level.WARNING,
String.format("Error in provisioning; agent=%s, template=%s", node, node.getTemplateId()),
ex);
LOGGER.log(Level.FINER, "Removing Jenkins node: {0}", node.getNodeName());
try {
node.terminate();
} catch (IOException | InterruptedException e) {
LOGGER.log(Level.WARNING, "Unable to remove Jenkins node", e);
}
throw new RuntimeException(ex);
}
}
private void checkTerminatedContainers(
List<ContainerStatus> terminatedContainers,
String podId,
String namespace,
KubernetesSlave slave,
KubernetesClient client) {
if (!terminatedContainers.isEmpty()) {
Map<String, Integer> errors = terminatedContainers.stream()
.collect(Collectors.toMap(
ContainerStatus::getName,
(info) -> info.getState().getTerminated().getExitCode()));
// Print the last lines of failed containers
logLastLines(terminatedContainers, podId, namespace, slave, errors, client);
throw new IllegalStateException("Containers are terminated with exit codes: " + errors);
}
}
/**
* Log the last lines of containers logs
*/
private void logLastLines(
@CheckForNull List<ContainerStatus> containers,
String podId,
String namespace,
KubernetesSlave slave,
Map<String, Integer> errors,
KubernetesClient client) {
if (containers != null) {
for (ContainerStatus containerStatus : containers) {
String containerName = containerStatus.getName();
String log = client.pods()
.inNamespace(namespace)
.withName(podId)
.inContainer(containerStatus.getName())
.tailingLines(30)
.getLog();
if (!StringUtils.isBlank(log)) {
String msg =
errors != null ? String.format(" exited with error %s", errors.get(containerName)) : "";
LOGGER.log(
Level.SEVERE,
"Error in provisioning; agent={0}, template={1}. Container {2}{3}. Logs: {4}",
new Object[] {slave, slave.getTemplateOrNull(), containerName, msg, log});
}
}
}
}
/**
* The last problem that occurred, if any.
* @return
*/
@CheckForNull
public Throwable getProblem() {
return problem;
}
public void setProblem(@CheckForNull Throwable problem) {
this.problem = problem;
}
}