Skip to content

Commit

Permalink
Fixed the total deletion caused when a deleted resource was null (#2417)
Browse files Browse the repository at this point in the history
* Fixed the total deletion caused when a deleted resource was null

Co-authored-by: 盖泉贺 <gaiquanhe@inspur.com>
  • Loading branch information
manusa and GQH1993 committed Aug 21, 2020
1 parent c5dc134 commit 71772d3
Showing 1 changed file with 25 additions and 22 deletions.
Expand Up @@ -57,6 +57,7 @@
import io.fabric8.kubernetes.client.utils.URLUtils;
import io.fabric8.kubernetes.client.utils.Utils;
import io.fabric8.kubernetes.client.utils.WatcherToggle;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
Expand All @@ -75,6 +76,7 @@
import java.util.concurrent.TimeUnit;
import java.util.concurrent.TimeoutException;
import java.util.function.Predicate;

import okhttp3.HttpUrl;
import okhttp3.Request;

Expand All @@ -83,7 +85,7 @@ public class BaseOperation<T extends HasMetadata, L extends KubernetesResourceLi
implements
OperationInfo,
MixedOperation<T, L, D, R>,
Resource<T,D> {
Resource<T, D> {

private static final Logger LOG = LoggerFactory.getLogger(BaseOperation.class);

Expand Down Expand Up @@ -134,8 +136,8 @@ protected BaseOperation(OperationContext ctx) {
* @param item The item.
* @param name The name to check.
* @param <T>
* @return
*/
* @return
*/
private static <T> String name(T item, String name) {
if (name != null && !name.isEmpty()) {
return name;
Expand All @@ -147,7 +149,7 @@ private static <T> String name(T item, String name) {
}


public BaseOperation<T,L,D,R> newInstance(OperationContext context) {
public BaseOperation<T, L, D, R> newInstance(OperationContext context) {
return new BaseOperation<T, L, D, R>(context);
}

Expand Down Expand Up @@ -178,7 +180,7 @@ protected URL fetchListUrl(URL url, ListOptions listOptions) throws MalformedURL
}

private void addQueryStringParam(HttpUrl.Builder requestUrlBuilder, String name, String value) {
if(Utils.isNotNullOrEmpty(value)) {
if (Utils.isNotNullOrEmpty(value)) {
requestUrlBuilder.addQueryParameter(name, value);
}
}
Expand Down Expand Up @@ -236,12 +238,12 @@ public T getMandatory() {
} catch (KubernetesClientException e) {
throw KubernetesClientException.launderThrowable(forOperationType("get"), e);
//if (e.getCode() != HttpURLConnection.HTTP_NOT_FOUND) {
// throw e;
// throw e;
//} else {
// String resourceType = type != null ? type.getSimpleName() : "Resource";
// String msg = resourceType + " with name: [" + getName() + "] not found in namespace: [" + (Utils.isNotNullOrEmpty(getNamespace()) ? getName() : getConfig().getNamespace()) + "]";
// throw new KubernetesClientException(msg, HttpURLConnection.HTTP_NOT_FOUND, new StatusBuilder().withCode(HttpURLConnection.HTTP_NOT_FOUND).withMessage(msg).build());
// }
// throw new KubernetesClientException(msg, HttpURLConnection.HTTP_NOT_FOUND, new StatusBuilder().withCode(HttpURLConnection.HTTP_NOT_FOUND).withMessage(msg).build());
// }
} catch (InterruptedException | ExecutionException | IOException e) {
throw KubernetesClientException.launderThrowable(forOperationType("get"), e);
}
Expand Down Expand Up @@ -577,9 +579,9 @@ public String getLabelQueryParam() {
}
Map.Entry<String, String> entry = iter.next();
if (entry.getValue() != null) {
sb.append(entry.getKey()).append("=").append(entry.getValue());
sb.append(entry.getKey()).append("=").append(entry.getValue());
} else {
sb.append(entry.getKey());
sb.append(entry.getKey());
}
}
}
Expand All @@ -598,7 +600,7 @@ public String getLabelQueryParam() {
sb.append(entry.getKey()).append("!=").append(entry.getValue()[i]);
}
} else {
sb.append('!').append(entry.getKey());
sb.append('!').append(entry.getKey());
}
}
}
Expand Down Expand Up @@ -709,6 +711,9 @@ public Boolean delete(List<T> items) {
boolean deleted = true;
if (items != null) {
for (T item : items) {
if (item == null) {
continue;
}
updateApiVersionResource(item);

try {
Expand All @@ -717,7 +722,7 @@ public Boolean delete(List<T> items) {
if (item instanceof HasMetadata
&& ((HasMetadata) item).getMetadata() != null
&& ((HasMetadata) item).getMetadata().getName() != null
&& !((HasMetadata) item).getMetadata().getName().isEmpty()) {
&& !((HasMetadata) item).getMetadata().getName().isEmpty()) {
op = (R) inNamespace(checkNamespace(item)).withName(((HasMetadata) item).getMetadata().getName());
} else {
op = (R) withItem(item);
Expand All @@ -744,7 +749,7 @@ public T updateStatus(T item) {
}
}

public BaseOperation<T,L,D,R> withItem(T item) {
public BaseOperation<T, L, D, R> withItem(T item) {
return newInstance(context.withItem(item));
}

Expand Down Expand Up @@ -805,15 +810,15 @@ public Watch watch(ListOptions options, final Watcher<T> watcher) {
} catch (KubernetesClientException ke) {

if (ke.getCode() != 200) {
if(watch != null){
if (watch != null) {
//release the watch
watch.close();
}

throw ke;
}

if(watch != null){
if (watch != null) {
//release the watch after disabling the watcher (to avoid premature call to onClose)
watcherToggle.disable();
watch.close();
Expand Down Expand Up @@ -1010,14 +1015,12 @@ public OperationInfo forOperationType(String type) {
}

@Override
public FilterWatchListDeletable<T, L, Boolean, Watch, Watcher<T>> withGracePeriod(long gracePeriodSeconds)
{
public FilterWatchListDeletable<T, L, Boolean, Watch, Watcher<T>> withGracePeriod(long gracePeriodSeconds) {
return newInstance(context.withGracePeriodSeconds(gracePeriodSeconds));
}

@Override
public EditReplacePatchDeletable<T, T, D, Boolean> withPropagationPolicy(DeletionPropagation propagationPolicy)
{
public EditReplacePatchDeletable<T, T, D, Boolean> withPropagationPolicy(DeletionPropagation propagationPolicy) {
return newInstance(context.withPropagationPolicy(propagationPolicy));
}

Expand Down Expand Up @@ -1100,7 +1103,7 @@ public boolean isApiGroup() {
@Override
public Boolean isReady() {
T i = get();
return i instanceof HasMetadata && Readiness.isReady((HasMetadata)i);
return i instanceof HasMetadata && Readiness.isReady((HasMetadata) i);
}

@Override
Expand All @@ -1127,8 +1130,8 @@ private T waitUntilConditionWithRetries(Predicate<T> condition, long timeoutNano
WaitForConditionWatcher<T> watcher = new WaitForConditionWatcher<>(condition);
Watch watch = item == null
? watch(new ListOptionsBuilder()
.withResourceVersion(null)
.build(), watcher)
.withResourceVersion(null)
.build(), watcher)
: watch(item.getMetadata().getResourceVersion(), watcher);

try {
Expand Down

0 comments on commit 71772d3

Please sign in to comment.