Search in sources :

Example 1 with CustomResourceDefinitionSpec

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

use of io.fabric8.kubernetes.api.model.apiextensions.v1beta1.CustomResourceDefinitionSpec in project stackgres by ongres.

the class WebhookConfiguratorImplTest method configureWebhook_shouldNotFail.

@Test
void configureWebhook_shouldNotFail() {
    var definition = crdFinder.scanDefinitions().get(0);
    final String certificate = StringUtil.generateRandom();
    ArgumentCaptor<CustomResourceDefinition> crdCaptor = ArgumentCaptor.forClass(CustomResourceDefinition.class);
    when(crdWriter.update(crdCaptor.capture())).thenReturn(definition);
    webhookConfigurator.configureWebhook(definition.getMetadata().getName(), certificate);
    CustomResourceDefinition crd = crdCaptor.getValue();
    final CustomResourceDefinitionSpec spec = crd.getSpec();
    final CustomResourceConversion conversion = spec.getConversion();
    final WebhookClientConfig clientConfig = conversion.getWebhook().getClientConfig();
    final ServiceReference service = clientConfig.getService();
    assertEquals("Webhook", conversion.getStrategy());
    assertEquals(OPERATOR_NAME, service.getName());
    assertEquals(OPERATOR_NAMESPACE, service.getNamespace());
    assertEquals("/stackgres/conversion/" + definition.getSpec().getNames().getSingular(), service.getPath());
    assertEquals(certificate, clientConfig.getCaBundle());
    assertFalse(spec.getPreserveUnknownFields());
}
Also used : CustomResourceConversion(io.fabric8.kubernetes.api.model.apiextensions.v1.CustomResourceConversion) WebhookClientConfig(io.fabric8.kubernetes.api.model.apiextensions.v1.WebhookClientConfig) CustomResourceDefinition(io.fabric8.kubernetes.api.model.apiextensions.v1.CustomResourceDefinition) CustomResourceDefinitionSpec(io.fabric8.kubernetes.api.model.apiextensions.v1.CustomResourceDefinitionSpec) ServiceReference(io.fabric8.kubernetes.api.model.apiextensions.v1.ServiceReference) Test(org.junit.jupiter.api.Test)

Example 3 with CustomResourceDefinitionSpec

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

the class CustomResourceDefinitionContextTest method v1beta1CRDFromCustomResourceType.

@Test
@DisplayName("v1beta1CRDFromCustomResourceType correctly generates CRD builder for v1beta1 version")
void v1beta1CRDFromCustomResourceType() {
    final CustomResourceDefinition crd = CustomResourceDefinitionContext.v1beta1CRDFromCustomResourceType(Good.class).build();
    final CustomResourceDefinitionSpec spec = crd.getSpec();
    final CustomResourceDefinitionNames names = spec.getNames();
    final String plural = "goods";
    Assertions.assertEquals(plural, names.getPlural());
    Assertions.assertEquals("good", names.getSingular());
    Assertions.assertEquals("Good", names.getKind());
    Assertions.assertEquals(plural + "." + Good.GROUP, crd.getMetadata().getName());
    Assertions.assertEquals(Good.VERSION, spec.getVersion());
    Assertions.assertEquals(Good.VERSION, spec.getVersions().get(0).getName());
}
Also used : CustomResourceDefinition(io.fabric8.kubernetes.api.model.apiextensions.v1beta1.CustomResourceDefinition) Good(io.fabric8.kubernetes.client.Good) CustomResourceDefinitionSpec(io.fabric8.kubernetes.api.model.apiextensions.v1beta1.CustomResourceDefinitionSpec) CustomResourceDefinitionNames(io.fabric8.kubernetes.api.model.apiextensions.v1beta1.CustomResourceDefinitionNames) Test(org.junit.jupiter.api.Test) DisplayName(org.junit.jupiter.api.DisplayName)

Aggregations

CustomResourceDefinition (io.fabric8.kubernetes.api.model.apiextensions.v1.CustomResourceDefinition)2 CustomResourceDefinitionSpec (io.fabric8.kubernetes.api.model.apiextensions.v1.CustomResourceDefinitionSpec)2 Test (org.junit.jupiter.api.Test)2 JavaGeneratorException (io.fabric8.java.generator.exceptions.JavaGeneratorException)1 AbstractJSONSchema2Pojo (io.fabric8.java.generator.nodes.AbstractJSONSchema2Pojo)1 GeneratorResult (io.fabric8.java.generator.nodes.GeneratorResult)1 JCRObject (io.fabric8.java.generator.nodes.JCRObject)1 HasMetadata (io.fabric8.kubernetes.api.model.HasMetadata)1 CustomResourceConversion (io.fabric8.kubernetes.api.model.apiextensions.v1.CustomResourceConversion)1 CustomResourceDefinitionVersion (io.fabric8.kubernetes.api.model.apiextensions.v1.CustomResourceDefinitionVersion)1 JSONSchemaProps (io.fabric8.kubernetes.api.model.apiextensions.v1.JSONSchemaProps)1 ServiceReference (io.fabric8.kubernetes.api.model.apiextensions.v1.ServiceReference)1 WebhookClientConfig (io.fabric8.kubernetes.api.model.apiextensions.v1.WebhookClientConfig)1 CustomResourceDefinition (io.fabric8.kubernetes.api.model.apiextensions.v1beta1.CustomResourceDefinition)1 CustomResourceDefinitionNames (io.fabric8.kubernetes.api.model.apiextensions.v1beta1.CustomResourceDefinitionNames)1 CustomResourceDefinitionSpec (io.fabric8.kubernetes.api.model.apiextensions.v1beta1.CustomResourceDefinitionSpec)1 Good (io.fabric8.kubernetes.client.Good)1 Serialization (io.fabric8.kubernetes.client.utils.Serialization)1 File (java.io.File)1 FileInputStream (java.io.FileInputStream)1