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 .models .V1Pod ;
17+ import io .kubernetes .client .util .WebSockets ;
18+ import io .kubernetes .client .util .WebSocketStreamHandler ;
19+
20+ import java .io .ByteArrayInputStream ;
21+ import java .io .InputStream ;
22+ import java .io .IOException ;
23+ import java .io .OutputStream ;
24+ import java .io .PipedInputStream ;
25+ import java .io .PipedOutputStream ;
26+ import java .io .Reader ;
27+
28+ import org .apache .commons .lang .StringUtils ;
29+
30+ public class Attach {
31+ private ApiClient apiClient ;
32+
33+ /**
34+ * Simple Attach API constructor, uses default configuration
35+ */
36+ public Attach () {
37+ this (Configuration .getDefaultApiClient ());
38+ }
39+
40+ /**
41+ * Attach API Constructor
42+ * @param apiClient The api client to use.
43+ */
44+ public Attach (ApiClient apiClient ) {
45+ this .apiClient = apiClient ;
46+ }
47+
48+ /**
49+ * Get the API client for these Attach operations.
50+ * @returns The API client that will be used.
51+ */
52+ public ApiClient getApiClient () {
53+ return apiClient ;
54+ }
55+
56+ /**
57+ * Set the API client for subsequent Attach operations.
58+ * @param apiClient The new API client to use.
59+ */
60+ public void setApiClient (ApiClient apiClient ) {
61+ this .apiClient = apiClient ;
62+ }
63+
64+ private String makePath (String namespace , String name , String container , boolean stdin , boolean tty ) {
65+ return "/api/v1/namespaces/" +
66+ namespace +
67+ "/pods/" +
68+ name +
69+ "/attach?" +
70+ "stdin=" + stdin +
71+ "&tty=" + tty +
72+ (container != null ? "&container=" + container : "" );
73+ }
74+
75+ /**
76+ * Attach to a running AttachResult in a container. If there are multiple containers in the pod, uses
77+ * the first container in the Pod.
78+ *
79+ * @param namespace The namespace of the Pod
80+ * @param name The name of the Pod
81+ * @param command The command to run
82+ * @param stdin If true, pass a stdin stream into the container
83+ */
84+ public AttachResult attach (String namespace , String name , boolean stdin ) throws ApiException , IOException {
85+ return attach (namespace , name , null , stdin , false );
86+ }
87+
88+ /**
89+ * Attach to a running AttachResult in a container. If there are multiple containers in the pod, uses
90+ * the first container in the Pod.
91+ *
92+ * @param pod The pod where the command is run.
93+ * @param stdin If true, pass a stdin stream into the container
94+ */
95+ public AttachResult attach (V1Pod pod , boolean stdin ) throws ApiException , IOException {
96+ return attach (pod , stdin , false );
97+ }
98+
99+
100+ /**
101+ * Attach to a running AttachResult in a container. If there are multiple containers in the pod, uses
102+ * the first container in the Pod.
103+ *
104+ * @param pod The pod where the command is run.
105+ * @param stdin If true, pass a stdin stream into the container
106+ * @param tty If true, stdin is a tty.
107+ */
108+ public AttachResult attach (V1Pod pod , boolean stdin , boolean tty ) throws ApiException , IOException {
109+ return attach (pod , null , stdin , tty );
110+ }
111+
112+ /**
113+ * Attach to a running AttachResult in a container. If there are multiple containers in the pod, uses
114+ * the first container in the Pod.
115+ *
116+ * @param pod The pod where the command is run.
117+ * @param container The container in the Pod where the command is run.
118+ * @param stdin If true, pass a stdin stream into the container.
119+ * @param tty If true, stdin is a TTY (only applies if stdin is true)
120+ */
121+ public AttachResult attach (V1Pod pod , String container , boolean stdin , boolean tty ) throws ApiException , IOException {
122+ return attach (pod .getMetadata ().getNamespace (), pod .getMetadata ().getName (), container , stdin , tty );
123+ }
124+
125+ /**
126+ * Attach to a running AttachResult in a container. If there are multiple containers in the pod, uses
127+ * the first container in the Pod.
128+ *
129+ * @param namespace The namespace of the Pod
130+ * @param name The name of the Pod
131+ * @param container The container in the Pod where the command is run.
132+ * @param stdin If true, pass a stdin stream into the container.
133+ * @param tty If true, stdin is a TTY (only applies if stdin is true)
134+ */
135+ public AttachResult attach (String namespace , String name , String container , boolean stdin , boolean tty ) throws ApiException , IOException {
136+ String path = makePath (namespace , name , container , stdin , tty );
137+
138+ WebSocketStreamHandler handler = new WebSocketStreamHandler ();
139+ AttachResult result = new AttachResult (handler );
140+ WebSockets .stream (path , "GET" , apiClient , handler );
141+
142+ return result ;
143+ }
144+
145+ /**
146+ * AttachResult contains the result of an Attach call, it includes streams for stdout
147+ * stderr and stdin.
148+ */
149+ public static class AttachResult {
150+ private WebSocketStreamHandler handler ;
151+
152+
153+ public AttachResult (WebSocketStreamHandler handler ) throws IOException {
154+ this .handler = handler ;
155+ }
156+
157+ public OutputStream getStandardInputStream () {
158+ return handler .getOutputStream (0 );
159+ }
160+
161+ public InputStream getStandardOutputStream () throws IOException {
162+ return handler .getInputStream (1 );
163+ }
164+
165+ public InputStream getErrorStream () throws IOException {
166+ return handler .getInputStream (2 );
167+ }
168+ }
169+ }
0 commit comments