diff --git a/extensions/service-catalog/client/pom.xml b/extensions/service-catalog/client/pom.xml index 2f5fa36987e..fb9aa209c96 100644 --- a/extensions/service-catalog/client/pom.xml +++ b/extensions/service-catalog/client/pom.xml @@ -52,12 +52,17 @@ io.fabric8 servicecatalog-model - ${project.version} - + io.fabric8 kubernetes-client + runtime + + + + io.fabric8 + kubernetes-client-api io.sundr diff --git a/extensions/service-catalog/client/src/main/java/io/fabric8/servicecatalog/client/DefaultServiceCatalogClient.java b/extensions/service-catalog/client/src/main/java/io/fabric8/servicecatalog/client/DefaultServiceCatalogClient.java index 3409941c0cf..e817c50fd1d 100644 --- a/extensions/service-catalog/client/src/main/java/io/fabric8/servicecatalog/client/DefaultServiceCatalogClient.java +++ b/extensions/service-catalog/client/src/main/java/io/fabric8/servicecatalog/client/DefaultServiceCatalogClient.java @@ -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; @@ -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 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 clusterServiceBrokers() { - return new ClusterServiceBrokerOperationsImpl(this); + public NonNamespaceOperation clusterServiceBrokers() { + return resources(ClusterServiceBroker.class, ClusterServiceBrokerList.class, ClusterServiceBrokerResource.class); } @Override -public NonNamespaceOperation clusterServiceClasses() { - return new ClusterServiceClassOperationsImpl(this); + public NonNamespaceOperation clusterServiceClasses() { + return resources(ClusterServiceClass.class, ClusterServiceClassList.class, ClusterServiceClassResource.class); } @Override -public NonNamespaceOperation clusterServicePlans() { - return new ClusterServicePlanOperationsImpl(this); + public NonNamespaceOperation clusterServicePlans() { + return resources(ClusterServicePlan.class, ClusterServicePlanList.class, ClusterServicePlanResource.class); } @Override -public MixedOperation serviceInstances() { - return new ServiceInstanceOperationsImpl(this); + public MixedOperation serviceInstances() { + return resources(ServiceInstance.class, ServiceInstanceList.class, ServiceInstanceResource.class); } @Override -public MixedOperation serviceBindings() { - return new ServiceBindingOperationsImpl(this); + public MixedOperation serviceBindings() { + return resources(ServiceBinding.class, ServiceBindingList.class, ServiceBindingResource.class); } @Override public MixedOperation> serviceBrokers() { - return Handlers.getOperation(ServiceBroker.class, ServiceBrokerList.class, this); + return resources(ServiceBroker.class, ServiceBrokerList.class); } @Override public MixedOperation> serviceClasses() { - return Handlers.getOperation(ServiceClass.class, ServiceClassList.class, this); + return resources(ServiceClass.class, ServiceClassList.class); } @Override public MixedOperation> 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 diff --git a/extensions/service-catalog/client/src/main/java/io/fabric8/servicecatalog/client/ServiceCatalogExtensionAdapter.java b/extensions/service-catalog/client/src/main/java/io/fabric8/servicecatalog/client/ServiceCatalogExtensionAdapter.java index f6883e7a15f..249680c3347 100644 --- a/extensions/service-catalog/client/src/main/java/io/fabric8/servicecatalog/client/ServiceCatalogExtensionAdapter.java +++ b/extensions/service-catalog/client/src/main/java/io/fabric8/servicecatalog/client/ServiceCatalogExtensionAdapter.java @@ -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; @@ -35,31 +33,33 @@ import java.util.concurrent.ConcurrentHashMap; import java.util.concurrent.ConcurrentMap; -public class ServiceCatalogExtensionAdapter extends ExtensionAdapterSupport implements ExtensionAdapter { +public class ServiceCatalogExtensionAdapter extends ExtensionAdapterSupport + implements ExtensionAdapter { static final ConcurrentMap IS_SERVICE_CATALOG = new ConcurrentHashMap<>(); static final ConcurrentMap 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 getExtensionType() { + return ServiceCatalogClient.class; } - @Override - public Class 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); + } } diff --git a/extensions/service-catalog/client/src/main/java/io/fabric8/servicecatalog/client/internal/ClusterServiceBrokerOperationsImpl.java b/extensions/service-catalog/client/src/main/java/io/fabric8/servicecatalog/client/internal/ClusterServiceBrokerOperationsImpl.java index ca8ab31a4c8..2f507d23436 100644 --- a/extensions/service-catalog/client/src/main/java/io/fabric8/servicecatalog/client/internal/ClusterServiceBrokerOperationsImpl.java +++ b/extensions/service-catalog/client/src/main/java/io/fabric8/servicecatalog/client/internal/ClusterServiceBrokerOperationsImpl.java @@ -15,47 +15,30 @@ */ 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 implements ClusterServiceBrokerResource { +public class ClusterServiceBrokerOperationsImpl extends ResourceAdapter 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 newInstance(OperationContext context) { - return new ClusterServiceBrokerOperationsImpl(context); + public ClusterServiceBrokerOperationsImpl(Resource 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(); } @@ -63,26 +46,27 @@ public ClusterServicePlanList listPlans() { @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 fields = new HashMap<>(); fields.put("spec.clusterServiceBrokerName", item.getMetadata().getName()); - fields.put("spec.externalName", externalName); + if (externalName != null) { + fields.put("spec.externalName", externalName); + } - List list = new ClusterServiceClassOperationsImpl(context.withFields(fields)).list().getItems(); + List 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); } } diff --git a/extensions/service-catalog/client/src/main/java/io/fabric8/servicecatalog/client/internal/ClusterServiceClassOperationsImpl.java b/extensions/service-catalog/client/src/main/java/io/fabric8/servicecatalog/client/internal/ClusterServiceClassOperationsImpl.java index e8186034b60..bf3e602362e 100644 --- a/extensions/service-catalog/client/src/main/java/io/fabric8/servicecatalog/client/internal/ClusterServiceClassOperationsImpl.java +++ b/extensions/service-catalog/client/src/main/java/io/fabric8/servicecatalog/client/internal/ClusterServiceClassOperationsImpl.java @@ -15,49 +15,33 @@ */ 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.ClusterServiceClass; -import io.fabric8.servicecatalog.api.model.ClusterServiceClassList; import io.fabric8.servicecatalog.api.model.ClusterServicePlan; import io.fabric8.servicecatalog.api.model.ClusterServicePlanList; import io.fabric8.servicecatalog.api.model.ServiceInstance; import io.fabric8.servicecatalog.api.model.ServiceInstanceBuilder; +import io.fabric8.servicecatalog.client.ServiceCatalogClient; import java.util.HashMap; import java.util.List; import java.util.Map; +public class ClusterServiceClassOperationsImpl extends ResourceAdapter + implements ClusterServiceClassResource { -public class ClusterServiceClassOperationsImpl extends HasMetadataOperation implements ClusterServiceClassResource { - - - public ClusterServiceClassOperationsImpl(ClientContext clientContext) { - this(HasMetadataOperationsImpl.defaultContext(clientContext)); - } - - public ClusterServiceClassOperationsImpl(OperationContext ctx) { - super(ctx.withApiGroupName("servicecatalog.k8s.io").withApiGroupVersion("v1beta1").withPlural("clusterserviceclasses"), - ClusterServiceClass.class, ClusterServiceClassList.class); + public ClusterServiceClassOperationsImpl(Resource resource, Client client) { + super(resource, client); } - @Override - public BaseOperation newInstance(OperationContext context) { - return new ClusterServiceClassOperationsImpl(context); - } - - @Override - public boolean isResourceNamespaced() { - return false; - } - @Override public ClusterServicePlanList listPlans() { ClusterServiceClass item = get(); - return new ClusterServicePlanOperationsImpl(context).withField("spec.clusterServiceClassRef.name", item != null ? item.getMetadata().getName() : null) + return client.adapt(ServiceCatalogClient.class) + .clusterServicePlans() + .withField("spec.clusterServiceClassRef.name", item != null ? item.getMetadata().getName() : null) .list(); } @@ -68,27 +52,35 @@ public ClusterServicePlanResource usePlan(String externalName) { fields.put("spec.clusterServiceClassRef.name", item.getMetadata().getName()); fields.put("spec.externalName", externalName); - List list = new ClusterServicePlanOperationsImpl(context.withFields(fields)).list().getItems(); + List list = client.adapt(ServiceCatalogClient.class) + .clusterServicePlans() + .withFields(fields) + .list() + .getItems(); if (list.size() != 1) { - throw new IllegalArgumentException("No unique ClusterServicePlan with external name: " + externalName + " found for ClusterServiceBroker: " + item.getSpec().getClusterServiceBrokerName()+" and ClusterServiceClass: " + item.getSpec().getExternalName()+"."); + throw new IllegalArgumentException("No unique ClusterServicePlan with external name: " + externalName + + " found for ClusterServiceBroker: " + item.getSpec().getClusterServiceBrokerName() + + " and ClusterServiceClass: " + item.getSpec().getExternalName() + "."); } - return new ClusterServicePlanOperationsImpl(context); + ClusterServicePlan plan = list.get(0); + return client.adapt(ServiceCatalogClient.class).clusterServicePlans().withItem(plan); } @Override public ServiceInstance instantiate(String instanceName, String plan) { - ClusterServiceClass item = get(); - return new ServiceInstanceOperationsImpl(context) - .create(new ServiceInstanceBuilder() - .withNewMetadata() - .withName(instanceName) - .endMetadata() - .withNewSpec() - .withClusterServiceClassExternalName(item.getMetadata().getName()) - .withClusterServicePlanExternalName(plan) - .endSpec() - .build()); + ClusterServiceClass item = get(); + return client.adapt(ServiceCatalogClient.class) + .serviceInstances() + .create(new ServiceInstanceBuilder() + .withNewMetadata() + .withName(instanceName) + .endMetadata() + .withNewSpec() + .withClusterServiceClassExternalName(item.getMetadata().getName()) + .withClusterServicePlanExternalName(plan) + .endSpec() + .build()); } } diff --git a/extensions/service-catalog/client/src/main/java/io/fabric8/servicecatalog/client/internal/ClusterServicePlanOperationsImpl.java b/extensions/service-catalog/client/src/main/java/io/fabric8/servicecatalog/client/internal/ClusterServicePlanOperationsImpl.java index ba5277a78f2..49c15701c7d 100644 --- a/extensions/service-catalog/client/src/main/java/io/fabric8/servicecatalog/client/internal/ClusterServicePlanOperationsImpl.java +++ b/extensions/service-catalog/client/src/main/java/io/fabric8/servicecatalog/client/internal/ClusterServicePlanOperationsImpl.java @@ -15,40 +15,20 @@ */ 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.ClusterServicePlan; -import io.fabric8.servicecatalog.api.model.ClusterServicePlanList; import io.fabric8.servicecatalog.api.model.ServiceInstance; import io.fabric8.servicecatalog.api.model.ServiceInstanceBuilder; -public class ClusterServicePlanOperationsImpl extends HasMetadataOperation - implements ClusterServicePlanResource { +public class ClusterServicePlanOperationsImpl extends ResourceAdapter implements ClusterServicePlanResource { - public ClusterServicePlanOperationsImpl(ClientContext clientContext) { - this(HasMetadataOperationsImpl.defaultContext(clientContext)); + public ClusterServicePlanOperationsImpl(Resource resource, Client client) { + super(resource, client); } - public ClusterServicePlanOperationsImpl(OperationContext ctx) { - super(ctx.withApiGroupName("servicecatalog.k8s.io").withApiGroupVersion("v1beta1").withPlural("clusterserviceplans"), - ClusterServicePlan.class, ClusterServicePlanList.class); - } - - @Override - public BaseOperation newInstance(OperationContext context) { - return new ClusterServicePlanOperationsImpl(context); - } - - @Override - public boolean isResourceNamespaced() { - return false; - } - - @Override public ServiceInstance instantiate(String... args) { String instanceName; diff --git a/extensions/service-catalog/client/src/main/java/io/fabric8/servicecatalog/client/internal/ServiceBindingOperationsImpl.java b/extensions/service-catalog/client/src/main/java/io/fabric8/servicecatalog/client/internal/ServiceBindingOperationsImpl.java index c81fd41f832..a011897dae8 100644 --- a/extensions/service-catalog/client/src/main/java/io/fabric8/servicecatalog/client/internal/ServiceBindingOperationsImpl.java +++ b/extensions/service-catalog/client/src/main/java/io/fabric8/servicecatalog/client/internal/ServiceBindingOperationsImpl.java @@ -17,41 +17,22 @@ import io.fabric8.kubernetes.api.model.Secret; import io.fabric8.kubernetes.api.model.SecretList; -import io.fabric8.kubernetes.client.ClientContext; -import io.fabric8.kubernetes.client.Handlers; -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.ServiceBinding; -import io.fabric8.servicecatalog.api.model.ServiceBindingList; +public class ServiceBindingOperationsImpl extends ResourceAdapter implements ServiceBindingResource { -public class ServiceBindingOperationsImpl extends HasMetadataOperation implements ServiceBindingResource { - - public ServiceBindingOperationsImpl(ClientContext clientContext) { - this(HasMetadataOperationsImpl.defaultContext(clientContext)); - } - - public ServiceBindingOperationsImpl(OperationContext ctx) { - super(ctx.withApiGroupName("servicecatalog.k8s.io").withApiGroupVersion("v1beta1").withPlural("servicebindings"), - ServiceBinding.class, ServiceBindingList.class); + public ServiceBindingOperationsImpl(Resource resource, Client client) { + super(resource, client); } @Override - public BaseOperation newInstance(OperationContext context) { - return new ServiceBindingOperationsImpl(context); + public Secret getSecret() { + ServiceBinding instance = get(); + return Handlers.getOperation(Secret.class, SecretList.class, context) + .newInstance(context.withItem(null).withName(instance.getSpec().getSecretName())) + .get(); } - - @Override - public boolean isResourceNamespaced() { - return true; - } - - @Override - public Secret getSecret() { - ServiceBinding instance = get(); - return Handlers.getOperation(Secret.class, SecretList.class, context) - .newInstance(context.withItem(null).withName(instance.getSpec().getSecretName())).get(); - } } diff --git a/extensions/service-catalog/client/src/main/java/io/fabric8/servicecatalog/client/internal/ServiceInstanceOperationsImpl.java b/extensions/service-catalog/client/src/main/java/io/fabric8/servicecatalog/client/internal/ServiceInstanceOperationsImpl.java index cdcc51bc478..f94eb3755ef 100644 --- a/extensions/service-catalog/client/src/main/java/io/fabric8/servicecatalog/client/internal/ServiceInstanceOperationsImpl.java +++ b/extensions/service-catalog/client/src/main/java/io/fabric8/servicecatalog/client/internal/ServiceInstanceOperationsImpl.java @@ -15,35 +15,17 @@ */ 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.ServiceBinding; import io.fabric8.servicecatalog.api.model.ServiceBindingBuilder; import io.fabric8.servicecatalog.api.model.ServiceInstance; -import io.fabric8.servicecatalog.api.model.ServiceInstanceList; -public class ServiceInstanceOperationsImpl extends HasMetadataOperation implements ServiceInstanceResource { +public class ServiceInstanceOperationsImpl extends ResourceAdapter implements ServiceInstanceResource { - public ServiceInstanceOperationsImpl(ClientContext clientContext) { - this(HasMetadataOperationsImpl.defaultContext(clientContext)); - } - - public ServiceInstanceOperationsImpl(OperationContext ctx) { - super(ctx.withApiGroupName("servicecatalog.k8s.io").withApiGroupVersion("v1beta1").withPlural("serviceinstances"), - ServiceInstance.class, ServiceInstanceList.class); - } - - @Override - public BaseOperation newInstance(OperationContext context) { - return new ServiceInstanceOperationsImpl(context); - } - - @Override - public boolean isResourceNamespaced() { - return true; + public ServiceInstanceOperationsImpl(Resource resource, Client client) { + super(resource, client); } @Override diff --git a/kubernetes-client-api/src/main/java/io/fabric8/kubernetes/client/Adapters.java b/kubernetes-client-api/src/main/java/io/fabric8/kubernetes/client/Adapters.java index d37659c82ce..65f39115245 100644 --- a/kubernetes-client-api/src/main/java/io/fabric8/kubernetes/client/Adapters.java +++ b/kubernetes-client-api/src/main/java/io/fabric8/kubernetes/client/Adapters.java @@ -34,6 +34,7 @@ public final class Adapters { private static final Set CLASS_LOADERS = new HashSet<>(); private static final Map EXTENSION_ADAPTER_MAP = new HashMap<>(); + private static volatile ExtensionAdapter.HandlerFactory HANDLER_FACTORY; static { //Register adapters @@ -46,17 +47,21 @@ private Adapters() { public static void register(ExtensionAdapter adapter) { EXTENSION_ADAPTER_MAP.put(adapter.getExtensionType(), adapter); + if (HANDLER_FACTORY != null) { + adapter.registerHandlers(HANDLER_FACTORY); + } } public static void unregister(ExtensionAdapter adapter) { EXTENSION_ADAPTER_MAP.remove(adapter.getExtensionType()); + // TODO: remove handlers } public static ExtensionAdapter get(Class type) { if (EXTENSION_ADAPTER_MAP.containsKey(type)) { return EXTENSION_ADAPTER_MAP.get(type); } else { - + // TODO: should handlers be registered as a side effect try { for (ExtensionAdapter adapter : ServiceLoader.load(ExtensionAdapter.class, ExtensionAdapter.class.getClassLoader())) { if (adapter.getExtensionType().equals(type)) { @@ -107,4 +112,9 @@ private static void discoverServices(ClassLoader classLoader) { } } } + + public static void initializeHandlers(ExtensionAdapter.HandlerFactory handlerFactory) { + HANDLER_FACTORY = handlerFactory; + EXTENSION_ADAPTER_MAP.values().forEach(ea -> ea.registerHandlers(handlerFactory)); + } } diff --git a/kubernetes-client-api/src/main/java/io/fabric8/kubernetes/client/ExtensionAdapter.java b/kubernetes-client-api/src/main/java/io/fabric8/kubernetes/client/ExtensionAdapter.java index 7c8f99af690..4252336ace7 100644 --- a/kubernetes-client-api/src/main/java/io/fabric8/kubernetes/client/ExtensionAdapter.java +++ b/kubernetes-client-api/src/main/java/io/fabric8/kubernetes/client/ExtensionAdapter.java @@ -16,14 +16,22 @@ package io.fabric8.kubernetes.client; +import io.fabric8.kubernetes.api.model.HasMetadata; +import io.fabric8.kubernetes.client.dsl.Resource; + +import java.util.function.BiFunction; /** - * An Adapter that can be used to adapt an instance of the {@link NamespacedKubernetesClient} . + * An Adapter that can be used to adapt an instance of the {@link Client} . * * @param The Client. */ public interface ExtensionAdapter { + public interface HandlerFactory { + > void register(Class type, BiFunction, Client, R> target); + } + /** * @return The concrete class of the {@link io.fabric8.kubernetes.client.Client}. */ @@ -32,7 +40,7 @@ public interface ExtensionAdapter { /** * Checks if it is possible to adapt. * It checks that the requirements of the target client are meet. (e.g. checks that openshift is available). - * @param client The instance of {@link NamespacedKubernetesClient} to adapt. + * @param client The instance of {@link Client} to adapt. * @return boolean value indicating whether client is adaptable or not. */ Boolean isAdaptable(Client client); @@ -40,8 +48,10 @@ public interface ExtensionAdapter { /** * The adapt function. * - * @param client The instance of {@link NamespacedKubernetesClient} to adapt. + * @param client The instance of {@link Client} to adapt. * @return The instance of the {@link io.fabric8.kubernetes.client.Client}. */ C adapt(Client client); + + default void registerHandlers(HandlerFactory factory) {} } diff --git a/kubernetes-client-api/src/main/java/io/fabric8/kubernetes/client/extension/ClientAdapter.java b/kubernetes-client-api/src/main/java/io/fabric8/kubernetes/client/extension/ClientAdapter.java new file mode 100644 index 00000000000..c98df948e48 --- /dev/null +++ b/kubernetes-client-api/src/main/java/io/fabric8/kubernetes/client/extension/ClientAdapter.java @@ -0,0 +1,110 @@ +package io.fabric8.kubernetes.client.extension; + +import io.fabric8.kubernetes.api.model.APIGroup; +import io.fabric8.kubernetes.api.model.APIGroupList; +import io.fabric8.kubernetes.api.model.APIResourceList; +import io.fabric8.kubernetes.api.model.HasMetadata; +import io.fabric8.kubernetes.api.model.KubernetesResourceList; +import io.fabric8.kubernetes.api.model.RootPaths; +import io.fabric8.kubernetes.client.Client; +import io.fabric8.kubernetes.client.Config; +import io.fabric8.kubernetes.client.NamespacedKubernetesClient; +import io.fabric8.kubernetes.client.dsl.MixedOperation; +import io.fabric8.kubernetes.client.dsl.Resource; +import io.fabric8.kubernetes.client.http.HttpClient; + +import java.net.URL; +import java.util.function.Function; + +public abstract class ClientAdapter implements Client { + + protected Client client; + private Function newInstance; + + public ClientAdapter(Client client, Function newInstance) { + this.client = client; + this.newInstance = newInstance; + } + + @Override + public HttpClient getHttpClient() { + return client.getHttpClient(); + } + + @Override + public Config getConfiguration() { + return client.getConfiguration(); + } + + @Override + public Boolean isAdaptable(Class type) { + return client.isAdaptable(type); + } + + @Override + public C adapt(Class type) { + return client.adapt(type); + } + + @Override + public URL getMasterUrl() { + return client.getMasterUrl(); + } + + @Override + public String getApiVersion() { + return client.getApiVersion(); + } + + @Override + public String getNamespace() { + return client.getNamespace(); + } + + @Override + public RootPaths rootPaths() { + return client.rootPaths(); + } + + @Override + public boolean supportsApiPath(String path) { + return client.supportsApiPath(path); + } + + @Override + public void close() { + client.close(); + } + + @Override + public APIGroupList getApiGroups() { + return client.getApiGroups(); + } + + @Override + public APIGroup getApiGroup(String name) { + return client.getApiGroup(name); + } + + @Override + public APIResourceList getApiResources(String groupVersion) { + return client.getApiResources(groupVersion); + } + + public , R extends Resource> MixedOperation resources( + Class resourceType, Class listClass, Class resourceClass) { + } + + public > MixedOperation> resources( + Class resourceType, Class listClass) { + } + + public T inAnyNamespace() { + return inNamespace(null); + } + + public T inNamespace(String namespace) { + return newInstance.apply(client.adapt(NamespacedKubernetesClient.class).inNamespace(namespace)); + } + +} diff --git a/kubernetes-client-api/src/main/java/io/fabric8/kubernetes/client/extension/ResourceAdapter.java b/kubernetes-client-api/src/main/java/io/fabric8/kubernetes/client/extension/ResourceAdapter.java new file mode 100644 index 00000000000..2a5cf3f50a6 --- /dev/null +++ b/kubernetes-client-api/src/main/java/io/fabric8/kubernetes/client/extension/ResourceAdapter.java @@ -0,0 +1,247 @@ +package io.fabric8.kubernetes.client.extension; + +import io.fabric8.kubernetes.api.builder.Visitor; +import io.fabric8.kubernetes.api.model.DeletionPropagation; +import io.fabric8.kubernetes.api.model.ListOptions; +import io.fabric8.kubernetes.client.Client; +import io.fabric8.kubernetes.client.ResourceNotFoundException; +import io.fabric8.kubernetes.client.Watch; +import io.fabric8.kubernetes.client.Watcher; +import io.fabric8.kubernetes.client.dsl.Deletable; +import io.fabric8.kubernetes.client.dsl.EditReplacePatchDeletable; +import io.fabric8.kubernetes.client.dsl.Gettable; +import io.fabric8.kubernetes.client.dsl.Informable; +import io.fabric8.kubernetes.client.dsl.ReplaceDeletable; +import io.fabric8.kubernetes.client.dsl.Resource; +import io.fabric8.kubernetes.client.dsl.Waitable; +import io.fabric8.kubernetes.client.dsl.WatchAndWaitable; +import io.fabric8.kubernetes.client.dsl.WritableOperation; +import io.fabric8.kubernetes.client.dsl.base.PatchContext; +import io.fabric8.kubernetes.client.informers.ResourceEventHandler; +import io.fabric8.kubernetes.client.informers.SharedIndexInformer; + +import java.util.List; +import java.util.Map; +import java.util.concurrent.CompletableFuture; +import java.util.concurrent.TimeUnit; +import java.util.function.Consumer; +import java.util.function.Function; +import java.util.function.Predicate; +import java.util.function.UnaryOperator; + +public class ResourceAdapter implements Resource { + + protected Resource resource; + protected Client client; + + public ResourceAdapter(Resource resource, Client client) { + this.resource = resource; + this.client = client; + } + + @Override + public Boolean delete() { + return resource.delete(); + } + + @Override + public boolean isReady() { + return resource.isReady(); + } + + @Override + public T get() { + return resource.get(); + } + + @Override + public ReplaceDeletable lockResourceVersion(String resourceVersion) { + return resource.lockResourceVersion(resourceVersion); + } + + @Override + public EditReplacePatchDeletable cascading(boolean enabled) { + return resource.cascading(enabled); + } + + @Override + public WritableOperation dryRun() { + return resource.dryRun(); + } + + @Override + public T replace(T item) { + return resource.replace(item); + } + + @Override + public T updateStatus(T item) { + return resource.updateStatus(item); + } + + @Override + public WatchAndWaitable withResourceVersion(String resourceVersion) { + return resource.withResourceVersion(resourceVersion); + } + + @Override + public Gettable fromServer() { + return resource.fromServer(); + } + + @Override + public T replaceStatus(T item) { + return resource.replaceStatus(item); + } + + @Override + public T create(T... item) { + return resource.create(item); + } + + @Override + public Deletable withGracePeriod(long gracePeriodSeconds) { + return resource.withGracePeriod(gracePeriodSeconds); + } + + @Override + public T createOrReplace(T... item) { + return resource.createOrReplace(item); + } + + @Override + public T create(T item) { + return resource.create(item); + } + + @Override + public T editStatus(UnaryOperator function) { + return resource.editStatus(function); + } + + @Override + public T require() throws ResourceNotFoundException { + return resource.require(); + } + + @Override + public EditReplacePatchDeletable withPropagationPolicy(DeletionPropagation propagationPolicy) { + return resource.withPropagationPolicy(propagationPolicy); + } + + @Override + public Watch watch(Watcher watcher) { + return resource.watch(watcher); + } + + @Override + public T patch(T item) { + return resource.patch(item); + } + + @Override + public T patchStatus(T item) { + return resource.patchStatus(item); + } + + @Override + public T edit(UnaryOperator function) { + return resource.edit(function); + } + + @Override + public T waitUntilReady(long amount, TimeUnit timeUnit) { + return resource.waitUntilReady(amount, timeUnit); + } + + @Override + public Watch watch(ListOptions options, Watcher watcher) { + return resource.watch(options, watcher); + } + + @Override + public T waitUntilCondition(Predicate condition, long amount, TimeUnit timeUnit) { + return resource.waitUntilCondition(condition, amount, timeUnit); + } + + @Override + public Waitable withWaitRetryBackoff(long initialBackoff, TimeUnit backoffUnit, double backoffMultiplier) { + return resource.withWaitRetryBackoff(initialBackoff, backoffUnit, backoffMultiplier); + } + + @Override + public Informable withIndexers(Map>> indexers) { + return resource.withIndexers(indexers); + } + + @Override + public WritableOperation dryRun(boolean isDryRun) { + return resource.dryRun(isDryRun); + } + + @Override + public T edit(Visitor... visitors) { + return resource.edit(visitors); + } + + @Override + public T patch(PatchContext patchContext, T item) { + return resource.patch(patchContext, item); + } + + @Override + public Informable withLimit(Long limit) { + return resource.withLimit(limit); + } + + @Override + public T edit(Class visitorType, Visitor visitor) { + return resource.edit(visitorType, visitor); + } + + @Override + public Watch watch(String resourceVersion, Watcher watcher) { + return resource.watch(resourceVersion, watcher); + } + + @Override + public T accept(Consumer function) { + return resource.accept(function); + } + + @Override + public SharedIndexInformer inform() { + return resource.inform(); + } + + @Override + public T patch(String patch) { + return resource.patch(patch); + } + + @Override + public T patch(PatchContext patchContext, String patch) { + return resource.patch(patchContext, patch); + } + + @Override + public SharedIndexInformer inform(ResourceEventHandler handler) { + return resource.inform(handler); + } + + @Override + public SharedIndexInformer inform(ResourceEventHandler handler, long resync) { + return resource.inform(handler, resync); + } + + @Override + public SharedIndexInformer runnableInformer(long resync) { + return resource.runnableInformer(resync); + } + + @Override + public CompletableFuture> informOnCondition(Predicate> condition) { + return resource.informOnCondition(condition); + } + +} diff --git a/kubernetes-client/src/main/java/io/fabric8/kubernetes/client/BaseKubernetesClient.java b/kubernetes-client/src/main/java/io/fabric8/kubernetes/client/BaseKubernetesClient.java index e979a37f26f..0ebb8d76c27 100644 --- a/kubernetes-client/src/main/java/io/fabric8/kubernetes/client/BaseKubernetesClient.java +++ b/kubernetes-client/src/main/java/io/fabric8/kubernetes/client/BaseKubernetesClient.java @@ -144,8 +144,40 @@ public abstract class BaseKubernetesClient extends BaseClient Handlers.register(ReplicationController.class, ReplicationControllerOperationsImpl::new); Handlers.register(StatefulSet.class, StatefulSetOperationsImpl::new); Handlers.register(io.fabric8.kubernetes.api.model.certificates.v1.CertificateSigningRequest.class, CertificateSigningRequestOperationsImpl::new); - // trigger a load of the other client handlers - Adapters.list(Client.class); + // trigger a load of the other client handlers and make KubernetesClient and DefaultKubernetesClietn + Adapters.initializeHandlers(ResourcedHasMetadataOperation::register); + Adapters.register(new ExtensionAdapter () { + @Override + public KubernetesClient adapt(Client client) { + return new DefaultKubernetesClient(client); + } + + @Override + public Class getExtensionType() { + return KubernetesClient.class; + } + + @Override + public Boolean isAdaptable(Client client) { + return true; + } + }); + Adapters.register(new ExtensionAdapter () { + @Override + public NamespacedKubernetesClient adapt(Client client) { + return new DefaultKubernetesClient(client); + } + + @Override + public Class getExtensionType() { + return NamespacedKubernetesClient.class; + } + + @Override + public Boolean isAdaptable(Client client) { + return true; + } + }); } protected BaseKubernetesClient() { diff --git a/kubernetes-client/src/main/java/io/fabric8/kubernetes/client/ResourcedHasMetadataOperation.java b/kubernetes-client/src/main/java/io/fabric8/kubernetes/client/ResourcedHasMetadataOperation.java new file mode 100644 index 00000000000..a8e66f9a9fe --- /dev/null +++ b/kubernetes-client/src/main/java/io/fabric8/kubernetes/client/ResourcedHasMetadataOperation.java @@ -0,0 +1,42 @@ +package io.fabric8.kubernetes.client; + +import io.fabric8.kubernetes.api.model.HasMetadata; +import io.fabric8.kubernetes.api.model.KubernetesResourceList; +import io.fabric8.kubernetes.client.dsl.Resource; +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.utils.KubernetesResourceUtil; + +import java.util.function.BiFunction; + +class ResourcedHasMetadataOperation, R extends Resource> + extends HasMetadataOperation { + + private BiFunction, Client, R> adapter; + + public ResourcedHasMetadataOperation(OperationContext ctx, Class type, Class listType, BiFunction, Client, R> adapter) { + super(ctx, type, listType); + this.adapter = adapter; + } + + @Override + protected R newResource(OperationContext context) { + return adapter.apply(super.newInstance(context), new BaseClient(context)); + } + + @Override + public BaseOperation newInstance(OperationContext context) { + return new ResourcedHasMetadataOperation<>(context, type, listType, adapter); + } + + public static , R extends Resource> void register( + Class type, BiFunction, Client, R> target) { + Handlers.register(type, (c) -> { + return new ResourcedHasMetadataOperation<>(HasMetadataOperationsImpl.defaultContext(c), type, + KubernetesResourceUtil.inferListType(type), target); + }); + } + +} diff --git a/kubernetes-client/src/main/java/io/fabric8/kubernetes/client/dsl/base/BaseOperation.java b/kubernetes-client/src/main/java/io/fabric8/kubernetes/client/dsl/base/BaseOperation.java index 42d4e81a089..b67d51c2eb4 100755 --- a/kubernetes-client/src/main/java/io/fabric8/kubernetes/client/dsl/base/BaseOperation.java +++ b/kubernetes-client/src/main/java/io/fabric8/kubernetes/client/dsl/base/BaseOperation.java @@ -120,6 +120,10 @@ protected BaseOperation(OperationContext ctx) { public BaseOperation newInstance(OperationContext context) { return new BaseOperation<>(context); } + + protected R newResource(OperationContext context) { + return (R) newInstance(context); + } /** * Helper method for list() and list(limit, continue) methods @@ -231,7 +235,7 @@ public R withName(String name) { if (name == null || name.length() == 0) { throw new IllegalArgumentException("Name must be provided."); } - return (R) newInstance(context.withName(name)); + return newResource(context.withName(name)); } @Override @@ -258,7 +262,7 @@ public EditReplacePatchDeletable cascading(boolean cascading) { @Override public R load(InputStream is) { - return (R) newInstance(context.withItem(unmarshal(is, type))); + return newResource(context.withItem(unmarshal(is, type))); } @Override diff --git a/kubernetes-client/src/main/java/io/fabric8/kubernetes/client/dsl/base/HasMetadataOperation.java b/kubernetes-client/src/main/java/io/fabric8/kubernetes/client/dsl/base/HasMetadataOperation.java index f61620a44c2..36cb1eae1a2 100644 --- a/kubernetes-client/src/main/java/io/fabric8/kubernetes/client/dsl/base/HasMetadataOperation.java +++ b/kubernetes-client/src/main/java/io/fabric8/kubernetes/client/dsl/base/HasMetadataOperation.java @@ -275,4 +275,5 @@ public T patch(PatchContext patchContext, String patch) { public BaseOperation newInstance(OperationContext context) { return new HasMetadataOperation<>(context, type, listType); } + }