-
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.
Merge pull request #2501 from FWiesner/issue/2292
- Loading branch information
Showing
14 changed files
with
610 additions
and
91 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
98 changes: 98 additions & 0 deletions
98
...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,98 @@ | ||
/** | ||
* 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.Config; | ||
import io.fabric8.kubernetes.client.HasMetadataVisitiableBuilder; | ||
import io.fabric8.kubernetes.client.KubernetesClientException; | ||
import io.fabric8.kubernetes.client.ResourceHandler; | ||
import okhttp3.OkHttpClient; | ||
|
||
import java.net.HttpURLConnection; | ||
import java.util.Objects; | ||
import java.util.concurrent.CompletableFuture; | ||
import java.util.concurrent.TimeUnit; | ||
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 UnaryOperator<T> reloadTask; | ||
|
||
public CreateOrReplaceHelper(UnaryOperator<T> createTask, UnaryOperator<T> replaceTask, UnaryOperator<T> waitTask, UnaryOperator<T> reloadTask) { | ||
this.createTask = createTask; | ||
this.replaceTask = replaceTask; | ||
this.waitTask = waitTask; | ||
this.reloadTask = reloadTask; | ||
} | ||
|
||
public T createOrReplace(T item) { | ||
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 (shouldRetry(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(replace(item, resourceVersion)); | ||
} | ||
} | ||
return future.join(); | ||
} | ||
|
||
public static HasMetadata createOrReplaceItem(OkHttpClient client, Config config, HasMetadata meta, ResourceHandler<HasMetadata, HasMetadataVisitiableBuilder> h, String namespaceToUse) { | ||
CreateOrReplaceHelper<HasMetadata> createOrReplaceHelper = new CreateOrReplaceHelper<>( | ||
m -> h.create(client, config, namespaceToUse, m), | ||
m -> h.replace(client, config, namespaceToUse, m), | ||
m -> { | ||
try { | ||
return h.waitUntilCondition(client, config, namespaceToUse, m, Objects::nonNull, 1, TimeUnit.SECONDS); | ||
} catch (InterruptedException interruptedException) { | ||
interruptedException.printStackTrace(); | ||
} | ||
return null; | ||
}, | ||
m -> h.reload(client, config, namespaceToUse, m) | ||
); | ||
|
||
return createOrReplaceHelper.createOrReplace(meta); | ||
} | ||
|
||
private T replace(T item, String resourceVersion) { | ||
KubernetesResourceUtil.setResourceVersion(item, resourceVersion); | ||
return replaceTask.apply(item); | ||
} | ||
|
||
private boolean shouldRetry(int responseCode) { | ||
return responseCode > 499; | ||
} | ||
} |
54 changes: 54 additions & 0 deletions
54
...rnetes-client/src/main/java/io/fabric8/kubernetes/client/utils/DeleteAndCreateHelper.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,54 @@ | ||
/** | ||
* 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.DeletionPropagation; | ||
import io.fabric8.kubernetes.api.model.HasMetadata; | ||
import io.fabric8.kubernetes.client.Config; | ||
import io.fabric8.kubernetes.client.HasMetadataVisitiableBuilder; | ||
import io.fabric8.kubernetes.client.KubernetesClientException; | ||
import io.fabric8.kubernetes.client.ResourceHandler; | ||
import okhttp3.OkHttpClient; | ||
|
||
import java.util.function.Function; | ||
import java.util.function.UnaryOperator; | ||
|
||
public class DeleteAndCreateHelper<T extends HasMetadata> { | ||
private final UnaryOperator<T> createTask; | ||
private final Function<T, Boolean> deleteTask; | ||
|
||
public DeleteAndCreateHelper(UnaryOperator<T> createTask, Function<T, Boolean> deleteTask) { | ||
this.createTask = createTask; | ||
this.deleteTask = deleteTask; | ||
} | ||
|
||
public T deleteAndCreate(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); | ||
} | ||
|
||
public static HasMetadata deleteAndCreateItem(OkHttpClient client, Config config, HasMetadata meta, ResourceHandler<HasMetadata, HasMetadataVisitiableBuilder> h, String namespaceToUse, DeletionPropagation propagationPolicy) { | ||
DeleteAndCreateHelper<HasMetadata> deleteAndCreateHelper = new DeleteAndCreateHelper<>( | ||
m -> h.create(client, config, namespaceToUse, m), | ||
m -> h.delete(client, config, namespaceToUse, propagationPolicy, m) | ||
); | ||
|
||
return deleteAndCreateHelper.deleteAndCreate(meta); | ||
} | ||
} |
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.