From 19c5ce6fc7b642b36a79310940381577d7c88479 Mon Sep 17 00:00:00 2001 From: Rohan Kumar Date: Mon, 15 Feb 2021 17:32:17 +0530 Subject: [PATCH] Fix #2745: Filtering Operations can't configure PropagationPolicy --- CHANGELOG.md | 1 + .../client/dsl/WatchListDeletable.java | 2 + .../client/utils/PodOperationUtilTest.java | 5 ++ .../client/mock/PropagationPolicyTest.java | 78 +++++++++++++++++-- 4 files changed, 80 insertions(+), 6 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 9520eb1176a..53a03f065aa 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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 diff --git a/kubernetes-client/src/main/java/io/fabric8/kubernetes/client/dsl/WatchListDeletable.java b/kubernetes-client/src/main/java/io/fabric8/kubernetes/client/dsl/WatchListDeletable.java index d4ac884d2fb..4116c7a0fa9 100644 --- a/kubernetes-client/src/main/java/io/fabric8/kubernetes/client/dsl/WatchListDeletable.java +++ b/kubernetes-client/src/main/java/io/fabric8/kubernetes/client/dsl/WatchListDeletable.java @@ -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 extends VersionWatchAndWaitable, Listable, Deletable, GracePeriodConfigurable, + PropagationPolicyConfigurable>, StatusUpdatable { } diff --git a/kubernetes-client/src/test/java/io/fabric8/kubernetes/client/utils/PodOperationUtilTest.java b/kubernetes-client/src/test/java/io/fabric8/kubernetes/client/utils/PodOperationUtilTest.java index 602129a741c..ade0613b3fe 100644 --- a/kubernetes-client/src/test/java/io/fabric8/kubernetes/client/utils/PodOperationUtilTest.java +++ b/kubernetes-client/src/test/java/io/fabric8/kubernetes/client/utils/PodOperationUtilTest.java @@ -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; @@ -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; @@ -137,6 +139,9 @@ private PodList getMockPodList(String controllerUid) { private FilterWatchListDeletable getMockPodFilterOperation(String controllerUid) { return new FilterWatchListDeletable() { + @Override + public EditReplacePatchDeletable withPropagationPolicy(DeletionPropagation propagationPolicy) { return null; } + @Override public Deletable withGracePeriod(long gracePeriodSeconds) { return null; } diff --git a/kubernetes-tests/src/test/java/io/fabric8/kubernetes/client/mock/PropagationPolicyTest.java b/kubernetes-tests/src/test/java/io/fabric8/kubernetes/client/mock/PropagationPolicyTest.java index 24f49346aa9..1437b4b6705 100644 --- a/kubernetes-tests/src/test/java/io/fabric8/kubernetes/client/mock/PropagationPolicyTest.java +++ b/kubernetes-tests/src/test/java/io/fabric8/kubernetes/client/mock/PropagationPolicyTest.java @@ -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; @@ -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; @@ -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 @@ -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()); }