From 3c2a32755963557f4a0066c93cc8c44b64bf6d99 Mon Sep 17 00:00:00 2001 From: "Michael N. Lipp" Date: Mon, 14 Apr 2025 14:05:23 +0200 Subject: [PATCH] Necessary changes. Still not working due to https://github.com/kubernetes-client/java/issues/3143. --- org.jdrupes.vmoperator.common/build.gradle | 2 +- .../common/DynamicTypeAdapterFactory.java | 17 +++++----- .../org/jdrupes/vmoperator/common/K8s.java | 7 +++-- .../jdrupes/vmoperator/common/K8sClient.java | 31 ++++++++++++------- .../common/K8sClusterGenericStub.java | 1 + .../common/K8sDynamicModelsBase.java | 6 ++-- .../vmoperator/common/K8sDynamicStub.java | 5 +-- .../vmoperator/common/K8sDynamicStubBase.java | 2 +- .../vmoperator/common/K8sGenericStub.java | 4 ++- .../vmoperator/common/VmDefinition.java | 2 +- .../vmoperator/common/VmDefinitionStub.java | 5 +-- .../manager/LoadBalancerReconciler.java | 3 +- .../vmoperator/manager/PoolMonitor.java | 3 +- .../vmoperator/manager/PvcReconciler.java | 10 +++--- .../vmoperator/runner/qemu/StatusUpdater.java | 2 +- .../vmoperator/runner/qemu/VmDefUpdater.java | 4 +-- 16 files changed, 56 insertions(+), 48 deletions(-) diff --git a/org.jdrupes.vmoperator.common/build.gradle b/org.jdrupes.vmoperator.common/build.gradle index e72cb14..c9f1413 100644 --- a/org.jdrupes.vmoperator.common/build.gradle +++ b/org.jdrupes.vmoperator.common/build.gradle @@ -11,7 +11,7 @@ plugins { dependencies { api project(':org.jdrupes.vmoperator.util') api 'org.jgrapes:org.jgrapes.core:[1.22.1,2)' - api 'io.kubernetes:client-java:[19.0.0,20.0.0)' + api 'io.kubernetes:client-java:[23.0.0,24.0.0)' api 'org.yaml:snakeyaml' api 'com.fasterxml.jackson.datatype:jackson-datatype-jsr310:[2.16.1,3]' } diff --git a/org.jdrupes.vmoperator.common/src/org/jdrupes/vmoperator/common/DynamicTypeAdapterFactory.java b/org.jdrupes.vmoperator.common/src/org/jdrupes/vmoperator/common/DynamicTypeAdapterFactory.java index d21eed4..9625b98 100644 --- a/org.jdrupes.vmoperator.common/src/org/jdrupes/vmoperator/common/DynamicTypeAdapterFactory.java +++ b/org.jdrupes.vmoperator.common/src/org/jdrupes/vmoperator/common/DynamicTypeAdapterFactory.java @@ -26,7 +26,7 @@ import com.google.gson.TypeAdapterFactory; import com.google.gson.reflect.TypeToken; import com.google.gson.stream.JsonReader; import com.google.gson.stream.JsonWriter; -import io.kubernetes.client.openapi.ApiClient; +import io.kubernetes.client.openapi.JSON; import java.io.IOException; import java.lang.reflect.InvocationTargetException; import java.lang.reflect.Type; @@ -45,18 +45,15 @@ public class DynamicTypeAdapterFactory queryParams, - List collectionQueryParams, Object body, - Map headerParams, Map cookieParams, - Map formParams, String[] authNames, + public Call buildCall(String baseUrl, String path, String method, + List queryParams, List collectionQueryParams, + Object body, Map headerParams, + Map cookieParams, Map formParams, + String[] authNames, ApiCallback callback) throws ApiException { - return apiClient().buildCall(path, method, queryParams, + return apiClient().buildCall(baseUrl, path, method, queryParams, collectionQueryParams, body, headerParams, cookieParams, formParams, authNames, callback); } @@ -833,6 +836,7 @@ public class K8sClient extends ApiClient { /** * Builds the request. * + * @param baseUrl the base url * @param path the path * @param method the method * @param queryParams the query params @@ -849,12 +853,12 @@ public class K8sClient extends ApiClient { */ @SuppressWarnings({ "rawtypes", "PMD.ExcessiveParameterList" }) @Override - public Request buildRequest(String path, String method, + public Request buildRequest(String baseUrl, String path, String method, List queryParams, List collectionQueryParams, Object body, Map headerParams, Map cookieParams, Map formParams, String[] authNames, ApiCallback callback) throws ApiException { - return apiClient().buildRequest(path, method, queryParams, + return apiClient().buildRequest(baseUrl, path, method, queryParams, collectionQueryParams, body, headerParams, cookieParams, formParams, authNames, callback); } @@ -862,6 +866,7 @@ public class K8sClient extends ApiClient { /** * Builds the url. * + * @param baseUrl the base url * @param path the path * @param queryParams the query params * @param collectionQueryParams the collection query params @@ -869,9 +874,10 @@ public class K8sClient extends ApiClient { * @see ApiClient#buildUrl(java.lang.String, java.util.List, java.util.List) */ @Override - public String buildUrl(String path, List queryParams, + public String buildUrl(String baseUrl, String path, List queryParams, List collectionQueryParams) { - return apiClient().buildUrl(path, queryParams, collectionQueryParams); + return apiClient().buildUrl(baseUrl, path, queryParams, + collectionQueryParams); } /** @@ -907,14 +913,15 @@ public class K8sClient extends ApiClient { * @param queryParams the query params * @param headerParams the header params * @param cookieParams the cookie params + * @throws ApiException * @see ApiClient#updateParamsForAuth(java.lang.String[], java.util.List, java.util.Map, java.util.Map) */ @Override public void updateParamsForAuth(String[] authNames, List queryParams, - Map headerParams, - Map cookieParams) { + Map headerParams, Map cookieParams, + String payload, String method, URI uri) throws ApiException { apiClient().updateParamsForAuth(authNames, queryParams, headerParams, - cookieParams); + cookieParams, payload, method, uri); } /** diff --git a/org.jdrupes.vmoperator.common/src/org/jdrupes/vmoperator/common/K8sClusterGenericStub.java b/org.jdrupes.vmoperator.common/src/org/jdrupes/vmoperator/common/K8sClusterGenericStub.java index af87af2..6b5fd7e 100644 --- a/org.jdrupes.vmoperator.common/src/org/jdrupes/vmoperator/common/K8sClusterGenericStub.java +++ b/org.jdrupes.vmoperator.common/src/org/jdrupes/vmoperator/common/K8sClusterGenericStub.java @@ -240,6 +240,7 @@ public class K8sClusterGenericStub the object list type * @param the result type */ + @FunctionalInterface public interface GenericSupplier> { diff --git a/org.jdrupes.vmoperator.common/src/org/jdrupes/vmoperator/common/K8sDynamicModelsBase.java b/org.jdrupes.vmoperator.common/src/org/jdrupes/vmoperator/common/K8sDynamicModelsBase.java index 1813621..61c17be 100644 --- a/org.jdrupes.vmoperator.common/src/org/jdrupes/vmoperator/common/K8sDynamicModelsBase.java +++ b/org.jdrupes.vmoperator.common/src/org/jdrupes/vmoperator/common/K8sDynamicModelsBase.java @@ -22,7 +22,7 @@ import com.google.gson.Gson; import com.google.gson.JsonElement; import com.google.gson.JsonObject; import io.kubernetes.client.common.KubernetesListObject; -import io.kubernetes.client.openapi.Configuration; +import io.kubernetes.client.openapi.JSON; import io.kubernetes.client.openapi.models.V1ListMeta; import java.lang.reflect.InvocationTargetException; import java.util.ArrayList; @@ -147,9 +147,7 @@ public class K8sDynamicModelsBase * @param objectMeta the new metadata */ public void setMetadata(V1ListMeta objectMeta) { - data.add("metadata", - Configuration.getDefaultApiClient().getJSON().getGson() - .toJsonTree(objectMeta)); + data.add("metadata", JSON.getGson().toJsonTree(objectMeta)); } @Override diff --git a/org.jdrupes.vmoperator.common/src/org/jdrupes/vmoperator/common/K8sDynamicStub.java b/org.jdrupes.vmoperator.common/src/org/jdrupes/vmoperator/common/K8sDynamicStub.java index afed802..42658d0 100644 --- a/org.jdrupes.vmoperator.common/src/org/jdrupes/vmoperator/common/K8sDynamicStub.java +++ b/org.jdrupes.vmoperator.common/src/org/jdrupes/vmoperator/common/K8sDynamicStub.java @@ -21,6 +21,7 @@ package org.jdrupes.vmoperator.common; import io.kubernetes.client.Discovery.APIResource; import io.kubernetes.client.apimachinery.GroupVersionKind; import io.kubernetes.client.openapi.ApiException; +import io.kubernetes.client.openapi.JSON; import io.kubernetes.client.util.generic.options.ListOptions; import java.io.Reader; import java.util.Collection; @@ -101,8 +102,8 @@ public class K8sDynamicStub */ public static K8sDynamicStub createFromYaml(K8sClient client, APIResource context, Reader yaml) throws ApiException { - var model = new K8sDynamicModel(client.getJSON().getGson(), - K8s.yamlToJson(client, yaml)); + var model + = new K8sDynamicModel(JSON.getGson(), K8s.yamlToJson(client, yaml)); return K8sGenericStub.create(K8sDynamicModel.class, K8sDynamicModels.class, client, context, model, (c, ns, n) -> new K8sDynamicStub(c, context, ns, n)); diff --git a/org.jdrupes.vmoperator.common/src/org/jdrupes/vmoperator/common/K8sDynamicStubBase.java b/org.jdrupes.vmoperator.common/src/org/jdrupes/vmoperator/common/K8sDynamicStubBase.java index 44f419c..1d39847 100644 --- a/org.jdrupes.vmoperator.common/src/org/jdrupes/vmoperator/common/K8sDynamicStubBase.java +++ b/org.jdrupes.vmoperator.common/src/org/jdrupes/vmoperator/common/K8sDynamicStubBase.java @@ -46,6 +46,6 @@ public abstract class K8sDynamicStubBase the object list type * @param the result type */ + @FunctionalInterface public interface GenericSupplier> { diff --git a/org.jdrupes.vmoperator.common/src/org/jdrupes/vmoperator/common/VmDefinition.java b/org.jdrupes.vmoperator.common/src/org/jdrupes/vmoperator/common/VmDefinition.java index 0a25dd6..2d5e2fe 100644 --- a/org.jdrupes.vmoperator.common/src/org/jdrupes/vmoperator/common/VmDefinition.java +++ b/org.jdrupes.vmoperator.common/src/org/jdrupes/vmoperator/common/VmDefinition.java @@ -54,7 +54,7 @@ public class VmDefinition extends K8sDynamicModel { private static final Logger logger = Logger.getLogger(VmDefinition.class.getName()); @SuppressWarnings("PMD.FieldNamingConventions") - private static final Gson gson = new JSON().getGson(); + private static final Gson gson = JSON.getGson(); @SuppressWarnings("PMD.FieldNamingConventions") private static final ObjectMapper objectMapper = new ObjectMapper().registerModule(new JavaTimeModule()); diff --git a/org.jdrupes.vmoperator.common/src/org/jdrupes/vmoperator/common/VmDefinitionStub.java b/org.jdrupes.vmoperator.common/src/org/jdrupes/vmoperator/common/VmDefinitionStub.java index 72194da..57cd210 100644 --- a/org.jdrupes.vmoperator.common/src/org/jdrupes/vmoperator/common/VmDefinitionStub.java +++ b/org.jdrupes.vmoperator.common/src/org/jdrupes/vmoperator/common/VmDefinitionStub.java @@ -21,6 +21,7 @@ package org.jdrupes.vmoperator.common; import io.kubernetes.client.Discovery.APIResource; import io.kubernetes.client.apimachinery.GroupVersionKind; import io.kubernetes.client.openapi.ApiException; +import io.kubernetes.client.openapi.JSON; import io.kubernetes.client.util.generic.options.ListOptions; import java.io.Reader; import java.util.Collection; @@ -101,8 +102,8 @@ public class VmDefinitionStub */ public static VmDefinitionStub createFromYaml(K8sClient client, APIResource context, Reader yaml) throws ApiException { - var model = new VmDefinition(client.getJSON().getGson(), - K8s.yamlToJson(client, yaml)); + var model + = new VmDefinition(JSON.getGson(), K8s.yamlToJson(client, yaml)); return K8sGenericStub.create(VmDefinition.class, VmDefinitions.class, client, context, model, (c, ns, n) -> new VmDefinitionStub(c, context, ns, n)); diff --git a/org.jdrupes.vmoperator.manager/src/org/jdrupes/vmoperator/manager/LoadBalancerReconciler.java b/org.jdrupes.vmoperator.manager/src/org/jdrupes/vmoperator/manager/LoadBalancerReconciler.java index d190cef..b04a35f 100644 --- a/org.jdrupes.vmoperator.manager/src/org/jdrupes/vmoperator/manager/LoadBalancerReconciler.java +++ b/org.jdrupes.vmoperator.manager/src/org/jdrupes/vmoperator/manager/LoadBalancerReconciler.java @@ -22,6 +22,7 @@ import com.google.gson.Gson; import freemarker.template.Configuration; import freemarker.template.TemplateException; import io.kubernetes.client.openapi.ApiException; +import io.kubernetes.client.openapi.JSON; import io.kubernetes.client.openapi.models.V1APIService; import io.kubernetes.client.openapi.models.V1ObjectMeta; import io.kubernetes.client.util.generic.dynamic.DynamicKubernetesObject; @@ -122,7 +123,7 @@ import org.yaml.snakeyaml.constructor.SafeConstructor; ? (Map>) lbsDef : null; var client = channel.client(); - mergeMetadata(client.getJSON().getGson(), svcDef, defaults, vmDef); + mergeMetadata(JSON.getGson(), svcDef, defaults, vmDef); // Apply var svcStub = K8sV1ServiceStub diff --git a/org.jdrupes.vmoperator.manager/src/org/jdrupes/vmoperator/manager/PoolMonitor.java b/org.jdrupes.vmoperator.manager/src/org/jdrupes/vmoperator/manager/PoolMonitor.java index 1bc323c..6c9a823 100644 --- a/org.jdrupes.vmoperator.manager/src/org/jdrupes/vmoperator/manager/PoolMonitor.java +++ b/org.jdrupes.vmoperator.manager/src/org/jdrupes/vmoperator/manager/PoolMonitor.java @@ -21,6 +21,7 @@ package org.jdrupes.vmoperator.manager; import com.google.gson.JsonObject; import io.kubernetes.client.apimachinery.GroupVersionKind; import io.kubernetes.client.openapi.ApiException; +import io.kubernetes.client.openapi.JSON; import io.kubernetes.client.util.Watch; import java.io.IOException; import java.util.Collections; @@ -130,7 +131,7 @@ public class PoolMonitor extends // Get pool and merge changes var vmPool = pools.computeIfAbsent(poolName, k -> new VmPool(poolName)); - vmPool.defineFrom(client().getJSON().getGson().fromJson( + vmPool.defineFrom(JSON.getGson().fromJson( GsonPtr.to(poolModel.data()).to("spec").get(), VmPool.class)); poolPipeline.fire(new VmPoolChanged(vmPool)); } diff --git a/org.jdrupes.vmoperator.manager/src/org/jdrupes/vmoperator/manager/PvcReconciler.java b/org.jdrupes.vmoperator.manager/src/org/jdrupes/vmoperator/manager/PvcReconciler.java index e297183..b0f97fd 100644 --- a/org.jdrupes.vmoperator.manager/src/org/jdrupes/vmoperator/manager/PvcReconciler.java +++ b/org.jdrupes.vmoperator.manager/src/org/jdrupes/vmoperator/manager/PvcReconciler.java @@ -25,6 +25,7 @@ import freemarker.template.TemplateException; import freemarker.template.TemplateNotFoundException; import io.kubernetes.client.custom.V1Patch; import io.kubernetes.client.openapi.ApiException; +import io.kubernetes.client.openapi.JSON; import io.kubernetes.client.util.generic.dynamic.Dynamics; import io.kubernetes.client.util.generic.options.ListOptions; import io.kubernetes.client.util.generic.options.PatchOptions; @@ -160,8 +161,7 @@ import org.yaml.snakeyaml.constructor.SafeConstructor; opts.setForce(true); opts.setFieldManager("kubernetes-java-kubectl-apply"); if (pvcStub.patch(V1Patch.PATCH_FORMAT_APPLY_YAML, - new V1Patch(channel.client().getJSON().serialize(pvcDef)), opts) - .isEmpty()) { + new V1Patch(JSON.serialize(pvcDef)), opts).isEmpty()) { logger.warning( () -> "Could not patch pvc for " + pvcStub.name()); } @@ -203,8 +203,7 @@ import org.yaml.snakeyaml.constructor.SafeConstructor; opts.setForce(true); opts.setFieldManager("kubernetes-java-kubectl-apply"); if (pvcStub.patch(V1Patch.PATCH_FORMAT_APPLY_YAML, - new V1Patch(channel.client().getJSON().serialize(pvcDef)), opts) - .isEmpty()) { + new V1Patch(JSON.serialize(pvcDef)), opts).isEmpty()) { logger.warning( () -> "Could not patch pvc for " + pvcStub.name()); } @@ -218,8 +217,7 @@ import org.yaml.snakeyaml.constructor.SafeConstructor; PatchOptions opts = new PatchOptions(); opts.setFieldManager("kubernetes-java-kubectl-apply"); if (pvcStub.patch(V1Patch.PATCH_FORMAT_JSON_MERGE_PATCH, - new V1Patch(channel.client().getJSON().serialize(pvcDef)), opts) - .isEmpty()) { + new V1Patch(JSON.serialize(pvcDef)), opts).isEmpty()) { logger.warning( () -> "Could not patch pvc for " + pvcStub.name()); } diff --git a/org.jdrupes.vmoperator.runner.qemu/src/org/jdrupes/vmoperator/runner/qemu/StatusUpdater.java b/org.jdrupes.vmoperator.runner.qemu/src/org/jdrupes/vmoperator/runner/qemu/StatusUpdater.java index b5d02c2..f6c7c48 100644 --- a/org.jdrupes.vmoperator.runner.qemu/src/org/jdrupes/vmoperator/runner/qemu/StatusUpdater.java +++ b/org.jdrupes.vmoperator.runner.qemu/src/org/jdrupes/vmoperator/runner/qemu/StatusUpdater.java @@ -71,7 +71,7 @@ import org.jgrapes.core.events.Start; public class StatusUpdater extends VmDefUpdater { @SuppressWarnings("PMD.FieldNamingConventions") - private static final Gson gson = new JSON().getGson(); + private static final Gson gson = JSON.getGson(); @SuppressWarnings("PMD.FieldNamingConventions") private static final ObjectMapper objectMapper = new ObjectMapper().registerModule(new JavaTimeModule()); diff --git a/org.jdrupes.vmoperator.runner.qemu/src/org/jdrupes/vmoperator/runner/qemu/VmDefUpdater.java b/org.jdrupes.vmoperator.runner.qemu/src/org/jdrupes/vmoperator/runner/qemu/VmDefUpdater.java index 49c9e67..a24b766 100644 --- a/org.jdrupes.vmoperator.runner.qemu/src/org/jdrupes/vmoperator/runner/qemu/VmDefUpdater.java +++ b/org.jdrupes.vmoperator.runner.qemu/src/org/jdrupes/vmoperator/runner/qemu/VmDefUpdater.java @@ -19,6 +19,7 @@ package org.jdrupes.vmoperator.runner.qemu; import com.google.gson.JsonObject; +import io.kubernetes.client.openapi.JSON; import java.io.IOException; import java.nio.file.Files; import java.nio.file.Path; @@ -161,8 +162,7 @@ public class VmDefUpdater extends Component { : cond) .collect(Collectors.toCollection(() -> new ArrayList<>())); newConds.addAll(toReplace); - status.add("conditions", - apiClient.getJSON().getGson().toJsonTree(newConds)); + status.add("conditions", JSON.getGson().toJsonTree(newConds)); return status; } }