Search in sources :

Example 16 with CustomResource

use of io.fabric8.kubernetes.client.CustomResource in project syndesis-qe by syndesisio.

the class CommonSteps method isProjectClean.

/**
 * Workaround for isProjectClean function from XTF. See issue: https://github.com/xtf-cz/xtf/issues/406
 * XTF cannot be update because it uses openshift-client 4.13.0 which contains kubernetes-model 4.11.2 where CRD was moved to the `v1beta1` .
 * Since Syndesis endpoints uses old kubernetes-model, the CRD is missing (`NoClassDefFoundError`)
 * When we let Syndesis endpoints to bring the old kubernetes-model with itself (remove <exclude> from parent pom.xml),
 * it causes undesired behaviour, e.g. portForwarding doesn't work correctly etc.
 * So we need to wait with bump xtf version until the Syndesis will contains newer kubernetes-model
 * <p>
 * This is implementation from XTF with workaround
 * To access to the protected methods, the JavaReflection API is used.
 */
private static Waiter isProjectClean() {
    return new SimpleWaiter(() -> {
        int crdInstances = 0;
        int removableResources = 0;
        try {
            Method privateMethodGetCRDContextProviders = OpenShift.class.getDeclaredMethod("getCRDContextProviders", null);
            privateMethodGetCRDContextProviders.setAccessible(true);
            Method privateMethodListRemovableResources = OpenShift.class.getDeclaredMethod("listRemovableResources", null);
            privateMethodListRemovableResources.setAccessible(true);
            ServiceLoader<CustomResourceDefinitionContextProvider> cRDContextProviders = (ServiceLoader<CustomResourceDefinitionContextProvider>) privateMethodGetCRDContextProviders.invoke(OpenShift.class, null);
            for (CustomResourceDefinitionContextProvider crdContextProvider : cRDContextProviders) {
                try {
                    crdInstances += ((List) (OpenShiftUtils.getInstance().customResource(crdContextProvider.getContext()).list(OpenShiftUtils.getInstance().getNamespace()).get("items"))).size();
                } catch (KubernetesClientException kce) {
                // CRD might not be installed on the cluster
                }
            }
            List<HasMetadata> removableResourcesList = (List<HasMetadata>) privateMethodListRemovableResources.invoke(OpenShiftUtils.getInstance(), null);
            removableResources = removableResourcesList.size();
        } catch (IllegalAccessException | InvocationTargetException | NoSuchMethodException e) {
            e.printStackTrace();
        }
        return crdInstances == 0 & // <=1 because configMap can be there on OCP 4.7, see https://github.com/xtf-cz/xtf/issues/406
        removableResources <= 1;
    }, TimeUnit.MILLISECONDS, WaitingConfig.timeoutCleanup(), "Cleaning project.");
}
Also used : HasMetadata(io.fabric8.kubernetes.api.model.HasMetadata) Method(java.lang.reflect.Method) CustomResourceDefinitionContextProvider(cz.xtf.core.openshift.crd.CustomResourceDefinitionContextProvider) InvocationTargetException(java.lang.reflect.InvocationTargetException) ServiceLoader(java.util.ServiceLoader) OpenShift(cz.xtf.core.openshift.OpenShift) SimpleWaiter(cz.xtf.core.waiting.SimpleWaiter) List(java.util.List) KubernetesClientException(io.fabric8.kubernetes.client.KubernetesClientException)

Example 17 with CustomResource

use of io.fabric8.kubernetes.client.CustomResource in project strimzi by strimzi.

the class CrdGenerator method buildSpec.

