io.fabric8.kubernetes.api.model.apiextensions.CustomResourceDefinitionBuilder Java Examples

The following examples show how to use io.fabric8.kubernetes.api.model.apiextensions.CustomResourceDefinitionBuilder. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: CustomResourceHandler.java    From dekorate with Apache License 2.0 6 votes vote down vote up
@Override
public void handle(CustomResourceConfig config) {
  TypeDef def = config.getAttribute(Keys.TYPE_DEFINITION);

  resources.add(new CustomResourceDefinitionBuilder()
    .withApiVersion("apiextensions.k8s.io/v1beta1")
    .withNewMetadata()
      .withName(config.getPlural() + "." + config.getGroup())
    .endMetadata()
    .withNewSpec()
    .withScope(config.getScope().name())
    .withGroup(config.getGroup())
    .withVersion(config.getVersion())
    .withNewNames()
      .withKind(config.getKind())
      .withShortNames(config.getShortName())
      .withPlural(config.getPlural())
      .withSingular(config.getKind().toLowerCase())
    .endNames()
    .withNewValidation()
      .withOpenAPIV3Schema(JsonSchema.from(def))
    .endValidation()
    .endSpec()
  .build());
}
 
Example #2
Source File: PropagationPolicyTest.java    From kubernetes-client with Apache License 2.0 6 votes vote down vote up
@Test
@DisplayName("Should delete a Custom Resource with PropagationPolicy=Background")
void testDeleteCustomResource() throws InterruptedException {
  // Given
  server.expect().delete().withPath("/apis/demo.k8s.io/v1alpha1/namespaces/test/podsets/example-podset").andReturn(HttpURLConnection.HTTP_OK, new PodSet()).once();
  MixedOperation<PodSet, PodSetList, DoneablePodSet, Resource<PodSet, DoneablePodSet>> podSetClient = server.getClient().customResources(new CustomResourceDefinitionBuilder()
    .withNewMetadata().withName("podsets.demo.k8s.io").endMetadata()
    .withNewSpec()
    .withGroup("demo.k8s.io")
    .withVersion("v1alpha1")
    .withNewNames().withKind("PodSet").withPlural("podsets").endNames()
    .withScope("Namespaced")
    .endSpec()
    .build(), PodSet.class, PodSetList.class, DoneablePodSet.class);

  // When
  boolean isDeleted = podSetClient.inNamespace("test").withName("example-podset").delete();

  // Then
  assertTrue(isDeleted);
  assertDeleteOptionsInLastRecordedRequest(DeletionPropagation.BACKGROUND.toString(), server.getLastRequest());
}
 
Example #3
Source File: CustomResourceDefinitionTest.java    From kubernetes-client with Apache License 2.0 6 votes vote down vote up
@BeforeEach
void setupCrd() throws IOException {
  customResourceDefinition = new CustomResourceDefinitionBuilder()
    .withApiVersion("apiextensions.k8s.io/v1beta1")
    .withNewMetadata().withName("sparkclusters.radanalytics.io")
    .endMetadata()
    .withNewSpec()
    .withNewNames()
    .withKind("SparkCluster")
    .withPlural("sparkclusters")
    .endNames()
    .withGroup("radanalytics.io")
    .withVersion("v1")
    .withScope("Namespaced")
    .withNewValidation()
    .withNewOpenAPIV3SchemaLike(readSchema())
    .endOpenAPIV3Schema()
    .endValidation()
    .endSpec()
    .build();
}
 
