Skip to content

Commit

Permalink
Fix #2745: Filtering Operations can't configure PropagationPolicy
Browse files Browse the repository at this point in the history
  • Loading branch information
rohanKanojia authored and manusa committed Feb 16, 2021
1 parent 8ec884f commit 19c5ce6
Show file tree
Hide file tree
Showing 4 changed files with 80 additions and 6 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

#### Bugs
* Fix #2748: Pass custom headers in kubernetes-client to watch api by modify WatchConnectionManager
* Fix #2745: Filtering Operations can't configure PropagationPolicy

#### Improvements
* Fix #2717: Remove edit() methods from RawCustomResourceOperationsImpl taking InputStream arguments
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,11 @@
package io.fabric8.kubernetes.client.dsl;

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

public interface WatchListDeletable<T, L> extends VersionWatchAndWaitable<T>, Listable<L>, Deletable,
GracePeriodConfigurable<Deletable>,
PropagationPolicyConfigurable<EditReplacePatchDeletable<T>>,
StatusUpdatable<T>
{
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
*/
package io.fabric8.kubernetes.client.utils;

import io.fabric8.kubernetes.api.model.DeletionPropagation;
import io.fabric8.kubernetes.api.model.LabelSelector;
import io.fabric8.kubernetes.api.model.ListOptions;
import io.fabric8.kubernetes.api.model.ObjectReference;
Expand All @@ -25,6 +26,7 @@
import io.fabric8.kubernetes.client.Watch;
import io.fabric8.kubernetes.client.Watcher;
import io.fabric8.kubernetes.client.dsl.Deletable;
import io.fabric8.kubernetes.client.dsl.EditReplacePatchDeletable;
import io.fabric8.kubernetes.client.dsl.FilterWatchListDeletable;
import io.fabric8.kubernetes.client.dsl.Gettable;
import io.fabric8.kubernetes.client.dsl.PodResource;
Expand Down Expand Up @@ -137,6 +139,9 @@ private PodList getMockPodList(String controllerUid) {

private FilterWatchListDeletable<Pod, PodList> getMockPodFilterOperation(String controllerUid) {
return new FilterWatchListDeletable<Pod, PodList>() {
@Override
public EditReplacePatchDeletable<Pod> withPropagationPolicy(DeletionPropagation propagationPolicy) { return null; }

@Override
public Deletable withGracePeriod(long gracePeriodSeconds) { return null; }

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,6 @@
*/
package io.fabric8.kubernetes.client.mock;

import static org.junit.Assert.assertTrue;
import static org.junit.jupiter.api.Assertions.assertEquals;

import io.fabric8.kubernetes.api.model.ConfigMapBuilder;
import io.fabric8.kubernetes.api.model.DeletionPropagation;
import io.fabric8.kubernetes.api.model.KubernetesListBuilder;
Expand All @@ -26,7 +23,9 @@
import io.fabric8.kubernetes.api.model.PodBuilder;
import io.fabric8.kubernetes.api.model.ReplicationControllerBuilder;
import io.fabric8.kubernetes.api.model.SecretBuilder;
import io.fabric8.kubernetes.api.model.Service;
import io.fabric8.kubernetes.api.model.ServiceBuilder;
import io.fabric8.kubernetes.api.model.ServiceListBuilder;
import io.fabric8.kubernetes.api.model.apps.DeploymentBuilder;
import io.fabric8.kubernetes.api.model.apps.StatefulSetBuilder;
import io.fabric8.kubernetes.api.model.batch.JobBuilder;
Expand All @@ -36,15 +35,21 @@
import io.fabric8.kubernetes.client.dsl.base.CustomResourceDefinitionContext;
import io.fabric8.kubernetes.client.mock.crd.PodSet;
import io.fabric8.kubernetes.client.server.mock.KubernetesServer;
import java.io.IOException;
import java.net.HttpURLConnection;
import java.util.Map;
import io.fabric8.kubernetes.client.utils.Utils;
import okhttp3.mockwebserver.RecordedRequest;
import org.junit.Rule;
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.migrationsupport.rules.EnableRuleMigrationSupport;

import java.io.IOException;
import java.net.HttpURLConnection;
import java.util.Collections;
import java.util.Map;

import static org.junit.Assert.assertTrue;
import static org.junit.jupiter.api.Assertions.assertEquals;

@EnableRuleMigrationSupport
class PropagationPolicyTest {
@Rule
Expand Down Expand Up @@ -415,6 +420,67 @@ void testDeleteCustomResource() throws InterruptedException {
assertDeleteOptionsInLastRecordedRequest(DeletionPropagation.BACKGROUND.toString(), server.getLastRequest());
}

@Test
void testFilterWithLabelDeletion() throws InterruptedException {
// Given
KubernetesClient kubernetesClient = setMockExpectationsForFilterDeletionAndGetClient("labelSelector=myLabel");

// When
Boolean isDeleted = kubernetesClient.services().inNamespace("myNameSpace").withLabel("myLabel").withPropagationPolicy(DeletionPropagation.BACKGROUND).delete();

// Then
assertTrue(isDeleted);
RecordedRequest recordedRequest = server.getLastRequest();
assertEquals("DELETE", recordedRequest.getMethod());
assertDeleteOptionsInLastRecordedRequest(DeletionPropagation.BACKGROUND.toString(), recordedRequest);
}

@Test
void testFilterWithLabelsDeletion() throws InterruptedException {
// Given
KubernetesClient kubernetesClient = setMockExpectationsForFilterDeletionAndGetClient("labelSelector=" + Utils.toUrlEncoded("foo=bar"));

// When
Boolean isDeleted = kubernetesClient.services().inNamespace("myNameSpace").withLabels(Collections.singletonMap("foo", "bar")).withPropagationPolicy(DeletionPropagation.BACKGROUND).delete();

// Then
assertTrue(isDeleted);
RecordedRequest recordedRequest = server.getLastRequest();
assertEquals("DELETE", recordedRequest.getMethod());
assertDeleteOptionsInLastRecordedRequest(DeletionPropagation.BACKGROUND.toString(), recordedRequest);
}

@Test
void testFilterWithFieldDeletion() throws InterruptedException {
// Given
KubernetesClient kubernetesClient = setMockExpectationsForFilterDeletionAndGetClient("fieldSelector=" + Utils.toUrlEncoded("status.phase=Running"));

// When
Boolean isDeleted = kubernetesClient.services().inNamespace("myNameSpace").withField("status.phase", "Running").withPropagationPolicy(DeletionPropagation.BACKGROUND).delete();

// Then
assertTrue(isDeleted);
RecordedRequest recordedRequest = server.getLastRequest();
assertEquals("DELETE", recordedRequest.getMethod());
assertDeleteOptionsInLastRecordedRequest(DeletionPropagation.BACKGROUND.toString(), recordedRequest);

}

private KubernetesClient setMockExpectationsForFilterDeletionAndGetClient(String filter) {
Service svc1 = new ServiceBuilder().withNewMetadata().withName("svc1").endMetadata().build();
Service svc2 = new ServiceBuilder().withNewMetadata().withName("svc2").endMetadata().build();
server.expect().get().withPath("/api/v1/namespaces/myNameSpace/services?" + filter)
.andReturn(HttpURLConnection.HTTP_OK, new ServiceListBuilder().addToItems(svc1, svc2).build())
.once();
server.expect().delete().withPath("/api/v1/namespaces/myNameSpace/services/svc1")
.andReturn(HttpURLConnection.HTTP_OK, svc1)
.once();
server.expect().delete().withPath("/api/v1/namespaces/myNameSpace/services/svc2")
.andReturn(HttpURLConnection.HTTP_OK, svc2)
.once();
return server.getClient();
}

private void assertDeleteOptionsInLastRecordedRequest(String propagationPolicy, RecordedRequest recordedRequest) {
assertEquals("{\"apiVersion\":\"v1\",\"kind\":\"DeleteOptions\",\"propagationPolicy\":\"" + propagationPolicy + "\"}", recordedRequest.getBody().readUtf8());
}
Expand Down

0 comments on commit 19c5ce6

Please sign in to comment.