@SuppressWarnings("NPathComplexity")
private ObjectNode buildSpec(ApiVersion crdApiVersion, Crd.Spec crd, Class<? extends CustomResource> crdClass) {
    checkKubeVersionsSupportCrdVersion(crdApiVersion);
    ObjectNode result = nf.objectNode();
    result.put("group", crd.group());
    ArrayNode versions = nf.arrayNode();
    // Kube apiserver with CRD v1beta1 is picky about only using per-version subresources, schemas and printercolumns
    // if they actually differ across the versions. If they're the same, it insists these things are
    // declared top level
    // With CRD v1 they have to be per-version :face-with-rolling-eyes:
    Map<ApiVersion, ObjectNode> subresources = buildSubresources(crd);
    boolean perVersionSubResources = needsPerVersion("subresources", subresources);
    Map<ApiVersion, ObjectNode> schemas = buildSchemas(crd, crdClass);
    boolean perVersionSchemas = needsPerVersion("schemas", schemas);
    Map<ApiVersion, ArrayNode> printerColumns = buildPrinterColumns(crd);
    boolean perVersionPrinterColumns = needsPerVersion("additionalPrinterColumns", printerColumns);
    result.set("names", buildNames(crd.names()));
    result.put("scope", crd.scope());
    if (!perVersionPrinterColumns) {
        ArrayNode cols = printerColumns.values().iterator().next();
        if (!cols.isEmpty()) {
            result.set("additionalPrinterColumns", cols);
        }
    }
    if (!perVersionSubResources) {
        ObjectNode subresource = subresources.values().iterator().next();
        if (!subresource.isEmpty()) {
            result.set("subresources", subresource);
        }
    }
    if (conversionStrategy instanceof WebhookConversionStrategy) {
        // "Webhook": must be None if spec.preserveUnknownFields is true
        result.put("preserveUnknownFields", false);
    }
    result.set("conversion", buildConversion(crdApiVersion));
    for (Crd.Spec.Version version : crd.versions()) {
        ApiVersion crApiVersion = ApiVersion.parse(version.name());
        if (!shouldIncludeVersion(crApiVersion)) {
            continue;
        }
        ObjectNode versionNode = versions.addObject();
        versionNode.put("name", crApiVersion.toString());
        versionNode.put("served", servedVersion != null ? servedVersion.contains(crApiVersion) : version.served());
        versionNode.put("storage", storageVersion != null ? crApiVersion.equals(storageVersion) : version.storage());
        if (perVersionSubResources) {
            ObjectNode subresourcesForVersion = subresources.get(crApiVersion);
            if (!subresourcesForVersion.isEmpty()) {
                versionNode.set("subresources", subresourcesForVersion);
            }
        }
        if (perVersionPrinterColumns) {
            ArrayNode cols = printerColumns.get(crApiVersion);
            if (!cols.isEmpty()) {
                versionNode.set("additionalPrinterColumns", cols);
            }
        }
        if (perVersionSchemas) {
            versionNode.set("schema", schemas.get(crApiVersion));
        }
    }
    result.set("versions", versions);
    if (crdApiVersion.compareTo(V1) < 0 && targetKubeVersions.intersects(KubeVersion.parseRange("1.11-1.15"))) {
        result.put("version", Arrays.stream(crd.versions()).map(v -> ApiVersion.parse(v.name())).filter(this::shouldIncludeVersion).findFirst().map(ApiVersion::toString).orElseThrow());
    }
    if (!perVersionSchemas) {
        result.set("validation", schemas.values().iterator().next());
    }
    return result;
}
Also used : JsonProperty(com.fasterxml.jackson.annotation.JsonProperty) Arrays(java.util.Arrays) YAMLGenerator(com.fasterxml.jackson.dataformat.yaml.YAMLGenerator) VersionRange(io.strimzi.api.annotations.VersionRange) Crd(io.strimzi.crdgenerator.annotations.Crd) Alternative(io.strimzi.crdgenerator.annotations.Alternative) Example(io.strimzi.crdgenerator.annotations.Example) YAMLMapper(com.fasterxml.jackson.dataformat.yaml.YAMLMapper) Locale(java.util.Locale) Arrays.asList(java.util.Arrays.asList) Map(java.util.Map) Property.properties(io.strimzi.crdgenerator.Property.properties) JsonNode(com.fasterxml.jackson.databind.JsonNode) Method(java.lang.reflect.Method) Collections.emptyList(java.util.Collections.emptyList) Collection(java.util.Collection) Property.hasAnyGetterAndAnySetter(io.strimzi.crdgenerator.Property.hasAnyGetterAndAnySetter) Pattern(io.strimzi.crdgenerator.annotations.Pattern) Collectors(java.util.stream.Collectors) StandardCharsets(java.nio.charset.StandardCharsets) TextNode(com.fasterxml.jackson.databind.node.TextNode) Objects(java.util.Objects) ArrayNode(com.fasterxml.jackson.databind.node.ArrayNode) MinimumItems(io.strimzi.crdgenerator.annotations.MinimumItems) Base64(java.util.Base64) List(java.util.List) JsonTypeInfo(com.fasterxml.jackson.annotation.JsonTypeInfo) Type(io.strimzi.crdgenerator.annotations.Type) JsonNodeFactory(com.fasterxml.jackson.databind.node.JsonNodeFactory) Modifier(java.lang.reflect.Modifier) Writer(java.io.Writer) Annotation(java.lang.annotation.Annotation) Minimum(io.strimzi.crdgenerator.annotations.Minimum) Property.sortedProperties(io.strimzi.crdgenerator.Property.sortedProperties) ApiVersion(io.strimzi.api.annotations.ApiVersion) Maximum(io.strimzi.crdgenerator.annotations.Maximum) AnnotatedElement(java.lang.reflect.AnnotatedElement) CustomResource(io.fabric8.kubernetes.client.CustomResource) JsonPropertyOrder(com.fasterxml.jackson.annotation.JsonPropertyOrder) HashMap(java.util.HashMap) Function(java.util.function.Function) ObjectNode(com.fasterxml.jackson.databind.node.ObjectNode) HashSet(java.util.HashSet) LinkedHashMap(java.util.LinkedHashMap) KubeVersion(io.strimzi.api.annotations.KubeVersion) OutputStreamWriter(java.io.OutputStreamWriter) ContainerNode(com.fasterxml.jackson.databind.node.ContainerNode) Property.subtypes(io.strimzi.crdgenerator.Property.subtypes) Modifier.isAbstract(java.lang.reflect.Modifier.isAbstract) Collections.emptyMap(java.util.Collections.emptyMap) Files(java.nio.file.Files) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper) FileOutputStream(java.io.FileOutputStream) IOException(java.io.IOException) Description(io.strimzi.crdgenerator.annotations.Description) Integer.parseInt(java.lang.Integer.parseInt) File(java.io.File) V1(io.strimzi.api.annotations.ApiVersion.V1) ParameterizedType(java.lang.reflect.ParameterizedType) TreeMap(java.util.TreeMap) OneOf(io.strimzi.crdgenerator.annotations.OneOf) JsonInclude(com.fasterxml.jackson.annotation.JsonInclude) Alternation(io.strimzi.crdgenerator.annotations.Alternation) ApiVersion(io.strimzi.api.annotations.ApiVersion) ObjectNode(com.fasterxml.jackson.databind.node.ObjectNode) ArrayNode(com.fasterxml.jackson.databind.node.ArrayNode)

