Skip to content

Commit

Permalink
fix fabric8io#3364: removing boilerplate methods from the resourcehan…
Browse files Browse the repository at this point in the history
…dler

this attempts to make all usage of resource operations regular.  it
requires splitting the base operationcontext from the derived bits so
that newInstance works with just the base
  • Loading branch information
shawkins committed Aug 5, 2021
1 parent bca5b11 commit 21462ba
Show file tree
Hide file tree
Showing 40 changed files with 615 additions and 1,880 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@
import io.fabric8.kubernetes.api.model.ComponentStatusList;
import io.fabric8.kubernetes.api.model.ConfigMap;
import io.fabric8.kubernetes.api.model.ConfigMapList;
import io.fabric8.kubernetes.api.model.DeletionPropagation;
import io.fabric8.kubernetes.api.model.Endpoints;
import io.fabric8.kubernetes.api.model.EndpointsList;
import io.fabric8.kubernetes.api.model.HasMetadata;
Expand Down Expand Up @@ -90,7 +89,6 @@
import io.fabric8.kubernetes.client.dsl.ServiceResource;
import io.fabric8.kubernetes.client.dsl.StorageAPIGroupDSL;
import io.fabric8.kubernetes.client.dsl.V1APIGroupDSL;
import io.fabric8.kubernetes.client.dsl.Waitable;
import io.fabric8.kubernetes.client.dsl.base.CustomResourceDefinitionContext;
import io.fabric8.kubernetes.client.dsl.base.ResourceDefinitionContext;
import io.fabric8.kubernetes.client.dsl.internal.ClusterOperationsImpl;
Expand Down Expand Up @@ -172,17 +170,19 @@ public NonNamespaceOperation<ComponentStatus, ComponentStatusList, Resource<Comp
*/
@Override
public ParameterNamespaceListVisitFromServerGetDeleteRecreateWaitApplicable<HasMetadata> load(InputStream is) {
return new NamespaceVisitFromServerGetWatchDeleteRecreateWaitApplicableListImpl(httpClient, getConfiguration(), getNamespace(), null, false, false, new ArrayList<>(), is, null, true, DeletionPropagation.BACKGROUND, false) {
};
return resourceListFor(is);
}

/**
* {@inheritDoc}
*/
@Override
public NamespaceListVisitFromServerGetDeleteRecreateWaitApplicable<HasMetadata> resourceList(KubernetesResourceList item) {
return new NamespaceVisitFromServerGetWatchDeleteRecreateWaitApplicableListImpl(httpClient, getConfiguration(), getNamespace(), null, false, false, new ArrayList<>(), item, null, DeletionPropagation.BACKGROUND, true, false) {
};
return resourceListFor(item);
}

public NamespaceVisitFromServerGetWatchDeleteRecreateWaitApplicableListImpl resourceListFor(Object item) {
return new NamespaceVisitFromServerGetWatchDeleteRecreateWaitApplicableListImpl(httpClient, getConfiguration(), item);
}

/**
Expand All @@ -206,16 +206,15 @@ public NamespaceListVisitFromServerGetDeleteRecreateWaitApplicable<HasMetadata>
*/
@Override
public ParameterNamespaceListVisitFromServerGetDeleteRecreateWaitApplicable<HasMetadata> resourceList(String s) {
return new NamespaceVisitFromServerGetWatchDeleteRecreateWaitApplicableListImpl(httpClient, getConfiguration(), getNamespace(), null, false, false, new ArrayList<>(), s, null, DeletionPropagation.BACKGROUND, true, false) {
};
return resourceListFor(s);
}

/**
* {@inheritDoc}
*/
@Override
public NamespaceVisitFromServerGetWatchDeleteRecreateWaitApplicable<HasMetadata> resource(HasMetadata item) {
return new NamespaceVisitFromServerGetWatchDeleteRecreateWaitApplicableImpl(httpClient, getConfiguration(), getNamespace(), null, false, false, new ArrayList<>(), item, -1, DeletionPropagation.BACKGROUND, true, Waitable.DEFAULT_INITIAL_BACKOFF_MILLIS, Waitable.DEFAULT_BACKOFF_MULTIPLIER, false);
return new NamespaceVisitFromServerGetWatchDeleteRecreateWaitApplicableImpl(httpClient, getConfiguration(), item);
}

