1
+ package io .kubernetes .client ;
2
+
3
+ import io .kubernetes .client .Configuration ;
4
+ import io .kubernetes .client .models .V1Pod ;
5
+ import io .kubernetes .client .util .WebSockets ;
6
+ import io .kubernetes .client .util .WebSocketStreamHandler ;
7
+
8
+ import java .io .InputStream ;
9
+ import java .io .IOException ;
10
+ import java .io .OutputStream ;
11
+ import java .util .ArrayList ;
12
+ import java .util .HashMap ;
13
+ import java .util .List ;
14
+
15
+ import com .squareup .okhttp .Call ;
16
+
17
+ public class PortForward {
18
+ private ApiClient apiClient ;
19
+
20
+ /**
21
+ * Simple PortForward API constructor, uses default configuration
22
+ */
23
+ public PortForward () {
24
+ this (Configuration .getDefaultApiClient ());
25
+ }
26
+
27
+ /**
28
+ * PortForward API Constructor
29
+ * @param apiClient The api client to use.
30
+ */
31
+ public PortForward (ApiClient apiClient ) {
32
+ this .apiClient = apiClient ;
33
+ }
34
+
35
+ /**
36
+ * Get the API client for these PortForward operations.
37
+ * @returns The API client that will be used.
38
+ */
39
+ public ApiClient getApiClient () {
40
+ return apiClient ;
41
+ }
42
+
43
+ /**
44
+ * Set the API client for subsequent PortForward operations.
45
+ * @param apiClient The new API client to use.
46
+ */
47
+ public void setApiClient (ApiClient apiClient ) {
48
+ this .apiClient = apiClient ;
49
+ }
50
+
51
+ private String makePath (String namespace , String name ) {
52
+ return "/api/v1/namespaces/" +
53
+ namespace +
54
+ "/pods/" +
55
+ name +
56
+ "/portforward" ;
57
+ }
58
+
59
+ /**
60
+ * PortForward to a container
61
+ *
62
+ * @param pod The pod where the command is run.
63
+ * @param ports The ports to forward
64
+ */
65
+ public PortForwardResult forward (V1Pod pod , List <Integer > ports ) throws ApiException , IOException {
66
+ return forward (pod .getMetadata ().getNamespace (), pod .getMetadata ().getNamespace (), ports );
67
+ }
68
+
69
+ /**
70
+ * PortForward to a container.
71
+ *
72
+ * @param namespace The namespace of the Pod
73
+ * @param name The name of the Pod
74
+ * @param ports The ports to forward
75
+ */
76
+ public PortForwardResult forward (String namespace , String name , List <Integer > ports ) throws ApiException , IOException {
77
+ String path = makePath (namespace , name );
78
+ WebSocketStreamHandler handler = new WebSocketStreamHandler ();
79
+ PortForwardResult result = new PortForwardResult (handler , ports );
80
+ List <Pair > queryParams = new ArrayList <>();
81
+ queryParams .add (new Pair ("ports" , "80" ));
82
+ WebSockets .stream (path , "GET" , queryParams , apiClient , handler );
83
+
84
+ // Wait for streams to start.
85
+ result .init ();
86
+
87
+ return result ;
88
+ }
89
+
90
+ /**
91
+ * PortForwardResult contains the result of an Attach call, it includes streams for stdout
92
+ * stderr and stdin.
93
+ */
94
+ public static class PortForwardResult {
95
+ private WebSocketStreamHandler handler ;
96
+ private HashMap <Integer , Integer > streams ;
97
+ private List <Integer > ports ;
98
+
99
+ public PortForwardResult (WebSocketStreamHandler handler , List <Integer > ports ) throws IOException {
100
+ this .handler = handler ;
101
+ this .streams = new HashMap <>();
102
+ this .ports = ports ;
103
+ }
104
+
105
+ public void init () throws IOException {
106
+ for (int i = 0 ; i < ports .size (); i ++) {
107
+ InputStream is = handler .getInputStream (i );
108
+ byte [] data = new byte [2 ];
109
+ is .read (data );
110
+ int port = data [0 ] + data [1 ] * 256 ;
111
+ streams .put (port , i );
112
+ }
113
+ }
114
+
115
+ private int findPortIndex (int portNumber ) {
116
+ Integer ix = streams .get (portNumber );
117
+ if (ix == null ) {
118
+ return -1 ;
119
+ }
120
+ return ix .intValue ();
121
+ }
122
+
123
+ /**
124
+ * Get the stream for the specified port index.
125
+ * Note that the first 2 bytes of the stream will be the port number.
126
+ * @param portNumber The port number to get the stream for.
127
+ */
128
+ public OutputStream getOutboundStream (int port ) {
129
+ int portIndex = findPortIndex (port );
130
+ if (portIndex == -1 ) {
131
+ throw new IllegalArgumentException ("No such port!" );
132
+ }
133
+ return handler .getOutputStream (portIndex * 2 );
134
+ }
135
+
136
+ public OutputStream getErrorStream (int port ) {
137
+ int portIndex = findPortIndex (port );
138
+ if (portIndex == -1 ) {
139
+ throw new IllegalArgumentException ("No such port!" );
140
+ }
141
+ return handler .getOutputStream (portIndex * 2 + 1 );
142
+ }
143
+
144
+ public InputStream getInputStream (int port ) throws IOException {
145
+ int portIndex = findPortIndex (port );
146
+ if (portIndex == -1 ) {
147
+ throw new IllegalArgumentException ("No such port!" );
148
+ }
149
+ return handler .getInputStream (portIndex * 2 );
150
+ }
151
+ }
152
+ }
0 commit comments