Skip to content

Commit

Permalink
Workaround for fabric8io#2292
Browse files Browse the repository at this point in the history
  • Loading branch information
FWiesner authored and rohanKanojia committed Oct 19, 2020
1 parent 1cd9f20 commit f76160d
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 11 deletions.
Expand Up @@ -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;
Expand Down Expand Up @@ -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<T> 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
Expand Down
Expand Up @@ -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");
Expand Down

0 comments on commit f76160d

Please sign in to comment.