Example 18 with CustomResource

use of io.fabric8.kubernetes.client.CustomResource in project strimzi by strimzi.

the class CrdGenerator method main.

public static void main(String[] args) throws IOException, ClassNotFoundException {
    CommandOptions opts = new CommandOptions(args);
    CrdGenerator generator = new CrdGenerator(opts.targetKubeVersions, opts.crdApiVersion, opts.yaml ? YAML_MAPPER.configure(YAMLGenerator.Feature.MINIMIZE_QUOTES, true) : JSON_MATTER, opts.labels, new DefaultReporter(), opts.apiVersions, opts.storageVersion, null, opts.conversionStrategy, opts.describeVersions);
    for (Map.Entry<String, Class<? extends CustomResource>> entry : opts.classes.entrySet()) {
        File file = new File(entry.getKey());
        if (file.getParentFile().exists()) {
            if (!file.getParentFile().isDirectory()) {
                generator.err(file.getParentFile() + " is not a directory");
            }
        } else if (!file.getParentFile().mkdirs()) {
            generator.err(file.getParentFile() + " does not exist and could not be created");
        }
        try (Writer w = new OutputStreamWriter(new FileOutputStream(file), StandardCharsets.UTF_8)) {
            generator.generate(entry.getValue(), w);
        }
    }
    if (generator.numErrors > 0) {
        System.err.println("There were " + generator.numErrors + " errors");
        System.exit(1);
    } else {
        System.exit(0);
    }
}
Also used : CustomResource(io.fabric8.kubernetes.client.CustomResource) FileOutputStream(java.io.FileOutputStream) OutputStreamWriter(java.io.OutputStreamWriter) Map(java.util.Map) HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap) Collections.emptyMap(java.util.Collections.emptyMap) TreeMap(java.util.TreeMap) File(java.io.File) Writer(java.io.Writer) OutputStreamWriter(java.io.OutputStreamWriter)

Example 19 with CustomResource

use of io.fabric8.kubernetes.client.CustomResource in project strimzi by strimzi.

the class MockKube method mockCrs.

@SuppressWarnings({ "unchecked", "deprecation" })
public void mockCrs(KubernetesClient mockClient) {
    when(mockClient.customResources(any(CustomResourceDefinitionContext.class), any(Class.class), any(Class.class))).thenAnswer(invocation -> {
        Class<CustomResource> crClass = invocation.getArgument(1);
        String key = crdKey(crClass);
        CreateOrReplaceable createOrReplaceable = crdMixedOps.get(key);
        if (createOrReplaceable == null) {
            throw new RuntimeException("Unknown CRD " + key);
        }
        return createOrReplaceable;
    });
    when(mockClient.customResources(any(Class.class), any(Class.class))).thenAnswer(invocation -> {
        Class<CustomResource> crClass = invocation.getArgument(0);
        String key = crdKey(crClass);
        CreateOrReplaceable createOrReplaceable = crdMixedOps.get(key);
        if (createOrReplaceable == null) {
            throw new RuntimeException("Unknown CRD " + key);
        }
        return createOrReplaceable;
    });
    when(mockClient.resources(any(Class.class), any(Class.class))).thenAnswer(invocation -> {
        Class<CustomResource> crClass = invocation.getArgument(0);
        String key = crdKey(crClass);
        CreateOrReplaceable createOrReplaceable = crdMixedOps.get(key);
        if (createOrReplaceable == null) {
            throw new RuntimeException("Unknown CRD " + key);
        }
        return createOrReplaceable;
    });
}
Also used : CustomResourceDefinitionContext(io.fabric8.kubernetes.client.dsl.base.CustomResourceDefinitionContext) CustomResource(io.fabric8.kubernetes.client.CustomResource) CreateOrReplaceable(io.fabric8.kubernetes.client.dsl.CreateOrReplaceable)

