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 Logs {
38
+ private ApiClient apiClient ;
39
+ private CoreV1Api coreClient ;
40
+
41
+ /**
42
+ * Simple Logs API constructor, uses default configuration
43
+ */
44
+ public Logs () {
45
+ this (Configuration .getDefaultApiClient ());
46
+ }
47
+
48
+ /**
49
+ * Logs API Constructor
50
+ * @param apiClient The api client to use.
51
+ */
52
+ public Logs (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
+ /**
66
+ * Set the API client for subsequent Logs operations.
67
+ * @param apiClient The new API client to use.
68
+ */
69
+ public void setApiClient (ApiClient apiClient ) {
70
+ this .apiClient = apiClient ;
71
+ }
72
+
73
+ public InputStream streamNamespacedPodLog (V1Pod pod ) throws ApiException , IOException {
74
+ return streamNamespacedPodLog (pod .getMetadata ().getNamespace (), pod .getMetadata ().getName (),
75
+ pod .getSpec ().getContainers ().get (0 ).getName ());
76
+ }
77
+
78
+ // Important note. You must close this stream or else you can leak connections.
79
+ public InputStream streamNamespacedPodLog (String namespace , String name , String container ) throws ApiException , IOException {
80
+ return streamNamespacedPodLog (namespace , name , container , null , null , false );
81
+ }
82
+
83
+
84
+ // Important note. You must close this stream or else you can leak connections.
85
+ public InputStream streamNamespacedPodLog (String namespace , String name , String container ,
86
+ Integer sinceSeconds , Integer tailLines , boolean timestamps ) throws ApiException , IOException {
87
+ Call call = coreClient .readNamespacedPodLogCall (name , namespace , container , true , null , "false" , false , sinceSeconds , tailLines , timestamps , null , null );
88
+ Response response = call .execute ();
89
+ if (!response .isSuccessful ()) {
90
+ throw new ApiException ("Logs request failed: " + response .code ());
91
+ }
92
+ return response .body ().byteStream ();
93
+ }
94
+ }
0 commit comments