Example #4
Source File: CrdDeployer.java    From abstract-operator with Apache License 2.0 5 votes vote down vote up
private CustomResourceDefinitionFluent.SpecNested<CustomResourceDefinitionBuilder> getCRDBuilder(String prefix,
                                                                                                        String entityName,
                                                                                                        String[] shortNames,
                                                                                                        String pluralName) {
    // if no plural name is specified, try to make one by adding "s"
    // also, plural names must be all lowercase
    String plural = pluralName;
    if (plural.isEmpty()) {
        plural = (entityName + "s");
    }
    plural = plural.toLowerCase();

    // short names must be all lowercase
    String[] shortNamesLower = Arrays.stream(shortNames)
                                     .map(sn -> sn.toLowerCase())
                                     .toArray(String[]::new);

    return new CustomResourceDefinitionBuilder()
            .withApiVersion("apiextensions.k8s.io/v1beta1")
            .withNewMetadata().withName(plural + "." + prefix)
            .endMetadata()
            .withNewSpec()
                .withNewNames()
                .withKind(entityName)
                .withPlural(plural)
                .withShortNames(Arrays.asList(shortNamesLower)).endNames()
            .withGroup(prefix)
            .withVersion("v1")
            .withScope("Namespaced")
            // add an empty status block to all CRDs created
            .withNewSubresources().withStatus(new CustomResourceSubresourceStatus()).endSubresources();
}
 
Example #5
Source File: CustomResources.java    From enmasse with Apache License 2.0 5 votes vote down vote up
public static CustomResourceDefinition createCustomResource(final String group, final String version, final String kind, final String scope) {
    String singular = kind.toLowerCase();
    String listKind = kind + "List";
    String plural = singular + "s";
    if (singular.endsWith("s")) {
        plural = singular + "es";
    } else if (singular.endsWith("y")) {
        plural = singular.substring(0, singular.length() - 1) + "ies";
    }
    return new CustomResourceDefinitionBuilder()
                    .editOrNewMetadata()
                    .withName(plural + "." + group)
                    .addToLabels("app", "enmasse")
                    .endMetadata()
                    .editOrNewSpec()
                    .withGroup(group)
                    .withVersion(version)
                    .withScope(scope)
                    .editOrNewNames()
                    .withKind(kind)
                    .withListKind(listKind)
                    .withPlural(plural)
                    .withSingular(singular)
                    .withCategories("enmasse")
                    .endNames()
                    .endSpec()
                    .build();
}
 
Example #6
Source File: TypedClusterScopeCustomResourceApiTest.java    From kubernetes-client with Apache License 2.0 5 votes vote down vote up
@BeforeEach
void setupCrd() {
  starCrd = new CustomResourceDefinitionBuilder()
    .withNewMetadata().withName("stars.example.crd.com").endMetadata()
    .withNewSpec()
    .withGroup("example.crd.com")
    .withVersion("v1alpha1")
    .withNewNames().withKind("Star").withPlural("stars").endNames()
    .withScope("Cluster")
    .endSpec()
    .build();

  crdContext = CustomResourceDefinitionContext.fromCrd(starCrd);
}
 
Example #7
Source File: TypedCustomResourceApiTest.java    From kubernetes-client with Apache License 2.0 5 votes vote down vote up
@BeforeEach
void setupCrd() {
  podSetCrd = new CustomResourceDefinitionBuilder()
    .withNewMetadata().withName("podsets.demo.k8s.io").endMetadata()
    .withNewSpec()
    .withGroup("demo.k8s.io")
    .withVersion("v1alpha1")
    .withNewNames().withKind("PodSet").withPlural("podsets").endNames()
    .withScope("Namespaced")
    .endSpec()
    .build();

  crdContext = CustomResourceDefinitionContext.fromCrd(podSetCrd);
}
 
Example #8
Source File: CustomResourceDefinitionIT.java    From kubernetes-client with Apache License 2.0 5 votes vote down vote up
@Before
public void init() {
  crd1 = new CustomResourceDefinitionBuilder()
    .withApiVersion("apiextensions.k8s.io/v1beta1")
    .withNewMetadata()
    .withName("itests.examples.fabric8.io")
    .endMetadata()
    .withNewSpec()
    .withGroup("examples.fabric8.io")
    .withVersion("v1")
    .addAllToVersions(Collections.singletonList(new CustomResourceDefinitionVersionBuilder()
      .withName("v1")
      .withServed(true)
      .withStorage(true)
      .build()))
    .withScope("Namespaced")
    .withNewNames()
    .withPlural("itests")
    .withSingular("itest")
    .withKind("Itest")
    .withShortNames("it")
    .endNames()
    .endSpec()
    .build();

  client.customResourceDefinitions().create(crd1);
}
 
