Search in sources :

Example 46 with CustomResourceDefinition

use of io.fabric8.kubernetes.api.model.apiextensions.v1beta1.CustomResourceDefinition in project hugegraph-computer by hugegraph.

the class AbstractK8sTest method createCRD.

private void createCRD(KubernetesClient client) {
    Resource<CustomResourceDefinition> crd = client.apiextensions().v1beta1().customResourceDefinitions().load(new File("../computer-k8s-operator/manifest" + "/hugegraph-computer-crd.v1beta1.yaml"));
    crd.createOrReplace();
    crd.waitUntilReady(2, TimeUnit.SECONDS);
    Assert.assertNotNull(crd.get());
}
Also used : CustomResourceDefinition(io.fabric8.kubernetes.api.model.apiextensions.v1beta1.CustomResourceDefinition) File(java.io.File)

Example 47 with CustomResourceDefinition

use of io.fabric8.kubernetes.api.model.apiextensions.v1beta1.CustomResourceDefinition in project strimzi-kafka-operator by strimzi.

the class MockKube2 method registerCrd.

/**
 * Registers Custom Resource definition in the mock Kubernetes cluster. This registers it for deserialization, but
 * also creates the CRD in the Kubernetes server.
 *
 * @param apiVersion    API version of the Custom Resource
 * @param kind          Kind of the Custom Resource
 * @param crdClass      Class with the Custom Resource model
 * @param crdPath       Path to the YAML with the CRD definition
 */
private void registerCrd(String apiVersion, String kind, Class<? extends KubernetesResource> crdClass, String crdPath) {
    KubernetesDeserializer.registerCustomKind(apiVersion, kind, crdClass);
    CustomResourceDefinition kafkaCrd = client.apiextensions().v1().customResourceDefinitions().load(crdPath).get();
    client.apiextensions().v1().customResourceDefinitions().create(kafkaCrd);
}
Also used : CustomResourceDefinition(io.fabric8.kubernetes.api.model.apiextensions.v1.CustomResourceDefinition)

Example 48 with CustomResourceDefinition

use of io.fabric8.kubernetes.api.model.apiextensions.v1beta1.CustomResourceDefinition in project strimzi-kafka-operator by strimzi.

the class SetupClusterOperator method applyClusterOperatorInstallFiles.

/**
 * Perform application of ServiceAccount, Roles and CRDs needed for proper cluster operator deployment.
 * Configuration files are loaded from packaging/install/cluster-operator directory.
 */
