From b6d9ba7f805981d63446a7f8a056f0dd5bc6637c Mon Sep 17 00:00:00 2001 From: Florian Wiesner Date: Fri, 18 Sep 2020 12:52:16 +0200 Subject: [PATCH] Workaround for #2292 --- .../client/dsl/base/BaseOperation.java | 40 ++++++++++++++----- .../kubernetes/client/utils/Utils.java | 1 + 2 files changed, 30 insertions(+), 11 deletions(-) diff --git a/kubernetes-client/src/main/java/io/fabric8/kubernetes/client/dsl/base/BaseOperation.java b/kubernetes-client/src/main/java/io/fabric8/kubernetes/client/dsl/base/BaseOperation.java index 6b1b4eff704..935dd89c3e5 100755 --- a/kubernetes-client/src/main/java/io/fabric8/kubernetes/client/dsl/base/BaseOperation.java +++ b/kubernetes-client/src/main/java/io/fabric8/kubernetes/client/dsl/base/BaseOperation.java @@ -71,6 +71,7 @@ import java.util.List; import java.util.Map; import java.util.Objects; +import java.util.concurrent.CompletableFuture; import java.util.concurrent.ExecutionException; import java.util.concurrent.TimeUnit; import java.util.concurrent.TimeoutException; @@ -405,19 +406,36 @@ public final T createOrReplace(T... items) { return withName(itemToCreateOrReplace.getMetadata().getName()).createOrReplace(itemToCreateOrReplace); } - try { - // Create - KubernetesResourceUtil.setResourceVersion(itemToCreateOrReplace, null); - return create(itemToCreateOrReplace); - } catch (KubernetesClientException exception) { - if (exception.getCode() != HttpURLConnection.HTTP_CONFLICT) { - throw exception; + final CompletableFuture future = new CompletableFuture<>(); + while (!future.isDone()) { + try { + // Create + KubernetesResourceUtil.setResourceVersion(itemToCreateOrReplace, null); + future.complete(create(itemToCreateOrReplace)); + } catch (KubernetesClientException exception) { + final T itemFromServer; + if (exception.getCode() == HttpURLConnection.HTTP_INTERNAL_ERROR) { + itemFromServer = fromServer().get(); + if (itemFromServer == null) { + try { + Thread.sleep(200); + } catch (InterruptedException e) { + Thread.currentThread().interrupt(); + } + continue; + } + } else if (exception.getCode() != HttpURLConnection.HTTP_CONFLICT) { + throw exception; + } else { + itemFromServer = fromServer().get(); + } + + // Conflict; Do Replace + KubernetesResourceUtil.setResourceVersion(itemToCreateOrReplace, KubernetesResourceUtil.getResourceVersion(itemFromServer)); + future.complete(replace(itemToCreateOrReplace)); } - // Conflict; Do Replace - final T itemFromServer = fromServer().get(); - KubernetesResourceUtil.setResourceVersion(itemToCreateOrReplace, KubernetesResourceUtil.getResourceVersion(itemFromServer)); - return replace(itemToCreateOrReplace); } + return future.join(); } @Override diff --git a/kubernetes-client/src/main/java/io/fabric8/kubernetes/client/utils/Utils.java b/kubernetes-client/src/main/java/io/fabric8/kubernetes/client/utils/Utils.java index e1deab12fe1..99a90c2315e 100644 --- a/kubernetes-client/src/main/java/io/fabric8/kubernetes/client/utils/Utils.java +++ b/kubernetes-client/src/main/java/io/fabric8/kubernetes/client/utils/Utils.java @@ -358,6 +358,7 @@ public static String getPluralFromKind(String kind) { break; case "NetworkPolicy": case "PodSecurityPolicy": + case "ServiceEntry": // an Istio resource. Needed as getPluralFromKind is currently not configurable #2489 // Delete last character pluralBuffer.deleteCharAt(pluralBuffer.length() - 1); pluralBuffer.append("ies");