Example #9
Source File: CrdDeployer.java    From abstract-operator with Apache License 2.0 4 votes vote down vote up
public CustomResourceDefinition initCrds(KubernetesClient client,
                                                String prefix,
                                                String entityName,
                                                String[] shortNames,
                                                String pluralName,
                                                String[] additionalPrinterColumnNames,
                                                String[] additionalPrinterColumnPaths,
                                                String[] additionalPrinterColumnTypes,
                                                Class<? extends EntityInfo> infoClass,
                                                boolean isOpenshift) {
    final String newPrefix = prefix.substring(0, prefix.length() - 1);
    CustomResourceDefinition crdToReturn;

    Serialization.jsonMapper().configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
    List<CustomResourceDefinition> crds = client.customResourceDefinitions()
            .list()
            .getItems()
            .stream()
            .filter(p -> entityName.equals(p.getSpec().getNames().getKind()) && newPrefix.equals(p.getSpec().getGroup()))
            .collect(Collectors.toList());
    if (!crds.isEmpty()) {
        crdToReturn = crds.get(0);
        log.info("CustomResourceDefinition for {} has been found in the K8s, so we are skipping the creation.", entityName);
    } else {
        log.info("Creating CustomResourceDefinition for {}.", entityName);
        JSONSchemaProps schema = JSONSchemaReader.readSchema(infoClass);
        CustomResourceDefinitionFluent.SpecNested<CustomResourceDefinitionBuilder> builder;

        if (schema != null) {
            removeDefaultValues(schema);
            builder = getCRDBuilder(newPrefix,
                                    entityName,
                                    shortNames,
                                    pluralName)
                    .withNewValidation()
                    .withNewOpenAPIV3SchemaLike(schema)
                    .endOpenAPIV3Schema()
                    .endValidation();
        } else {
            builder = getCRDBuilder(newPrefix,
                                    entityName,
                                    shortNames,
                                    pluralName);
        }
        if (additionalPrinterColumnNames != null && additionalPrinterColumnNames.length > 0) {
            for (int i = 0; i < additionalPrinterColumnNames.length; i++) {
                builder = builder.addNewAdditionalPrinterColumn().withName(additionalPrinterColumnNames[i]).withJSONPath(additionalPrinterColumnPaths[i]).endAdditionalPrinterColumn();
            }
        }
        crdToReturn = builder.endSpec().build();
        try {
            if (schema != null) {
                // https://github.com/fabric8io/kubernetes-client/issues/1486
                crdToReturn.getSpec().getValidation().getOpenAPIV3Schema().setDependencies(null);
            }

            client.customResourceDefinitions().createOrReplace(crdToReturn);
        } catch (KubernetesClientException e) {
            // old version of K8s/openshift -> don't use schema validation
            log.warn("Consider upgrading the {}. Your version doesn't support schema validation for custom resources."
                    , isOpenshift ? "OpenShift" : "Kubernetes");
            crdToReturn = getCRDBuilder(newPrefix,
                                        entityName,
                                        shortNames,
                                        pluralName)
                    .endSpec()
                    .build();
            client.customResourceDefinitions().createOrReplace(crdToReturn);
        }
    }

    // register the new crd for json serialization
    io.fabric8.kubernetes.internal.KubernetesDeserializer.registerCustomKind(newPrefix + "/" + crdToReturn.getSpec().getVersion() + "#" + entityName, InfoClass.class);
    io.fabric8.kubernetes.internal.KubernetesDeserializer.registerCustomKind(newPrefix + "/" + crdToReturn.getSpec().getVersion() + "#" + entityName + "List", CustomResourceList.class);

    return crdToReturn;
}