-
Notifications
You must be signed in to change notification settings - Fork 14
/
Copy pathExecDemo.java
79 lines (65 loc) · 2.78 KB
/
ExecDemo.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
package io.fabric8;
import java.io.IOException;
import java.io.OutputStream;
import java.util.concurrent.TimeUnit;
import java.util.logging.Level;
import java.util.logging.Logger;
import io.fabric8.kubernetes.api.model.Pod;
import io.fabric8.kubernetes.api.model.PodBuilder;
import io.fabric8.kubernetes.client.KubernetesClientBuilder;
import io.fabric8.kubernetes.client.KubernetesClient;
import io.fabric8.kubernetes.client.KubernetesClientException;
import io.fabric8.kubernetes.client.dsl.ExecListener;
import io.fabric8.kubernetes.client.dsl.ExecWatch;
public class ExecDemo {
private static final Logger logger = Logger.getLogger(ExecDemo.class
.getName());
public static void main(String[] args) {
try (KubernetesClient client = new KubernetesClientBuilder().build()) {
String namespace = "default";
Pod pod1 = new PodBuilder().withNewMetadata().withName("pod1")
.endMetadata().withNewSpec().addNewContainer()
.withName("mysql").withImage("openshift/mysql-55-centos7")
.addNewPort().withContainerPort(3306).endPort().addNewEnv()
.withName("MYSQL_ROOT_PASSWORD").withValue("password")
.endEnv().addNewEnv().withName("MYSQL_DATABASE")
.withValue("foodb").endEnv().addNewEnv()
.withName("MYSQL_USER").withValue("luke").endEnv()
.addNewEnv().withName("MYSQL_PASSWORD")
.withValue("password").endEnv().endContainer().endSpec()
.build();
client.pods().inNamespace(namespace).resource(pod1).create();
logger.log(Level.INFO, "created pod");
logger.log(Level.INFO, "Waiting for the pod to start");
client.pods().inNamespace(namespace).withName(pod1.getMetadata().getName()).waitUntilReady(1, TimeUnit.MINUTES);
logger.log(Level.INFO, "executing a simple command");
ExecWatch execWatch = client.pods().inNamespace(namespace)
.withName("pod1").redirectingInput()
.writingOutput(System.out).writingError(System.err)
.withTTY().usingListener(new SimpleListener()).exec("ls");
OutputStream input = execWatch.getInput();
input.write("hello".getBytes());
input.flush();
execWatch.close();
execWatch.exitCode().join();
client.pods().inNamespace(namespace).withName("pod1").delete();
logger.info("Closing client now...");
} catch (KubernetesClientException | IOException exception) {
exception.printStackTrace();
}
}
private static class SimpleListener implements ExecListener {
public void onClose(int arg0, String arg1) {
// TODO Auto-generated method stub
logger.log(Level.INFO, "Closing shell now");
}
public void onFailure(Throwable arg0, Response arg1) {
// TODO Auto-generated method stub
logger.log(Level.SEVERE, "The shell is failed somehow");
}
public void onOpen(Response arg0) {
// TODO Auto-generated method stub
logger.log(Level.INFO, "The shell is open now...");
}
}
}