Search in sources :

Example 1 with ApiVersion

use of io.strimzi.api.annotations.ApiVersion in project strimzi by strimzi.

the class CrdGenerator method buildStatus.

private ObjectNode buildStatus(Crd.Spec crd, ApiVersion crApiVersion) {
    ObjectNode status;
    long length = Arrays.stream(crd.subresources().status()).filter(st -> ApiVersion.parseRange(st.apiVersion()).contains(crApiVersion)).count();
    if (length == 1) {
        status = nf.objectNode();
    } else if (length > 1) {
        err("Each custom resource definition can have only one status sub-resource.");
        status = null;
    } else {
        status = null;
    }
    return status;
}
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) ObjectNode(com.fasterxml.jackson.databind.node.ObjectNode)

Example 2 with ApiVersion

use of io.strimzi.api.annotations.ApiVersion in project strimzi by strimzi.

the class CrdGenerator method buildScale.

private ObjectNode buildScale(Crd.Spec crd, ApiVersion crApiVersion) {
    ObjectNode scaleNode;
    Crd.Spec.Subresources.Scale[] scales = crd.subresources().scale();
    if (scales.length > 1 && !KubeVersion.supportsSchemaPerVersion(targetKubeVersions)) {
        err("Multiple scales specified but " + targetKubeVersions.lower() + " doesn't support schema per version");
    }
    List<Crd.Spec.Subresources.Scale> filteredScales = Arrays.stream(scales).filter(sc -> ApiVersion.parseRange(sc.apiVersion()).contains(crApiVersion)).collect(Collectors.toList());
    if (filteredScales.size() == 1) {
        scaleNode = nf.objectNode();
        Crd.Spec.Subresources.Scale scale = filteredScales.get(0);
        scaleNode.put("specReplicasPath", scale.specReplicasPath());
        scaleNode.put("statusReplicasPath", scale.statusReplicasPath());
        if (!scale.labelSelectorPath().isEmpty()) {
            scaleNode.put("labelSelectorPath", scale.labelSelectorPath());
        }
    } else if (filteredScales.size() > 1 && KubeVersion.supportsSchemaPerVersion(targetKubeVersions)) {
        throw new RuntimeException("Each custom resource definition can have only one scale sub-resource.");
    } else {
        scaleNode = null;
    }
    return scaleNode;
}
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) ObjectNode(com.fasterxml.jackson.databind.node.ObjectNode) Crd(io.strimzi.crdgenerator.annotations.Crd)

Example 3 with ApiVersion

use of io.strimzi.api.annotations.ApiVersion in project strimzi by strimzi.

the class CrdGenerator method buildAdditionalPrinterColumns.

private ArrayNode buildAdditionalPrinterColumns(Crd.Spec crd, ApiVersion crApiVersion) {
    ArrayNode cols = nf.arrayNode();
    if (crd.additionalPrinterColumns().length != 0) {
        for (Crd.Spec.AdditionalPrinterColumn col : Arrays.stream(crd.additionalPrinterColumns()).filter(col -> crApiVersion == null || ApiVersion.parseRange(col.apiVersion()).contains(crApiVersion)).collect(Collectors.toList())) {
            ObjectNode colNode = cols.addObject();
            colNode.put("name", col.name());
            colNode.put("description", col.description());
            colNode.put(crdApiVersion.compareTo(V1) >= 0 ? "jsonPath" : "JSONPath", col.jsonPath());
            colNode.put("type", col.type());
            if (col.priority() != 0) {
                colNode.put("priority", col.priority());
            }
            if (!col.format().isEmpty()) {
                colNode.put("format", col.format());
            }
        }
    }
    return cols;
}
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) ObjectNode(com.fasterxml.jackson.databind.node.ObjectNode) ArrayNode(com.fasterxml.jackson.databind.node.ArrayNode)

Example 4 with ApiVersion

use of io.strimzi.api.annotations.ApiVersion in project strimzi by strimzi.

the class DocGenerator method main.

