Search in sources :

Example 1 with JSONSchemaPropsBuilder

use of io.fabric8.kubernetes.api.model.apiextensions.v1beta1.JSONSchemaPropsBuilder in project kubernetes-client by fabric8io.

the class CRDExample method main.

/**
 * Example of Cluster and Namespaced scoped K8S Custom Resources.
 * To test Cluster scoped resource use "--cluster" as first argument.
 * To test Namespaced resource provide namespace as first argument (namespace must exists in K8S).
 *
 * @param args Either "--cluster" or namespace name.
 */
public static void main(String[] args) {
    boolean resourceNamespaced = true;
    String namespace = null;
    if (args.length > 0) {
        if ("--cluster".equals(args[0])) {
            resourceNamespaced = false;
        } else {
            namespace = args[0];
        }
    }
    try (final KubernetesClient client = new KubernetesClientBuilder().build()) {
        if (resourceNamespaced) {
            if (namespace == null) {
                namespace = client.getNamespace();
            }
            if (namespace == null) {
                System.err.println("No namespace specified and no default defined!");
                return;
            }
            System.out.println("Using namespace: " + namespace);
        } else {
            System.out.println("Creating cluster scoped resource");
        }
        if (LOG_ROOT_PATHS) {
            RootPaths rootPaths = client.rootPaths();
            if (rootPaths != null) {
                List<String> paths = rootPaths.getPaths();
                if (paths != null) {
                    System.out.println("Supported API Paths:");
                    for (String path : paths) {
                        System.out.println("    " + path);
                    }
                    System.out.println();
                }
            }
        }
        CustomResourceDefinitionList crds = client.apiextensions().v1().customResourceDefinitions().list();
        List<CustomResourceDefinition> crdsItems = crds.getItems();
        System.out.println("Found " + crdsItems.size() + " CRD(s)");
        CustomResourceDefinition dummyCRD = null;
        final String dummyCRDName = CustomResource.getCRDName(Dummy.class);
        for (CustomResourceDefinition crd : crdsItems) {
            ObjectMeta metadata = crd.getMetadata();
            if (metadata != null) {
                String name = metadata.getName();
                System.out.println("    " + name + " => " + metadata.getSelfLink());
                if (dummyCRDName.equals(name)) {
                    dummyCRD = crd;
                }
            }
        }
        if (dummyCRD != null) {
            System.out.println("Found CRD: " + dummyCRD.getMetadata().getSelfLink());
        } else {
            dummyCRD = CustomResourceDefinitionContext.v1CRDFromCustomResourceType(Dummy.class).editSpec().editVersion(0).withNewSchema().withNewOpenAPIV3Schema().withTitle("dummy").withType("object").addToRequired("spec").addToProperties("spec", new JSONSchemaPropsBuilder().withType("object").addToProperties("foo", new JSONSchemaPropsBuilder().withType("string").build()).addToProperties("bar", new JSONSchemaPropsBuilder().withType("string").build()).build()).endOpenAPIV3Schema().endSchema().endVersion().endSpec().build();
            client.apiextensions().v1().customResourceDefinitions().create(dummyCRD);
            System.out.println("Created CRD " + dummyCRD.getMetadata().getName());
        }
        // wait a beat for the endpoints to be ready
        Thread.sleep(5000);
        // lets create a client for the CRD
        NonNamespaceOperation<Dummy, DummyList, Resource<Dummy>> dummyClient = client.resources(Dummy.class, DummyList.class);
        if (resourceNamespaced) {
            dummyClient = ((MixedOperation<Dummy, DummyList, Resource<Dummy>>) dummyClient).inNamespace(namespace);
        }
        CustomResourceList<Dummy> dummyList = dummyClient.list();
        List<Dummy> items = dummyList.getItems();
        System.out.println("  found " + items.size() + " dummies");
        for (Dummy item : items) {
            System.out.println("    " + item);
        }
        Dummy dummy = new Dummy();
        ObjectMeta metadata = new ObjectMeta();
        metadata.setName("foo");
        dummy.setMetadata(metadata);
        DummySpec dummySpec = new DummySpec();
        Date now = new Date();
        dummySpec.setBar("beer: " + now);
        dummySpec.setFoo("cheese: " + now);
        dummy.setSpec(dummySpec);
        Dummy created = dummyClient.createOrReplace(dummy);
        System.out.println("Upserted " + dummy);
        created.getSpec().setBar("otherBar");
        dummyClient.createOrReplace(created);
        System.out.println("Watching for changes to Dummies");
        dummyClient.withResourceVersion(created.getMetadata().getResourceVersion()).watch(new Watcher<Dummy>() {

            @Override
            public void eventReceived(Action action, Dummy resource) {
                System.out.println("==> " + action + " for " + resource);
                if (resource.getSpec() == null) {
                    logger.error("No Spec for resource {}", resource);
                }
            }

            @Override
            public void onClose(WatcherException cause) {
            }
        });
        System.in.read();
    } catch (KubernetesClientException e) {
        logger.error(e.getMessage(), e);
    } catch (Exception e) {
        logger.error(e.getMessage(), e);
    }
}
Also used : DummySpec(io.fabric8.kubernetes.examples.crds.DummySpec) JSONSchemaPropsBuilder(io.fabric8.kubernetes.api.model.apiextensions.v1.JSONSchemaPropsBuilder) KubernetesClientBuilder(io.fabric8.kubernetes.client.KubernetesClientBuilder) ObjectMeta(io.fabric8.kubernetes.api.model.ObjectMeta) KubernetesClient(io.fabric8.kubernetes.client.KubernetesClient) RootPaths(io.fabric8.kubernetes.api.model.RootPaths) CustomResourceDefinition(io.fabric8.kubernetes.api.model.apiextensions.v1.CustomResourceDefinition) Resource(io.fabric8.kubernetes.client.dsl.Resource) CustomResource(io.fabric8.kubernetes.client.CustomResource) DummyList(io.fabric8.kubernetes.examples.crds.DummyList) Date(java.util.Date) KubernetesClientException(io.fabric8.kubernetes.client.KubernetesClientException) WatcherException(io.fabric8.kubernetes.client.WatcherException) WatcherException(io.fabric8.kubernetes.client.WatcherException) CustomResourceDefinitionList(io.fabric8.kubernetes.api.model.apiextensions.v1.CustomResourceDefinitionList) Dummy(io.fabric8.kubernetes.examples.crds.Dummy) KubernetesClientException(io.fabric8.kubernetes.client.KubernetesClientException)