Example 20 with CustomResource

use of io.fabric8.kubernetes.client.CustomResource in project strimzi by strimzi.

the class ResourceManager method logCurrentResourceStatus.

/**
 * Log actual status of custom resource with pods.
 * @param customResource - Kafka, KafkaConnect etc. - every resource that HasMetadata and HasStatus (Strimzi status)
 */
public static <T extends CustomResource<? extends Spec, ? extends Status>> void logCurrentResourceStatus(T customResource) {
    if (customResource != null) {
        List<String> printWholeCR = Arrays.asList(KafkaConnector.RESOURCE_KIND, KafkaTopic.RESOURCE_KIND, KafkaUser.RESOURCE_KIND);
        String kind = customResource.getKind();
        String name = customResource.getMetadata().getName();
        if (printWholeCR.contains(kind)) {
            LOGGER.info(customResource);
        } else {
            List<String> log = new ArrayList<>(asList(kind, " status:\n", "\nConditions:\n"));
            if (customResource.getStatus() != null) {
                List<Condition> conditions = customResource.getStatus().getConditions();
                if (conditions != null) {
                    for (Condition condition : customResource.getStatus().getConditions()) {
                        if (condition.getMessage() != null) {
                            log.add("\tType: " + condition.getType() + "\n");
                            log.add("\tMessage: " + condition.getMessage() + "\n");
                        }
                    }
                }
                log.add("\nPods with conditions and messages:\n\n");
                for (Pod pod : kubeClient().namespace(customResource.getMetadata().getNamespace()).listPodsByPrefixInName(name)) {
                    log.add(pod.getMetadata().getName() + ":");
                    for (PodCondition podCondition : pod.getStatus().getConditions()) {
                        if (podCondition.getMessage() != null) {
                            log.add("\n\tType: " + podCondition.getType() + "\n");
                            log.add("\tMessage: " + podCondition.getMessage() + "\n");
                        } else {
                            log.add("\n\tType: <EMPTY>\n");
                            log.add("\tMessage: <EMPTY>\n");
                        }
                    }
                    log.add("\n\n");
                }
                LOGGER.info("{}", String.join("", log).strip());
            }
        }
    }
}
Also used : Condition(io.strimzi.api.kafka.model.status.Condition) PodCondition(io.fabric8.kubernetes.api.model.PodCondition) Pod(io.fabric8.kubernetes.api.model.Pod) ArrayList(java.util.ArrayList) PodCondition(io.fabric8.kubernetes.api.model.PodCondition)

Aggregations

CustomResource (io.fabric8.kubernetes.client.CustomResource)22 Collections.emptyMap (java.util.Collections.emptyMap)16 HasMetadata (io.fabric8.kubernetes.api.model.HasMetadata)15 Condition (io.strimzi.api.kafka.model.status.Condition)14 HashSet (java.util.HashSet)14 ObjectMeta (io.fabric8.kubernetes.api.model.ObjectMeta)13 Group (io.fabric8.kubernetes.model.annotation.Group)12 Version (io.fabric8.kubernetes.model.annotation.Version)12 Spec (io.strimzi.api.kafka.model.Spec)12 Status (io.strimzi.api.kafka.model.status.Status)12 Labels (io.strimzi.operator.common.model.Labels)12 AbstractWatchableStatusedResourceOperator (io.strimzi.operator.common.operator.resource.AbstractWatchableStatusedResourceOperator)12 Future (io.vertx.core.Future)12 Promise (io.vertx.core.Promise)12 Vertx (io.vertx.core.Vertx)12 Collections (java.util.Collections)12 Set (java.util.Set)12 MixedOperation (io.fabric8.kubernetes.client.dsl.MixedOperation)10 MeterRegistry (io.micrometer.core.instrument.MeterRegistry)10 Tag (io.micrometer.core.instrument.Tag)10