public void applyClusterOperatorInstallFiles(String namespace) {
    List<File> operatorFiles = Arrays.stream(new File(CO_INSTALL_DIR).listFiles()).sorted().filter(File::isFile).filter(file -> !file.getName().matches(".*(Binding|Deployment)-.*")).collect(Collectors.toList());
    for (File operatorFile : operatorFiles) {
        File createFile = operatorFile;
        if (createFile.getName().contains(Constants.CLUSTER_ROLE + "-")) {
            createFile = switchClusterRolesToRolesIfNeeded(createFile);
        }
        final String resourceType = createFile.getName().split("-")[1];
        LOGGER.debug("Installation resource type: {}", resourceType);
        switch(resourceType) {
            case Constants.ROLE:
                Role role = TestUtils.configFromYaml(createFile, Role.class);
                ResourceManager.getInstance().createResource(extensionContext, new RoleBuilder(role).editMetadata().withNamespace(namespace).endMetadata().build());
                break;
            case Constants.CLUSTER_ROLE:
                ClusterRole clusterRole = TestUtils.configFromYaml(createFile, ClusterRole.class);
                ResourceManager.getInstance().createResource(extensionContext, clusterRole);
                break;
            case Constants.SERVICE_ACCOUNT:
                ServiceAccount serviceAccount = TestUtils.configFromYaml(createFile, ServiceAccount.class);
                ResourceManager.getInstance().createResource(extensionContext, new ServiceAccountBuilder(serviceAccount).editMetadata().withNamespace(namespace).endMetadata().build());
                break;
            case Constants.CONFIG_MAP:
                ConfigMap configMap = TestUtils.configFromYaml(createFile, ConfigMap.class);
                ResourceManager.getInstance().createResource(extensionContext, new ConfigMapBuilder(configMap).editMetadata().withNamespace(namespace).endMetadata().build());
                break;
            case Constants.CUSTOM_RESOURCE_DEFINITION_SHORT:
                CustomResourceDefinition customResourceDefinition = TestUtils.configFromYaml(createFile, CustomResourceDefinition.class);
                ResourceManager.getInstance().createResource(extensionContext, customResourceDefinition);
                break;
            default:
                LOGGER.error("Unknown installation resource type: {}", resourceType);
                throw new RuntimeException("Unknown installation resource type:" + resourceType);
        }
    }
}
Also used : Environment(io.strimzi.systemtest.Environment) ClusterRoleBinding(io.fabric8.kubernetes.api.model.rbac.ClusterRoleBinding) EnvVar(io.fabric8.kubernetes.api.model.EnvVar) Arrays(java.util.Arrays) OlmResource(io.strimzi.systemtest.resources.operator.specific.OlmResource) RoleBindingResource(io.strimzi.systemtest.resources.kubernetes.RoleBindingResource) OlmInstallationStrategy(io.strimzi.systemtest.enums.OlmInstallationStrategy) NetworkPolicyResource(io.strimzi.systemtest.resources.kubernetes.NetworkPolicyResource) Role(io.fabric8.kubernetes.api.model.rbac.Role) Level(org.apache.logging.log4j.Level) HashMap(java.util.HashMap) ExtensionContext(org.junit.jupiter.api.extension.ExtensionContext) ClusterRoleBindingResource(io.strimzi.systemtest.resources.kubernetes.ClusterRoleBindingResource) ArrayList(java.util.ArrayList) RoleBuilder(io.fabric8.kubernetes.api.model.rbac.RoleBuilder) HelmResource(io.strimzi.systemtest.resources.operator.specific.HelmResource) KubeClusterResource(io.strimzi.test.k8s.KubeClusterResource) Map(java.util.Map) TestUtils(io.strimzi.test.TestUtils) Assumptions.assumeTrue(org.junit.jupiter.api.Assumptions.assumeTrue) CustomResourceDefinition(io.fabric8.kubernetes.api.model.apiextensions.v1.CustomResourceDefinition) StUtils(io.strimzi.systemtest.utils.StUtils) ClusterOperatorRBACType(io.strimzi.systemtest.enums.ClusterOperatorRBACType) BeforeAllOnce(io.strimzi.systemtest.BeforeAllOnce) OpenShift(io.strimzi.test.k8s.cluster.OpenShift) Predicate(java.util.function.Predicate) CollectorElement(io.strimzi.test.logs.CollectorElement) ServiceAccountBuilder(io.fabric8.kubernetes.api.model.ServiceAccountBuilder) Constants(io.strimzi.systemtest.Constants) IOException(java.io.IOException) ClusterRoleBindingTemplates(io.strimzi.systemtest.templates.kubernetes.ClusterRoleBindingTemplates) Collectors(java.util.stream.Collectors) File(java.io.File) ConfigMap(io.fabric8.kubernetes.api.model.ConfigMap) KubeClusterResource.kubeClient(io.strimzi.test.k8s.KubeClusterResource.kubeClient) ConfigMapBuilder(io.fabric8.kubernetes.api.model.ConfigMapBuilder) Objects(java.util.Objects) RoleResource(io.strimzi.systemtest.resources.kubernetes.RoleResource) List(java.util.List) Exec(io.strimzi.test.executor.Exec) Logger(org.apache.logging.log4j.Logger) ResourceManager(io.strimzi.systemtest.resources.ResourceManager) ClusterRole(io.fabric8.kubernetes.api.model.rbac.ClusterRole) ServiceAccount(io.fabric8.kubernetes.api.model.ServiceAccount) LogManager(org.apache.logging.log4j.LogManager) Collections(java.util.Collections) SuppressFBWarnings(edu.umd.cs.findbugs.annotations.SuppressFBWarnings) ServiceAccount(io.fabric8.kubernetes.api.model.ServiceAccount) ConfigMap(io.fabric8.kubernetes.api.model.ConfigMap) CustomResourceDefinition(io.fabric8.kubernetes.api.model.apiextensions.v1.CustomResourceDefinition) ServiceAccountBuilder(io.fabric8.kubernetes.api.model.ServiceAccountBuilder) RoleBuilder(io.fabric8.kubernetes.api.model.rbac.RoleBuilder) ClusterRole(io.fabric8.kubernetes.api.model.rbac.ClusterRole) Role(io.fabric8.kubernetes.api.model.rbac.Role) ClusterRole(io.fabric8.kubernetes.api.model.rbac.ClusterRole) ConfigMapBuilder(io.fabric8.kubernetes.api.model.ConfigMapBuilder) File(java.io.File)

