Skip to content

Commit

Permalink
refactor: Allow editing using visitors.
Browse files Browse the repository at this point in the history
  • Loading branch information
iocanel committed Dec 4, 2020
1 parent 74d9194 commit 1e1fdb8
Show file tree
Hide file tree
Showing 43 changed files with 300 additions and 1 deletion.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
#### Improvements

#### Dependency Upgrade
* edit methods, now accept visitors as vararg or lambda.

#### New Features

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@

package io.fabric8.chaosmesh.client.internal;

import io.fabric8.kubernetes.api.builder.Visitor;
import io.fabric8.kubernetes.client.Config;
import io.fabric8.kubernetes.client.dsl.Resource;
import io.fabric8.kubernetes.client.dsl.base.HasMetadataOperation;
Expand All @@ -45,6 +46,7 @@ import io.fabric8.kubernetes.client.dsl.base.OperationContext;
import okhttp3.OkHttpClient;

import ${model.packageName}.${model.name};
import ${model.packageName}.${model.name}Builder;
import ${model.packageName}.${model.name}List;

import java.util.Map;
Expand All @@ -69,6 +71,11 @@ public class ${model.name}OperationsImpl extends HasMetadataOperation<${model.na
return new ${model.name}OperationsImpl(context);
}

@Override
public ${model.name} edit(Visitor... visitors) {
return patch(new ${model.name}Builder(getMandatory()).accept(visitors).build());
}

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

package io.fabric8.knative.client.${group}.${apiVersion}.internal;

import io.fabric8.kubernetes.api.builder.Visitor;
import io.fabric8.kubernetes.client.Config;
import io.fabric8.kubernetes.client.dsl.Resource;
import io.fabric8.kubernetes.client.dsl.base.HasMetadataOperation;
Expand All @@ -45,6 +46,7 @@ import io.fabric8.kubernetes.client.dsl.base.OperationContext;
import okhttp3.OkHttpClient;

import ${model.packageName}.${model.name};
import ${model.packageName}.${model.name}Builder;
import ${model.packageName}.${model.name}List;

import java.util.Map;
Expand All @@ -69,6 +71,11 @@ public class ${model.name}OperationsImpl extends HasMetadataOperation<${model.na
return new ${model.name}OperationsImpl(context);
}

@Override
public ${model.name} edit(Visitor... visitors) {
return patch(new ${model.name}Builder(getMandatory()).accept(visitors).build());
}

