Skip to content

Commit bc382e2

Browse files
authored
Merge pull request #87 from brendandburns/logs
Add an initial logs utility class.
2 parents 7766cef + ef18af3 commit bc382e2

File tree

2 files changed

+138
-0
lines changed

2 files changed

+138
-0
lines changed
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
/*
2+
Copyright 2017 The Kubernetes Authors.
3+
Licensed under the Apache License, Version 2.0 (the "License");
4+
you may not use this file except in compliance with the License.
5+
You may obtain a copy of the License at
6+
http://www.apache.org/licenses/LICENSE-2.0
7+
Unless required by applicable law or agreed to in writing, software
8+
distributed under the License is distributed on an "AS IS" BASIS,
9+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
10+
See the License for the specific language governing permissions and
11+
limitations under the License.
12+
*/
13+
package io.kubernetes.client.examples;
14+
15+
import io.kubernetes.client.ApiClient;
16+
import io.kubernetes.client.ApiException;
17+
import io.kubernetes.client.Configuration;
18+
import io.kubernetes.client.PodLogs;
19+
import io.kubernetes.client.apis.CoreV1Api;
20+
import io.kubernetes.client.models.V1Pod;
21+
import io.kubernetes.client.models.V1PodList;
22+
import io.kubernetes.client.util.Config;
23+
24+
import com.google.common.io.ByteStreams;
25+
26+
import java.io.BufferedReader;
27+
import java.io.InputStream;
28+
import java.io.InputStreamReader;
29+
import java.io.IOException;
30+
import java.io.OutputStream;
31+
32+
/**
33+
* A simple example of how to use the Java API
34+
*
35+
* Easiest way to run this:
36+
* mvn exec:java -Dexec.mainClass="io.kubernetes.client.examples.LogsExample"
37+
*
38+
* From inside $REPO_DIR/examples
39+
*/
40+
public class LogsExample {
41+
public static void main(String[] args) throws IOException, ApiException, InterruptedException {
42+
ApiClient client = Config.defaultClient();
43+
Configuration.setDefaultApiClient(client);
44+
CoreV1Api coreApi = new CoreV1Api(client);
45+
46+
PodLogs logs = new PodLogs();
47+
V1Pod pod = coreApi.listNamespacedPod("default", "false", null, null, null, null, null, null, null, null).getItems().get(0);
48+
49+
InputStream is = logs.streamNamespacedPodLog(pod);
50+
ByteStreams.copy(is, System.out);
51+
}
52+
}
Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
/*
2+
Copyright 2017 The Kubernetes Authors.
3+
Licensed under the Apache License, Version 2.0 (the "License");
4+
you may not use this file except in compliance with the License.
5+
You may obtain a copy of the License at
6+
http://www.apache.org/licenses/LICENSE-2.0
7+
Unless required by applicable law or agreed to in writing, software
8+
distributed under the License is distributed on an "AS IS" BASIS,
9+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
10+
See the License for the specific language governing permissions and
11+
limitations under the License.
12+
*/
13+
package io.kubernetes.client;
14+
15+
import io.kubernetes.client.Configuration;
16+
import io.kubernetes.client.apis.CoreV1Api;
17+
import io.kubernetes.client.models.V1Pod;
18+
import io.kubernetes.client.util.WebSockets;
19+
import io.kubernetes.client.util.WebSocketStreamHandler;
20+
21+
import java.io.ByteArrayInputStream;
22+
import java.io.InputStream;
23+
import java.io.IOException;
24+
import java.io.OutputStream;
25+
import java.io.PipedInputStream;
26+
import java.io.PipedOutputStream;
27+
import java.io.Reader;
28+
29+
import org.apache.commons.lang.StringUtils;
30+
31+
import com.squareup.okhttp.Call;
32+
import com.squareup.okhttp.Response;
33+
34+
/**
35+
* Utility class offering streaming access to Pod logs.
36+
*/
37+
public class PodLogs {
38+
private ApiClient apiClient;
39+
private CoreV1Api coreClient;
40+
41+
/**
42+
* Simple PodLogs API constructor, uses default configuration
43+
*/
44+
public PodLogs() {
45+
this(Configuration.getDefaultApiClient());
46+
}
47+
48+
/**
49+
* PodLogs API Constructor
50+
* @param apiClient The api client to use.
51+
*/
52+
public PodLogs(ApiClient apiClient) {
53+
this.apiClient = apiClient;
54+
this.coreClient = new CoreV1Api(apiClient);
55+
}
56+
57+
/**
58+
* Get the API client for these Logs operations.
59+
* @return The API client that will be used.
60+
*/
61+
public ApiClient getApiClient() {
62+
return apiClient;
63+
}
64+
65+
public InputStream streamNamespacedPodLog(V1Pod pod) throws ApiException, IOException {
66+
return streamNamespacedPodLog(pod.getMetadata().getNamespace(), pod.getMetadata().getName(),
67+
pod.getSpec().getContainers().get(0).getName());
68+
}
69+
70+
// Important note. You must close this stream or else you can leak connections.
71+
public InputStream streamNamespacedPodLog(String namespace, String name, String container) throws ApiException, IOException {
72+
return streamNamespacedPodLog(namespace, name, container, null, null, false);
73+
}
74+
75+
76+
// Important note. You must close this stream or else you can leak connections.
77+
public InputStream streamNamespacedPodLog(String namespace, String name, String container,
78+
Integer sinceSeconds, Integer tailLines, boolean timestamps) throws ApiException, IOException {
79+
Call call = coreClient.readNamespacedPodLogCall(name, namespace, container, true, null, "false", false, sinceSeconds, tailLines, timestamps, null, null);
80+
Response response = call.execute();
81+
if (!response.isSuccessful()) {
82+
throw new ApiException("Logs request failed: " + response.code());
83+
}
84+
return response.body().byteStream();
85+
}
86+
}

0 commit comments

Comments
 (0)