Example 49 with CustomResourceDefinition

use of io.fabric8.kubernetes.api.model.apiextensions.v1beta1.CustomResourceDefinition in project strimzi-kafka-operator by strimzi.

the class StructuralCrdIT method assertApiVersionsAreStructuralInApiextensionsV1.

private void assertApiVersionsAreStructuralInApiextensionsV1(String api, VersionRange<ApiVersion> shouldBeStructural) {
    Pattern pattern = Pattern.compile("[^.]spec\\.versions\\[([0-9]+)\\]\\.[^,]*?");
    CustomResourceDefinition crd = cluster.client().getClient().apiextensions().v1().customResourceDefinitions().withName(api).get();
    // We can't make the following assertion because the current version of fabric8 always requests
    // the CRD using v1beta1 api version, so the apiserver just replaces it and serves it.
    // assertEquals(crdApiVersion, ApiVersion.parse(crd.getApiVersion().replace("apiextensions.k8s.io/", "")));
    Set<ApiVersion> presentCrdApiVersions = crd.getSpec().getVersions().stream().map(v -> ApiVersion.parse(v.getName())).collect(Collectors.toSet());
    assertTrue(presentCrdApiVersions.contains(shouldBeStructural.lower()), "CRD has versions " + presentCrdApiVersions + " which doesn't include " + shouldBeStructural.lower() + " which should be structural");
    Map<Integer, ApiVersion> indexedVersions = new HashMap<>();
    int i = 0;
    for (CustomResourceDefinitionVersion version : crd.getSpec().getVersions()) {
        indexedVersions.put(i, ApiVersion.parse(version.getName()));
    }
    Optional<CustomResourceDefinitionCondition> first = crd.getStatus().getConditions().stream().filter(cond -> "NonStructuralSchema".equals(cond.getType()) && "True".equals(cond.getStatus())).findFirst();
    if (first.isPresent()) {
        Matcher matcher = pattern.matcher(first.get().getMessage());
        while (matcher.find()) {
            Integer index = Integer.valueOf(matcher.group(1));
            ApiVersion nonStructuralVersion = indexedVersions.get(index);
            if (shouldBeStructural.contains(nonStructuralVersion)) {
                fail(api + "/ " + nonStructuralVersion + " should be structural but there's a complaint about " + matcher.group());
            }
        }
    }
}
Also used : Assertions.fail(org.junit.jupiter.api.Assertions.fail) Set(java.util.Set) HashMap(java.util.HashMap) VersionRange(io.strimzi.api.annotations.VersionRange) Collectors(java.util.stream.Collectors) Test(org.junit.jupiter.api.Test) CustomResourceDefinitionCondition(io.fabric8.kubernetes.api.model.apiextensions.v1.CustomResourceDefinitionCondition) Matcher(java.util.regex.Matcher) CustomResourceDefinitionVersion(io.fabric8.kubernetes.api.model.apiextensions.v1.CustomResourceDefinitionVersion) Map(java.util.Map) Assertions.assertTrue(org.junit.jupiter.api.Assertions.assertTrue) TestUtils(io.strimzi.test.TestUtils) Optional(java.util.Optional) CustomResourceDefinition(io.fabric8.kubernetes.api.model.apiextensions.v1.CustomResourceDefinition) Pattern(java.util.regex.Pattern) ApiVersion(io.strimzi.api.annotations.ApiVersion) Pattern(java.util.regex.Pattern) ApiVersion(io.strimzi.api.annotations.ApiVersion) HashMap(java.util.HashMap) Matcher(java.util.regex.Matcher) CustomResourceDefinition(io.fabric8.kubernetes.api.model.apiextensions.v1.CustomResourceDefinition) CustomResourceDefinitionCondition(io.fabric8.kubernetes.api.model.apiextensions.v1.CustomResourceDefinitionCondition) CustomResourceDefinitionVersion(io.fabric8.kubernetes.api.model.apiextensions.v1.CustomResourceDefinitionVersion)

