[BAEL-4849] Article code (#10566)
This commit is contained in:
parent
438b8163ca
commit
d1a87c711b
@ -16,6 +16,12 @@
|
||||
<artifactId>client-java</artifactId>
|
||||
<version>11.0.0</version>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>ch.qos.logback</groupId>
|
||||
<artifactId>logback-classic</artifactId>
|
||||
<version>1.2.3</version>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
|
||||
<build>
|
||||
|
@ -0,0 +1,11 @@
|
||||
package com.baeldung.kubernetes.intro;
|
||||
|
||||
import io.kubernetes.client.openapi.ApiCallback;
|
||||
import io.kubernetes.client.openapi.ApiException;
|
||||
import io.kubernetes.client.openapi.apis.CoreV1Api;
|
||||
import okhttp3.Call;
|
||||
|
||||
@FunctionalInterface
|
||||
public interface ApiInvoker<R> {
|
||||
Call apply(CoreV1Api api, ApiCallback<R> callback) throws ApiException;
|
||||
}
|
@ -0,0 +1,74 @@
|
||||
package com.baeldung.kubernetes.intro;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.Optional;
|
||||
import java.util.concurrent.CompletableFuture;
|
||||
import java.util.concurrent.Future;
|
||||
import java.util.function.BiFunction;
|
||||
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
|
||||
import io.kubernetes.client.openapi.ApiCallback;
|
||||
import io.kubernetes.client.openapi.ApiException;
|
||||
import io.kubernetes.client.openapi.apis.CoreV1Api;
|
||||
import okhttp3.Call;
|
||||
|
||||
public class AsyncHelper<R> implements ApiCallback<R> {
|
||||
|
||||
private static final Logger log = LoggerFactory.getLogger(AsyncHelper.class);
|
||||
|
||||
private CoreV1Api api;
|
||||
private CompletableFuture<R> callResult;
|
||||
|
||||
private AsyncHelper(CoreV1Api api) {
|
||||
this.api = api;
|
||||
}
|
||||
|
||||
public static <T> CompletableFuture<T> doAsync(CoreV1Api api, ApiInvoker<T> invoker) {
|
||||
|
||||
AsyncHelper<T> p = new AsyncHelper<>(api);
|
||||
return p.execute(invoker);
|
||||
}
|
||||
|
||||
private CompletableFuture<R> execute( ApiInvoker<R> invoker) {
|
||||
|
||||
try {
|
||||
callResult = new CompletableFuture<>();
|
||||
log.info("[I38] Calling API...");
|
||||
final Call call = invoker.apply(api,this);
|
||||
log.info("[I41] API Succesfully invoked: method={}, url={}",
|
||||
call.request().method(),
|
||||
call.request().url());
|
||||
return callResult;
|
||||
}
|
||||
catch(ApiException aex) {
|
||||
callResult.completeExceptionally(aex);
|
||||
return callResult;
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onFailure(ApiException e, int statusCode, Map<String, List<String>> responseHeaders) {
|
||||
log.error("[E53] onFailure",e);
|
||||
callResult.completeExceptionally(e);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onSuccess(R result, int statusCode, Map<String, List<String>> responseHeaders) {
|
||||
log.error("[E61] onSuccess: statusCode={}",statusCode);
|
||||
callResult.complete(result);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onUploadProgress(long bytesWritten, long contentLength, boolean done) {
|
||||
log.info("[E61] onUploadProgress: bytesWritten={}, contentLength={}, done={}",bytesWritten,contentLength,done);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onDownloadProgress(long bytesRead, long contentLength, boolean done) {
|
||||
log.info("[E75] onDownloadProgress: bytesRead={}, contentLength={}, done={}",bytesRead,contentLength,done);
|
||||
}
|
||||
}
|
||||
|
@ -0,0 +1,50 @@
|
||||
/**
|
||||
*
|
||||
*/
|
||||
package com.baeldung.kubernetes.intro;
|
||||
|
||||
|
||||
import java.util.concurrent.CompletableFuture;
|
||||
import java.util.concurrent.TimeUnit;
|
||||
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
|
||||
import io.kubernetes.client.openapi.ApiClient;
|
||||
import io.kubernetes.client.openapi.apis.CoreV1Api;
|
||||
import io.kubernetes.client.openapi.models.V1NodeList;
|
||||
import io.kubernetes.client.util.Config;
|
||||
|
||||
/**
|
||||
* @author Philippe
|
||||
*
|
||||
*/
|
||||
public class ListNodesAsync {
|
||||
|
||||
private static Logger log = LoggerFactory.getLogger(ListNodesAsync.class);
|
||||
/**
|
||||
* @param args
|
||||
*/
|
||||
public static void main(String[] args) throws Exception {
|
||||
|
||||
// Initial setup
|
||||
ApiClient client = Config.defaultClient();
|
||||
CoreV1Api api = new CoreV1Api(client);
|
||||
|
||||
// Start async call
|
||||
CompletableFuture<V1NodeList> p = AsyncHelper.doAsync(api,(capi,cb) ->
|
||||
capi.listNodeAsync(null, null, null, null, null, null, null, null, 10, false, cb)
|
||||
);
|
||||
|
||||
p.thenAcceptAsync((nodeList) -> {
|
||||
log.info("[I40] Processing results...");
|
||||
nodeList.getItems()
|
||||
.stream()
|
||||
.forEach((node) -> System.out.println(node.getMetadata()));
|
||||
});
|
||||
|
||||
log.info("[I46] Waiting results...");
|
||||
p.get(10, TimeUnit.SECONDS);
|
||||
}
|
||||
|
||||
}
|
@ -0,0 +1,46 @@
|
||||
/**
|
||||
*
|
||||
*/
|
||||
package com.baeldung.kubernetes.intro;
|
||||
|
||||
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
|
||||
import io.kubernetes.client.openapi.ApiClient;
|
||||
import io.kubernetes.client.openapi.apis.CoreV1Api;
|
||||
import io.kubernetes.client.openapi.models.V1NodeList;
|
||||
import io.kubernetes.client.openapi.models.V1PodList;
|
||||
import io.kubernetes.client.util.Config;
|
||||
|
||||
/**
|
||||
* @author Philippe
|
||||
*
|
||||
*/
|
||||
public class ListPodsPaged {
|
||||
|
||||
private static final Logger log = LoggerFactory.getLogger(ListPodsPaged.class);
|
||||
|
||||
/**
|
||||
* @param args
|
||||
*/
|
||||
public static void main(String[] args) throws Exception {
|
||||
|
||||
ApiClient client = Config.defaultClient();
|
||||
CoreV1Api api = new CoreV1Api(client);
|
||||
String continuationToken = null;
|
||||
int limit = 2; // Just for illustration purposes. Real world values would range from ~100 to ~1000/page
|
||||
Long remaining = null;
|
||||
do {
|
||||
log.info("==========================================================================");
|
||||
log.info("Retrieving data: continuationToken={}, remaining={}", continuationToken,remaining);
|
||||
V1PodList items = api.listPodForAllNamespaces(null, continuationToken, null, null, limit, null, null, null, 10, false);
|
||||
continuationToken = items.getMetadata().getContinue();
|
||||
remaining = items.getMetadata().getRemainingItemCount();
|
||||
items.getItems()
|
||||
.stream()
|
||||
.forEach((node) -> System.out.println(node.getMetadata()));
|
||||
} while( continuationToken != null );
|
||||
}
|
||||
|
||||
}
|
@ -0,0 +1,10 @@
|
||||
package com.baeldung.kubernetes.intro;
|
||||
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
class ListNodesAsyncLiveTest {
|
||||
@Test
|
||||
void whenListNodes_thenSuccess() throws Exception {
|
||||
ListNodesAsync.main(new String[] {});
|
||||
}
|
||||
}
|
@ -0,0 +1,10 @@
|
||||
package com.baeldung.kubernetes.intro;
|
||||
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
class ListPodsPagedLiveTest {
|
||||
@Test
|
||||
void whenListPodsPage_thenSuccess() throws Exception {
|
||||
ListPodsPaged.main(new String[] {});
|
||||
}
|
||||
}
|
11
kubernetes/k8s-intro/src/test/resources/logback.xml
Normal file
11
kubernetes/k8s-intro/src/test/resources/logback.xml
Normal file
@ -0,0 +1,11 @@
|
||||
<configuration>
|
||||
<appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
|
||||
<encoder>
|
||||
<pattern>%d{HH:mm:ss.SSS} [%thread] %-5level %logger{36} - %msg%n</pattern>
|
||||
</encoder>
|
||||
</appender>
|
||||
|
||||
<root level="debug">
|
||||
<appender-ref ref="STDOUT" />
|
||||
</root>
|
||||
</configuration>
|
Loading…
x
Reference in New Issue
Block a user