use of io.fabric8.kubernetes.client.dsl.base.ResourceDefinitionContext in project kubernetes-client by fabric8io.
the class Handlers method get.
/**
* Returns a {@link ResourceHandler} for the given item. The client is optional, if it is supplied, then the server can be
* consulted for resource metadata if a generic item is passed in.
*
* @return the handler
* @throws KubernetesClientException if a handler cannot be found
*/
public static <T extends HasMetadata, V extends VisitableBuilder<T, V>> ResourceHandler<T, V> get(T meta, BaseClient client) {
Class<T> type = (Class<T>) meta.getClass();
if (type.equals(GenericKubernetesResource.class)) {
ResourceDefinitionContext rdc = getResourceDefinitionContext((GenericKubernetesResource) meta, client);
if (rdc != null) {
return new ResourceHandlerImpl(GenericKubernetesResource.class, GenericKubernetesResourceList.class, rdc);
}
}
ResourceHandler<T, V> result = get(type);
if (result == null) {
throw new KubernetesClientException("Could not find a registered handler for item: [" + meta + "].");
}
return result;
}
use of io.fabric8.kubernetes.client.dsl.base.ResourceDefinitionContext in project kubernetes-client by fabric8io.
the class Handlers method getResourceDefinitionContext.
public static ResourceDefinitionContext getResourceDefinitionContext(String apiVersion, String kind, BaseClient client) {
GenericKubernetesResource resource = new GenericKubernetesResource();
resource.setKind(kind);
resource.setApiVersion(apiVersion);
return getResourceDefinitionContext(resource, client);
}
use of io.fabric8.kubernetes.client.dsl.base.ResourceDefinitionContext in project kubernetes-client by fabric8io.
the class ResourcedHasMetadataOperation method register.
public static <T extends HasMetadata, R extends ExtensibleResourceAdapter<T>> void register(Class<T> type, Supplier<R> target) {
ResourceDefinitionContext definitionContest = ResourceDefinitionContext.fromResourceType(type);
Class<? extends KubernetesResourceList> listType = KubernetesResourceUtil.inferListType(type);
Handlers.register(type, c -> new ResourcedHasMetadataOperation<>(HasMetadataOperationsImpl.defaultContext(c), definitionContest, type, listType, target));
}
use of io.fabric8.kubernetes.client.dsl.base.ResourceDefinitionContext in project kubernetes-client by fabric8io.
the class GenericKubernetesResourceExample method main.
public static void main(String[] args) throws Exception {
final ConfigBuilder configBuilder = new ConfigBuilder();
configBuilder.withWatchReconnectInterval(500);
configBuilder.withWatchReconnectLimit(5);
try (KubernetesClient client = new KubernetesClientBuilder().withConfig(configBuilder.build()).build()) {
String namespace = "default";
CustomResourceDefinition prometheousRuleCrd = client.apiextensions().v1beta1().customResourceDefinitions().load(GenericKubernetesResourceExample.class.getResourceAsStream("/prometheous-rule-crd.yml")).get();
client.apiextensions().v1beta1().customResourceDefinitions().createOrReplace(prometheousRuleCrd);
logger.info("Successfully created Prometheous custom resource definition");
// Creating Custom Resources Now:
ResourceDefinitionContext crdContext = CustomResourceDefinitionContext.fromCrd(prometheousRuleCrd);
client.load(GenericKubernetesResourceExample.class.getResourceAsStream("/prometheous-rule-cr.yml")).inNamespace(namespace).createOrReplace();
logger.info("Created Custom Resource successfully too");
// Listing all custom resources in given namespace:
NonNamespaceOperation<GenericKubernetesResource, GenericKubernetesResourceList, Resource<GenericKubernetesResource>> resourcesInNamespace = client.genericKubernetesResources(crdContext).inNamespace(namespace);
GenericKubernetesResourceList list = resourcesInNamespace.list();
List<GenericKubernetesResource> items = list.getItems();
logger.info("Custom Resources :- ");
for (GenericKubernetesResource customResource : items) {
ObjectMeta metadata = customResource.getMetadata();
final String name = metadata.getName();
logger.info(name);
}
// Watching custom resources now
logger.info("Watching custom resources now");
final CountDownLatch closeLatch = new CountDownLatch(1);
resourcesInNamespace.watch(new Watcher<GenericKubernetesResource>() {
@Override
public void eventReceived(Action action, GenericKubernetesResource resource) {
logger.info("{}: {}", action, resource);
}
@Override
public void onClose(WatcherException e) {
logger.debug("Watcher onClose");
closeLatch.countDown();
if (e != null) {
logger.error(e.getMessage(), e);
}
}
});
closeLatch.await(10, TimeUnit.MINUTES);
// Cleanup
logger.info("Deleting custom resources...");
resourcesInNamespace.withName("prometheus-example-rules").delete();
client.apiextensions().v1beta1().customResourceDefinitions().withName(prometheousRuleCrd.getMetadata().getName()).delete();
} catch (KubernetesClientException e) {
logger.error("Could not create resource: {}", e.getMessage(), e);
}
}
use of io.fabric8.kubernetes.client.dsl.base.ResourceDefinitionContext in project kubernetes-client by fabric8io.
the class CustomResourceCreateDemoTypeless method main.
public static void main(String[] args) {
try (final KubernetesClient k8s = new KubernetesClientBuilder().build()) {
// Create Custom Resource Context
ResourceDefinitionContext context = new ResourceDefinitionContext.Builder().withGroup("demo.fabric8.io").withKind("Dummy").withPlural("dummies").withNamespaced(true).withVersion("v1").build();
// Load from Yaml
Resource<GenericKubernetesResource> dummyObject = k8s.genericKubernetesResources(context).load(CustomResourceCreateDemoTypeless.class.getResourceAsStream("/test-customresource.yaml"));
// Create Custom Resource
dummyObject.create();
}
}
Aggregations