Example 50 with CustomResourceDefinition

use of io.fabric8.kubernetes.api.model.apiextensions.v1beta1.CustomResourceDefinition in project strimzi-kafka-operator by strimzi.

the class Crds method crd.

@SuppressWarnings({ "checkstyle:JavaNCSS" })
private static CustomResourceDefinition crd(Class<? extends CustomResource> cls) {
    String scope, plural, singular, group, kind, listKind;
    List<String> versions;
    CustomResourceSubresourceStatus status = null;
    if (cls.equals(Kafka.class)) {
        scope = Kafka.SCOPE;
        plural = Kafka.RESOURCE_PLURAL;
        singular = Kafka.RESOURCE_SINGULAR;
        group = Kafka.RESOURCE_GROUP;
        kind = Kafka.RESOURCE_KIND;
        listKind = Kafka.RESOURCE_LIST_KIND;
        versions = Kafka.VERSIONS;
        status = new CustomResourceSubresourceStatus();
    } else if (cls.equals(KafkaConnect.class)) {
        scope = KafkaConnect.SCOPE;
        plural = KafkaConnect.RESOURCE_PLURAL;
        singular = KafkaConnect.RESOURCE_SINGULAR;
        group = KafkaConnect.RESOURCE_GROUP;
        kind = KafkaConnect.RESOURCE_KIND;
        listKind = KafkaConnect.RESOURCE_LIST_KIND;
        versions = KafkaConnect.VERSIONS;
        status = new CustomResourceSubresourceStatus();
    } else if (cls.equals(KafkaTopic.class)) {
        scope = KafkaTopic.SCOPE;
        plural = KafkaTopic.RESOURCE_PLURAL;
        singular = KafkaTopic.RESOURCE_SINGULAR;
        group = KafkaTopic.RESOURCE_GROUP;
        kind = KafkaTopic.RESOURCE_KIND;
        listKind = KafkaTopic.RESOURCE_LIST_KIND;
        versions = KafkaTopic.VERSIONS;
    } else if (cls.equals(KafkaUser.class)) {
        scope = KafkaUser.SCOPE;
        plural = KafkaUser.RESOURCE_PLURAL;
        singular = KafkaUser.RESOURCE_SINGULAR;
        group = KafkaUser.RESOURCE_GROUP;
        kind = KafkaUser.RESOURCE_KIND;
        listKind = KafkaUser.RESOURCE_LIST_KIND;
        versions = KafkaUser.VERSIONS;
        status = new CustomResourceSubresourceStatus();
    } else if (cls.equals(KafkaMirrorMaker.class)) {
        scope = KafkaMirrorMaker.SCOPE;
        plural = KafkaMirrorMaker.RESOURCE_PLURAL;
        singular = KafkaMirrorMaker.RESOURCE_SINGULAR;
        group = KafkaMirrorMaker.RESOURCE_GROUP;
        kind = KafkaMirrorMaker.RESOURCE_KIND;
        listKind = KafkaMirrorMaker.RESOURCE_LIST_KIND;
        versions = KafkaMirrorMaker.VERSIONS;
        status = new CustomResourceSubresourceStatus();
    } else if (cls.equals(KafkaBridge.class)) {
        scope = KafkaBridge.SCOPE;
        plural = KafkaBridge.RESOURCE_PLURAL;
        singular = KafkaBridge.RESOURCE_SINGULAR;
        group = KafkaBridge.RESOURCE_GROUP;
        kind = KafkaBridge.RESOURCE_KIND;
        listKind = KafkaBridge.RESOURCE_LIST_KIND;
        versions = KafkaBridge.VERSIONS;
        status = new CustomResourceSubresourceStatus();
    } else if (cls.equals(KafkaConnector.class)) {
        scope = KafkaConnector.SCOPE;
        plural = KafkaConnector.RESOURCE_PLURAL;
        singular = KafkaConnector.RESOURCE_SINGULAR;
        group = KafkaConnector.RESOURCE_GROUP;
        kind = KafkaConnector.RESOURCE_KIND;
        listKind = KafkaConnector.RESOURCE_LIST_KIND;
        versions = KafkaConnector.VERSIONS;
        status = new CustomResourceSubresourceStatus();
    } else if (cls.equals(KafkaMirrorMaker2.class)) {
        scope = KafkaMirrorMaker2.SCOPE;
        plural = KafkaMirrorMaker2.RESOURCE_PLURAL;
        singular = KafkaMirrorMaker2.RESOURCE_SINGULAR;
        group = KafkaMirrorMaker2.RESOURCE_GROUP;
        kind = KafkaMirrorMaker2.RESOURCE_KIND;
        listKind = KafkaMirrorMaker2.RESOURCE_LIST_KIND;
        versions = KafkaMirrorMaker2.VERSIONS;
        status = new CustomResourceSubresourceStatus();
    } else if (cls.equals(KafkaRebalance.class)) {
        scope = KafkaRebalance.SCOPE;
        plural = KafkaRebalance.RESOURCE_PLURAL;
        singular = KafkaRebalance.RESOURCE_SINGULAR;
        group = KafkaRebalance.RESOURCE_GROUP;
        kind = KafkaRebalance.RESOURCE_KIND;
        listKind = KafkaRebalance.RESOURCE_LIST_KIND;
        versions = KafkaRebalance.VERSIONS;
        status = new CustomResourceSubresourceStatus();
    } else if (cls.equals(StrimziPodSet.class)) {
        scope = StrimziPodSet.SCOPE;
        plural = StrimziPodSet.RESOURCE_PLURAL;
        singular = StrimziPodSet.RESOURCE_SINGULAR;
        group = StrimziPodSet.RESOURCE_GROUP;
        kind = StrimziPodSet.RESOURCE_KIND;
        listKind = StrimziPodSet.RESOURCE_LIST_KIND;
        versions = StrimziPodSet.VERSIONS;
        status = new CustomResourceSubresourceStatus();
    } else {
        throw new RuntimeException();
    }
    List<CustomResourceDefinitionVersion> crVersions = new ArrayList<>(versions.size());
    for (String apiVersion : versions) {
        crVersions.add(new CustomResourceDefinitionVersionBuilder().withName(apiVersion).withNewSubresources().withStatus(status).endSubresources().withNewSchema().withNewOpenAPIV3Schema().withType("object").withXKubernetesPreserveUnknownFields(true).endOpenAPIV3Schema().endSchema().withStorage("v1beta2".equals(apiVersion)).withServed(true).build());
    }
    return new CustomResourceDefinitionBuilder().withNewMetadata().withName(plural + "." + group).endMetadata().withNewSpec().withScope(scope).withGroup(group).withVersions(crVersions).withNewNames().withSingular(singular).withPlural(plural).withKind(kind).withListKind(listKind).endNames().endSpec().build();
}
Also used : CustomResourceDefinitionBuilder(io.fabric8.kubernetes.api.model.apiextensions.v1.CustomResourceDefinitionBuilder) StrimziPodSet(io.strimzi.api.kafka.model.StrimziPodSet) KafkaBridge(io.strimzi.api.kafka.model.KafkaBridge) ArrayList(java.util.ArrayList) KafkaConnect(io.strimzi.api.kafka.model.KafkaConnect) CustomResourceDefinitionVersionBuilder(io.fabric8.kubernetes.api.model.apiextensions.v1.CustomResourceDefinitionVersionBuilder) CustomResourceSubresourceStatus(io.fabric8.kubernetes.api.model.apiextensions.v1.CustomResourceSubresourceStatus) KafkaMirrorMaker2(io.strimzi.api.kafka.model.KafkaMirrorMaker2) CustomResourceDefinitionVersion(io.fabric8.kubernetes.api.model.apiextensions.v1.CustomResourceDefinitionVersion) KafkaUser(io.strimzi.api.kafka.model.KafkaUser)

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