-
Notifications
You must be signed in to change notification settings - Fork 1.5k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Changes in BaseOperation to retry createOrReplace on server failure
+ Moved createOrReplace logic to CreateOrReplaceHelper class + Added tests
- Loading branch information
1 parent
625b4db
commit 57838a0
Showing
11 changed files
with
530 additions
and
88 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
85 changes: 85 additions & 0 deletions
85
...rnetes-client/src/main/java/io/fabric8/kubernetes/client/utils/CreateOrReplaceHelper.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,85 @@ | ||
/** | ||
* Copyright (C) 2015 Red Hat, Inc. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
package io.fabric8.kubernetes.client.utils; | ||
|
||
import io.fabric8.kubernetes.api.model.HasMetadata; | ||
import io.fabric8.kubernetes.client.KubernetesClientException; | ||
|
||
import java.net.HttpURLConnection; | ||
import java.util.concurrent.CompletableFuture; | ||
import java.util.function.Function; | ||
import java.util.function.UnaryOperator; | ||
|
||
public class CreateOrReplaceHelper<T extends HasMetadata> { | ||
public static final int CREATE_OR_REPLACE_RETRIES = 3; | ||
private final UnaryOperator<T> createTask; | ||
private final UnaryOperator<T> replaceTask; | ||
private final UnaryOperator<T> waitTask; | ||
private final Function<T, Boolean> deleteTask; | ||
private final UnaryOperator<T> reloadTask; | ||
|
||
public CreateOrReplaceHelper(UnaryOperator<T> createTask, UnaryOperator<T> replaceTask, UnaryOperator<T> waitTask, Function<T, Boolean> deleteTask, UnaryOperator<T> reloadTask) { | ||
this.createTask = createTask; | ||
this.replaceTask = replaceTask; | ||
this.waitTask = waitTask; | ||
this.deleteTask = deleteTask; | ||
this.reloadTask = reloadTask; | ||
} | ||
|
||
public T createOrReplace(T item, boolean deletingExisting) { | ||
String resourceVersion = KubernetesResourceUtil.getResourceVersion(item); | ||
final CompletableFuture<T> future = new CompletableFuture<>(); | ||
int nTries = 0; | ||
while (!future.isDone() && nTries < CREATE_OR_REPLACE_RETRIES) { | ||
try { | ||
// Create | ||
KubernetesResourceUtil.setResourceVersion(item, null); | ||
return createTask.apply(item); | ||
} catch (KubernetesClientException exception) { | ||
if (Utils.isHttpStatusCodeFromErrorEncounteredByServer(exception.getCode())) { | ||
T itemFromServer = reloadTask.apply(item); | ||
if (itemFromServer == null) { | ||
waitTask.apply(item); | ||
nTries++; | ||
continue; | ||
} | ||
} else if (exception.getCode() != HttpURLConnection.HTTP_CONFLICT) { | ||
throw exception; | ||
} | ||
|
||
future.complete(checkDeletingExistingOrReplace(item, deletingExisting, resourceVersion)); | ||
} | ||
} | ||
return future.join(); | ||
} | ||
|
||
private T handleDeletingExistingAndCreate(T item) { | ||
Boolean deleted = deleteTask.apply(item); | ||
if (Boolean.FALSE.equals(deleted)) { | ||
throw new KubernetesClientException("Failed to delete existing item:" + item.getMetadata().getName()); | ||
} | ||
return createTask.apply(item); | ||
} | ||
|
||
private T checkDeletingExistingOrReplace(T item, boolean deletingExisting, String resourceVersion) { | ||
if (Boolean.TRUE.equals(deletingExisting)) { | ||
return handleDeletingExistingAndCreate(item); | ||
} else { | ||
KubernetesResourceUtil.setResourceVersion(item, resourceVersion); | ||
return replaceTask.apply(item); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.