use of io.kubernetes.client.openapi.ApiClient in project java by kubernetes-client.
the class InClusterClientExample method main.
public static void main(String[] args) throws IOException, ApiException {
// loading the in-cluster config, including:
// 1. service-account CA
// 2. service-account bearer-token
// 3. service-account namespace
// 4. master endpoints(ip, port) from pre-set environment variables
ApiClient client = ClientBuilder.cluster().build();
// if you prefer not to refresh service account token, please use:
// ApiClient client = ClientBuilder.oldCluster().build();
// set the global default api-client to the in-cluster one from above
Configuration.setDefaultApiClient(client);
// the CoreV1Api loads default api-client from global configuration.
CoreV1Api api = new CoreV1Api();
// invokes the CoreV1Api client
V1PodList list = api.listPodForAllNamespaces(null, null, null, null, null, null, null, null, null, null);
for (V1Pod item : list.getItems()) {
System.out.println(item.getMetadata().getName());
}
}
use of io.kubernetes.client.openapi.ApiClient in project java by kubernetes-client.
the class LeaderElectionExample method main.
public static void main(String[] args) throws Exception {
ApiClient client = Config.defaultClient();
Configuration.setDefaultApiClient(client);
// New
String appNamespace = "default";
String appName = "leader-election-foobar";
// Anything unique
String lockHolderIdentityName = UUID.randomUUID().toString();
EndpointsLock lock = new EndpointsLock(appNamespace, appName, lockHolderIdentityName);
LeaderElectionConfig leaderElectionConfig = new LeaderElectionConfig(lock, Duration.ofMillis(10000), Duration.ofMillis(8000), Duration.ofMillis(2000));
try (LeaderElector leaderElector = new LeaderElector(leaderElectionConfig)) {
leaderElector.run(() -> {
System.out.println("Do something when getting leadership.");
}, () -> {
System.out.println("Do something when losing leadership.");
});
}
}
use of io.kubernetes.client.openapi.ApiClient in project java by kubernetes-client.
the class MetricsExample method main.
public static void main(String[] args) throws IOException, ApiException {
ApiClient client = Config.defaultClient();
Configuration.setDefaultApiClient(client);
Metrics metrics = new Metrics(client);
NodeMetricsList list = metrics.getNodeMetrics();
for (NodeMetrics item : list.getItems()) {
System.out.println(item.getMetadata().getName());
System.out.println("------------------------------");
for (String key : item.getUsage().keySet()) {
System.out.println("\t" + key);
System.out.println("\t" + item.getUsage().get(key));
}
System.out.println();
}
for (PodMetrics item : metrics.getPodMetrics("default").getItems()) {
System.out.println(item.getMetadata().getName());
System.out.println("------------------------------");
if (item.getContainers() == null) {
continue;
}
for (ContainerMetrics container : item.getContainers()) {
System.out.println(container.getName());
System.out.println("-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-");
for (String key : container.getUsage().keySet()) {
System.out.println("\t" + key);
System.out.println("\t" + container.getUsage().get(key));
}
System.out.println();
}
}
}
use of io.kubernetes.client.openapi.ApiClient in project java by kubernetes-client.
the class ParseExample method main.
public static void main(String[] args) throws IOException, ApiException, ClassNotFoundException {
if (args.length < 2) {
System.err.println("Usage: ParseExample <file-name> <class-name e.g. V1Pod>");
System.exit(1);
}
ApiClient client = Config.defaultClient();
FileReader json = new FileReader(args[0]);
Object obj = client.getJSON().getGson().fromJson(json, Class.forName("io.kubernetes.client.models." + args[1]));
String output = client.getJSON().getGson().toJson(obj);
// Test round tripping...
Object obj2 = client.getJSON().getGson().fromJson(new StringReader(output), Class.forName("io.kubernetes.client.models." + args[1]));
String output2 = client.getJSON().getGson().toJson(obj2);
// Validate round trip
if (!output.equals(output2)) {
System.err.println("Error, expected:\n" + output + "\nto equal\n" + output2);
System.exit(2);
}
System.out.println(output);
}
use of io.kubernetes.client.openapi.ApiClient in project java by kubernetes-client.
the class PortForwardExample method main.
public static void main(String[] args) throws IOException, ApiException, InterruptedException {
ApiClient client = Config.defaultClient();
Configuration.setDefaultApiClient(client);
PortForward forward = new PortForward();
List<Integer> ports = new ArrayList<>();
int localPort = 8080;
int targetPort = 8080;
ports.add(targetPort);
final PortForward.PortForwardResult result = forward.forward("default", "camera-viz-7949dbf7c6-lpxkd", ports);
System.out.println("Forwarding!");
ServerSocket ss = new ServerSocket(localPort);
final Socket s = ss.accept();
System.out.println("Connected!");
new Thread(new Runnable() {
public void run() {
try {
Streams.copy(result.getInputStream(targetPort), s.getOutputStream());
} catch (IOException ex) {
ex.printStackTrace();
} catch (Exception ex) {
ex.printStackTrace();
}
}
}).start();
new Thread(new Runnable() {
public void run() {
try {
Streams.copy(s.getInputStream(), result.getOutboundStream(targetPort));
} catch (IOException ex) {
ex.printStackTrace();
} catch (Exception ex) {
ex.printStackTrace();
}
}
}).start();
Thread.sleep(10 * 1000);
System.exit(0);
}
Aggregations