Skip to content

Commit

Permalink
possible mechanism for extension based only on api
Browse files Browse the repository at this point in the history
  • Loading branch information
shawkins committed Feb 15, 2022
1 parent 71153e0 commit c438ddf
Show file tree
Hide file tree
Showing 16 changed files with 583 additions and 223 deletions.
9 changes: 7 additions & 2 deletions extensions/service-catalog/client/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -52,12 +52,17 @@
<dependency>
<groupId>io.fabric8</groupId>
<artifactId>servicecatalog-model</artifactId>
<version>${project.version}</version>
</dependency>

<dependency>
<groupId>io.fabric8</groupId>
<artifactId>kubernetes-client</artifactId>
<scope>runtime</scope>
</dependency>

<dependency>
<groupId>io.fabric8</groupId>
<artifactId>kubernetes-client-api</artifactId>
<exclusions>
<exclusion>
<groupId>io.sundr</groupId>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,17 +15,16 @@
*/
package io.fabric8.servicecatalog.client;

import io.fabric8.kubernetes.client.BaseClient;
import io.fabric8.kubernetes.client.ClientContext;
import io.fabric8.kubernetes.client.Client;
import io.fabric8.kubernetes.client.Config;
import io.fabric8.kubernetes.client.ConfigBuilder;
import io.fabric8.kubernetes.client.Handlers;
import io.fabric8.kubernetes.client.KubernetesClientBuilder;
import io.fabric8.kubernetes.client.RequestConfig;
import io.fabric8.kubernetes.client.WithRequestCallable;
import io.fabric8.kubernetes.client.dsl.FunctionCallable;
import io.fabric8.kubernetes.client.dsl.MixedOperation;
import io.fabric8.kubernetes.client.dsl.NonNamespaceOperation;
import io.fabric8.kubernetes.client.dsl.Resource;
import io.fabric8.kubernetes.client.extension.ClientAdapter;
import io.fabric8.servicecatalog.api.model.ClusterServiceBroker;
import io.fabric8.servicecatalog.api.model.ClusterServiceBrokerList;
import io.fabric8.servicecatalog.api.model.ClusterServiceClass;
Expand All @@ -42,83 +41,64 @@
import io.fabric8.servicecatalog.api.model.ServiceInstanceList;
import io.fabric8.servicecatalog.api.model.ServicePlan;
import io.fabric8.servicecatalog.api.model.ServicePlanList;
import io.fabric8.servicecatalog.client.internal.ClusterServiceBrokerOperationsImpl;
import io.fabric8.servicecatalog.client.internal.ClusterServiceBrokerResource;
import io.fabric8.servicecatalog.client.internal.ClusterServiceClassOperationsImpl;
import io.fabric8.servicecatalog.client.internal.ClusterServiceClassResource;
import io.fabric8.servicecatalog.client.internal.ClusterServicePlanOperationsImpl;
import io.fabric8.servicecatalog.client.internal.ClusterServicePlanResource;
import io.fabric8.servicecatalog.client.internal.ServiceBindingOperationsImpl;
import io.fabric8.servicecatalog.client.internal.ServiceBindingResource;
import io.fabric8.servicecatalog.client.internal.ServiceInstanceOperationsImpl;
import io.fabric8.servicecatalog.client.internal.ServiceInstanceResource;

