Search in sources :

Example 1 with ResourceDefinitionContext

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;
}
Also used : ResourceDefinitionContext(io.fabric8.kubernetes.client.dsl.base.ResourceDefinitionContext)

Example 2 with ResourceDefinitionContext

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);
}
Also used : GenericKubernetesResource(io.fabric8.kubernetes.api.model.GenericKubernetesResource)

Example 3 with ResourceDefinitionContext

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));
}
Also used : ResourceDefinitionContext(io.fabric8.kubernetes.client.dsl.base.ResourceDefinitionContext)

Example 4 with ResourceDefinitionContext

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);
    }
}
Also used : ObjectMeta(io.fabric8.kubernetes.api.model.ObjectMeta) KubernetesClient(io.fabric8.kubernetes.client.KubernetesClient) CustomResourceDefinition(io.fabric8.kubernetes.api.model.apiextensions.v1beta1.CustomResourceDefinition) GenericKubernetesResource(io.fabric8.kubernetes.api.model.GenericKubernetesResource) Resource(io.fabric8.kubernetes.client.dsl.Resource) CountDownLatch(java.util.concurrent.CountDownLatch) GenericKubernetesResource(io.fabric8.kubernetes.api.model.GenericKubernetesResource) WatcherException(io.fabric8.kubernetes.client.WatcherException) CustomResourceDefinitionContext(io.fabric8.kubernetes.client.dsl.base.CustomResourceDefinitionContext) ResourceDefinitionContext(io.fabric8.kubernetes.client.dsl.base.ResourceDefinitionContext) KubernetesClientBuilder(io.fabric8.kubernetes.client.KubernetesClientBuilder) GenericKubernetesResourceList(io.fabric8.kubernetes.api.model.GenericKubernetesResourceList) ConfigBuilder(io.fabric8.kubernetes.client.ConfigBuilder) KubernetesClientException(io.fabric8.kubernetes.client.KubernetesClientException)

Example 5 with ResourceDefinitionContext

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();
    }
}
Also used : KubernetesClient(io.fabric8.kubernetes.client.KubernetesClient) ResourceDefinitionContext(io.fabric8.kubernetes.client.dsl.base.ResourceDefinitionContext) KubernetesClientBuilder(io.fabric8.kubernetes.client.KubernetesClientBuilder) KubernetesClientBuilder(io.fabric8.kubernetes.client.KubernetesClientBuilder) GenericKubernetesResource(io.fabric8.kubernetes.api.model.GenericKubernetesResource)

Aggregations

ResourceDefinitionContext (io.fabric8.kubernetes.client.dsl.base.ResourceDefinitionContext)9 GenericKubernetesResource (io.fabric8.kubernetes.api.model.GenericKubernetesResource)7 GenericKubernetesResourceList (io.fabric8.kubernetes.api.model.GenericKubernetesResourceList)2 HasMetadata (io.fabric8.kubernetes.api.model.HasMetadata)2 KubernetesClient (io.fabric8.kubernetes.client.KubernetesClient)2 KubernetesClientBuilder (io.fabric8.kubernetes.client.KubernetesClientBuilder)2 KubernetesClientException (io.fabric8.kubernetes.client.KubernetesClientException)2 Resource (io.fabric8.kubernetes.client.dsl.Resource)2 CustomResourceDefinitionContext (io.fabric8.kubernetes.client.dsl.base.CustomResourceDefinitionContext)2 CountDownLatch (java.util.concurrent.CountDownLatch)2 DisplayName (org.junit.jupiter.api.DisplayName)2 VisitableBuilder (io.fabric8.kubernetes.api.builder.VisitableBuilder)1 APIResourceList (io.fabric8.kubernetes.api.model.APIResourceList)1 KubernetesList (io.fabric8.kubernetes.api.model.KubernetesList)1 KubernetesResource (io.fabric8.kubernetes.api.model.KubernetesResource)1 KubernetesResourceList (io.fabric8.kubernetes.api.model.KubernetesResourceList)1 ObjectMeta (io.fabric8.kubernetes.api.model.ObjectMeta)1 WatchEvent (io.fabric8.kubernetes.api.model.WatchEvent)1 CustomResourceDefinition (io.fabric8.kubernetes.api.model.apiextensions.v1beta1.CustomResourceDefinition)1 ConfigBuilder (io.fabric8.kubernetes.client.ConfigBuilder)1