@Override
public boolean isResourceNamespaced() {
return $isResourceNamespacedFlag;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
*/
package io.fabric8.servicecatalog.client.internal;

import io.fabric8.kubernetes.api.builder.Visitor;
import io.fabric8.kubernetes.client.Config;
import io.fabric8.kubernetes.client.dsl.base.BaseOperation;
import io.fabric8.kubernetes.client.dsl.base.HasMetadataOperation;
Expand Down Expand Up @@ -80,4 +81,10 @@ public ClusterServiceClassResource useServiceClass(String externalName) {
ClusterServiceClass c = list.get(0);
return new ClusterServiceClassOperationsImpl(context);
}

@Override
public ClusterServiceBroker edit(Visitor... visitors) {
return patch(new ClusterServiceBrokerBuilder(getMandatory()).accept(visitors).build());
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
*/
package io.fabric8.servicecatalog.client.internal;

import io.fabric8.kubernetes.api.builder.Visitor;
import io.fabric8.kubernetes.client.Config;
import io.fabric8.kubernetes.client.dsl.base.BaseOperation;
import io.fabric8.kubernetes.client.dsl.base.HasMetadataOperation;
Expand Down Expand Up @@ -88,4 +89,9 @@ public ServiceInstance instantiate(String instanceName, String plan) {
.endSpec()
.build());
}
@Override
public ClusterServiceClass edit(Visitor... visitors) {
return patch(new ClusterServiceClassBuilder(getMandatory()).accept(visitors).build());
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
*/
package io.fabric8.servicecatalog.client.internal;

import io.fabric8.kubernetes.api.builder.Visitor;
import io.fabric8.kubernetes.client.Config;
import io.fabric8.kubernetes.client.dsl.base.BaseOperation;
import io.fabric8.kubernetes.client.dsl.base.HasMetadataOperation;
Expand Down Expand Up @@ -81,4 +82,9 @@ public ServiceInstanceResource instantiateAnd(String... args) {
ServiceInstance item = instantiate(args);
return new ServiceInstanceOperationsImpl(context.withItem(item));
}

@Override
public ClusterServicePlan edit(Visitor... visitors) {
return patch(new ClusterServicePlanBuilder(getMandatory()).accept(visitors).build());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
*/
package io.fabric8.servicecatalog.client.internal;

import io.fabric8.kubernetes.api.builder.Visitor;
import io.fabric8.kubernetes.api.model.Secret;
import io.fabric8.kubernetes.client.Config;
import io.fabric8.kubernetes.client.dsl.base.BaseOperation;
Expand Down Expand Up @@ -48,6 +49,11 @@ public boolean isResourceNamespaced() {
return true;
}

@Override
public ServiceBinding edit(Visitor... visitors) {
return patch(new ServiceBindingBuilder(getMandatory()).accept(visitors).build());
}

@Override
public Secret getSecret() {
ServiceBinding instance = get();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
*/
package io.fabric8.servicecatalog.client.internal;

import io.fabric8.kubernetes.api.builder.Visitor;
import io.fabric8.kubernetes.client.Config;
import io.fabric8.kubernetes.client.dsl.base.BaseOperation;
import io.fabric8.kubernetes.client.dsl.base.HasMetadataOperation;
Expand Down Expand Up @@ -58,4 +59,10 @@ public ServiceBinding bind(String secretName) {
.endSpec()
.build());
}

@Override
public ServiceInstance edit(Visitor... visitors) {
return patch(new ServiceInstanceBuilder(getMandatory()).accept(visitors).build());
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@

package io.fabric8.servicecatalog.client.internal;

import io.fabric8.kubernetes.api.builder.Visitor;
import io.fabric8.kubernetes.client.Config;
import io.fabric8.kubernetes.client.dsl.Resource;
import io.fabric8.kubernetes.client.dsl.base.HasMetadataOperation;
Expand All @@ -45,6 +46,7 @@ import io.fabric8.kubernetes.client.dsl.base.OperationContext;
import okhttp3.OkHttpClient;

import ${model.packageName}.${model.name};
import ${model.packageName}.${model.name}Builder;
import ${model.packageName}.${model.name}List;

import java.util.Map;
Expand All @@ -69,6 +71,11 @@ public class ${model.name}OperationsImpl extends HasMetadataOperation<${model.na
return new ${model.name}OperationsImpl(context);
}

@Override
public ${model.name} edit(Visitor... visitors) {
return patch(new ${model.name}Builder(getMandatory()).accept(visitors).build());
}

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

package io.fabric8.tekton.client.internal.$apiGroupVersion;

import io.fabric8.kubernetes.api.builder.Visitor;
import io.fabric8.kubernetes.client.Config;
import io.fabric8.kubernetes.client.dsl.Resource;
import io.fabric8.kubernetes.client.dsl.base.HasMetadataOperation;
Expand All @@ -45,6 +46,7 @@ import io.fabric8.kubernetes.client.dsl.base.OperationContext;
import okhttp3.OkHttpClient;

import ${model.packageName}.${model.name};
import ${model.packageName}.${model.name}Builder;
import ${model.packageName}.${model.name}List;

import java.util.Map;
Expand All @@ -69,6 +71,11 @@ public class ${model.name}OperationsImpl extends HasMetadataOperation<${model.na
return new ${model.name}OperationsImpl(context);
}

@Override
public ${model.name} edit(Visitor... visitors) {
return patch(new ${model.name}Builder(getMandatory()).accept(visitors).build());
}

@Override
public boolean isResourceNamespaced() {
return $isResourceNamespacedFlag;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,13 @@
*/
package io.fabric8.volumesnapshot.client.internal;

import io.fabric8.kubernetes.api.builder.Visitor;
import io.fabric8.kubernetes.client.Config;
import io.fabric8.kubernetes.client.dsl.base.BaseOperation;
import io.fabric8.kubernetes.client.dsl.base.HasMetadataOperation;
import io.fabric8.kubernetes.client.dsl.base.OperationContext;
import io.fabric8.volumesnapshot.api.model.VolumeSnapshotClass;
import io.fabric8.volumesnapshot.api.model.VolumeSnapshotClassBuilder;
import io.fabric8.volumesnapshot.api.model.VolumeSnapshotClassList;
import io.fabric8.volumesnapshot.api.model.VolumeSnapshotList;
import okhttp3.OkHttpClient;
Expand Down Expand Up @@ -59,4 +61,10 @@ public VolumeSnapshotList listSnapshots() {
.withFields(fields)
.list();
}

@Override
public VolumeSnapshotClass edit(Visitor... visitors) {
return patch(new VolumeSnapshotClassBuilder(getMandatory()).accept(visitors).build());
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,13 @@
*/
package io.fabric8.volumesnapshot.client.internal;

import io.fabric8.kubernetes.api.builder.Visitor;
import io.fabric8.kubernetes.client.Config;
import io.fabric8.kubernetes.client.dsl.base.BaseOperation;
import io.fabric8.kubernetes.client.dsl.base.HasMetadataOperation;
import io.fabric8.kubernetes.client.dsl.base.OperationContext;
import io.fabric8.volumesnapshot.api.model.VolumeSnapshotContent;
import io.fabric8.volumesnapshot.api.model.VolumeSnapshotContentBuilder;
import io.fabric8.volumesnapshot.api.model.VolumeSnapshotContentList;
import okhttp3.OkHttpClient;

Expand All @@ -41,6 +43,11 @@ public BaseOperation<VolumeSnapshotContent, VolumeSnapshotContentList, VolumeSna
return new VolumeSnapshotContentOperationsImpl(context);
}

@Override
public VolumeSnapshotContent edit(Visitor... visitors) {
return patch(new VolumeSnapshotContentBuilder(getMandatory()).accept(visitors).build());
}

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

package io.fabric8.volumesnapshot.client.internal;

import io.fabric8.kubernetes.api.builder.Visitor;
import io.fabric8.kubernetes.client.Config;
import io.fabric8.kubernetes.client.dsl.Resource;
import io.fabric8.kubernetes.client.dsl.base.HasMetadataOperation;
Expand All @@ -45,6 +46,7 @@ import io.fabric8.kubernetes.client.dsl.base.OperationContext;
import okhttp3.OkHttpClient;

import ${model.packageName}.${model.name};
import ${model.packageName}.${model.name}Builder;
import ${model.packageName}.${model.name}List;

import java.util.Map;
Expand All @@ -69,6 +71,11 @@ public class ${model.name}OperationsImpl extends HasMetadataOperation<${model.na
return new ${model.name}OperationsImpl(context);
}

@Override
public ${model.name} edit(Visitor... visitors) {
return patch(new ${model.name}Builder(getMandatory()).accept(visitors).build());
}

@Override
public boolean isResourceNamespaced() {
return $isResourceNamespacedFlag;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,15 @@
import java.util.function.Consumer;
import java.util.function.UnaryOperator;

import io.fabric8.kubernetes.api.builder.Visitor;

public interface Editable<T> {

T edit(UnaryOperator<T> function);

T edit(Visitor... visitors);

<V> T edit(Class<V> visitorType, Visitor<V> visitor);

T accept(Consumer<T> function);
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import io.fabric8.kubernetes.api.builder.TypedVisitor;
import io.fabric8.kubernetes.api.builder.Visitor;
import io.fabric8.kubernetes.api.model.DeletionPropagation;
import io.fabric8.kubernetes.api.model.HasMetadata;
import io.fabric8.kubernetes.api.model.KubernetesResourceList;
Expand Down Expand Up @@ -247,6 +249,26 @@ public T edit(UnaryOperator<T> function) {
throw new KubernetesClientException("Cannot edit read-only resources");
}

@Override
public T edit(Visitor... visitors) {
throw new KubernetesClientException("Cannot edit read-only resources");
}

@Override
public <V> T edit(final Class<V> visitorType, final Visitor<V> visitor) {
return edit(new TypedVisitor<V>() {
@Override
public Class<V> getType() {
return visitorType;
}
@Override
public void visit(V item) {
visitor.visit(item);
}
});
}


@Override
public T accept(Consumer<T> consumer) {
throw new KubernetesClientException("Cannot edit read-only resources");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -74,4 +74,5 @@ protected static VersionInfo fetchVersionInfoFromResponse(Map<String, String> re
.withCompiler(responseAsMap.get(VersionInfo.VersionKeys.COMPILER))
.build();
}

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

import io.fabric8.kubernetes.api.builder.Visitor;
import io.fabric8.kubernetes.api.model.Container;
import io.fabric8.kubernetes.api.model.OwnerReference;
import io.fabric8.kubernetes.api.model.Status;
Expand All @@ -28,6 +29,7 @@
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 @@ -383,6 +385,11 @@ public Loggable<LogWatch> withLogWaitTimeout(Integer logWaitTimeout) {
return new DeploymentOperationsImpl(((RollingOperationContext)context), logWaitTimeout);
}

@Override
public Deployment edit(Visitor... visitors) {
return patch(new DeploymentBuilder(getMandatory()).accept(visitors).build());
}

private Deployment sendPatchedDeployment(Map<String, Object> patchedUpdate) {
Deployment oldDeployment = get();
try {
Expand Down
Loading

0 comments on commit 1e1fdb8

Please sign in to comment.