Skip to content

Commit

Permalink
Fix fabric8io#2107: Set PropagationPolicy to Background by default
Browse files Browse the repository at this point in the history
  • Loading branch information
rohanKanojia committed Apr 29, 2020
1 parent 791fbac commit 9a4aefe
Show file tree
Hide file tree
Showing 51 changed files with 681 additions and 102 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -63,12 +63,8 @@ public ResourceBuilder edit(Resource item) {
}

@Override
public Boolean delete(OkHttpClient client, Config config, String namespace, Boolean cascading, Resource item) {
if(Boolean.TRUE.equals(cascading)) {
return new ResourceOperationsImpl(client, config).withItem(item).cascading(cascading).delete();
} else {
return new ResourceOperationsImpl(client, config).withItem(item).inNamespace(namespace).delete(item);
}
public Boolean delete(OkHttpClient client, Config config, String namespace, String propagationPolicy, Resource item) {
return new ResourceOperationsImpl(client, config).withItem(item).inNamespace(namespace).withPropagationPolicy(propagationPolicy).delete();
}

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

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

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ import java.util.TreeMap;
public class ${model.name}OperationsImpl extends HasMetadataOperation<${model.name}, ${model.name}List, Doneable${model.name}, Resource<${model.name}, Doneable${model.name}>> {

public ${model.name}OperationsImpl(OkHttpClient client, Config config) {
this(new OperationContext().withOkhttpClient(client).withConfig(config));
this(new OperationContext().withOkhttpClient(client).withConfig(config).withPropagationPolicy(DEFAULT_PROPAGATION_POLICY));
}

public ${model.name}OperationsImpl(OperationContext context) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
/**
* 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.knative.test;

import io.fabric8.knative.client.KnativeClient;
import io.fabric8.knative.mock.KnativeServer;
import io.fabric8.knative.serving.v1.Service;
import io.fabric8.knative.serving.v1.ServiceBuilder;
import okhttp3.mockwebserver.RecordedRequest;
import org.junit.Rule;
import org.junit.Test;
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.migrationsupport.rules.EnableRuleMigrationSupport;

import java.net.HttpURLConnection;

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

@EnableRuleMigrationSupport
public class ServiceTest {
@Rule
public KnativeServer server = new KnativeServer();

@Test
@DisplayName("Should get a Knative Service")
public void testGet() {
Service service2 = new ServiceBuilder().withNewMetadata().withName("service2").endMetadata().build();
server.expect().get().withPath("/apis/serving.knative.dev/v1/namespaces/ns2/services/service2")
.andReturn(HttpURLConnection.HTTP_OK, service2)
.once();
KnativeClient client = server.getKnativeClient();

Service service = client.services().inNamespace("ns2").withName("service2").get();
assertNotNull(service);
assertEquals("service2", service.getMetadata().getName());
}

@Test
@DisplayName("Should Create a Knative Service")
public void testCreate() {
Service service = new ServiceBuilder().withNewMetadata().withName("service").endMetadata().build();
server.expect().post().withPath("/apis/serving.knative.dev/v1/namespaces/ns2/services")
.andReturn(HttpURLConnection.HTTP_OK, service)
.once();
KnativeClient client = server.getKnativeClient();
service = client.services().inNamespace("ns2").create(service);
assertNotNull(service);
}

@Test
@DisplayName("Should Delete a Knative Service")
public void testDelete() throws InterruptedException {
server.expect().delete().withPath("/apis/serving.knative.dev/v1/namespaces/ns3/services/service3")
.andReturn(HttpURLConnection.HTTP_OK, new ServiceBuilder().build())
.once();
KnativeClient client = server.getKnativeClient();
Boolean deleted = client.services().inNamespace("ns3").withName("service3").delete();
assertTrue(deleted);

RecordedRequest recordedRequest = server.getMockServer().takeRequest();
assertEquals("{\"apiVersion\":\"v1\",\"kind\":\"DeleteOptions\",\"propagationPolicy\":\"Background\"}", recordedRequest.getBody().readUtf8());
}

@Test
@DisplayName("Should delete with PropagationPolicy=Orphan")
public void testDeleteOrphan() throws InterruptedException {
server.expect().delete().withPath("/apis/serving.knative.dev/v1/namespaces/ns3/services/service3")
.andReturn(HttpURLConnection.HTTP_OK, new ServiceBuilder().build())
.once();
KnativeClient client = server.getKnativeClient();
Boolean deleted = client.services().inNamespace("ns3").withName("service3").withPropagationPolicy("Orphan").delete();
assertTrue(deleted);

RecordedRequest recordedRequest = server.getMockServer().takeRequest();
assertEquals("{\"apiVersion\":\"v1\",\"kind\":\"DeleteOptions\",\"propagationPolicy\":\"Orphan\"}", recordedRequest.getBody().readUtf8());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -82,8 +82,8 @@ public class ${model.name}Handler implements ResourceHandler<${model.name}, ${mo
}

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

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ import java.util.TreeMap;
public class ${model.name}OperationsImpl extends HasMetadataOperation<${model.name}, ${model.name}List, Doneable${model.name}, Resource<${model.name}, Doneable${model.name}>> {

public ${model.name}OperationsImpl(OkHttpClient client, Config config) {
this(new OperationContext().withOkhttpClient(client).withConfig(config));
this(new OperationContext().withOkhttpClient(client).withConfig(config).withPropagationPolicy(DEFAULT_PROPAGATION_POLICY));
}

public ${model.name}OperationsImpl(OperationContext context) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -82,12 +82,8 @@ public class ${model.name}Handler implements ResourceHandler<${model.name}, ${mo
}

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

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ import java.util.TreeMap;
public class ${model.name}OperationsImpl extends HasMetadataOperation<${model.name}, ${model.name}List, Doneable${model.name}, Resource<${model.name}, Doneable${model.name}>> {

public ${model.name}OperationsImpl(OkHttpClient client, Config config) {
this(new OperationContext().withOkhttpClient(client).withConfig(config));
this(new OperationContext().withOkhttpClient(client).withConfig(config).withPropagationPolicy(DEFAULT_PROPAGATION_POLICY));
}

public ${model.name}OperationsImpl(OperationContext context) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
/**
* 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.tekton.pipeline.v1beta1;

import io.fabric8.tekton.client.TektonClient;
import io.fabric8.tekton.mock.TektonServer;
import okhttp3.mockwebserver.RecordedRequest;
import org.junit.Rule;
import org.junit.Test;
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.migrationsupport.rules.EnableRuleMigrationSupport;

import java.net.HttpURLConnection;

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

@EnableRuleMigrationSupport
public class PipelineTest {
@Rule
public TektonServer server = new TektonServer();

@Test
@DisplayName("Should get a pipeline")
public void testGet() {
server.expect().get().withPath("/apis/tekton.dev/v1beta1/namespaces/ns1/pipelines/pipeline")
.andReturn(HttpURLConnection.HTTP_OK, new io.fabric8.tekton.pipeline.v1beta1.PipelineBuilder()
.withNewMetadata()
.withName("pipeline")
.endMetadata()
.build()).once();
TektonClient client = server.getTektonClient();

Pipeline pipeline = client.v1beta1().pipelines().inNamespace("ns1").withName("pipeline").get();
assertNotNull(pipeline);
}

@Test
@DisplayName("Should create a pipeline")
public void testCreate() {
Pipeline pipeline = new io.fabric8.tekton.pipeline.v1beta1.PipelineBuilder().withNewMetadata().withName("pipeline").endMetadata().build();
server.expect().post().withPath("/apis/tekton.dev/v1beta1/namespaces/ns1/pipelines")
.andReturn(HttpURLConnection.HTTP_OK, pipeline).once();
TektonClient client = server.getTektonClient();

pipeline = client.v1beta1().pipelines().inNamespace("ns1").create(pipeline);
assertNotNull(pipeline);
}

@Test
@DisplayName("Should delete a pipeline")
public void testDelete() throws InterruptedException {
server.expect().delete().withPath("/apis/tekton.dev/v1beta1/namespaces/ns1/pipelines/pipeline")
.andReturn(HttpURLConnection.HTTP_OK, new io.fabric8.tekton.pipeline.v1beta1.PipelineBuilder().build())
.once();
TektonClient client = server.getTektonClient();

Boolean isDeleted = client.v1beta1().pipelines().inNamespace("ns1").withName("pipeline").delete();
assertTrue(isDeleted);

RecordedRequest recordedRequest = server.getMockServer().takeRequest();
assertEquals("{\"apiVersion\":\"v1\",\"kind\":\"DeleteOptions\",\"propagationPolicy\":\"Background\"}", recordedRequest.getBody().readUtf8());
}

@Test
@DisplayName("Should delete pipeline with some explicit propagationpolicy")
public void testDeleteOrphan() throws InterruptedException {
server.expect().delete().withPath("/apis/tekton.dev/v1beta1/namespaces/ns1/pipelines/pipeline")
.andReturn(HttpURLConnection.HTTP_OK, new io.fabric8.tekton.pipeline.v1beta1.PipelineBuilder().build())
.once();
TektonClient client = server.getTektonClient();

Boolean isDeleted = client.v1beta1().pipelines().inNamespace("ns1").withName("pipeline").withPropagationPolicy("Orphan").delete();
assertTrue(isDeleted);

RecordedRequest recordedRequest = server.getMockServer().takeRequest();
assertEquals("{\"apiVersion\":\"v1\",\"kind\":\"DeleteOptions\",\"propagationPolicy\":\"Orphan\"}", recordedRequest.getBody().readUtf8());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -150,13 +150,13 @@ public MixedOperation<ComponentStatus, ComponentStatusList, DoneableComponentSta

@Override
public ParameterNamespaceListVisitFromServerGetDeleteRecreateWaitApplicable<HasMetadata, Boolean> load(InputStream is) {
return new NamespaceVisitFromServerGetWatchDeleteRecreateWaitApplicableListImpl(httpClient, getConfiguration(), getNamespace(), null, false, false, new ArrayList<>(), is, null, true) {
return new NamespaceVisitFromServerGetWatchDeleteRecreateWaitApplicableListImpl(httpClient, getConfiguration(), getNamespace(), null, false, false, new ArrayList<>(), is, null, true, "Background") {
};
}

@Override
public NamespaceListVisitFromServerGetDeleteRecreateWaitApplicable<HasMetadata, Boolean> resourceList(KubernetesResourceList item) {
return new NamespaceVisitFromServerGetWatchDeleteRecreateWaitApplicableListImpl(httpClient, getConfiguration(), getNamespace(), null, false, false, new ArrayList<Visitor>(), item, null, null, -1, null, true) {
return new NamespaceVisitFromServerGetWatchDeleteRecreateWaitApplicableListImpl(httpClient, getConfiguration(), getNamespace(), null, false, false, new ArrayList<Visitor>(), item, null, null, -1, "Background", true) {
};
}

Expand All @@ -172,20 +172,20 @@ public NamespaceListVisitFromServerGetDeleteRecreateWaitApplicable<HasMetadata,

@Override
public ParameterNamespaceListVisitFromServerGetDeleteRecreateWaitApplicable<HasMetadata, Boolean> resourceList(String s) {
return new NamespaceVisitFromServerGetWatchDeleteRecreateWaitApplicableListImpl(httpClient, getConfiguration(), getNamespace(), null, false, false, new ArrayList<Visitor>(), s, null, null, -1, null, true) {
return new NamespaceVisitFromServerGetWatchDeleteRecreateWaitApplicableListImpl(httpClient, getConfiguration(), getNamespace(), null, false, false, new ArrayList<Visitor>(), s, null, null, -1, "Background", true) {
};
}


@Override
public NamespaceVisitFromServerGetWatchDeleteRecreateWaitApplicable<HasMetadata, Boolean> resource(HasMetadata item) {
return new NamespaceVisitFromServerGetWatchDeleteRecreateWaitApplicableImpl(httpClient, getConfiguration(), getNamespace(), null, false, false, new ArrayList<Visitor>(), item, -1, null, true) {
return new NamespaceVisitFromServerGetWatchDeleteRecreateWaitApplicableImpl(httpClient, getConfiguration(), getNamespace(), null, false, false, new ArrayList<Visitor>(), item, -1, "Background", true) {
};
}

@Override
public NamespaceVisitFromServerGetWatchDeleteRecreateWaitApplicable<HasMetadata, Boolean> resource(String s) {
return new NamespaceVisitFromServerGetWatchDeleteRecreateWaitApplicableImpl(httpClient, getConfiguration(), getNamespace(), null, false, false, new ArrayList<Visitor>(), s, -1, null, true) {
return new NamespaceVisitFromServerGetWatchDeleteRecreateWaitApplicableImpl(httpClient, getConfiguration(), getNamespace(), null, false, false, new ArrayList<Visitor>(), s, -1, "Background", true) {
};
}

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

public interface PropagationPolicyConfigurable<T>
{
/**
* Whether and how garbage collection will be performed.
* Either this field or OrphanDependents may be set, but not both.
* The default policy is decided by the existing finalizer set in the metadata.finalizers and the resource-specific default policy.
* Acceptable values are:
* 'Orphan' - orphan the dependents;
* 'Background' - allow the garbage collector to delete the dependents in the background;
* 'Foreground' - a cascading policy that deletes all dependents in the foreground.
*
* @param propagationPolicy propagation policy in form of string
* @return resource
*/
T withPropagationPolicy(String propagationPolicy);
}
Original file line number Diff line number Diff line change
Expand Up @@ -117,11 +117,11 @@ public boolean equals(Object obj) {
* @param client An instance of the http client.
* @param config The client config.
* @param namespace The target namespace.
* @param cascading Whether deletion needs to be cascading or not
* @param propagationPolicy Whether and how garbage collection will be performed.
* @param item The resource to delete.
* @return The true if the resource was successfully deleted.
*/
Boolean delete(OkHttpClient client, Config config, String namespace, Boolean cascading, T item);
Boolean delete(OkHttpClient client, Config config, String namespace, String propagationPolicy, T item);


/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,5 +17,13 @@
package io.fabric8.kubernetes.client.dsl;

public interface Cascading<T> {
/**
* deletes dependent resources. Sets `orphanDependents` field to `false` when set `true`
*
* @deprecated Please Use {@link io.fabric8.kubernetes.client.PropagationPolicyConfigurable} instead. This field has been deprecated since 1.7.
* @param enabled whether dependents should be orphaned or not.
* @return resource
*/
@Deprecated
T cascading(boolean enabled);
}
Loading

0 comments on commit 9a4aefe

Please sign in to comment.