CrdGenerator { public int generate(Class<? extends CustomResource> crdClass, Writer out) throws IOException { ObjectNode node = nf.objectNode(); Crd crd = crdClass.getAnnotation(Crd.class); if (crd == null) { err(crdClass + " is not annotated with @Crd"); } else { node.put("apiVersion", "apiextensions.k8s.io/" + crdApiVersion) .put("kind", "CustomResourceDefinition") .putObject("metadata") .put("name", crd.spec().names().plural() + "." + crd.spec().group()); if (!labels.isEmpty()) { ((ObjectNode) node.get("metadata")) .putObject("labels") .setAll(labels.entrySet().stream() .collect(Collectors.<Map.Entry<String, String>, String, JsonNode, LinkedHashMap<String, JsonNode>>toMap( Map.Entry::getKey, e -> new TextNode( e.getValue() .replace("%group%", crd.spec().group()) .replace("%plural%", crd.spec().names().plural()) .replace("%singular%", crd.spec().names().singular())), (x, y) -> x, LinkedHashMap::new))); } node.set("spec", buildSpec(crdApiVersion, crd.spec(), crdClass)); } mapper.writeValue(out, node); return numErrors; } CrdGenerator(VersionRange<KubeVersion> targetKubeVersions, ApiVersion crdApiVersion);  CrdGenerator(VersionRange<KubeVersion> targetKubeVersions, ApiVersion crdApiVersion,
                 ObjectMapper mapper, Map<String, String> labels, Reporter reporter,
                 List<ApiVersion> apiVersions,
                 ApiVersion storageVersion,
                 VersionRange<ApiVersion> servedVersions,
                 ConversionStrategy conversionStrategy); void warn(String s); static void argParseErr(String s); void err(String s); int generate(Class<? extends CustomResource> crdClass, Writer out); static void main(String[] args); static final YAMLMapper YAML_MAPPER; static final ObjectMapper JSON_MATTER; }
@Test public void simpleTest() throws IOException { CrdGenerator crdGenerator = new CrdGenerator(KubeVersion.V1_11_PLUS, ApiVersion.V1BETA1); StringWriter w = new StringWriter(); crdGenerator.generate(ExampleCrd.class, w); String s = w.toString(); assertEquals(CrdTestUtils.readResource("simpleTest.yaml"), s); }
@Test public void simpleTestWithSubresources() throws IOException { CrdGenerator crdGenerator = new CrdGenerator(KubeVersion.V1_11_PLUS, ApiVersion.V1BETA1); StringWriter w = new StringWriter(); crdGenerator.generate(ExampleWithSubresourcesCrd.class, w); String s = w.toString(); assertEquals(CrdTestUtils.readResource("simpleTestWithSubresources.yaml"), s); }
@Test public void generateHelmMetadataLabels() throws IOException { Map<String, String> labels = new LinkedHashMap<>(); labels.put("app", "{{ template \"strimzi.name\" . }}"); labels.put("chart", "{{ template \"strimzi.chart\" . }}"); labels.put("component", "%plural%.%group%-crd"); labels.put("release", "{{ .Release.Name }}"); labels.put("heritage", "{{ .Release.Service }}"); CrdGenerator crdGenerator = new CrdGenerator(KubeVersion.V1_11_PLUS, ApiVersion.V1BETA1, CrdGenerator.YAML_MAPPER, labels, new CrdGenerator.DefaultReporter(), emptyList(), null, null, new CrdGenerator.NoneConversionStrategy()); StringWriter w = new StringWriter(); crdGenerator.generate(ExampleCrd.class, w); String s = w.toString(); assertEquals(CrdTestUtils.readResource("simpleTestHelmMetadata.yaml"), s); }
@Test public void versionedTest() throws IOException { CrdGenerator crdGenerator = new CrdGenerator(KubeVersion.parseRange("1.16+"), ApiVersion.V1BETA1); StringWriter w = new StringWriter(); crdGenerator.generate(VersionedExampleCrd.class, w); String s = w.toString(); assertEquals(CrdTestUtils.readResource("versionedTest.yaml"), s); }
@Test public void kubeV1_11ErrorWithMultiVersions() throws IOException { Set<String> errors = new HashSet<>(); CrdGenerator crdGenerator = new CrdGenerator(KubeVersion.parseRange("1.11+"), ApiVersion.V1BETA1, CrdGenerator.YAML_MAPPER, emptyMap(), new CrdGenerator.Reporter() { @Override public void warn(String s) { } @Override public void err(String s) { errors.add(s); } }, emptyList(), null, null, new CrdGenerator.NoneConversionStrategy()); StringWriter w = new StringWriter(); crdGenerator.generate(VersionedExampleCrd.class, w); assertTrue(errors.contains("Multiple scales specified but 1.11 doesn't support schema per version"), errors.toString()); assertTrue(errors.contains("Target kubernetes versions 1.11+ don't support schema-per-version, but multiple versions present on io.strimzi.crdgenerator.VersionedExampleCrd.ignored"), errors.toString()); assertTrue(errors.contains("Target kubernetes versions 1.11+ don't support schema-per-version, but multiple versions present on io.strimzi.crdgenerator.VersionedExampleCrd.someInt"), errors.toString()); }