JAVA-14173 Rename kubernetes to kubernetes-modules (#12667)
* JAVA-14173 Rename kubernetes to kubernetes-modules * JAVA-14173 Remove failing module * JAVA-14173 Revert commenting spring-cloud-openfeign-2 module
This commit is contained in:
58
kubernetes-modules/k8s-intro/src/k8s/create-http-server.yml
Normal file
58
kubernetes-modules/k8s-intro/src/k8s/create-http-server.yml
Normal file
@@ -0,0 +1,58 @@
|
||||
apiVersion: v1
|
||||
kind: Namespace
|
||||
metadata:
|
||||
name: ns1
|
||||
---
|
||||
apiVersion: v1
|
||||
kind: Namespace
|
||||
metadata:
|
||||
name: ns2
|
||||
---
|
||||
apiVersion: apps/v1
|
||||
kind: Deployment
|
||||
metadata:
|
||||
name: httpd
|
||||
namespace: ns1
|
||||
labels:
|
||||
app: httpd
|
||||
version: "1"
|
||||
spec:
|
||||
replicas: 3
|
||||
selector:
|
||||
matchLabels:
|
||||
app: httpd
|
||||
template:
|
||||
metadata:
|
||||
labels:
|
||||
app: httpd
|
||||
spec:
|
||||
containers:
|
||||
- name: main
|
||||
image: httpd:alpine
|
||||
ports:
|
||||
- containerPort: 80
|
||||
---
|
||||
apiVersion: apps/v1
|
||||
kind: Deployment
|
||||
metadata:
|
||||
name: httpd
|
||||
namespace: ns2
|
||||
labels:
|
||||
app: httpd
|
||||
version: "2"
|
||||
foo: bar
|
||||
spec:
|
||||
replicas: 3
|
||||
selector:
|
||||
matchLabels:
|
||||
app: httpd
|
||||
template:
|
||||
metadata:
|
||||
labels:
|
||||
app: httpd
|
||||
spec:
|
||||
containers:
|
||||
- name: main
|
||||
image: httpd:alpine
|
||||
ports:
|
||||
- containerPort: 80
|
||||
@@ -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,31 @@
|
||||
/**
|
||||
*
|
||||
*/
|
||||
package com.baeldung.kubernetes.intro;
|
||||
|
||||
|
||||
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 ListNodes {
|
||||
|
||||
/**
|
||||
* @param args
|
||||
*/
|
||||
public static void main(String[] args) throws Exception {
|
||||
|
||||
ApiClient client = Config.defaultClient();
|
||||
CoreV1Api api = new CoreV1Api(client);
|
||||
V1NodeList nodeList = api.listNode(null, null, null, null, null, null, null, null, 10, false);
|
||||
nodeList.getItems()
|
||||
.stream()
|
||||
.forEach((node) -> System.out.println(node.getMetadata()));
|
||||
}
|
||||
|
||||
}
|
||||
@@ -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,68 @@
|
||||
/**
|
||||
*
|
||||
*/
|
||||
package com.baeldung.kubernetes.intro;
|
||||
|
||||
|
||||
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.openapi.models.V1PodList;
|
||||
import io.kubernetes.client.util.Config;
|
||||
import okhttp3.OkHttpClient;
|
||||
import okhttp3.logging.HttpLoggingInterceptor;
|
||||
|
||||
/**
|
||||
* @author Philippe
|
||||
*
|
||||
*/
|
||||
public class ListPodsWithFieldSelectors {
|
||||
|
||||
private static final Logger log = LoggerFactory.getLogger(ListPodsWithFieldSelectors.class);
|
||||
|
||||
/**
|
||||
* @param args
|
||||
*/
|
||||
public static void main(String[] args) throws Exception {
|
||||
|
||||
ApiClient client = Config.defaultClient();
|
||||
HttpLoggingInterceptor interceptor = new HttpLoggingInterceptor(message -> log.info(message));
|
||||
interceptor.setLevel(HttpLoggingInterceptor.Level.BODY);
|
||||
OkHttpClient newClient = client.getHttpClient()
|
||||
.newBuilder()
|
||||
.addInterceptor(interceptor)
|
||||
.readTimeout(0, TimeUnit.SECONDS)
|
||||
.build();
|
||||
|
||||
client.setHttpClient(newClient);
|
||||
CoreV1Api api = new CoreV1Api(client);
|
||||
|
||||
String fs = createSelector(args);
|
||||
V1PodList items = api.listPodForAllNamespaces(null, null, fs, null, null, null, null, null, 10, false);
|
||||
items.getItems()
|
||||
.stream()
|
||||
.map((pod) -> pod.getMetadata().getName() )
|
||||
.forEach((name) -> System.out.println("name=" + name));
|
||||
|
||||
}
|
||||
|
||||
private static String createSelector(String[] args) {
|
||||
|
||||
StringBuilder b = new StringBuilder();
|
||||
for( int i = 0 ; i < args.length; i++ ) {
|
||||
if( b.length() > 0 ) {
|
||||
b.append(',');
|
||||
}
|
||||
|
||||
b.append(args[i]);
|
||||
}
|
||||
|
||||
return b.toString();
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,68 @@
|
||||
/**
|
||||
*
|
||||
*/
|
||||
package com.baeldung.kubernetes.intro;
|
||||
|
||||
|
||||
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.openapi.models.V1PodList;
|
||||
import io.kubernetes.client.util.Config;
|
||||
import okhttp3.OkHttpClient;
|
||||
import okhttp3.logging.HttpLoggingInterceptor;
|
||||
|
||||
/**
|
||||
* @author Philippe
|
||||
*
|
||||
*/
|
||||
public class ListPodsWithLabelSelectors {
|
||||
|
||||
private static final Logger log = LoggerFactory.getLogger(ListPodsWithLabelSelectors.class);
|
||||
|
||||
/**
|
||||
* @param args
|
||||
*/
|
||||
public static void main(String[] args) throws Exception {
|
||||
|
||||
ApiClient client = Config.defaultClient();
|
||||
HttpLoggingInterceptor interceptor = new HttpLoggingInterceptor(message -> log.info(message));
|
||||
interceptor.setLevel(HttpLoggingInterceptor.Level.BODY);
|
||||
OkHttpClient newClient = client.getHttpClient()
|
||||
.newBuilder()
|
||||
.addInterceptor(interceptor)
|
||||
.readTimeout(0, TimeUnit.SECONDS)
|
||||
.build();
|
||||
|
||||
client.setHttpClient(newClient);
|
||||
CoreV1Api api = new CoreV1Api(client);
|
||||
|
||||
String selector = createSelector(args);
|
||||
V1PodList items = api.listPodForAllNamespaces(null, null, null, selector, null, null, null, null, 10, false);
|
||||
items.getItems()
|
||||
.stream()
|
||||
.map((pod) -> pod.getMetadata().getName() )
|
||||
.forEach((name) -> System.out.println("name=" + name));
|
||||
|
||||
}
|
||||
|
||||
private static String createSelector(String[] args) {
|
||||
|
||||
StringBuilder b = new StringBuilder();
|
||||
for( int i = 0 ; i < args.length; i++ ) {
|
||||
if( b.length() > 0 ) {
|
||||
b.append(',');
|
||||
}
|
||||
|
||||
b.append(args[i]);
|
||||
}
|
||||
|
||||
return b.toString();
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,52 @@
|
||||
/**
|
||||
*
|
||||
*/
|
||||
package com.baeldung.kubernetes.intro;
|
||||
|
||||
|
||||
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.openapi.models.V1PodList;
|
||||
import io.kubernetes.client.util.Config;
|
||||
import okhttp3.OkHttpClient;
|
||||
import okhttp3.logging.HttpLoggingInterceptor;
|
||||
|
||||
/**
|
||||
* @author Philippe
|
||||
*
|
||||
*/
|
||||
public class ListPodsWithNamespaces {
|
||||
|
||||
private static final Logger log = LoggerFactory.getLogger(ListPodsWithNamespaces.class);
|
||||
|
||||
/**
|
||||
* @param args
|
||||
*/
|
||||
public static void main(String[] args) throws Exception {
|
||||
|
||||
ApiClient client = Config.defaultClient();
|
||||
HttpLoggingInterceptor interceptor = new HttpLoggingInterceptor(message -> log.info(message));
|
||||
interceptor.setLevel(HttpLoggingInterceptor.Level.BODY);
|
||||
OkHttpClient newClient = client.getHttpClient()
|
||||
.newBuilder()
|
||||
.addInterceptor(interceptor)
|
||||
.readTimeout(0, TimeUnit.SECONDS)
|
||||
.build();
|
||||
|
||||
client.setHttpClient(newClient);
|
||||
CoreV1Api api = new CoreV1Api(client);
|
||||
String namespace = "ns1";
|
||||
V1PodList items = api.listNamespacedPod(namespace,null, null, null, null, null, null, null, null, 10, false);
|
||||
items.getItems()
|
||||
.stream()
|
||||
.map((pod) -> pod.getMetadata().getName() )
|
||||
.forEach((name) -> System.out.println("name=" + name));
|
||||
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,152 @@
|
||||
/**
|
||||
*
|
||||
*/
|
||||
package com.baeldung.kubernetes.intro;
|
||||
|
||||
import java.util.Collections;
|
||||
import java.util.concurrent.TimeUnit;
|
||||
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
|
||||
import io.kubernetes.client.custom.V1Patch;
|
||||
import io.kubernetes.client.custom.V1Patch.V1PatchAdapter;
|
||||
import io.kubernetes.client.openapi.ApiClient;
|
||||
import io.kubernetes.client.openapi.apis.BatchV1Api;
|
||||
import io.kubernetes.client.openapi.apis.CoreV1Api;
|
||||
import io.kubernetes.client.openapi.models.V1DeleteOptions;
|
||||
import io.kubernetes.client.openapi.models.V1DeleteOptionsBuilder;
|
||||
import io.kubernetes.client.openapi.models.V1Job;
|
||||
import io.kubernetes.client.openapi.models.V1JobBuilder;
|
||||
import io.kubernetes.client.openapi.models.V1JobSpec;
|
||||
import io.kubernetes.client.openapi.models.V1JobSpecBuilder;
|
||||
import io.kubernetes.client.openapi.models.V1ObjectMeta;
|
||||
import io.kubernetes.client.openapi.models.V1ObjectMetaBuilder;
|
||||
import io.kubernetes.client.openapi.models.V1Status;
|
||||
import io.kubernetes.client.util.Config;
|
||||
import io.kubernetes.client.util.PatchUtils;
|
||||
import okhttp3.OkHttpClient;
|
||||
import okhttp3.logging.HttpLoggingInterceptor;
|
||||
|
||||
/**
|
||||
* @author Philippe
|
||||
*
|
||||
*/
|
||||
public class RunJob {
|
||||
|
||||
private static Logger log = LoggerFactory.getLogger(RunJob.class);
|
||||
|
||||
public static void main(String[] args) throws Exception {
|
||||
|
||||
// Create client with logginginterceptor
|
||||
ApiClient client = Config.defaultClient();
|
||||
HttpLoggingInterceptor interceptor = new HttpLoggingInterceptor(message -> log.info(message));
|
||||
interceptor.setLevel(HttpLoggingInterceptor.Level.BODY);
|
||||
OkHttpClient newClient = client.getHttpClient()
|
||||
.newBuilder()
|
||||
.addInterceptor(interceptor)
|
||||
.readTimeout(0, TimeUnit.SECONDS)
|
||||
.build();
|
||||
client.setHttpClient(newClient);
|
||||
|
||||
// Create Job Spec
|
||||
BatchV1Api api = new BatchV1Api(client);
|
||||
String ns = "report-jobs";
|
||||
V1Job body = new V1JobBuilder()
|
||||
.withNewMetadata()
|
||||
.withNamespace(ns)
|
||||
.withName("payroll-report-job")
|
||||
.endMetadata()
|
||||
.withNewSpec()
|
||||
.withCompletions(2)
|
||||
.withParallelism(1)
|
||||
.withNewTemplate()
|
||||
.withNewMetadata()
|
||||
.addToLabels("name", "payroll-report")
|
||||
.endMetadata()
|
||||
.editOrNewSpec()
|
||||
.addNewContainer()
|
||||
.withName("main")
|
||||
.withImage("alpine")
|
||||
.addNewCommand("/bin/sh")
|
||||
.addNewArg("-c")
|
||||
.addNewArg("sleep 10")
|
||||
.endContainer()
|
||||
.withRestartPolicy("Never")
|
||||
.endSpec()
|
||||
.endTemplate()
|
||||
.endSpec()
|
||||
.build();
|
||||
|
||||
// Send to K8S
|
||||
V1Job createdJob = api.createNamespacedJob(ns, body, null, null, null);
|
||||
log.info("job: uid={}", createdJob.getMetadata().getUid());
|
||||
|
||||
// Let's change its parallelism value
|
||||
V1Job patchedJob = new V1JobBuilder(createdJob)
|
||||
.withNewMetadata()
|
||||
.withName(createdJob.getMetadata().getName())
|
||||
.withNamespace(createdJob.getMetadata().getNamespace())
|
||||
.endMetadata()
|
||||
.editSpec()
|
||||
.withParallelism(2)
|
||||
.endSpec()
|
||||
.build();
|
||||
|
||||
String patchedJobJSON = client.getJSON().serialize(patchedJob);
|
||||
V1Patch patch = new V1Patch(patchedJobJSON);
|
||||
|
||||
PatchUtils.patch(
|
||||
V1Job.class,
|
||||
() -> api.patchNamespacedJobCall(
|
||||
createdJob.getMetadata().getName(),
|
||||
createdJob.getMetadata().getNamespace(),
|
||||
patch,
|
||||
null,
|
||||
null,
|
||||
"acme",
|
||||
true,
|
||||
null),
|
||||
V1Patch.PATCH_FORMAT_APPLY_YAML,
|
||||
api.getApiClient());
|
||||
|
||||
while(!jobCompleted(api,createdJob)) {
|
||||
log.info("[I75] still running...");
|
||||
Thread.sleep(1000);
|
||||
}
|
||||
|
||||
V1Status response = api.deleteNamespacedJob(
|
||||
createdJob.getMetadata().getName(),
|
||||
createdJob.getMetadata().getNamespace(),
|
||||
null,
|
||||
null,
|
||||
null,
|
||||
null,
|
||||
null,
|
||||
null ) ;
|
||||
|
||||
log.info("[I122] response={}", response);
|
||||
}
|
||||
|
||||
private static boolean jobCompleted(BatchV1Api api, V1Job createdJob) throws Exception {
|
||||
|
||||
V1Job job = api.readNamespacedJob(
|
||||
createdJob.getMetadata().getName(),
|
||||
createdJob.getMetadata().getNamespace(),
|
||||
null,null,null);
|
||||
|
||||
if ( job.getStatus() == null ) {
|
||||
return false;
|
||||
}
|
||||
|
||||
log.info("[I88] Status: active={}, succeeded={}, failed={}",
|
||||
job.getStatus().getActive(),
|
||||
job.getStatus().getSucceeded(),
|
||||
job.getStatus().getFailed()
|
||||
);
|
||||
Integer active = job.getStatus().getActive();
|
||||
|
||||
return active == null || active == 0 ;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,72 @@
|
||||
package com.baeldung.kubernetes.intro;
|
||||
|
||||
import java.util.concurrent.TimeUnit;
|
||||
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
|
||||
import com.google.gson.reflect.TypeToken;
|
||||
|
||||
import io.kubernetes.client.openapi.ApiClient;
|
||||
import io.kubernetes.client.openapi.ApiException;
|
||||
import io.kubernetes.client.openapi.apis.CoreV1Api;
|
||||
import io.kubernetes.client.openapi.models.V1ObjectMeta;
|
||||
import io.kubernetes.client.openapi.models.V1Pod;
|
||||
import io.kubernetes.client.util.Config;
|
||||
import io.kubernetes.client.util.Watch;
|
||||
import io.kubernetes.client.util.Watch.Response;
|
||||
import okhttp3.OkHttpClient;
|
||||
import okhttp3.logging.HttpLoggingInterceptor;
|
||||
|
||||
public class WatchPods {
|
||||
|
||||
private static Logger log = LoggerFactory.getLogger(WatchPods.class);
|
||||
|
||||
public static void main(String[] args) throws Exception {
|
||||
|
||||
ApiClient client = Config.defaultClient();
|
||||
|
||||
// Optional, put helpful during tests: disable client timeout and enable
|
||||
// HTTP wire-level logs
|
||||
HttpLoggingInterceptor interceptor = new HttpLoggingInterceptor(message -> log.info(message));
|
||||
interceptor.setLevel(HttpLoggingInterceptor.Level.BODY);
|
||||
OkHttpClient newClient = client.getHttpClient()
|
||||
.newBuilder()
|
||||
.addInterceptor(interceptor)
|
||||
.readTimeout(0, TimeUnit.SECONDS)
|
||||
.build();
|
||||
|
||||
client.setHttpClient(newClient);
|
||||
CoreV1Api api = new CoreV1Api(client);
|
||||
|
||||
// Create the watch object that monitors pod creation/deletion/update events
|
||||
while (true) {
|
||||
log.info("[I46] Creating watch...");
|
||||
try (Watch<V1Pod> watch = Watch.createWatch(
|
||||
client,
|
||||
api.listPodForAllNamespacesCall(false, null, null, null, null, "false", null, null, 10, true, null),
|
||||
new TypeToken<Response<V1Pod>>(){}.getType())) {
|
||||
|
||||
log.info("[I52] Receiving events:");
|
||||
for (Response<V1Pod> event : watch) {
|
||||
V1Pod pod = event.object;
|
||||
V1ObjectMeta meta = pod.getMetadata();
|
||||
switch (event.type) {
|
||||
case "ADDED":
|
||||
case "MODIFIED":
|
||||
case "DELETED":
|
||||
log.info("event.type: {}, namespace={}, name={}",
|
||||
event.type,
|
||||
meta.getNamespace(),
|
||||
meta.getName());
|
||||
break;
|
||||
default:
|
||||
log.warn("[W66] Unknown event type: {}", event.type);
|
||||
}
|
||||
}
|
||||
} catch (ApiException ex) {
|
||||
log.error("[E70] ApiError", ex);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,87 @@
|
||||
package com.baeldung.kubernetes.intro;
|
||||
|
||||
import java.util.concurrent.TimeUnit;
|
||||
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
|
||||
import com.google.gson.reflect.TypeToken;
|
||||
|
||||
import io.kubernetes.client.openapi.ApiClient;
|
||||
import io.kubernetes.client.openapi.ApiException;
|
||||
import io.kubernetes.client.openapi.apis.CoreV1Api;
|
||||
import io.kubernetes.client.openapi.models.V1ObjectMeta;
|
||||
import io.kubernetes.client.openapi.models.V1Pod;
|
||||
import io.kubernetes.client.openapi.models.V1PodList;
|
||||
import io.kubernetes.client.util.Config;
|
||||
import io.kubernetes.client.util.Watch;
|
||||
import io.kubernetes.client.util.Watch.Response;
|
||||
import okhttp3.OkHttpClient;
|
||||
import okhttp3.logging.HttpLoggingInterceptor;
|
||||
|
||||
public class WatchPodsUsingBookmarks {
|
||||
|
||||
private static Logger log = LoggerFactory.getLogger(WatchPodsUsingBookmarks.class);
|
||||
|
||||
public static void main(String[] args) throws Exception {
|
||||
|
||||
ApiClient client = Config.defaultClient();
|
||||
|
||||
// Optional, put helpful during tests: disable client timeout and enable
|
||||
// HTTP wire-level logs
|
||||
HttpLoggingInterceptor interceptor = new HttpLoggingInterceptor(message -> log.info(message));
|
||||
interceptor.setLevel(HttpLoggingInterceptor.Level.BODY);
|
||||
OkHttpClient newClient = client.getHttpClient()
|
||||
.newBuilder()
|
||||
.addInterceptor(interceptor)
|
||||
.readTimeout(0, TimeUnit.SECONDS)
|
||||
.build();
|
||||
|
||||
client.setHttpClient(newClient);
|
||||
CoreV1Api api = new CoreV1Api(client);
|
||||
|
||||
String resourceVersion = null;
|
||||
while (true) {
|
||||
// Get a fresh list only we need to resync
|
||||
if ( resourceVersion == null ) {
|
||||
log.info("[I48] Creating initial POD list...");
|
||||
V1PodList podList = api.listPodForAllNamespaces(true, null, null, null, null, "false", resourceVersion, null, null, null);
|
||||
resourceVersion = podList.getMetadata().getResourceVersion();
|
||||
}
|
||||
|
||||
while (true) {
|
||||
log.info("[I54] Creating watch: resourceVersion={}", resourceVersion);
|
||||
try (Watch<V1Pod> watch = Watch.createWatch(
|
||||
client,
|
||||
api.listPodForAllNamespacesCall(true, null, null, null, null, "false", resourceVersion, null, 10, true, null),
|
||||
new TypeToken<Response<V1Pod>>(){}.getType())) {
|
||||
|
||||
log.info("[I60] Receiving events:");
|
||||
for (Response<V1Pod> event : watch) {
|
||||
V1Pod pod = event.object;
|
||||
V1ObjectMeta meta = pod.getMetadata();
|
||||
switch (event.type) {
|
||||
case "BOOKMARK":
|
||||
resourceVersion = meta.getResourceVersion();
|
||||
log.info("[I67] event.type: {}, resourceVersion={}", event.type,resourceVersion);
|
||||
break;
|
||||
case "ADDED":
|
||||
case "MODIFIED":
|
||||
case "DELETED":
|
||||
log.info("event.type: {}, namespace={}, name={}",
|
||||
event.type,
|
||||
meta.getNamespace(),
|
||||
meta.getName());
|
||||
break;
|
||||
default:
|
||||
log.warn("[W76] Unknown event type: {}", event.type);
|
||||
}
|
||||
}
|
||||
} catch (ApiException ex) {
|
||||
log.error("[E80] ApiError", ex);
|
||||
resourceVersion = null;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,111 @@
|
||||
package com.baeldung.kubernetes.intro;
|
||||
|
||||
import java.util.Map;
|
||||
import java.util.concurrent.TimeUnit;
|
||||
import java.util.regex.Matcher;
|
||||
import java.util.regex.Pattern;
|
||||
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
|
||||
import com.google.gson.Gson;
|
||||
import com.google.gson.reflect.TypeToken;
|
||||
|
||||
import io.kubernetes.client.openapi.ApiClient;
|
||||
import io.kubernetes.client.openapi.ApiException;
|
||||
import io.kubernetes.client.openapi.apis.CoreV1Api;
|
||||
import io.kubernetes.client.openapi.models.V1ObjectMeta;
|
||||
import io.kubernetes.client.openapi.models.V1Pod;
|
||||
import io.kubernetes.client.openapi.models.V1PodList;
|
||||
import io.kubernetes.client.util.Config;
|
||||
import io.kubernetes.client.util.Watch;
|
||||
import io.kubernetes.client.util.Watch.Response;
|
||||
import okhttp3.OkHttpClient;
|
||||
import okhttp3.logging.HttpLoggingInterceptor;
|
||||
|
||||
public class WatchPodsUsingResourceVersions {
|
||||
|
||||
private static Logger log = LoggerFactory.getLogger(WatchPodsUsingResourceVersions.class);
|
||||
|
||||
public static void main(String[] args) throws Exception {
|
||||
|
||||
ApiClient client = Config.defaultClient();
|
||||
|
||||
// Optional, put helpful during tests: disable client timeout and enable
|
||||
// HTTP wire-level logs
|
||||
HttpLoggingInterceptor interceptor = new HttpLoggingInterceptor(message -> log.info(message));
|
||||
interceptor.setLevel(HttpLoggingInterceptor.Level.BODY);
|
||||
OkHttpClient newClient = client.getHttpClient()
|
||||
.newBuilder()
|
||||
.addInterceptor(interceptor)
|
||||
.readTimeout(0, TimeUnit.SECONDS)
|
||||
.build();
|
||||
|
||||
client.setHttpClient(newClient);
|
||||
CoreV1Api api = new CoreV1Api(client);
|
||||
|
||||
String resourceVersion = null;
|
||||
while (true) {
|
||||
try {
|
||||
if ( resourceVersion == null ) {
|
||||
V1PodList podList = api.listPodForAllNamespaces(null, null, null, null, null, null, resourceVersion, null, null, null);
|
||||
resourceVersion = podList.getMetadata().getResourceVersion();
|
||||
}
|
||||
|
||||
log.info("[I59] Creating watch: resourceVersion={}", resourceVersion);
|
||||
try (Watch<V1Pod> watch = Watch.createWatch(
|
||||
client,
|
||||
api.listPodForAllNamespacesCall(null, null, null, null, null, null, resourceVersion, null, 60, true, null),
|
||||
new TypeToken<Response<V1Pod>>(){}.getType())) {
|
||||
|
||||
log.info("[I65] Receiving events:");
|
||||
for (Response<V1Pod> event : watch) {
|
||||
V1Pod pod = event.object;
|
||||
V1ObjectMeta meta = pod.getMetadata();
|
||||
switch (event.type) {
|
||||
case "ADDED":
|
||||
case "MODIFIED":
|
||||
case "DELETED":
|
||||
log.info("event: type={}, namespace={}, name={}",
|
||||
event.type,
|
||||
meta.getNamespace(),
|
||||
meta.getName());
|
||||
break;
|
||||
default:
|
||||
log.warn("[W76] Unknown event type: {}", event.type);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
catch (ApiException ex) {
|
||||
if ( ex.getCode() == 504 || ex.getCode() == 410 ) {
|
||||
resourceVersion = extractResourceVersionFromException(ex);
|
||||
}
|
||||
else {
|
||||
// Reset resource version
|
||||
resourceVersion = null;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private static String extractResourceVersionFromException(ApiException ex) {
|
||||
|
||||
String body = ex.getResponseBody();
|
||||
if (body == null) {
|
||||
return null;
|
||||
}
|
||||
|
||||
Gson gson = new Gson();
|
||||
Map<?,?> st = gson.fromJson(body, Map.class);
|
||||
Pattern p = Pattern.compile("Timeout: Too large resource version: (\\d+), current: (\\d+)");
|
||||
String msg = (String)st.get("message");
|
||||
Matcher m = p.matcher(msg);
|
||||
if (!m.matches()) {
|
||||
return null;
|
||||
}
|
||||
|
||||
return m.group(2);
|
||||
}
|
||||
|
||||
}
|
||||
@@ -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 ListNodesLiveTest {
|
||||
@Test
|
||||
void whenListNodes_thenSuccess() throws Exception {
|
||||
ListNodes.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[] {});
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,27 @@
|
||||
package com.baeldung.kubernetes.intro;
|
||||
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
class ListPodsWithFieldSelectorsLiveTest {
|
||||
@Test
|
||||
void givenEqualitySelector_whenListPodsWithFieldSelectors_thenSuccess() throws Exception {
|
||||
ListPodsWithFieldSelectors.main(new String[] {
|
||||
"metadata.namespace=ns1"
|
||||
});
|
||||
}
|
||||
|
||||
@Test
|
||||
void givenInequalitySelector_whenListPodsWithFieldSelectors_thenSuccess() throws Exception {
|
||||
ListPodsWithFieldSelectors.main(new String[] {
|
||||
"metadata.namespace!=ns1"
|
||||
});
|
||||
}
|
||||
|
||||
@Test
|
||||
void givenChainedSelector_whenListPodsWithFieldSelectors_thenSuccess() throws Exception {
|
||||
ListPodsWithFieldSelectors.main(new String[] {
|
||||
"metadata.namespace=ns1",
|
||||
"status.phase=Running"
|
||||
});
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,55 @@
|
||||
package com.baeldung.kubernetes.intro;
|
||||
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
class ListPodsWithLabelSelectorsLiveTest {
|
||||
@Test
|
||||
void givenEqualitySelector_whenListPodsWithLabelSelectors_thenSuccess() throws Exception {
|
||||
ListPodsWithLabelSelectors.main(new String[] {
|
||||
"app=httpd"
|
||||
});
|
||||
}
|
||||
|
||||
@Test
|
||||
void givenInqualitySelector_whenListPodsWithLabelSelectors_thenSuccess() throws Exception {
|
||||
ListPodsWithLabelSelectors.main(new String[] {
|
||||
"app!=httpd"
|
||||
});
|
||||
}
|
||||
|
||||
@Test
|
||||
void givenInSetSelector_whenListPodsWithLabelSelectors_thenSuccess() throws Exception {
|
||||
ListPodsWithLabelSelectors.main(new String[] {
|
||||
"app in (httpd,test)"
|
||||
});
|
||||
}
|
||||
|
||||
@Test
|
||||
void givenNotInSetSelector_whenListPodsWithLabelSelectors_thenSuccess() throws Exception {
|
||||
ListPodsWithLabelSelectors.main(new String[] {
|
||||
"app notin (httpd)"
|
||||
});
|
||||
}
|
||||
|
||||
@Test
|
||||
void givenLabelPresentSelector_whenListPodsWithLabelSelectors_thenSuccess() throws Exception {
|
||||
ListPodsWithLabelSelectors.main(new String[] {
|
||||
"app"
|
||||
});
|
||||
}
|
||||
|
||||
@Test
|
||||
void givenLabelNotPresentSelector_whenListPodsWithLabelSelectors_thenSuccess() throws Exception {
|
||||
ListPodsWithLabelSelectors.main(new String[] {
|
||||
"!app"
|
||||
});
|
||||
}
|
||||
|
||||
@Test
|
||||
void givenChainedSelector_whenListPodsWithLabelSelectors_thenSuccess() throws Exception {
|
||||
ListPodsWithLabelSelectors.main(new String[] {
|
||||
"app=httpd",
|
||||
"!foo"
|
||||
});
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,10 @@
|
||||
package com.baeldung.kubernetes.intro;
|
||||
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
class RunJobLiveTest {
|
||||
@Test
|
||||
void whenWatchPods_thenSuccess() throws Exception {
|
||||
RunJob.main(new String[] {});
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,10 @@
|
||||
package com.baeldung.kubernetes.intro;
|
||||
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
class WatchPodsLiveTest {
|
||||
@Test
|
||||
void whenWatchPods_thenSuccess() throws Exception {
|
||||
WatchPods.main(new String[] {});
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,10 @@
|
||||
package com.baeldung.kubernetes.intro;
|
||||
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
class WatchPodsUsingBookmarksLiveTest {
|
||||
@Test
|
||||
void whenWatchPods_thenSuccess() throws Exception {
|
||||
WatchPodsUsingBookmarks.main(new String[] {});
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,10 @@
|
||||
package com.baeldung.kubernetes.intro;
|
||||
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
class WatchPodsUsingResourceVersionsLiveTest {
|
||||
@Test
|
||||
void whenWatchPods_thenSuccess() throws Exception {
|
||||
WatchPodsUsingResourceVersions.main(new String[] {});
|
||||
}
|
||||
}
|
||||
11
kubernetes-modules/k8s-intro/src/test/resources/logback.xml
Normal file
11
kubernetes-modules/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>
|
||||
Reference in New Issue
Block a user