Search in sources :

Example 11 with CustomResourceDefinition

use of io.fabric8.kubernetes.api.model.apiextensions.v1beta1.CustomResourceDefinition in project entando-k8s-controller-coordinator by entando-k8s.

the class PodManagementTests method prepareCrds.

@BeforeEach
void prepareCrds() throws IOException {
    System.setProperty(ControllerCoordinatorProperty.ENTANDO_STORE_LOG_ENTRIES.getJvmSystemProperty(), "true");
    System.clearProperty(EntandoOperatorConfigProperty.ENTANDO_NAMESPACES_TO_OBSERVE.getJvmSystemProperty());
    System.clearProperty(ControllerCoordinatorProperty.ENTANDO_K8S_OPERATOR_VERSION.getJvmSystemProperty());
    System.clearProperty(ControllerCoordinatorProperty.ENTANDO_K8S_OPERATOR_VERSION_TO_REPLACE.getJvmSystemProperty());
    final CustomResourceDefinition testResourceDefinition = objectMapper.readValue(Thread.currentThread().getContextClassLoader().getResource("testresources.test.org.crd.yaml"), CustomResourceDefinition.class);
    clientDouble.getCluster().putCustomResourceDefinition(new CustomResourceDefinitionBuilder(testResourceDefinition).editMetadata().addToLabels(LabelNames.CRD_OF_INTEREST.getName(), "TestResource").addToAnnotations(AnnotationNames.CONTROLLER_IMAGE.getName(), "test/my-controller").addToAnnotations(AnnotationNames.SUPPORTED_CAPABILITIES.getName(), "dbms").endMetadata().build());
    LogInterceptor.listenToClass(EntandoResourceObserver.class);
}
Also used : CustomResourceDefinitionBuilder(io.fabric8.kubernetes.api.model.apiextensions.v1beta1.CustomResourceDefinitionBuilder) CustomResourceDefinition(io.fabric8.kubernetes.api.model.apiextensions.v1beta1.CustomResourceDefinition) BeforeEach(org.junit.jupiter.api.BeforeEach)

Example 12 with CustomResourceDefinition

use of io.fabric8.kubernetes.api.model.apiextensions.v1beta1.CustomResourceDefinition in project entando-k8s-controller-coordinator by entando-k8s.

the class CrdNameMapSync method syncName.

private void syncName(CustomResourceDefinition r) {
    String key = CoordinatorUtils.keyOf(r);
    crdNameMap = new ConfigMapBuilder(crdNameMap).addToData(key, r.getMetadata().getName()).build();
}
Also used : ConfigMapBuilder(io.fabric8.kubernetes.api.model.ConfigMapBuilder)

Example 13 with CustomResourceDefinition

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

the class CRGeneratorRunner method generate.

public List<WritableCRCompilationUnit> generate(CustomResourceDefinition crd, String basePackageName) {
    CustomResourceDefinitionSpec crSpec = crd.getSpec();
    String crName = crSpec.getNames().getKind();
    String group = crSpec.getGroup();
    List<WritableCRCompilationUnit> writableCUs = new ArrayList<>(crSpec.getVersions().size());
    for (CustomResourceDefinitionVersion crdv : crSpec.getVersions()) {
        String version = crdv.getName();
        String pkg = Optional.ofNullable(basePackageName).map(p -> p + "." + version).orElse(version);
        AbstractJSONSchema2Pojo specGenerator = null;
        String prefix = crName;
        if (config.getPrefixStrategy() == Config.Prefix.NEVER) {
            prefix = "";
        }
        JSONSchemaProps spec = crdv.getSchema().getOpenAPIV3Schema().getProperties().get("spec");
        if (spec != null) {
            String suffix = (config.getSuffixStrategy() != Config.Suffix.NEVER) ? "Spec" : "";
            specGenerator = AbstractJSONSchema2Pojo.fromJsonSchema("spec", spec, pkg, prefix, suffix, config);
        }
        AbstractJSONSchema2Pojo statusGenerator = null;
        JSONSchemaProps status = crdv.getSchema().getOpenAPIV3Schema().getProperties().get("status");
        if (status != null) {
            String suffix = (config.getSuffixStrategy() != Config.Suffix.NEVER) ? "Status" : "";
            statusGenerator = AbstractJSONSchema2Pojo.fromJsonSchema("status", status, pkg, prefix, suffix, config);
        }
        AbstractJSONSchema2Pojo crGenerator = new JCRObject(pkg, crName, group, version, prefix + "Spec", prefix + "Status", specGenerator != null, statusGenerator != null, crdv.getStorage(), crdv.getServed(), config);
        List<GeneratorResult.ClassResult> classResults = validateAndAggregate(crGenerator, specGenerator, statusGenerator);
        writableCUs.add(new WritableCRCompilationUnit(classResults));
    }
    return writableCUs;
}
Also used : java.util(java.util) Logger(org.slf4j.Logger) AbstractJSONSchema2Pojo(io.fabric8.java.generator.nodes.AbstractJSONSchema2Pojo) Files(java.nio.file.Files) CustomResourceDefinitionSpec(io.fabric8.kubernetes.api.model.apiextensions.v1.CustomResourceDefinitionSpec) LoggerFactory(org.slf4j.LoggerFactory) IOException(java.io.IOException) FileInputStream(java.io.FileInputStream) HasMetadata(io.fabric8.kubernetes.api.model.HasMetadata) Collectors(java.util.stream.Collectors) GeneratorResult(io.fabric8.java.generator.nodes.GeneratorResult) File(java.io.File) FileNotFoundException(java.io.FileNotFoundException) Stream(java.util.stream.Stream) CustomResourceDefinitionVersion(io.fabric8.kubernetes.api.model.apiextensions.v1.CustomResourceDefinitionVersion) Serialization(io.fabric8.kubernetes.client.utils.Serialization) FileVisitOption(java.nio.file.FileVisitOption) JavaGeneratorException(io.fabric8.java.generator.exceptions.JavaGeneratorException) CustomResourceDefinition(io.fabric8.kubernetes.api.model.apiextensions.v1.CustomResourceDefinition) JCRObject(io.fabric8.java.generator.nodes.JCRObject) JSONSchemaProps(io.fabric8.kubernetes.api.model.apiextensions.v1.JSONSchemaProps) Path(java.nio.file.Path) JCRObject(io.fabric8.java.generator.nodes.JCRObject) AbstractJSONSchema2Pojo(io.fabric8.java.generator.nodes.AbstractJSONSchema2Pojo) JSONSchemaProps(io.fabric8.kubernetes.api.model.apiextensions.v1.JSONSchemaProps) CustomResourceDefinitionVersion(io.fabric8.kubernetes.api.model.apiextensions.v1.CustomResourceDefinitionVersion) CustomResourceDefinitionSpec(io.fabric8.kubernetes.api.model.apiextensions.v1.CustomResourceDefinitionSpec)