public static void main(String[] args) throws IOException, ClassNotFoundException {
    Linker linker = null;
    File out = null;
    List<Class<? extends CustomResource>> classes = new ArrayList<>();
    outer: for (int i = 0; i < args.length; i++) {
        String arg = args[i];
        if (arg.startsWith("-")) {
            if ("--linker".equals(arg)) {
                String className = args[++i];
                Class<? extends Linker> linkerClass = classInherits(Class.forName(className), Linker.class);
                if (linkerClass != null) {
                    try {
                        linker = linkerClass.getConstructor(String.class).newInstance(args[++i]);
                    } catch (ReflectiveOperationException e) {
                        throw new RuntimeException("--linker option can't be handled", e);
                    }
                } else {
                    System.err.println(className + " is not a subclass of " + Linker.class.getName());
                }
            } else {
                throw new RuntimeException("Unsupported option " + arg);
            }
        } else {
            if (out == null) {
                out = new File(arg);
            } else {
                String className = arg;
                Class<? extends CustomResource> cls = classInherits(Class.forName(className), CustomResource.class);
                if (cls != null) {
                    classes.add(cls);
                } else {
                    System.err.println(className + " is not a subclass of " + CustomResource.class.getName());
                }
            }
        }
    }
    ApiVersion crApiVersion = ApiVersion.V1BETA2;
    try (OutputStreamWriter writer = new OutputStreamWriter(new FileOutputStream(out), StandardCharsets.UTF_8)) {
        writer.append("// This file is auto-generated by ").append(DocGenerator.class.getName()).append(".").append(NL);
        writer.append("// To change this documentation you need to edit the Java sources.").append(NL);
        writer.append(NL);
        DocGenerator dg = new DocGenerator(crApiVersion, 3, classes, writer, linker);
        for (Class<? extends CustomResource> c : classes) {
            dg.generate(c);
        }
        if (dg.numErrors > 0) {
            System.err.println("There were " + dg.numErrors + " errors");
            System.exit(1);
        }
    }
}
Also used : ApiVersion(io.strimzi.api.annotations.ApiVersion) CustomResource(io.fabric8.kubernetes.client.CustomResource) ArrayList(java.util.ArrayList) FileOutputStream(java.io.FileOutputStream) OutputStreamWriter(java.io.OutputStreamWriter) DescriptionFile(io.strimzi.crdgenerator.annotations.DescriptionFile) File(java.io.File)

Example 5 with ApiVersion

use of io.strimzi.api.annotations.ApiVersion in project strimzi 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)

Aggregations

ApiVersion (io.strimzi.api.annotations.ApiVersion)12 CustomResource (io.fabric8.kubernetes.client.CustomResource)10 VersionRange (io.strimzi.api.annotations.VersionRange)10 File (java.io.File)10 FileOutputStream (java.io.FileOutputStream)10 OutputStreamWriter (java.io.OutputStreamWriter)10 JsonInclude (com.fasterxml.jackson.annotation.JsonInclude)8 JsonProperty (com.fasterxml.jackson.annotation.JsonProperty)8 JsonPropertyOrder (com.fasterxml.jackson.annotation.JsonPropertyOrder)8 JsonTypeInfo (com.fasterxml.jackson.annotation.JsonTypeInfo)8 JsonNode (com.fasterxml.jackson.databind.JsonNode)8 ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)8 ArrayNode (com.fasterxml.jackson.databind.node.ArrayNode)8 ContainerNode (com.fasterxml.jackson.databind.node.ContainerNode)8 JsonNodeFactory (com.fasterxml.jackson.databind.node.JsonNodeFactory)8 ObjectNode (com.fasterxml.jackson.databind.node.ObjectNode)8 TextNode (com.fasterxml.jackson.databind.node.TextNode)8 YAMLGenerator (com.fasterxml.jackson.dataformat.yaml.YAMLGenerator)8 YAMLMapper (com.fasterxml.jackson.dataformat.yaml.YAMLMapper)8 V1 (io.strimzi.api.annotations.ApiVersion.V1)8