Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix #2269: Setting a grace period when deleting resource using withPropagationPolicy() #2324

Merged
merged 1 commit into from
Jul 13, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
* Fix #2328: Failure in deserialization while watching events
* Fix KubernetesAttributesExctractor to extract metadata from unregistered custom resources, such when using Raw CustomResource API
* Fix #2296: No adapter available for type:interface io.fabric8.kubernetes.client.dsl.V1APIGroupDSL
* Fix #2269: Setting a grace period when deleting resource using `withPropagationPolicy()`

#### Improvements
* Fix #2233: client.service().getUrl(..) should be able to fetch URL for ClusterIP based services
Expand Down
4 changes: 4 additions & 0 deletions doc/CHEATSHEET.md
Original file line number Diff line number Diff line change
Expand Up @@ -2050,6 +2050,10 @@ Boolean isDeleted = client.apps().deployments().inNamespace("default").withName(
```
Boolean isDeleted = client.apps().deployments().inNamespace("default").withName("nginx-deploy").withPropagationPolicy("Foreground").delete();
```
- Specifying grace period for deletion:
```
Boolean isDeleted = client.apps().deployments().inNamespace("ns1").withName("mydeployment").withPropagationPolicy(DeletionPropagation.FOREGROUND).withGracePeriod(10).delete();
```

### Watch Options
Kubernetes Client provides namely three different ways of using `Watch`:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ public class ${model.name}Handler implements ResourceHandler<${model.name}, ${mo

@Override
public Boolean delete(OkHttpClient client, Config config, String namespace, DeletionPropagation propagationPolicy, ${model.name} item) {
return new ${model.name}OperationsImpl(client, config).withItem(item).inNamespace(namespace).withPropagationPolicy(propagationPolicy).delete();
return new ${model.name}OperationsImpl(client, config).withItem(item).inNamespace(namespace).withName(item.getMetadata().getName()).withPropagationPolicy(propagationPolicy).delete();
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ public class ${model.name}Handler implements ResourceHandler<${model.name}, ${mo

@Override
public Boolean delete(OkHttpClient client, Config config, String namespace, DeletionPropagation propagationPolicy, ${model.name} item) {
return new ${model.name}OperationsImpl(client, config).withItem(item).inNamespace(namespace).withPropagationPolicy(propagationPolicy).delete();
return new ${model.name}OperationsImpl(client, config).withItem(item).inNamespace(namespace).withName(item.getMetadata().getName()).withPropagationPolicy(propagationPolicy).delete();
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ public class ${model.name}Handler implements ResourceHandler<${model.name}, ${mo

@Override
public Boolean delete(OkHttpClient client, Config config, String namespace, DeletionPropagation propagationPolicy, ${model.name} item) {
return new ${model.name}OperationsImpl(client, config).withItem(item).inNamespace(namespace).withPropagationPolicy(propagationPolicy).delete();
return new ${model.name}OperationsImpl(client, config).withItem(item).inNamespace(namespace).withName(item.getMetadata().getName()).withPropagationPolicy(propagationPolicy).delete();
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,5 +17,14 @@

public interface GracePeriodConfigurable<T>
{
/**
* The duration in seconds before the object should be deleted. Value must be non-negative integer.
* The value zero indicates delete immediately. If this value is nil, the default grace period for
* the specified type will be used. Defaults to a per object value if not specified. zero means
* delete immediately.
*
* @param gracePeriodSeconds grace period integer value in seconds
* @return the object for which grace period is configured
*/
T withGracePeriod(long gracePeriodSeconds);
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,11 @@

package io.fabric8.kubernetes.client.dsl;

import io.fabric8.kubernetes.client.PropagationPolicyConfigurable;

public interface CascadingEditReplacePatchDeletable<I,D,T,B> extends
EditReplacePatchDeletable<I,D,T,B>,
Cascading<EditReplacePatchDeletable<I,D,T,B>>,
PropagationPolicyConfigurable<EditReplacePatchDeletable<I,D,T,B>>,
Lockable<Replaceable<I, I>> {
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,9 @@
package io.fabric8.kubernetes.client.dsl;

import io.fabric8.kubernetes.client.GracePeriodConfigurable;
import io.fabric8.kubernetes.client.PropagationPolicyConfigurable;

public interface EditReplacePatchDeletable<I, T, D, B> extends EditReplacePatchable<I, T, D>, Deletable<B>,
GracePeriodConfigurable<Deletable<B>>,
PropagationPolicyConfigurable<Deletable<B>>
GracePeriodConfigurable<Deletable<B>>

{
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,9 @@
package io.fabric8.kubernetes.client.dsl;

import io.fabric8.kubernetes.client.GracePeriodConfigurable;
import io.fabric8.kubernetes.client.PropagationPolicyConfigurable;

public interface WatchListDeletable<T, L, B, H, W> extends VersionWatchable<H, W>, Listable<L>, Deletable<B>,
GracePeriodConfigurable<Deletable<B>>,
StatusUpdatable<T>,
PropagationPolicyConfigurable<Deletable<B>>
StatusUpdatable<T>
{
}
Original file line number Diff line number Diff line change
Expand Up @@ -998,7 +998,7 @@ public FilterWatchListDeletable<T, L, Boolean, Watch, Watcher<T>> withGracePerio
}

@Override
public FilterWatchListDeletable<T, L, Boolean, Watch, Watcher<T>> withPropagationPolicy(DeletionPropagation propagationPolicy)
public EditReplacePatchDeletable<T, T, D, Boolean> withPropagationPolicy(DeletionPropagation propagationPolicy)
{
return newInstance(context.withPropagationPolicy(propagationPolicy));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
import static org.hamcrest.CoreMatchers.notNullValue;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.junit.Assert.assertEquals;
import static org.junit.jupiter.api.Assertions.assertNotNull;

import java.net.MalformedURLException;
import java.net.URL;
Expand All @@ -32,6 +33,7 @@
import io.fabric8.kubernetes.api.model.ListOptionsBuilder;
import io.fabric8.kubernetes.api.model.Pod;
import io.fabric8.kubernetes.api.model.PodList;
import io.fabric8.kubernetes.client.dsl.EditReplacePatchDeletable;
import io.fabric8.kubernetes.client.dsl.Resource;
import io.fabric8.kubernetes.client.utils.URLUtils;
import org.junit.jupiter.api.Test;
Expand Down Expand Up @@ -90,14 +92,9 @@ public void testDefaultGracePeriod() {
@Test
public void testChainingGracePeriodAndPropagationPolicy() {
final BaseOperation operation = new BaseOperation(new OperationContext());
FilterWatchListDeletable<?, ?, Boolean, Watch, Watcher<?>> operationWithGracePeriod = operation.withGracePeriod(10L);
FilterWatchListDeletable<?, ?, Boolean, Watch, Watcher<?>> operationWithPropagationPolicy = operation.withPropagationPolicy(DeletionPropagation.FOREGROUND);

Object chainedGracePeriod = operationWithPropagationPolicy.withGracePeriod(10);
Object chainedPropagationPolicy = operationWithGracePeriod.withPropagationPolicy(DeletionPropagation.FOREGROUND);

assertThat(chainedGracePeriod, is(notNullValue()));
assertThat(chainedPropagationPolicy, is(notNullValue()));
EditReplacePatchDeletable<?, ?, ?, Boolean> operationWithPropagationPolicy = operation.withPropagationPolicy(DeletionPropagation.FOREGROUND);
assertThat(operationWithPropagationPolicy, is(notNullValue()));
assertNotNull(operationWithPropagationPolicy.withGracePeriod(10));
}

@Test
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -189,6 +189,23 @@ void testDeleteDeploymentWithExplicitPropagationPolicy() throws InterruptedExcep
assertDeleteOptionsInLastRecordedRequest(DeletionPropagation.FOREGROUND.toString(), server.getLastRequest());
}

@Test
@DisplayName("Should delete a Deployment with explicitly set PropagationPolicy And Grace Period")
void testDeleteDeploymentWithExplicitPropagationPolicyAndGracePeriod() throws InterruptedException {
// Given
server.expect().delete().withPath("/apis/apps/v1/namespaces/ns1/deployments/mydeployment")
.andReturn(HttpURLConnection.HTTP_OK, new DeploymentBuilder().build())
.once();
KubernetesClient client = server.getClient();

// When
Boolean isDeleted = client.apps().deployments().inNamespace("ns1").withName("mydeployment").withPropagationPolicy(DeletionPropagation.FOREGROUND).withGracePeriod(10).delete();

// Then
assertTrue(isDeleted);
assertEquals("{\"apiVersion\":\"v1\",\"kind\":\"DeleteOptions\",\"gracePeriodSeconds\":10,\"propagationPolicy\":\"" + DeletionPropagation.FOREGROUND + "\"}", server.getLastRequest().getBody().readUtf8());
}

@Test
@DisplayName("Should delete a StatefulSet with PropagationPolicy=Background")
void testDeleteStatefulSet() throws InterruptedException {
Expand Down