io.fabric8.kubernetes.api.model.KubernetesResource Java Examples
The following examples show how to use
io.fabric8.kubernetes.api.model.KubernetesResource.
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: HelmMojo.java From jkube with Eclipse Public License 2.0 | 6 votes |
private List<Template> findTemplates() throws IOException { final List<Template> ret = new ArrayList<>(); final File[] sourceFiles; if (kubernetesTemplate != null && kubernetesTemplate.isDirectory()) { sourceFiles = kubernetesTemplate.listFiles((dir, filename) -> filename.endsWith("-template.yml")); } else if (kubernetesTemplate != null) { sourceFiles = new File[] { kubernetesTemplate }; } else { sourceFiles = new File[0]; } for (File sourceFile : Objects.requireNonNull(sourceFiles, "No template files found in the provided directory")) { final KubernetesResource dto = ResourceUtil.load(sourceFile, KubernetesResource.class, ResourceFileType.yaml); if (dto instanceof Template) { ret.add((Template) dto); } else if (dto instanceof KubernetesList) { Optional.ofNullable(((KubernetesList)dto).getItems()) .map(List::stream) .map(items -> items.filter(Template.class::isInstance).map(item -> (Template)item).collect(Collectors.toList())) .ifPresent(ret::addAll); } } return ret; }
Example #2
Source File: HelmMojoTest.java From jkube with Eclipse Public License 2.0 | 6 votes |
@Test public void executeInternalFindTemplatesFromProvidedFile( @Mocked File kubernetesTemplate, @Mocked ResourceUtil resourceUtil, @Mocked Template template) throws Exception { // Given helmMojo.kubernetesTemplate = kubernetesTemplate; new Expectations() {{ mavenProject.getProperties(); result = new Properties(); ResourceUtil.load(kubernetesTemplate, KubernetesResource.class, ResourceFileType.yaml); result = template; }}; // When helmMojo.executeInternal(); // Then assertThat(helmMojo.helm.getTemplates(), contains(template)); }
Example #3
Source File: CustomResourceOperationsImplTest.java From kubernetes-client with Apache License 2.0 | 6 votes |
private void assertForContext(CustomResourceOperationContext context) throws IOException { // CustomResourceOperationsImpl constructor invokes KubernetesDeserializer::registerCustomKind new CustomResourceOperationsImpl<>(context); JsonFactory factory = new MappingJsonFactory(); JsonParser parser = factory.createParser("{\n" + " \"apiVersion\": \"custom.group/v1alpha1\",\n" + " \"kind\": \"MyCustomResource\"\n" + "}"); KubernetesDeserializer deserializer = new KubernetesDeserializer(); KubernetesResource resource = deserializer.deserialize(parser, null); assertThat(resource, instanceOf(MyCustomResource.class)); assertEquals("custom.group/v1alpha1", ((MyCustomResource) resource).getApiVersion()); }
Example #4
Source File: Serialization.java From kubernetes-client with Apache License 2.0 | 6 votes |
/** * Unmarshals a stream optionally performing placeholder substitution to the stream. * @param is The {@link InputStream}. * @param mapper The {@link ObjectMapper} to use. * @param parameters A {@link Map} with parameters for placeholder substitution. * @param <T> The target type. * @return returns de-serialized object */ public static <T> T unmarshal(InputStream is, ObjectMapper mapper, Map<String, String> parameters) { try ( InputStream wrapped = parameters != null && !parameters.isEmpty() ? ReplaceValueStream.replaceValues(is, parameters) : is; BufferedInputStream bis = new BufferedInputStream(wrapped) ) { bis.mark(-1); int intch; do { intch = bis.read(); } while (intch > -1 && Character.isWhitespace(intch)); bis.reset(); if (intch != '{') { mapper = YAML_MAPPER; } return mapper.readerFor(KubernetesResource.class).readValue(bis); } catch (IOException e) { throw KubernetesClientException.launderThrowable(e); } }
Example #5
Source File: WatchHTTPManager.java From kubernetes-client with Apache License 2.0 | 6 votes |
protected static WatchEvent readWatchEvent(String messageSource) throws IOException { WatchEvent event = Serialization.unmarshal(messageSource, WatchEvent.class); KubernetesResource object = null; if (event != null) { object = event.getObject();; } // when watching API Groups we don't get a WatchEvent resource // so the object will be null // so lets try parse the message as a KubernetesResource // as it will probably be a list of resources like a BuildList if (object == null) { object = Serialization.unmarshal(messageSource, KubernetesResource.class); if (event == null) { event = new WatchEvent(object, "MODIFIED"); } else { event.setObject(object); } } if (event.getType() == null) { event.setType("MODIFIED"); } return event; }
Example #6
Source File: CustomResourceOperationsImpl.java From kubernetes-client with Apache License 2.0 | 6 votes |
public CustomResourceOperationsImpl(CustomResourceOperationContext context) { super(context.withApiGroupName(context.getCrdContext().getGroup()) .withApiGroupVersion(context.getCrdContext().getVersion()) .withPlural(context.getCrdContext().getPlural())); this.type = context.getType(); this.listType = context.getListType(); this.doneableType = context.getDoneableType(); this.resourceNamespaced = resourceNamespaced(context.getCrdContext()); this.apiVersion = getAPIGroup() + "/" + getAPIVersion(); KubernetesDeserializer.registerCustomKind(apiVersion, kind(context.getCrdContext()), type); if (KubernetesResource.class.isAssignableFrom(listType)) { KubernetesDeserializer.registerCustomKind(listType.getSimpleName(), (Class<? extends KubernetesResource>) listType); } }
Example #7
Source File: Serialization.java From dekorate with Apache License 2.0 | 6 votes |
/** * Unmarshals a stream optionally performing placeholder substitution to the stream. * @param is The {@link InputStream}. * @param mapper The {@link ObjectMapper} to use. * @param parameters A {@link Map} with parameters for placeholder substitution. * @param <T> The target type. * @return */ public static <T> T unmarshal(InputStream is, ObjectMapper mapper, Map<String, String> parameters) { try (BufferedInputStream bis = new BufferedInputStream(is)) { bis.mark(-1); int intch; do { intch = bis.read(); } while (intch > -1 && Character.isWhitespace(intch)); bis.reset(); if (intch != '{') { mapper = YAML_MAPPER; } return mapper.readerFor(KubernetesResource.class).readValue(bis); } catch (Exception e) { e.printStackTrace(); throw DekorateException.launderThrowable(e); } }
Example #8
Source File: AptReader.java From dekorate with Apache License 2.0 | 6 votes |
private Map<String, KubernetesList> read(File file) { try (InputStream is = new FileInputStream(file)) { final Matcher fileNameMatcher = inputFileIncludePattern.matcher(file.getName()); final String name = fileNameMatcher.find() ? fileNameMatcher.group(1) : "invalid-name"; final KubernetesResource resource = Serialization.unmarshal(is, KubernetesResource.class); if (resource instanceof KubernetesList) { return Collections.singletonMap(name, (KubernetesList) resource); } else if (resource instanceof HasMetadata) { final KubernetesListBuilder klb = new KubernetesListBuilder(); klb.addToItems((HasMetadata) resource); return Collections.singletonMap(name, klb.build()); } } catch (IOException e) { processingEnv.getMessager().printMessage(Diagnostic.Kind.ERROR, file.getAbsolutePath() + " not found."); } return null; }
Example #9
Source File: ApplyStepExecution.java From kubernetes-pipeline-plugin with Apache License 2.0 | 6 votes |
private List<HasMetadata> loadImageStreams() throws IOException, InterruptedException { if (kubernetes.isAdaptable(OpenShiftClient.class)) { FilePath child = workspace.child("target"); if (child.exists() && child.isDirectory()) { List<FilePath> paths = child.list(); if (paths != null) { for (FilePath path : paths) { String name = path.getName(); if (path.exists() && !path.isDirectory() && name.endsWith("-is.yml")) { try (InputStream is = path.read()) { listener.getLogger().println("Loading OpenShift ImageStreams file: " + name); KubernetesResource dto = KubernetesHelper.loadYaml(is, KubernetesResource.class); return KubernetesHelper.toItemList(dto); } } } } } } return Collections.emptyList(); }
Example #10
Source File: KubernetesDeserializer.java From kubernetes-client with Apache License 2.0 | 5 votes |
public void registerProvider(KubernetesResourceMappingProvider provider) { if (provider == null) { return; } Map<String, Class<? extends KubernetesResource>> providerMappings = provider.getMappings().entrySet().stream() //If the model is shaded (which is as part of kubernetes-client uberjar) this is going to cause conflicts. //This is why we NEED TO filter out incompatible resources. .filter(entry -> KubernetesResource.class.isAssignableFrom(entry.getValue())) .collect(Collectors.toMap(Map.Entry::getKey, Map.Entry::getValue)); mappings.putAll(providerMappings); }
Example #11
Source File: KubernetesDeserializer.java From kubernetes-client with Apache License 2.0 | 5 votes |
private String getKeyFromClass(Class<? extends KubernetesResource> clazz) { String apiGroup = Helper.getAnnotationValue(clazz, ApiGroup.class); String apiVersion = Helper.getAnnotationValue(clazz, ApiVersion.class); if (apiGroup != null && !apiGroup.isEmpty() && apiVersion != null && !apiVersion.isEmpty()) { return createKey(apiGroup + "/" + apiVersion, clazz.getSimpleName()); } return clazz.getSimpleName(); }
Example #12
Source File: HelmMojo.java From jkube with Eclipse Public License 2.0 | 5 votes |
private String findIconURL() throws MojoExecutionException { String answer = null; if (kubernetesManifest != null && kubernetesManifest.isFile()) { KubernetesResource dto; try { dto = ResourceUtil.load(kubernetesManifest, KubernetesResource.class); } catch (IOException e) { throw new MojoExecutionException("Failed to load kubernetes YAML " + kubernetesManifest + ". " + e, e); } if (dto instanceof HasMetadata) { answer = KubernetesHelper.getOrCreateAnnotations((HasMetadata) dto).get("jkube.io/iconUrl"); } if (StringUtils.isBlank(answer) && dto instanceof KubernetesList) { KubernetesList list = (KubernetesList) dto; List<HasMetadata> items = list.getItems(); if (items != null) { for (HasMetadata item : items) { answer = KubernetesHelper.getOrCreateAnnotations(item).get("jkube.io/iconUrl"); if (StringUtils.isNotBlank(answer)) { break; } } } } } else { getLog().warn("No kubernetes manifest file has been generated yet by the kubernetes:resource goal at: " + kubernetesManifest); } return answer; }
Example #13
Source File: KubernetesDeserializer.java From kubernetes-client with Apache License 2.0 | 5 votes |
private Class<? extends KubernetesResource> loadClassIfExists(String className) { try { Class<?> clazz = KubernetesDeserializer.class.getClassLoader().loadClass(className); if (!KubernetesResource.class.isAssignableFrom(clazz)) { return null; } return (Class<? extends KubernetesResource>) clazz; } catch (Exception t) { return null; } }
Example #14
Source File: KubernetesDeserializerTest.java From kubernetes-client with Apache License 2.0 | 5 votes |
@Test public void shouldRegisterKind() { // given String version = "version1"; String kind = "kind1"; String key = mapping.createKey(version, kind); assertThat(mapping.getForKey(key), is(nullValue())); // when mapping.registerKind(version, kind, SmurfResource.class); // then Class<? extends KubernetesResource> clazz = mapping.getForKey(key); assertThat(clazz, equalTo(SmurfResource.class)); }
Example #15
Source File: KubernetesDeserializerTest.java From kubernetes-client with Apache License 2.0 | 5 votes |
@Test public void shouldRegisterKindWithoutVersionIfNullVersion() { // given String version = null; String kind = "kind1"; String key = mapping.createKey(version, kind); // when mapping.registerKind(version, kind, SmurfResource.class); // then Class<? extends KubernetesResource> clazz = mapping.getForKey(key); assertThat(clazz, equalTo(SmurfResource.class)); }
Example #16
Source File: KubernetesDeserializerTest.java From kubernetes-client with Apache License 2.0 | 5 votes |
@Test public void shouldRegisterProvider() { // given String key = mapping.createKey("42", "Hitchhiker"); assertThat(mapping.getForKey(key), is(nullValue())); KubernetesResourceMappingProvider provider = createProvider( Pair.of(key, SmurfResource.class)); // when mapping.registerProvider(provider); // then Class<? extends KubernetesResource> clazz = mapping.getForKey(key); assertThat(clazz, equalTo(SmurfResource.class)); }
Example #17
Source File: KubernetesDeserializerTest.java From kubernetes-client with Apache License 2.0 | 5 votes |
@Test public void shouldReturnMappedClass() { // given String version = "version1"; String kind = SmurfResource.class.getSimpleName(); String key = mapping.createKey(version, kind); assertThat(mapping.getForKey(key), is(nullValue())); mapping.registerKind(version, kind, SmurfResource.class); // when Class<? extends KubernetesResource> clazz = mapping.getForKey(key); // then assertThat(clazz, equalTo(SmurfResource.class)); }
Example #18
Source File: KubernetesDeserializerTest.java From kubernetes-client with Apache License 2.0 | 5 votes |
@Test public void shouldReturnNullIfKeyIsNull() { // given // when Class<? extends KubernetesResource> clazz = mapping.getForKey(null); // then assertThat(clazz, is(nullValue())); }
Example #19
Source File: KubernetesDeserializerTest.java From kubernetes-client with Apache License 2.0 | 5 votes |
@Test public void shouldLoadClassInPackage() { // given String key = mapping.createKey("42", Pod.class.getSimpleName()); // when Class<? extends KubernetesResource> clazz = mapping.getForKey(key); // then assertThat(clazz, equalTo(Pod.class)); }
Example #20
Source File: KubernetesDeserializerTest.java From kubernetes-client with Apache License 2.0 | 5 votes |
@Test public void shouldNotLoadClassInPackageIfNotKubernetesResource() { // given Quantity is not a KuberntesResource String key = mapping.createKey("42", Quantity.class.getSimpleName()); // when Class<? extends KubernetesResource> clazz = mapping.getForKey(key); // then assertThat(clazz, is(nullValue())); }
Example #21
Source File: KubernetesDeserializerTest.java From kubernetes-client with Apache License 2.0 | 5 votes |
@Test public void shouldLoadClassIfKeyOnlyHasKind() { // given Quantity is not a KuberntesResource String key = mapping.createKey(null, Pod.class.getSimpleName()); // when Class<? extends KubernetesResource> clazz = mapping.getForKey(key); // then assertThat(clazz, equalTo(Pod.class)); }
Example #22
Source File: SerializationTest.java From kubernetes-client with Apache License 2.0 | 5 votes |
@Test @DisplayName("unmarshal, String containing List with windows like line-ends (CRLF), all list items should be available") void unmarshalListWithWindowsLineSeparatorsString() throws Exception { // Given final String crlfFile = readYamlToString("/test-list.yml"); // When final KubernetesResource result = Serialization.unmarshal(crlfFile, KubernetesResource.class); // Then assertTrue(result instanceof KubernetesList); final KubernetesList kubernetesList = (KubernetesList)result; assertEquals(2, kubernetesList.getItems().size()); assertTrue(kubernetesList.getItems().get(0) instanceof Service); assertTrue(kubernetesList.getItems().get(1) instanceof Deployment); }
Example #23
Source File: KubernetesDeserializer.java From kubernetes-client with Apache License 2.0 | 5 votes |
public Class<? extends KubernetesResource> getForKey(String key) { if (key == null) { return null; } Class<? extends KubernetesResource> clazz = mappings.get(key); if (clazz != null) { return clazz; } clazz = getInternalTypeForName(key); if (clazz != null) { mappings.put(key, clazz); } return clazz; }
Example #24
Source File: KubernetesDeserializer.java From kubernetes-client with Apache License 2.0 | 5 votes |
private static KubernetesResource fromObjectNode(JsonParser jp, JsonNode node) throws IOException { String key = getKey(node); if (key != null) { Class<? extends KubernetesResource> resourceType = mapping.getForKey(key); if (resourceType == null) { throw JsonMappingException.from(jp,"No resource type found for:" + key); } else if (KubernetesResource.class.isAssignableFrom(resourceType)){ return jp.getCodec().treeToValue(node, resourceType); } } return null; }
Example #25
Source File: KubernetesDeserializer.java From kubernetes-client with Apache License 2.0 | 5 votes |
private KubernetesResource fromArrayNode(JsonParser jp, JsonNode node) throws IOException { Iterator<JsonNode> iterator = node.elements(); List<HasMetadata> list = new ArrayList<>(); while (iterator.hasNext()) { JsonNode jsonNode = iterator.next(); if (jsonNode.isObject()) { KubernetesResource resource = fromObjectNode(jp, jsonNode); if (resource instanceof HasMetadata) { list.add((HasMetadata)resource); } } } return new KubernetesListBuilder().withItems(list).build(); }
Example #26
Source File: KubernetesDeserializer.java From kubernetes-client with Apache License 2.0 | 5 votes |
@Override public KubernetesResource deserialize(JsonParser jp, DeserializationContext ctxt) throws IOException { JsonNode node = jp.readValueAsTree(); if (node.isObject()) { return fromObjectNode(jp, node); } else if (node.isArray()) { return fromArrayNode(jp, node); } else { return null; } }
Example #27
Source File: KubernetesResourceUtil.java From jkube with Eclipse Public License 2.0 | 5 votes |
public static Set<HasMetadata> loadResources(File manifest) throws IOException { final Set<HasMetadata> entities = new TreeSet<>(new HasMetadataComparator()); for (KubernetesResource dto : ResourceUtil.loadList(manifest, KubernetesResource.class)) { if (dto == null) { throw new IllegalStateException("Cannot load kubernetes manifest " + manifest); } if (dto instanceof Template) { Template template = (Template) dto; dto = OpenshiftHelper.processTemplatesLocally(template, false); } entities.addAll(KubernetesHelper.toItemList(dto)); } return entities; }
Example #28
Source File: HelmService.java From jkube with Eclipse Public License 2.0 | 5 votes |
private static void processSourceFiles(File sourceDir, File templatesDir) throws IOException { for (File file : listYamls(sourceDir)) { final KubernetesResource dto = ResourceUtil.load(file, KubernetesResource.class, ResourceFileType.yaml); if (dto instanceof Template) { splitAndSaveTemplate((Template) dto, templatesDir); } else { final String fileName = FileUtil.stripPostfix(file.getName(), ".yml") + YAML_EXTENSION; File targetFile = new File(templatesDir, fileName); // lets escape any {{ or }} characters to avoid creating invalid templates String text = FileUtils.readFileToString(file, Charset.defaultCharset()); text = escapeYamlTemplate(text); FileUtils.write(targetFile, text, Charset.defaultCharset()); } } }
Example #29
Source File: ExamplesTest.java From strimzi-kafka-operator with Apache License 2.0 | 5 votes |
private void validate(String content) { // Validate first using the Fabric8 KubernetesResource // This uses a custom deserializer which knows about all the built-in // k8s and os kinds, plus the custom kinds registered via Crds // But the custom deserializer always allows unknown properties KubernetesResource resource = TestUtils.fromYamlString(content, KubernetesResource.class, false); recurseForAdditionalProperties(new Stack(), resource); }
Example #30
Source File: KubernetesHelper.java From jkube with Eclipse Public License 2.0 | 5 votes |
public static Set<HasMetadata> loadResources(File manifest) throws IOException { Object dto = ResourceUtil.load(manifest, KubernetesResource.class); if (dto == null) { throw new IllegalStateException("Cannot load kubernetes manifest " + manifest); } if (dto instanceof Template) { Template template = (Template) dto; dto = OpenshiftHelper.processTemplatesLocally(template, false); } Set<HasMetadata> entities = new TreeSet<>(new HasMetadataComparator()); entities.addAll(KubernetesHelper.toItemList(dto)); return entities; }