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 May 1, 2020
1 parent f94496b commit 15290eb
Show file tree
Hide file tree
Showing 66 changed files with 846 additions and 144 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@
#### Improvements
* Fix #2174: Change log level to warn for multiple `kubeconfig` warning
* Fix #2088: Support networking.k8s.io/v1beta1 alongside extensions/v1beta1
* Fix #2107: Set PropagationPolicy to Background by default
(_Note: Deletion option `cascading(..)` has been marked as deprecated_)

#### Dependency Upgrade
* Updated Kubernetes Model to v1.18.0
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
import io.fabric8.kubernetes.client.Watch;
import io.fabric8.kubernetes.client.Watcher;
import io.fabric8.knative.client.duck.v1alpha1.internal.ResourceOperationsImpl;
import io.fabric8.kubernetes.api.model.DeletionPropagation;

import okhttp3.OkHttpClient;

Expand Down Expand Up @@ -63,12 +64,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, DeletionPropagation 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 @@ -40,6 +40,7 @@ import io.fabric8.knative.client.${group}.${apiVersion}.internal.${model.name}Op
import io.fabric8.kubernetes.client.dsl.base.OperationContext;
import okhttp3.OkHttpClient;

import io.fabric8.kubernetes.api.model.DeletionPropagation;
import ${model.fullyQualifiedName};
import ${model.fullyQualifiedName}Builder;

Expand Down Expand Up @@ -83,12 +84,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, DeletionPropagation 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.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 io.fabric8.kubernetes.api.model.DeletionPropagation;
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(DeletionPropagation.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 @@ -39,6 +39,7 @@ import io.fabric8.servicecatalog.client.internal.${model.name}OperationsImpl;
import io.fabric8.kubernetes.client.dsl.base.OperationContext;
import okhttp3.OkHttpClient;

import io.fabric8.kubernetes.api.model.DeletionPropagation;
import ${model.fullyQualifiedName};
import ${model.fullyQualifiedName}Builder;

Expand Down Expand Up @@ -82,8 +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) {
return new ${model.name}OperationsImpl(client, config).withItem(item).inNamespace(namespace).delete(item);
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();
}

@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 @@ -39,6 +39,7 @@ import io.fabric8.tekton.client.internal.$apiVersion.${model.name}OperationsImpl
import io.fabric8.kubernetes.client.dsl.base.OperationContext;
import okhttp3.OkHttpClient;

import io.fabric8.kubernetes.api.model.DeletionPropagation;
import ${model.fullyQualifiedName};
import ${model.fullyQualifiedName}Builder;

Expand Down Expand Up @@ -82,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, DeletionPropagation 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,94 @@
/**
* 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.kubernetes.api.model.DeletionPropagation;
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(DeletionPropagation.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 @@ -17,6 +17,7 @@

import io.fabric8.kubernetes.api.builder.Visitor;
import io.fabric8.kubernetes.api.model.Binding;
import io.fabric8.kubernetes.api.model.DeletionPropagation;
import io.fabric8.kubernetes.api.model.Doneable;
import io.fabric8.kubernetes.api.model.DoneableBinding;
import io.fabric8.kubernetes.api.model.HasMetadata;
Expand Down Expand Up @@ -150,13 +151,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, DeletionPropagation.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, DeletionPropagation.BACKGROUND, true) {
};
}

Expand All @@ -172,20 +173,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, DeletionPropagation.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, DeletionPropagation.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, DeletionPropagation.BACKGROUND, true) {
};
}

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

import io.fabric8.kubernetes.api.model.DeletionPropagation;

public interface PropagationPolicyConfigurable<T>
{
T withPropagationPolicy(String propagationPolicy);
/**
* Whether and how garbage collection will be performed.
* Either this field or OrphanDependents may be set, but not both.
*
* <p>The default policy is decided by the existing finalizer set in the metadata.finalizers and the resource-specific default policy.
*
* <p>Acceptable values are:
* <br>'Orphan' - orphan the dependents;</li>
* <br>'Background' - allow the garbage collector to delete the dependents in the background;</li>
* <br>'Foreground' - a cascading policy that deletes all dependents in the foreground.</li>
*
* @param propagationPolicy propagation policy in form of string {@link DeletionPropagation}
* @return resource
*/
T withPropagationPolicy(DeletionPropagation propagationPolicy);
}
Loading

0 comments on commit 15290eb

Please sign in to comment.