Skip to content

Commit

Permalink
Add Pod log for kubernetes agent
Browse files Browse the repository at this point in the history
  • Loading branch information
LinuxSuRen committed Aug 17, 2018
1 parent ec1d3a6 commit 2763d97
Show file tree
Hide file tree
Showing 4 changed files with 162 additions and 1 deletion.
Original file line number Diff line number Diff line change
@@ -1,11 +1,27 @@
package org.csanchez.jenkins.plugins.kubernetes;

import hudson.EnvVars;
import hudson.model.Executor;
import hudson.model.Queue;
import hudson.slaves.AbstractCloudComputer;
import io.fabric8.kubernetes.api.model.Container;
import io.fabric8.kubernetes.api.model.Pod;
import io.fabric8.kubernetes.client.KubernetesClient;
import org.apache.commons.lang.StringUtils;
import org.kohsuke.stapler.QueryParameter;
import org.kohsuke.stapler.StaplerRequest;
import org.kohsuke.stapler.StaplerResponse;
import org.kohsuke.stapler.export.Exported;
import org.kohsuke.stapler.framework.io.ByteBuffer;
import org.kohsuke.stapler.framework.io.LargeText;

import java.io.IOException;

import java.security.KeyStoreException;
import java.security.NoSuchAlgorithmException;
import java.security.UnrecoverableKeyException;
import java.security.cert.CertificateEncodingException;
import java.util.ArrayList;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;

Expand Down Expand Up @@ -40,6 +56,39 @@ public void taskCompletedWithProblems(Executor executor, Queue.Task task, long d
LOGGER.log(Level.FINE, " Computer " + this + " taskCompletedWithProblems");
}

@Exported
public List<Container> getContainers() throws UnrecoverableKeyException, CertificateEncodingException, NoSuchAlgorithmException, KeyStoreException, IOException {
KubernetesSlave slave = getNode();
if(slave == null) {
return new ArrayList<>();
}

KubernetesCloud cloud = slave.getKubernetesCloud();
KubernetesClient client = cloud.connect();

String namespace = StringUtils.defaultIfBlank(slave.getNamespace(), client.getNamespace());
Pod pod = client.pods().inNamespace(namespace).withName(getName()).get();

return pod.getSpec().getContainers();
}

public void doContainerLog(@QueryParameter String containerId,
StaplerRequest req, StaplerResponse rsp) throws UnrecoverableKeyException, CertificateEncodingException, NoSuchAlgorithmException, KeyStoreException, IOException {
ByteBuffer outputStream = new ByteBuffer();
KubernetesSlave slave = getNode();
if(slave != null) {
KubernetesCloud cloud = slave.getKubernetesCloud();
KubernetesClient client = cloud.connect();

String namespace = StringUtils.defaultIfBlank(slave.getNamespace(), client.getNamespace());

client.pods().inNamespace(namespace).withName(getName())
.inContainer(containerId).tailingLines(20).watchLog(outputStream);
}

new LargeText(outputStream, false).doProgressText(req, rsp);
}

@Override
public String toString() {
return String.format("KubernetesComputer name: %s slave: %s", getName(), getNode());
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
<!--
The MIT License
Copyright (c) 2018, Alauda
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.
-->

<?jelly escape-by-default='true'?>
<j:jelly xmlns:j="jelly:core" xmlns:st="jelly:stapler" xmlns:d="jelly:define" xmlns:l="/lib/layout" xmlns:t="/lib/hudson" xmlns:f="/lib/form" xmlns:i="jelly:fmt">
<l:layout title="${it.displayName} log" secured="true">
<st:include page="sidepanel.jelly" />
<l:main-panel>
<pre id="out" />
<div id="spinner">
<img src="${imagesURL}/spinner.gif" alt=""/>
</div>
<t:progressiveText href="containerLog?containerId=${request.getParameter('name')}" idref="out" spinner="spinner" />
</l:main-panel>
</l:layout>
</j:jelly>
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
<!--
The MIT License
Copyright (c) 2018,Alauda
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.
-->

<?jelly escape-by-default='true'?>
<j:jelly xmlns:j="jelly:core" xmlns:st="jelly:stapler" xmlns:d="jelly:define" xmlns:l="/lib/layout" xmlns:t="/lib/hudson" xmlns:f="/lib/form" xmlns:i="jelly:fmt">
<l:layout title="${it.displayName} log" secured="true">
<st:include page="sidepanel.jelly" />
<l:main-panel>
<table class="sortable pane bigtable">
<tr>
<th>Name</th><th>Image</th><th>imagePullPolicy</th><th>workingDir</th>
</tr>

<j:forEach var="v" items="${it.containers}">
<tr>
<td><f:link href="container?name=${v.name}">${v.name}</f:link></td>
<td>${v.image}</td>
<td>${v.imagePullPolicy}</td>
<td>${v.workingDir}</td>
</tr>
</j:forEach>
</table>
</l:main-panel>
</l:layout>
</j:jelly>
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
<!--
The MIT License
Copyright (c) 2018,Alauda
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.
-->

<?jelly escape-by-default='true'?>
<j:jelly xmlns:j="jelly:core" xmlns:st="jelly:stapler" xmlns:d="jelly:define" xmlns:l="/lib/layout" xmlns:t="/lib/hudson" xmlns:f="/lib/form" xmlns:i="jelly:fmt">
<l:task icon="icon-clipboard icon-md" href="${rootURL}/${it.url}log" title="${%Log}" permission="${it.CONNECT}" />
<l:task icon="icon-computer icon-md" href="${rootURL}/${it.url}podLog" title="${%PodLog}"/>
</j:jelly>

0 comments on commit 2763d97

Please sign in to comment.