Example 2 with JSONSchemaPropsBuilder

use of io.fabric8.kubernetes.api.model.apiextensions.v1beta1.JSONSchemaPropsBuilder in project kubernetes-client by fabric8io.

the class JsonSchema method newBuilder.

@Override
public JSONSchemaPropsBuilder newBuilder() {
    final JSONSchemaPropsBuilder builder = new JSONSchemaPropsBuilder();
    builder.withType("object");
    return builder;
}
Also used : JSONSchemaPropsBuilder(io.fabric8.kubernetes.api.model.apiextensions.v1beta1.JSONSchemaPropsBuilder)

Example 3 with JSONSchemaPropsBuilder

use of io.fabric8.kubernetes.api.model.apiextensions.v1beta1.JSONSchemaPropsBuilder in project kubernetes-client by fabric8io.

the class JsonSchema method newBuilder.

@Override
public JSONSchemaPropsBuilder newBuilder() {
    final JSONSchemaPropsBuilder builder = new JSONSchemaPropsBuilder();
    builder.withType("object");
    return builder;
}
Also used : JSONSchemaPropsBuilder(io.fabric8.kubernetes.api.model.apiextensions.v1.JSONSchemaPropsBuilder)

Example 4 with JSONSchemaPropsBuilder

use of io.fabric8.kubernetes.api.model.apiextensions.v1beta1.JSONSchemaPropsBuilder in project kubernetes-client by fabric8io.

the class KameletModelTest method shouldCreateKamelet.

@Test
void shouldCreateKamelet() {
    Kamelet kamelet = new KameletBuilder().withNewMetadata().withName("my-kamelet").endMetadata().withNewSpec().withDefinition(new JSONSchemaPropsBuilder().build()).endSpec().build();
    assertNotNull(kamelet);
    assertEquals("my-kamelet", kamelet.getMetadata().getName());
}
Also used : JSONSchemaPropsBuilder(io.fabric8.kubernetes.api.model.apiextensions.v1.JSONSchemaPropsBuilder) KameletBuilder(io.fabric8.camelk.v1alpha1.KameletBuilder) Kamelet(io.fabric8.camelk.v1alpha1.Kamelet) Test(org.junit.jupiter.api.Test)

