|
| 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