17
17
import io .kubernetes .client .openapi .ApiException ;
18
18
import io .kubernetes .client .openapi .Configuration ;
19
19
import io .kubernetes .client .openapi .models .V1Pod ;
20
+ import io .kubernetes .client .util .exception .CopyNotSupportedException ;
20
21
import java .io .BufferedInputStream ;
21
22
import java .io .File ;
22
23
import java .io .FileInputStream ;
@@ -93,12 +94,12 @@ public void copyFileFromPod(
93
94
}
94
95
95
96
public void copyDirectoryFromPod (V1Pod pod , String srcPath , Path destination )
96
- throws ApiException , IOException {
97
+ throws ApiException , IOException , CopyNotSupportedException {
97
98
copyDirectoryFromPod (pod , null , srcPath , destination );
98
99
}
99
100
100
101
public void copyDirectoryFromPod (V1Pod pod , String container , String srcPath , Path destination )
101
- throws ApiException , IOException {
102
+ throws ApiException , IOException , CopyNotSupportedException {
102
103
copyDirectoryFromPod (
103
104
pod .getMetadata ().getNamespace (),
104
105
pod .getMetadata ().getName (),
@@ -108,14 +109,17 @@ public void copyDirectoryFromPod(V1Pod pod, String container, String srcPath, Pa
108
109
}
109
110
110
111
public void copyDirectoryFromPod (String namespace , String pod , String srcPath , Path destination )
111
- throws ApiException , IOException {
112
+ throws ApiException , IOException , CopyNotSupportedException {
112
113
copyDirectoryFromPod (namespace , pod , null , srcPath , destination );
113
114
}
114
115
115
116
public void copyDirectoryFromPod (
116
117
String namespace , String pod , String container , String srcPath , Path destination )
117
- throws ApiException , IOException {
118
- // TODO: Test that 'tar' is present in the container?
118
+ throws ApiException , IOException , CopyNotSupportedException {
119
+ // Test that 'tar' is present in the container?
120
+ if (!isTarPresentInContainer (namespace , pod , container )) {
121
+ throw new CopyNotSupportedException ("Tar is not present in the target container" );
122
+ }
119
123
final Process proc =
120
124
this .exec (
121
125
namespace ,
@@ -202,4 +206,20 @@ public void copyFileToPod(
202
206
203
207
return ;
204
208
}
209
+
210
+ private boolean isTarPresentInContainer (String namespace , String pod , String container )
211
+ throws ApiException , IOException {
212
+ final Process proc =
213
+ this .exec (
214
+ namespace , pod , new String [] {"sh" , "-c" , "tar --version" }, container , false , false );
215
+ // This will work for POSIX based operating systems
216
+ try {
217
+ int status = proc .waitFor ();
218
+ return status == 127 ? false : true ;
219
+ } catch (InterruptedException ex ) {
220
+ throw new IOException (ex );
221
+ } finally {
222
+ proc .destroy ();
223
+ }
224
+ }
205
225
}
0 commit comments