Example 14 with CustomResourceDefinition

use of io.fabric8.kubernetes.api.model.apiextensions.v1beta1.CustomResourceDefinition 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 15 with CustomResourceDefinition

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

the class CustomResourceTest method testListWithFields.

@Test
void testListWithFields() {
    final CustomResourceDefinitionList customResourceDefinitionList = new CustomResourceDefinitionListBuilder().addNewItem().and().addNewItem().and().build();
    server.expect().get().withPath("/apis/apiextensions.k8s.io/v1beta1/customresourcedefinitions?fieldSelector=" + Utils.toUrlEncoded("key1=value1,key2=value2,key3!=value3,key3!=value4")).andReturn(HttpURLConnection.HTTP_CREATED, customResourceDefinitionList).once();
    CustomResourceDefinitionList list = client.apiextensions().v1beta1().customResourceDefinitions().withField("key1", "value1").withField("key2", "value2").withoutField("key3", "value3").withoutField("key3", "value4").list();
    List<CustomResourceDefinition> items = list.getItems();
    assertNotNull(items);
    assertEquals(2, items.size());
}
Also used : CustomResourceDefinitionList(io.fabric8.kubernetes.api.model.apiextensions.v1beta1.CustomResourceDefinitionList) CustomResourceDefinitionListBuilder(io.fabric8.kubernetes.api.model.apiextensions.v1beta1.CustomResourceDefinitionListBuilder) CustomResourceDefinition(io.fabric8.kubernetes.api.model.apiextensions.v1beta1.CustomResourceDefinition) Test(org.junit.jupiter.api.Test)

Aggregations

CustomResourceDefinition (io.fabric8.kubernetes.api.model.apiextensions.v1.CustomResourceDefinition)60 Test (org.junit.jupiter.api.Test)49 CustomResourceDefinition (io.fabric8.kubernetes.api.model.apiextensions.v1beta1.CustomResourceDefinition)30 Description (io.qameta.allure.Description)12 KubernetesClient (io.fabric8.kubernetes.client.KubernetesClient)11 IOException (java.io.IOException)11 ObjectMetaBuilder (io.fabric8.kubernetes.api.model.ObjectMetaBuilder)10 CustomResourceDefinitionBuilder (io.fabric8.kubernetes.api.model.apiextensions.v1beta1.CustomResourceDefinitionBuilder)10 Resource (io.fabric8.kubernetes.client.dsl.Resource)10 CustomResourceDefinitionContext (io.fabric8.kubernetes.client.dsl.base.CustomResourceDefinitionContext)10 ArrayList (java.util.ArrayList)10 BeforeEach (org.junit.jupiter.api.BeforeEach)10 CustomResource (io.fabric8.kubernetes.client.CustomResource)9 ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)7 YAMLFactory (com.fasterxml.jackson.dataformat.yaml.YAMLFactory)7 StartupEvent (io.quarkus.runtime.StartupEvent)7 ConfigMap (io.fabric8.kubernetes.api.model.ConfigMap)6 ConfigMapBuilder (io.fabric8.kubernetes.api.model.ConfigMapBuilder)6 HasMetadata (io.fabric8.kubernetes.api.model.HasMetadata)6 ServiceAccount (io.fabric8.kubernetes.api.model.ServiceAccount)6