public class DefaultServiceCatalogClient extends BaseClient implements NamespacedServiceCatalogClient {
public class DefaultServiceCatalogClient extends ClientAdapter<DefaultServiceCatalogClient> implements NamespacedServiceCatalogClient {

public DefaultServiceCatalogClient() {
super();
this(new KubernetesClientBuilder().build());
}

public DefaultServiceCatalogClient(Config configuration) {
super(configuration);
this(new KubernetesClientBuilder().withConfig(configuration).build());
}

public DefaultServiceCatalogClient(ClientContext clientContext) {
super(clientContext);
public DefaultServiceCatalogClient(Client client) {
super(client, DefaultServiceCatalogClient::new);
}

@Override
public NonNamespaceOperation<ClusterServiceBroker, ClusterServiceBrokerList, ClusterServiceBrokerResource> clusterServiceBrokers() {
return new ClusterServiceBrokerOperationsImpl(this);
public NonNamespaceOperation<ClusterServiceBroker, ClusterServiceBrokerList, ClusterServiceBrokerResource> clusterServiceBrokers() {
return resources(ClusterServiceBroker.class, ClusterServiceBrokerList.class, ClusterServiceBrokerResource.class);
}

@Override
public NonNamespaceOperation<ClusterServiceClass, ClusterServiceClassList, ClusterServiceClassResource> clusterServiceClasses() {
return new ClusterServiceClassOperationsImpl(this);
public NonNamespaceOperation<ClusterServiceClass, ClusterServiceClassList, ClusterServiceClassResource> clusterServiceClasses() {
return resources(ClusterServiceClass.class, ClusterServiceClassList.class, ClusterServiceClassResource.class);
}

@Override
public NonNamespaceOperation<ClusterServicePlan, ClusterServicePlanList, ClusterServicePlanResource> clusterServicePlans() {
return new ClusterServicePlanOperationsImpl(this);
public NonNamespaceOperation<ClusterServicePlan, ClusterServicePlanList, ClusterServicePlanResource> clusterServicePlans() {
return resources(ClusterServicePlan.class, ClusterServicePlanList.class, ClusterServicePlanResource.class);
}

@Override
public MixedOperation<ServiceInstance, ServiceInstanceList, ServiceInstanceResource> serviceInstances() {
return new ServiceInstanceOperationsImpl(this);
public MixedOperation<ServiceInstance, ServiceInstanceList, ServiceInstanceResource> serviceInstances() {
return resources(ServiceInstance.class, ServiceInstanceList.class, ServiceInstanceResource.class);
}

@Override
public MixedOperation<ServiceBinding, ServiceBindingList, ServiceBindingResource> serviceBindings() {
return new ServiceBindingOperationsImpl(this);
public MixedOperation<ServiceBinding, ServiceBindingList, ServiceBindingResource> serviceBindings() {
return resources(ServiceBinding.class, ServiceBindingList.class, ServiceBindingResource.class);
}

@Override
public MixedOperation<ServiceBroker, ServiceBrokerList, Resource<ServiceBroker>> serviceBrokers() {
return Handlers.getOperation(ServiceBroker.class, ServiceBrokerList.class, this);
return resources(ServiceBroker.class, ServiceBrokerList.class);
}

@Override
public MixedOperation<ServiceClass, ServiceClassList, Resource<ServiceClass>> serviceClasses() {
return Handlers.getOperation(ServiceClass.class, ServiceClassList.class, this);
return resources(ServiceClass.class, ServiceClassList.class);
}

@Override
public MixedOperation<ServicePlan, ServicePlanList, Resource<ServicePlan>> servicePlans() {
return Handlers.getOperation(ServicePlan.class, ServicePlanList.class, this);
}

@Override
public NamespacedServiceCatalogClient inAnyNamespace() {
return inNamespace(null);
}

@Override
public NamespacedServiceCatalogClient inNamespace(String namespace) {
Config updated = new ConfigBuilder(getConfiguration())
.withNamespace(namespace)
.build();

return new DefaultServiceCatalogClient(newState(updated));
return resources(ServicePlan.class, ServicePlanList.class);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,6 @@
import io.fabric8.kubernetes.client.Client;
import io.fabric8.kubernetes.client.ExtensionAdapter;
import io.fabric8.kubernetes.client.ExtensionAdapterSupport;
import io.fabric8.kubernetes.client.Handlers;
import io.fabric8.kubernetes.client.http.HttpClient;
import io.fabric8.servicecatalog.api.model.ClusterServiceBroker;
import io.fabric8.servicecatalog.api.model.ClusterServiceClass;
import io.fabric8.servicecatalog.api.model.ClusterServicePlan;
Expand All @@ -35,31 +33,33 @@
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ConcurrentMap;

public class ServiceCatalogExtensionAdapter extends ExtensionAdapterSupport implements ExtensionAdapter<ServiceCatalogClient> {
public class ServiceCatalogExtensionAdapter extends ExtensionAdapterSupport
implements ExtensionAdapter<ServiceCatalogClient> {

static final ConcurrentMap<URL, Boolean> IS_SERVICE_CATALOG = new ConcurrentHashMap<>();
static final ConcurrentMap<URL, Boolean> USES_SERVICE_CATALOG_APIGROUPS = new ConcurrentHashMap<>();

static {
Handlers.register(ClusterServiceBroker.class, ClusterServiceBrokerOperationsImpl::new);
Handlers.register(ClusterServiceClass.class, ClusterServiceClassOperationsImpl::new);
Handlers.register(ClusterServicePlan.class, ClusterServicePlanOperationsImpl::new);
Handlers.register(ServiceBinding.class, ServiceBindingOperationsImpl::new);
Handlers.register(ServiceInstance.class, ServiceInstanceOperationsImpl::new);
@Override
public Class<ServiceCatalogClient> getExtensionType() {
return ServiceCatalogClient.class;
}

@Override
public Class<ServiceCatalogClient> getExtensionType() {
return ServiceCatalogClient.class;
}
@Override
public Boolean isAdaptable(Client client) {
return isAdaptable(client, IS_SERVICE_CATALOG, USES_SERVICE_CATALOG_APIGROUPS, "servicecatalog.k8s.io");
}

@Override
public Boolean isAdaptable(Client client) {
return isAdaptable(client, IS_SERVICE_CATALOG, USES_SERVICE_CATALOG_APIGROUPS, "servicecatalog.k8s.io");
}
@Override
public ServiceCatalogClient adapt(Client client) {
return new DefaultServiceCatalogClient(client);
}

@Override
public ServiceCatalogClient adapt(Client client) {
return new DefaultServiceCatalogClient(client);
}
@Override
public void registerHandlers(HandlerFactory factory) {
factory.register(ClusterServiceBroker.class, ClusterServiceBrokerOperationsImpl::new);
factory.register(ClusterServiceClass.class, ClusterServiceClassOperationsImpl::new);
factory.register(ClusterServicePlan.class, ClusterServicePlanOperationsImpl::new);
factory.register(ServiceBinding.class, ServiceBindingOperationsImpl::new);
factory.register(ServiceInstance.class, ServiceInstanceOperationsImpl::new);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,74 +15,58 @@
*/
package io.fabric8.servicecatalog.client.internal;

import io.fabric8.kubernetes.client.ClientContext;
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.kubernetes.client.dsl.internal.HasMetadataOperationsImpl;
import io.fabric8.kubernetes.client.Client;
import io.fabric8.kubernetes.client.dsl.Resource;
import io.fabric8.kubernetes.client.extension.ResourceAdapter;
import io.fabric8.servicecatalog.api.model.ClusterServiceBroker;
import io.fabric8.servicecatalog.api.model.ClusterServiceBrokerList;
import io.fabric8.servicecatalog.api.model.ClusterServiceClass;
import io.fabric8.servicecatalog.api.model.ClusterServiceClassList;
import io.fabric8.servicecatalog.api.model.ClusterServicePlanList;
import io.fabric8.servicecatalog.client.ServiceCatalogClient;

import java.util.HashMap;
import java.util.List;
import java.util.Map;


public class ClusterServiceBrokerOperationsImpl extends HasMetadataOperation<ClusterServiceBroker, ClusterServiceBrokerList, ClusterServiceBrokerResource> implements ClusterServiceBrokerResource {
public class ClusterServiceBrokerOperationsImpl extends ResourceAdapter<ClusterServiceBroker> implements ClusterServiceBrokerResource {

public ClusterServiceBrokerOperationsImpl(ClientContext clientContext) {
this(HasMetadataOperationsImpl.defaultContext(clientContext));
}

public ClusterServiceBrokerOperationsImpl(OperationContext context) {
super(context.withApiGroupName("servicecatalog.k8s.io").withApiGroupVersion("v1beta1").withPlural("clusterservicebrokers"),
ClusterServiceBroker.class, ClusterServiceBrokerList.class);
}

@Override
public BaseOperation<ClusterServiceBroker, ClusterServiceBrokerList, ClusterServiceBrokerResource> newInstance(OperationContext context) {
return new ClusterServiceBrokerOperationsImpl(context);
public ClusterServiceBrokerOperationsImpl(Resource<ClusterServiceBroker> resource, Client client) {
super(resource, client);
}

@Override
public boolean isResourceNamespaced() {
return false;
}

@Override
public ClusterServicePlanList listPlans() {
ClusterServiceBroker item = get();
return new ClusterServicePlanOperationsImpl(context)
return client.adapt(ServiceCatalogClient.class).clusterServicePlans()
.withField("spec.clusterServiceBrokerName", item.getMetadata().getName())
.list();
}

@Override
public ClusterServiceClassList listClasses() {
ClusterServiceBroker item = get();
return new ClusterServiceClassOperationsImpl(context)
return client.adapt(ServiceCatalogClient.class).clusterServiceClasses()
.withField("spec.clusterServiceBrokerName", item.getMetadata().getName())
.list();
}


@Override
public ClusterServiceClassResource useServiceClass(String externalName) {
ClusterServiceBroker item = get();
Map<String, String> fields = new HashMap<>();
fields.put("spec.clusterServiceBrokerName", item.getMetadata().getName());
fields.put("spec.externalName", externalName);
if (externalName != null) {
fields.put("spec.externalName", externalName);
}

List<ClusterServiceClass> list = new ClusterServiceClassOperationsImpl(context.withFields(fields)).list().getItems();
List<ClusterServiceClass> list = client.adapt(ServiceCatalogClient.class).clusterServiceClasses().withFields(fields).list().getItems();

if (list.size() != 1) {
throw new IllegalArgumentException("No unique ClusterServiceClass with external name:" + externalName + "found for ClusterServiceBroker: " + item.getMetadata().getName());
}
ClusterServiceClass c = list.get(0);
return new ClusterServiceClassOperationsImpl(context);
return client.adapt(ServiceCatalogClient.class).clusterServiceClasses().withItem(c);
}

}
Loading

0 comments on commit c438ddf

Please sign in to comment.