/**
Expand Down Expand Up @@ -340,7 +339,7 @@ public NonNamespaceOperation<APIService, APIServiceList, Resource<APIService>> a
*/
@Override
public KubernetesListMixedOperation lists() {
return new KubernetesListOperationsImpl(httpClient, getConfiguration(), getNamespace());
return new KubernetesListOperationsImpl(httpClient, getConfiguration());
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,6 @@

package io.fabric8.kubernetes.client;

import java.util.concurrent.TimeUnit;
import java.util.function.Predicate;

import io.fabric8.kubernetes.api.model.DeletionPropagation;
import io.fabric8.kubernetes.api.model.HasMetadata;
import io.fabric8.kubernetes.api.model.KubernetesResourceList;
import io.fabric8.kubernetes.client.dsl.Resource;
Expand All @@ -29,106 +25,13 @@

public interface ResourceHandler<T extends HasMetadata, V extends VisitableBuilder<T, V>> {

/**
* Create the specified resource
* @param client An instance of the http client.
* @param config The client config.
* @param namespace The target namespace.
* @param item The resource to create.
* @param dryRun Enable dry run
* @return The created resource.
*/
default T create(OkHttpClient client, Config config, String namespace, T item, boolean dryRun) {
return resource(client, config, namespace, item).dryRun(dryRun).create(item);
}

/**
* Replace the specified resource
* @param client An instance of the http client.
* @param config The client config.
* @param namespace The target namespace.
* @param item The resource to replace.
* @param dryRun Enable dry run
* @return The replaced resource.
*/
default T replace(OkHttpClient client, Config config, String namespace, T item, boolean dryRun) {
return resource(client, config, namespace, item).dryRun(dryRun).replace(item);
}

/**
* Reload the specified resource (if exists).
* @param client An instance of the http client.
* @param config The client config.
* @param namespace The target namespace.
* @param item The resource to reload.
* @return The reloaded resource.
*/
default T reload(OkHttpClient client, Config config, String namespace, T item) {
return resource(client, config, namespace, item).fromServer().get();
}

/**
* Edit the specified resource.
* @param item The resource to edit.
* @return The resource editor.
*/
V edit(T item);

/**
* Delete the specified resource (if exists).
* @param client An instance of the http client.
* @param config The client config.
* @param namespace The target namespace.
* @param propagationPolicy Whether and how garbage collection will be performed.
* @param gracePeriodSeconds The duration in seconds before the object should be deleted.
* @param item The resource to delete.
* @param dryRun enable dry run
* @return The true if the resource was successfully deleted.
*/
default Boolean delete(OkHttpClient client, Config config, String namespace, DeletionPropagation propagationPolicy, long gracePeriodSeconds, T item, boolean dryRun) {
return resource(client, config, namespace, item).dryRun(dryRun).withPropagationPolicy(propagationPolicy).withGracePeriod(gracePeriodSeconds).delete();
}

/**
* Waits until the specified resource is Ready.
* For resources that 'readiness' is not applicable the method is equivalent to get.
* @param client An instance of the http client.
* @param config The client config.
* @param namespace The target namespace.
* @param item The resource to wait.
* @param amount The amount of time to wait
* @param timeUnit The wait {@link TimeUnit}.
* @return The true if the resource was successfully deleted.
*/
default T waitUntilReady(OkHttpClient client, Config config, String namespace, T item, long amount, TimeUnit timeUnit) {
return resource(client, config, namespace, item).waitUntilReady(amount, timeUnit);
}

/**
* Waits until the specified condition is true.
* @param client An instance of the http client.
* @param config The client config.
* @param namespace The target namespace.
* @param item The resource to wait.
* @param amount The amount of time to wait
* @param timeUnit The wait {@link TimeUnit}.
* @param condition The condition to wait for
* @return The true if the resource was successfully deleted.
*/
default T waitUntilCondition(OkHttpClient client, Config config, String namespace, T item, Predicate<T> condition, long amount, TimeUnit timeUnit) {
return resource(client, config, namespace, item).waitUntilCondition(condition, amount, timeUnit);
}

/**
* Gets the Resource for the given item
* @param client An instance of the http client.
* @param config The client config.
* @param namespace The target namespace.
* @param item The resource to wait.
* @return The true if the resource was successfully deleted.
*/
Resource<T> resource(OkHttpClient client, Config config, String namespace, T item);

/**
* Create the operation support for the current resource
* @param client An instance of the http client.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -59,11 +59,6 @@ public V edit(T item) {
}
}

@Override
public Resource<T> resource(OkHttpClient client, Config config, String namespace, T item) {
return operation(client, config, defaultListClass).withItem(item).inNamespace(namespace).withName(item.getMetadata().getName());
}

@Override
public <L extends KubernetesResourceList<T>> HasMetadataOperation<T, L, Resource<T>> operation(OkHttpClient client, Config config, Class<L> listType) {
if (operationConstructor != null) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ final class FilterNestedImpl<T extends HasMetadata, L extends KubernetesResource
this.baseOperation = baseOperation;
// create a context copy
context = this.baseOperation.context;
context = this.context.withConfig(context.config);
context = this.context.clone();
// create map copies - this could be done lazily if needed
context.labels = new LinkedHashMap<>(this.baseOperation.context.getLabels());
context.labelsNot = new LinkedHashMap<>(this.baseOperation.context.getLabelsNot());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -188,6 +188,15 @@ public boolean isNamespaceFromGlobalConfig() {
public boolean getDryRun() {
return dryRun;
}

@Override
public OperationContext clone() {
return new OperationContext(client, this.config, this.plural, this.namespace, this.name, this.apiGroupName,
this.apiGroupVersion, this.cascading, this.item, this.labels, this.labelsNot, this.labelsIn, this.labelsNotIn,
this.fields, this.fieldsNot, this.resourceVersion, this.reloadingFromServer, this.gracePeriodSeconds,
this.propagationPolicy, this.watchRetryInitialBackoffMillis, this.watchRetryBackoffMultiplier,
this.namespaceFromGlobalConfig, this.dryRun);
}

public OperationContext withOkhttpClient(OkHttpClient client) {
if (this.client == client) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,23 +15,22 @@
*/
package io.fabric8.kubernetes.client.dsl.internal;

import io.fabric8.kubernetes.api.model.DeletionPropagation;
import io.fabric8.kubernetes.client.dsl.KubernetesListMixedOperation;
import io.fabric8.kubernetes.client.dsl.KubernetesListOperation;
import io.fabric8.kubernetes.client.dsl.Loadable;
import okhttp3.OkHttpClient;
import io.fabric8.kubernetes.api.builder.VisitableBuilder;
import io.fabric8.kubernetes.api.model.HasMetadata;
import io.fabric8.kubernetes.api.model.KubernetesList;
import io.fabric8.kubernetes.api.model.KubernetesListBuilder;
import io.fabric8.kubernetes.client.Config;
import io.fabric8.kubernetes.client.Handlers;
import io.fabric8.kubernetes.client.KubernetesClientException;
import io.fabric8.kubernetes.client.ResourceHandler;
import io.fabric8.kubernetes.client.dsl.KubernetesListNonNamespaceOperation;
import io.fabric8.kubernetes.client.dsl.Createable;
import io.fabric8.kubernetes.client.dsl.Gettable;
import io.fabric8.kubernetes.client.dsl.RecreateFromServerGettable;
import io.fabric8.kubernetes.client.dsl.base.OperationSupport;
import io.fabric8.kubernetes.client.dsl.Resource;
import io.fabric8.kubernetes.client.dsl.base.OperationContext;
import io.fabric8.kubernetes.client.utils.Serialization;

import java.io.File;
import java.io.FileInputStream;
Expand All @@ -41,35 +40,30 @@
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

import static io.fabric8.kubernetes.client.dsl.base.HasMetadataOperation.DEFAULT_GRACE_PERIOD_IN_SECONDS;
import java.util.stream.Collectors;

public class KubernetesListOperationsImpl
extends OperationSupport
implements KubernetesListOperation,
KubernetesListMixedOperation,
Loadable<RecreateFromServerGettable<KubernetesList>>,
RecreateFromServerGettable<KubernetesList> {

private final KubernetesList item;
private final Boolean fromServer;
private final Boolean deletingExisting;
private final NamespaceVisitOperationContext namespaceVisitOperationContext;
private final OperationContext context;

public KubernetesListOperationsImpl(OkHttpClient client, Config config, String namespace) {
this(client, config, namespace, null, null, DEFAULT_GRACE_PERIOD_IN_SECONDS, false, false, null, null, false);
public KubernetesListOperationsImpl(OkHttpClient client, Config config) {
this.context = HasMetadataOperationsImpl.defaultContext(new OperationContext(), client, config);
this.namespaceVisitOperationContext = new NamespaceVisitOperationContext();
}

public KubernetesListOperationsImpl(OkHttpClient client, Config config, String namespace, String name, DeletionPropagation propagationPolicy, long gracePeriodSeconds, Boolean fromServer, Boolean deletingExisting, KubernetesList item, String resourceVersion, boolean dryRun) {
super(client, config, namespace, propagationPolicy, gracePeriodSeconds);
this.fromServer = fromServer;
this.deletingExisting = deletingExisting;
this.item = item;
this.dryRun = dryRun;

public KubernetesListOperationsImpl(OperationContext context, NamespaceVisitOperationContext namespaceVisitOperationContext) {
this.context = context;
this.namespaceVisitOperationContext = namespaceVisitOperationContext;
}

@Override
public KubernetesListNonNamespaceOperation inNamespace(String namespace) {
return new KubernetesListOperationsImpl(client, config, namespace);
return new KubernetesListOperationsImpl(context.withNamespace(namespace), namespaceVisitOperationContext.withExplicitNamespace(namespace));
}

@Override
Expand Down Expand Up @@ -102,7 +96,7 @@ public RecreateFromServerGettable<KubernetesList> load(URL url) {
}

@Override
public RecreateFromServerGettable<KubernetesList> load(File file) {
public RecreateFromServerGettable<KubernetesList> load(File file) {
try (FileInputStream fis = new FileInputStream(file)) {
return load(fis);
} catch (IOException e) {
Expand All @@ -111,26 +105,26 @@ public RecreateFromServerGettable<KubernetesList> load(File file) {
}

@Override
public RecreateFromServerGettable<KubernetesList> load(String path) {
public RecreateFromServerGettable<KubernetesList> load(String path) {
return load(new File(path));
}

@Override
public RecreateFromServerGettable<KubernetesList> load(InputStream is) {
return new KubernetesListOperationsImpl(client, config, namespace, null, context.getPropagationPolicy(), context.getGracePeriodSeconds(), fromServer, deletingExisting, unmarshal(is, KubernetesList.class), null, dryRun);
return new KubernetesListOperationsImpl(context.withItem(Serialization.unmarshal(is, KubernetesList.class)), namespaceVisitOperationContext);
}

@Override
public KubernetesList get() {
return item;
KubernetesList list = (KubernetesList)context.getItem();
if (list == null) {
return null;
}
return new KubernetesListBuilder(list).withItems(list.getItems().stream().map(meta -> getResource(meta).get()).collect(Collectors.toList())).build();
}

private <T extends HasMetadata, V extends VisitableBuilder<T, V>> T create(T resource) {
ResourceHandler<T, V> handler = Handlers.get(resource);
if (handler != null) {
return handler.create(client, config, namespace, resource, dryRun);
}
throw new IllegalStateException("Could not find handler");
private Resource<HasMetadata> getResource(HasMetadata resource) {
return new NamespaceVisitFromServerGetWatchDeleteRecreateWaitApplicableImpl(context, namespaceVisitOperationContext).getResource();
}

@Override
Expand All @@ -142,9 +136,8 @@ public Boolean delete(KubernetesList... lists) {
public Boolean delete(List<KubernetesList> lists) {
for (KubernetesList list : lists) {
for (HasMetadata item : list.getItems()) {
ResourceHandler<HasMetadata, ?> handler = Handlers.get(item);
if (!handler.delete(client, config, namespace, context.getPropagationPolicy(), context.getGracePeriodSeconds(), item, dryRun)) {
return false;
if (!getResource(item).delete()) {
return false;
}
}
}
Expand All @@ -153,18 +146,18 @@ public Boolean delete(List<KubernetesList> lists) {

@Override
public Gettable<KubernetesList> fromServer() {
return new KubernetesListOperationsImpl(client, config, namespace, null, context.getPropagationPolicy(), context.getGracePeriodSeconds(), true, deletingExisting, item, null, dryRun);
return new KubernetesListOperationsImpl(context.withReloadingFromServer(true), namespaceVisitOperationContext);
}

@Override
public Createable<KubernetesList> deletingExisting() {
return new KubernetesListOperationsImpl(client, config, namespace, null, context.getPropagationPolicy(), context.getGracePeriodSeconds(), fromServer, true, item, null, dryRun);
return new KubernetesListOperationsImpl(context, namespaceVisitOperationContext.withDeletingExisting(true));
}

private List<HasMetadata> createItemsInKubernetesList(KubernetesList list) {
List<HasMetadata> createdItems = new ArrayList<>();
for (HasMetadata r : list.getItems()) {
HasMetadata created = create(r);
HasMetadata created = getResource(r).create(r);
createdItems.add(created);
}
return createdItems;
Expand Down

0 comments on commit 21462ba

Please sign in to comment.