Skip to content

Commit

Permalink
fix fabric8io#3284: using handlers to get a builder
Browse files Browse the repository at this point in the history
also just using serialization for cloning
  • Loading branch information
shawkins committed Jul 1, 2021
1 parent bf80377 commit 9059481
Show file tree
Hide file tree
Showing 17 changed files with 30 additions and 105 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -924,7 +924,7 @@ protected Class<? extends Config> getConfigType() {
* @param list Kubernetes resource list
*/
protected void updateApiVersion(KubernetesResourceList<T> list) {
String version = getApiVersion();
String version = apiVersion;
if (list != null && version != null && version.length() > 0 && list.getItems() != null) {
list.getItems().forEach(this::updateApiVersion);
}
Expand All @@ -937,7 +937,7 @@ protected void updateApiVersion(KubernetesResourceList<T> list) {
* @param hasMetadata object whose api version needs to be updated
*/
protected void updateApiVersion(HasMetadata hasMetadata) {
String version = getApiVersion();
String version = apiVersion;
if (hasMetadata != null && version != null && version.length() > 0) {
String current = hasMetadata.getApiVersion();
// lets overwrite the api version if its currently missing, the resource uses an API Group with '/'
Expand All @@ -948,19 +948,6 @@ protected void updateApiVersion(HasMetadata hasMetadata) {
}
}

public String getApiVersion() {
return apiVersion;
}

/**
* Return true if this is an API Group where the versions include a slash in them
*
* @return boolean value indicating whether API group or not
*/
public boolean isApiGroup() {
return apiVersion != null && apiVersion.indexOf('/') > 0;
}

public Readiness getReadiness() {
return Readiness.getInstance();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,12 @@
import io.fabric8.kubernetes.api.model.HasMetadata;
import io.fabric8.kubernetes.api.model.KubernetesResourceList;
import io.fabric8.kubernetes.api.model.ObjectMeta;
import io.fabric8.kubernetes.client.Handlers;
import io.fabric8.kubernetes.client.KubernetesClientException;
import io.fabric8.kubernetes.client.ResourceHandler;
import io.fabric8.kubernetes.client.ResourceNotFoundException;
import io.fabric8.kubernetes.client.dsl.Resource;
import io.fabric8.kubernetes.client.utils.ApiVersionUtil;
import io.fabric8.kubernetes.client.utils.KubernetesResourceUtil;
import io.fabric8.kubernetes.client.utils.Serialization;
import io.fabric8.kubernetes.client.utils.Utils;
Expand Down Expand Up @@ -58,11 +61,7 @@ public T edit(UnaryOperator<T> function) {
}

private T clone(T item) {
try {
return createVisitableBuilder(item).build();
} catch (KubernetesClientException e) {
return Serialization.clone(item);
}
return Serialization.clone(item);
}

@Override
Expand All @@ -80,7 +79,11 @@ public T accept(Consumer<T> consumer) {
return patch(null, clone, item, false);
}

protected VisitableBuilder<T, ?> createVisitableBuilder(T item) {
protected <V extends VisitableBuilder<T, V>> VisitableBuilder<T, V> createVisitableBuilder(T item) {
ResourceHandler<T, V> handler = Handlers.get(getKind(), ApiVersionUtil.joinApiGroupAndVersion(getAPIGroupName(), getAPIGroupVersion()));
if (handler != null) {
return handler.edit(item);
}
throw new KubernetesClientException(NO_BUILDER);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -117,11 +117,11 @@ public OperationSupport(OperationContext ctx) {
this.retryIntervalCalculator = new ExponentialBackoffIntervalCalculator(requestRetryBackoffInterval, maxRetryIntervalExponent);
}

public String getAPIGroup() {
public String getAPIGroupName() {
return apiGroupName;
}

public String getAPIVersion() {
public String getAPIGroupVersion() {
return apiGroupVersion;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
import io.fabric8.kubernetes.client.dsl.base.CustomResourceDefinitionContext;
import io.fabric8.kubernetes.client.dsl.base.HasMetadataOperation;
import io.fabric8.kubernetes.client.dsl.base.OperationContext;
import io.fabric8.kubernetes.client.utils.ApiVersionUtil;
import io.fabric8.kubernetes.internal.KubernetesDeserializer;
import okhttp3.OkHttpClient;

Expand All @@ -48,7 +49,7 @@ public CustomResourceOperationsImpl(CustomResourceOperationContext context) {
this.listType = context.getListType() != null ? context.getListType() : (Class) inferListType(this.type);

this.resourceNamespaced = resourceNamespaced(context.getCrdContext());
this.apiVersion = getAPIGroup() + "/" + getAPIVersion();
this.apiVersion = ApiVersionUtil.joinApiGroupAndVersion(getAPIGroupName(), getAPIGroupVersion());

KubernetesDeserializer.registerCustomKind(apiVersion, kind(context.getCrdContext()), type);
if (KubernetesResource.class.isAssignableFrom(listType)) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@
*/
package io.fabric8.kubernetes.client.dsl.internal.apps.v1;

import io.fabric8.kubernetes.api.builder.VisitableBuilder;
import io.fabric8.kubernetes.api.model.Container;
import io.fabric8.kubernetes.api.model.OwnerReference;
import io.fabric8.kubernetes.api.model.Status;
Expand All @@ -30,7 +29,6 @@
import io.fabric8.kubernetes.client.utils.Utils;
import okhttp3.OkHttpClient;
import io.fabric8.kubernetes.api.model.apps.Deployment;
import io.fabric8.kubernetes.api.model.apps.DeploymentBuilder;
import io.fabric8.kubernetes.api.model.apps.DeploymentList;
import io.fabric8.kubernetes.client.Config;
import io.fabric8.kubernetes.client.KubernetesClientException;
Expand Down Expand Up @@ -385,11 +383,6 @@ public Loggable<LogWatch> withLogWaitTimeout(Integer logWaitTimeout) {
return new DeploymentOperationsImpl(((RollingOperationContext)context), logWaitTimeout);
}

@Override
protected VisitableBuilder<Deployment, ?> createVisitableBuilder(Deployment item) {
return new DeploymentBuilder(item);
}

private Deployment sendPatchedDeployment(Map<String, Object> patchedUpdate) {
Deployment oldDeployment = get();
try {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,10 @@
*/
package io.fabric8.kubernetes.client.dsl.internal.apps.v1;

import io.fabric8.kubernetes.api.builder.VisitableBuilder;
import io.fabric8.kubernetes.api.model.Container;
import io.fabric8.kubernetes.api.model.Pod;
import io.fabric8.kubernetes.api.model.Status;
import io.fabric8.kubernetes.api.model.apps.ReplicaSet;
import io.fabric8.kubernetes.api.model.apps.ReplicaSetBuilder;
import io.fabric8.kubernetes.api.model.apps.ReplicaSetList;
import io.fabric8.kubernetes.api.model.extensions.DeploymentRollback;
import io.fabric8.kubernetes.client.Config;
Expand Down Expand Up @@ -233,11 +231,6 @@ public Loggable<LogWatch> withLogWaitTimeout(Integer logWaitTimeout) {
return new ReplicaSetOperationsImpl(((RollingOperationContext)context), logWaitTimeout);
}

@Override
protected VisitableBuilder<ReplicaSet, ?> createVisitableBuilder(ReplicaSet item) {
return new ReplicaSetBuilder(item);
}

static Map<String, String> getReplicaSetSelectorLabels(ReplicaSet replicaSet) {
Map<String, String> labels = new HashMap<>();

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

import io.fabric8.kubernetes.api.builder.VisitableBuilder;
import io.fabric8.kubernetes.api.model.Container;
import io.fabric8.kubernetes.api.model.HasMetadata;
import io.fabric8.kubernetes.api.model.Pod;
import io.fabric8.kubernetes.api.model.Status;
import io.fabric8.kubernetes.api.model.apps.ControllerRevision;
import io.fabric8.kubernetes.api.model.apps.ControllerRevisionList;
import io.fabric8.kubernetes.api.model.apps.StatefulSet;
import io.fabric8.kubernetes.api.model.apps.StatefulSetBuilder;
import io.fabric8.kubernetes.api.model.apps.StatefulSetList;
import io.fabric8.kubernetes.api.model.extensions.DeploymentRollback;
import io.fabric8.kubernetes.client.Config;
Expand Down Expand Up @@ -261,11 +259,6 @@ public StatefulSet undo() {
return sendPatchedStatefulSetData(previousControllerRevision.getData());
}

@Override
protected VisitableBuilder<StatefulSet, ?> createVisitableBuilder(StatefulSet item) {
return new StatefulSetBuilder(item);
}

private StatefulSet sendPatchedStatefulSet(Map<String, Object> patchedUpdate) {
StatefulSet oldStatefulSet = get();
try {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@
*/
package io.fabric8.kubernetes.client.dsl.internal.batch.v1;

import io.fabric8.kubernetes.api.builder.VisitableBuilder;
import io.fabric8.kubernetes.api.model.Pod;
import io.fabric8.kubernetes.api.model.autoscaling.v1.Scale;
import io.fabric8.kubernetes.client.dsl.LogWatch;
Expand All @@ -25,7 +24,6 @@
import io.fabric8.kubernetes.client.utils.PodOperationUtil;
import okhttp3.OkHttpClient;
import io.fabric8.kubernetes.api.model.batch.v1.Job;
import io.fabric8.kubernetes.api.model.batch.v1.JobBuilder;
import io.fabric8.kubernetes.api.model.batch.v1.JobList;
import io.fabric8.kubernetes.client.Config;
import io.fabric8.kubernetes.client.KubernetesClientException;
Expand Down Expand Up @@ -238,11 +236,6 @@ public Job replace(Job job) {
return super.replace(job);
}

@Override
protected VisitableBuilder<Job, ?> createVisitableBuilder(Job item) {
return new JobBuilder(item);
}

static Map<String, String> getJobPodLabels(Job job) {
Map<String, String> labels = new HashMap<>();
if (job != null && job.getMetadata() != null && job.getMetadata().getUid() != null) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,7 @@

import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.type.TypeFactory;
import io.fabric8.kubernetes.api.builder.VisitableBuilder;
import io.fabric8.kubernetes.api.model.Binding;
import io.fabric8.kubernetes.api.model.BindingBuilder;
import io.fabric8.kubernetes.api.model.KubernetesResourceList;
import io.fabric8.kubernetes.client.Config;
import io.fabric8.kubernetes.client.dsl.Resource;
Expand All @@ -44,11 +42,6 @@ public BindingOperationsImpl(OperationContext context) {
this.listType = (Class<KubernetesResourceList<Binding>>) TypeFactory.rawClass(new TypeReference<KubernetesResourceList<Binding>>(){}.getType());
}

@Override
protected VisitableBuilder<Binding, ?> createVisitableBuilder(Binding item) {
return new BindingBuilder(item);
}

public BindingOperationsImpl newInstance(OperationContext context) {
return new BindingOperationsImpl(context);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,14 +15,11 @@
*/
package io.fabric8.kubernetes.client.dsl.internal.core.v1;

import io.fabric8.kubernetes.api.builder.VisitableBuilder;
import io.fabric8.kubernetes.api.builder.Visitor;
import io.fabric8.kubernetes.client.dsl.Resource;
import io.fabric8.kubernetes.client.dsl.base.OperationContext;
import okhttp3.OkHttpClient;

import io.fabric8.kubernetes.api.model.ComponentStatus;
import io.fabric8.kubernetes.api.model.ComponentStatusBuilder;
import io.fabric8.kubernetes.api.model.ComponentStatusList;
import io.fabric8.kubernetes.client.Config;
import io.fabric8.kubernetes.client.dsl.base.HasMetadataOperation;
Expand All @@ -49,11 +46,6 @@ public ComponentStatusOperationsImpl newInstance(OperationContext context) {
return new ComponentStatusOperationsImpl(context);
}

@Override
protected VisitableBuilder<ComponentStatus, ?> createVisitableBuilder(ComponentStatus item) {
return new ComponentStatusBuilder(item);
}

@Override
public boolean isResourceNamespaced() {
return false;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,6 @@

import io.fabric8.kubernetes.api.model.DeleteOptions;
import io.fabric8.kubernetes.api.model.Pod;
import io.fabric8.kubernetes.api.model.PodBuilder;
import io.fabric8.kubernetes.api.model.PodList;
import io.fabric8.kubernetes.api.model.policy.v1beta1.Eviction;
import io.fabric8.kubernetes.api.model.policy.v1beta1.EvictionBuilder;
Expand Down Expand Up @@ -73,7 +72,6 @@
import io.fabric8.kubernetes.client.dsl.internal.uploadable.PodUpload;
import io.fabric8.kubernetes.client.utils.URLUtils;
import io.fabric8.kubernetes.client.utils.Utils;
import io.fabric8.kubernetes.api.builder.VisitableBuilder;
import okhttp3.HttpUrl;
import okhttp3.OkHttpClient;
import okhttp3.Request;
Expand Down Expand Up @@ -687,9 +685,5 @@ public BytesLimitTerminateTimeTailPrettyLoggable<LogWatch> usingTimestamps() {
return new PodOperationsImpl(getContext().withTimestamps(true));
}

@Override
protected VisitableBuilder<Pod, ?> createVisitableBuilder(Pod item) {
return new PodBuilder(item);
}
}

Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,9 @@
*/
package io.fabric8.kubernetes.client.dsl.internal.core.v1;

import io.fabric8.kubernetes.api.builder.VisitableBuilder;
import io.fabric8.kubernetes.api.model.Container;
import io.fabric8.kubernetes.api.model.Pod;
import io.fabric8.kubernetes.api.model.ReplicationController;
import io.fabric8.kubernetes.api.model.ReplicationControllerBuilder;
import io.fabric8.kubernetes.api.model.ReplicationControllerList;
import io.fabric8.kubernetes.api.model.Status;
import io.fabric8.kubernetes.api.model.extensions.DeploymentRollback;
Expand Down Expand Up @@ -241,11 +239,6 @@ public ReplicationController undo() {
throw new UnsupportedOperationException("no rollbacker has been implemented for \"" + get().getKind() +"\"");
}

@Override
protected VisitableBuilder<ReplicationController, ?> createVisitableBuilder(ReplicationController item) {
return new ReplicationControllerBuilder(item);
}

static Map<String, String> getReplicationControllerPodLabels(ReplicationController replicationController) {
Map<String, String> labels = new HashMap<>();
if (replicationController != null && replicationController.getSpec() != null && replicationController.getSpec().getSelector() != null) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@
import io.fabric8.kubernetes.api.model.Service;
import io.fabric8.kubernetes.api.model.ServiceBuilder;
import io.fabric8.kubernetes.api.model.ServiceList;
import io.fabric8.kubernetes.api.builder.VisitableBuilder;
import io.fabric8.kubernetes.api.model.*;
import io.fabric8.kubernetes.client.*;
import io.fabric8.kubernetes.client.Config;
Expand Down Expand Up @@ -137,11 +136,6 @@ public LocalPortForward portForward(int port) {
.portForward(port);
}

@Override
protected VisitableBuilder<Service, ?> createVisitableBuilder(Service item) {
return new ServiceBuilder(item);
}

public class ServiceToUrlSortComparator implements Comparator<ServiceToURLProvider> {
public int compare(ServiceToURLProvider first, ServiceToURLProvider second) {
return first.getPriority() - second.getPriority();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,10 @@
*/
package io.fabric8.kubernetes.client.dsl.internal.extensions.v1beta1;

import io.fabric8.kubernetes.api.builder.VisitableBuilder;
import io.fabric8.kubernetes.api.model.Container;
import io.fabric8.kubernetes.api.model.OwnerReference;
import io.fabric8.kubernetes.api.model.Status;
import io.fabric8.kubernetes.api.model.extensions.Deployment;
import io.fabric8.kubernetes.api.model.extensions.DeploymentBuilder;
import io.fabric8.kubernetes.api.model.extensions.DeploymentList;
import io.fabric8.kubernetes.api.model.extensions.DeploymentRollback;
import io.fabric8.kubernetes.api.model.extensions.ReplicaSet;
Expand Down Expand Up @@ -392,11 +390,6 @@ public Loggable<LogWatch> withLogWaitTimeout(Integer logWaitTimeout) {
return new DeploymentOperationsImpl(((RollingOperationContext) context), logWaitTimeout);
}

@Override
protected VisitableBuilder<Deployment, ?> createVisitableBuilder(Deployment item) {
return new DeploymentBuilder(item);
}

private Deployment sendPatchedDeployment(Map<String, Object> patchedUpdate) {
Deployment oldDeployment = get();
try {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,11 @@
*/
package io.fabric8.kubernetes.client.dsl.internal.extensions.v1beta1;

import io.fabric8.kubernetes.api.builder.VisitableBuilder;
import io.fabric8.kubernetes.api.model.Container;
import io.fabric8.kubernetes.api.model.Pod;
import io.fabric8.kubernetes.api.model.Status;
import io.fabric8.kubernetes.api.model.extensions.DeploymentRollback;
import io.fabric8.kubernetes.api.model.extensions.ReplicaSet;
import io.fabric8.kubernetes.api.model.extensions.ReplicaSetBuilder;
import io.fabric8.kubernetes.api.model.extensions.ReplicaSetList;
import io.fabric8.kubernetes.client.Config;
import io.fabric8.kubernetes.client.KubernetesClientException;
Expand Down Expand Up @@ -236,11 +234,6 @@ public Loggable<LogWatch> withLogWaitTimeout(Integer logWaitTimeout) {
return new ReplicaSetOperationsImpl(((RollingOperationContext) context), logWaitTimeout);
}

@Override
protected VisitableBuilder<ReplicaSet, ?> createVisitableBuilder(ReplicaSet item) {
return new ReplicaSetBuilder(item);
}

static Map<String, String> getReplicaSetSelectorLabels(ReplicaSet replicaSet) {
Map<String, String> labels = new HashMap<>();

Expand Down
Loading

0 comments on commit 9059481

Please sign in to comment.