Example 5 with JSONSchemaPropsBuilder

use of io.fabric8.kubernetes.api.model.apiextensions.v1beta1.JSONSchemaPropsBuilder in project kubernetes-client by fabric8io.

the class CustomResourceV1Example method main.

@SuppressWarnings("java:S106")
public static void main(String... args) {
    try (KubernetesClient kc = new KubernetesClientBuilder().build()) {
        // @formatter:off
        final CustomResourceDefinition crd = CustomResourceDefinitionContext.v1CRDFromCustomResourceType(Show.class).editSpec().editVersion(0).withNewSchema().withNewOpenAPIV3Schema().withTitle("Shows").withType("object").addToRequired("spec").addToProperties("spec", new JSONSchemaPropsBuilder().withType("object").addToProperties("name", new JSONSchemaPropsBuilder().withType("string").build()).addToProperties("score", new JSONSchemaPropsBuilder().withType("number").build()).build()).endOpenAPIV3Schema().endSchema().endVersion().endSpec().build();
        // @formatter:on
        kc.apiextensions().v1().customResourceDefinitions().createOrReplace(crd);
        System.out.println("Created custom shows.example.com Kubernetes API");
        final NonNamespaceOperation<Show, ShowList, Resource<Show>> shows = kc.customResources(Show.class, ShowList.class).inNamespace("default");
        shows.list();
        shows.createOrReplace(new Show("breaking-bad", new ShowSpec("Breaking Bad", 10)));
        shows.createOrReplace(new Show("better-call-saul", new ShowSpec("Better call Saul", 8)));
        shows.createOrReplace(new Show("the-wire", new ShowSpec("The Wire", 10)));
        System.out.println("Added three shows");
        shows.list().getItems().forEach(s -> System.out.printf(" - %s%n", s.getSpec().name));
        final Show theWire = shows.withName("the-wire").fromServer().get();
        System.out.printf("The Wire Score is: %s%n", theWire.getSpec().score);
    }
}
Also used : KubernetesClient(io.fabric8.kubernetes.client.KubernetesClient) JSONSchemaPropsBuilder(io.fabric8.kubernetes.api.model.apiextensions.v1.JSONSchemaPropsBuilder) KubernetesClientBuilder(io.fabric8.kubernetes.client.KubernetesClientBuilder) CustomResourceDefinition(io.fabric8.kubernetes.api.model.apiextensions.v1.CustomResourceDefinition) Resource(io.fabric8.kubernetes.client.dsl.Resource) CustomResource(io.fabric8.kubernetes.client.CustomResource)

Aggregations

JSONSchemaPropsBuilder (io.fabric8.kubernetes.api.model.apiextensions.v1beta1.JSONSchemaPropsBuilder)5 Test (org.junit.jupiter.api.Test)5 ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)4 JSONSchemaPropsBuilder (io.fabric8.kubernetes.api.model.apiextensions.v1.JSONSchemaPropsBuilder)4 JSONSchemaPropsOrBoolBuilder (io.fabric8.kubernetes.api.model.apiextensions.v1beta1.JSONSchemaPropsOrBoolBuilder)4 CustomResourceDefinition (io.fabric8.kubernetes.api.model.apiextensions.v1.CustomResourceDefinition)2 JSONSchemaProps (io.fabric8.kubernetes.api.model.apiextensions.v1beta1.JSONSchemaProps)2 CustomResource (io.fabric8.kubernetes.client.CustomResource)2 KubernetesClient (io.fabric8.kubernetes.client.KubernetesClient)2 KubernetesClientBuilder (io.fabric8.kubernetes.client.KubernetesClientBuilder)2 Resource (io.fabric8.kubernetes.client.dsl.Resource)2 BufferedReader (java.io.BufferedReader)2 InputStream (java.io.InputStream)2 InputStreamReader (java.io.InputStreamReader)2 Kamelet (io.fabric8.camelk.v1alpha1.Kamelet)1 KameletBuilder (io.fabric8.camelk.v1alpha1.KameletBuilder)1 ObjectMeta (io.fabric8.kubernetes.api.model.ObjectMeta)1 RootPaths (io.fabric8.kubernetes.api.model.RootPaths)1 CustomResourceDefinitionList (io.fabric8.kubernetes.api.model.apiextensions.v1.CustomResourceDefinitionList)1 KubernetesClientException (io.fabric8.kubernetes.client.KubernetesClientException)1